Real inner-product in python

Nadav Horesh NadavH at VisionSense.com
Sun Jan 26 03:33:57 EST 2003


The issue turned out to be very simple --- one have to unfold the 
tesor(s) into matrices, where the last dimension of the 1st tensor and 
the first dimension of the second tensor are preserved, then carry a 
usual dot matrix and fold the result into the appropriate dimensions. A 
working (but not 100% full-proof) code is as the follows:

import numarray as N # Should work also with Numeric

def innerproduct(a1, a2):
    ' True inner product'
    newshape = a1.shape[:-1] + a2.shape[1:]
    if a1.shape[-1] != a2.shape[0]:
        raise IndexError, "Unmatched dimensions"
    if N.rank(a1) > 2:
        a1 = N.reshape(a1, (N.product(a1.shape[:-1]), a1.shape[-1]))
    if N.rank(a2) > 2:
        a2 = N.reshape (a2,  (a2.shape[0], N.product(a2.shape[1:])))
    return N.reshape(N.dot(a1,a2), newshape)


   Nadav

Chad Netzer wrote:

>On Wednesday 22 January 2003 01:49, Nadav Horesh wrote:
>  
>
>>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
>>    
>>
>
>[snip]
>
>  
>
>>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.
>>    
>>
>
>Well, in my response I almost discussed tensors, but hedged, since I'm 
>not all that comfortable with thinking about them (haven't had to 
>discipline myself with figuring out all the indexing yet).
>
>But, you may be able to fake it with careful use of transpose(), if I 
>understand your meaning...
>
>Continuing your example:
>  
>
>>>>c
>>>>        
>>>>
>[[[ 0, 1,]
>  [ 2, 3,]]
> [[ 4, 5,]
>  [ 6, 7,]]
> [[ 8, 9,]
>  [10,11,]]]
>
>  
>
>>>>d = N.transpose(c)
>>>>        
>>>>
>
>  
>
>>>>d
>>>>        
>>>>
>[[[ 0, 4, 8,]
>  [ 2, 6,10,]]
> [[ 1, 5, 9,]
>  [ 3, 7,11,]]]
>
>  
>
>>>>d.shape
>>>>        
>>>>
>(2, 2, 3)
>
>  
>
>>>>b.shape
>>>>        
>>>>
>(3,)
>
>  
>
>>>>N.dot(d,b)
>>>>        
>>>>
>[[20,26,]
> [ 8,12,]]
>
>  
>
>>>>N.transpose(N.dot(d,b))
>>>>        
>>>>
>[[20, 8,]
> [26,12,]]
>
>Other than that, the only thing I can think of is to look at 
>ScientificPython, which purports to do Tensor operations.
>
>http://starship.python.net/~hinsen/ScientificPython/
>
>Also, you could check out SciPy (CVS) which wraps the BLAS/LAPACK 
>libraries, and Yorick, which may also do various tensor math functions. 
>
>  
>







More information about the Python-list mailing list