Real inner-product in python

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Jan 22 04:46:18 CET 2003


On Saturday 18 January 2003 22:56, Nadav Horesh wrote:
> Is there a package/routine that implements inner-product for arrays
> with rank>2?

> I read in an old thread (1995) a thought to implement an APL-like dot
> (.) operator  in Numeric package (add.inner.subtract <=> +.-) does
> anyone know about an implementation of  the idea?

Numeric does have dot(), and it may do exactly what you want.  Here's 
an example with 1, 2, and 3 dimensional arrays:

$ python
Python 2.2.2 (#1, Jan  3 2003, 12:42:27) 
>>> import Numeric as Num

>>> a = Num.array( [1,2,3] )
>>> b = Num.array( [a, a+3,a+6])
>>> c = Num.array( [b, b+10, b+20] )

>>> Num.rank(a)    
1
>>> Num.rank(b)
2
>>> Num.rank(c)
3

>>> Num.dot(a,a)
14

>>> Num.dot(b,a)
array([14, 32, 50])

>>> Num.dot(a,b)
array([30, 36, 42])

>>> Num.dot(b,b)
array([[ 30,  36,  42],
       [ 66,  81,  96],
       [102, 126, 150]])

>>> Num.dot(c,a)
array([[ 14,  32,  50],
       [ 74,  92, 110],
       [134, 152, 170]])

>>> Num.dot(a,c)
array([[30, 36, 42],
       [51, 57, 63],
       [71, 77, 83]])

>>> Num.dot(c,b)
array([[[ 30,  36,  42],
        [ 66,  81,  96],
        [102, 126, 150]],
       [[150, 186, 222],
        [186, 231, 276],
        [222, 276, 330]],
       [[270, 336, 402],
        [306, 381, 456],
        [342, 426, 510]]])

>>> Num.dot(b,c)
array([[[ 30,  36,  42],
        [ 51,  57,  63],
        [ 71,  77,  83]],
       [[ 66,  81,  96],
        [117, 132, 147],
        [167, 182, 197]],
       [[102, 126, 150],
        [183, 207, 231],
        [263, 287, 311]]])

etc...


Is that all you need?

>
>   Nadav.

-- 
Bay Area Python Interest Group - http://www.baypiggies.net/

Chad Netzer






More information about the Python-list mailing list