Real inner-product in python

Chad Netzer cnetzer at mail.arc.nasa.gov
Wed Jan 22 17:11:42 EST 2003


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. 

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

Chad Netzer






More information about the Python-list mailing list