Re: Cross Product

From: Mary Haley <haley_at_nyahnyahspammersnyahnyah>
Date: Thu, 11 Sep 2008 08:20:29 -0600 (MDT)

On Thu, 11 Sep 2008, Carl J. Schreck, III wrote:

> Hi All,
>
> Sorry for mixing up my linear algebra terms. I think what I'm looking
> for is an "outer product"
> (http://en.wikipedia.org/wiki/Outer_product), essentially the same as
> the Kronecker product that Travis mentions. "Inner products" are the
> same as "dot products", but I mistakenly thought that cross and outer
> were also the same (cross involves the determinant).

I think this has been an informative discussion and I learned a few
things in the process. Thanks to all for responding.

> In any case, what Andrew shows is what I was looking for. Another,
> but equally clumsy, method would be:
>
> outer = new( (/ dimsizes(a), dimsizes(b) /), integer )
> a2d = conform( outer, a, 0 )
> b2d = conform( outer, b, 1 )
> outer = a2d * b2d

Just an FYI, in V5.0.1, we will have a "conform_dims" function,
so you won't need to create the "outer" array beforehand:

    nout = (/dimsizes(a),dimsizes(b)/)
    outer = conform_dims(nout,a,0) * conform_dims(nout,b,1)

>
> By contrast, in matlab it would simply be:
>
> outer = a' * b
>
> But of course enough other things are so much easier in ncl that I
> won't complain too loudly!

Well, I agree it could be easier. We'll take a look and see if we can
make this easier, either via a function or a new operator.

--Mary

> Thanks,
> Carl
>
>
> On Wed, Sep 10, 2008 at 10:18 PM, Andrew Mai <mai_at_ucar.edu> 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.
>>
>>
>> The following simple example program shows how to do what you want, albeit a
>> bit clumsily. Note that it does use the matrix multiply operator.
>>
>> ; Example of cross product: C = A x B
>>
>> begin
>>
>> a = (/1,2,3,4/)
>> b = (/5,6,7/)
>> a1 = new((/dimsizes(a),1/),double)
>> b1 = new((/1,dimsizes(b)/),double)
>> a1(:,0) = a
>> b1(0,:) = b
>> c = a1 # b1
>> print(c)
>>
>> end
>>
>>
>> The output is:
>>
>> Copyright (C) 1995-2007 - All Rights Reserved
>> University Corporation for Atmospheric Research
>> NCAR Command Language Version 5.0.0
>> The use of this software is governed by a License Agreement.
>> See http://www.ncl.ucar.edu/ for more details.
>>
>>
>> Variable: c
>> Type: double
>> Total Size: 96 bytes
>> 12 values
>> Number of Dimensions: 2
>> Dimensions and sizes: [4] x [3]
>> Coordinates:
>> Number Of Attributes: 1
>> _FillValue : -9999
>> (0,0) 5
>> (0,1) 6
>> (0,2) 7
>> (1,0) 10
>> (1,1) 12
>> (1,2) 14
>> (2,0) 15
>> (2,1) 18
>> (2,2) 21
>> (3,0) 20
>> (3,1) 24
>> (3,2) 28
> _______________________________________________
> ncl-talk mailing list
> ncl-talk_at_ucar.edu
> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
>
_______________________________________________
ncl-talk mailing list
ncl-talk_at_ucar.edu
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Thu Sep 11 2008 - 08:20:29 MDT

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