Re: Cross Product

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Wed, 10 Sep 2008 16:52:30 -0600

oops .... when I did some cleaning up for posting I mistyped the last one

function crossp(a[*][3]:numeric,b[*][3]:numeric)
; calculate a cross product: c = a x b
begin
  c = new ( dimsizes(a), typeof(a), "No_FillValue")

  c(:,0) = a(:,1)*b(:,2)-a(:,2)*b(:,1)
  c(:,1) = a(:,2)*b(:,0)-a(:,0)*b(:,2)
  c(:,2) = a(:,0)*b(:,1)-a(:,1)*b(:,0)

  return(c)
end

Dennis Shea wrote:
> Carl J. Schreck, III wrote:
>> Is there a built-in way to do a cross product in ncl? For example, if
>> you have an array of length m and another of length n, it would return
>> a matrix that's m x n? I see dot products mentioned in the
>> documentation for matrix multiply (#), but no cross product.
>>
> I guess I don't see exactly what you mean.
> Are not cross products only defined in 3D space?
> I have a cross product function but it is pretty simple.
>
>
> function cross_prod(a[*][3]:numeric, b[*][3]:numeric)
> ; calculate a cross product: c = a x b
> begin
> c = new ( dimsizes(a), typeof(a), "No_FillValue")
>
> c(:,0) = a(:,1)*b(:,2)-a(:,2)*b(:,1)
> c(:,1) = a(:,2)*b(:,0)-a(:,0)*b(:,2)
> c(:,3) = a(:,0)*b(:,1)-a(:,1)*b(:,0)
>
> return(c)
> end
>

_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Sep 10 2008 - 16:52:30 MDT

This archive was generated by hypermail 2.2.0 : Fri Sep 12 2008 - 21:42:04 MDT