Real inner-product in python

Nadav Horesh NadavH at VisionSense.com
Wed Jan 22 04:49:25 EST 2003


Whats about:

 >>> c = N.reshape(N.arange(12), (3,2,2))
 >>> b = N.arange(3)
 >>> N.dot(b,c)
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in ?
    N.dot(b,c)
  File "/usr/local/lib/python2.3/site-packages/Numeric/Numeric.py", line 
335, in dot
    return multiarray.matrixproduct(a, b)
ValueError: matrices are not aligned

**** but:
 >>> a = N.arange(2)
 >>> N.dot(a,c)
array([[2, 3],
       [4, 5],
       [6, 7]])
 >>> N.dot(c,a)
array([[ 1,  3],
       [ 5,  7],
       [ 9, 11]])

As I see inner product between two tensors --- A of rank $n$ and B of 
rank $m$ it should be like
(in TeX style):
$$
  C = A \cdot B
$$
requires:

1.  The last dimension of A must be equal to the first dimension of B, 
and ...
2.
$$
  C_{p_1, ... p_{m-1},q_2, ... q_n} = \sum_{i=1}^{q_1} A_{p_1, ... 
p_{m-1},i} B_{i, q_2, ... q_{n}}
$$

Thus, I don't see the *dot* function as a proper inner product.

  Nadav


Chad Netzer wrote:

>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.
>>    
>>
>
>  
>







More information about the Python-list mailing list