[Numpy-discussion] tensor ops in dim > 3?

Konrad Hinsen hinsen at cnrs-orleans.fr
Wed Aug 28 01:03:04 EDT 2002

"Dr. Dmitry Gokhman" <gokhman at sphere.math.utsa.edu> writes:

> I have a rank three n-dim tensor A.  For two of the axes I want to perform
> v^t A v (a quadratic form with scalar output, where v is a vector). The
> final output should be a vector.  I also need to compute the derivative of
> this with respect to v.  This involves symmetrizing and matrix-vector
> multiplication (2 sym(A)v using two axes of A only, which gives a vector)
> with the final result being a matrix.

Whenever dealing with somewhat more complex operations of this time, I think
it's best to go back to the basic NumPy functionality rather then figuring
out if there happens to be a function that magically does it. In this case,
assuming that the first axis of A is the one that is not summed over:

   sum( sum(A*v[NewAxis, NewAxis, :], -1) * v[NewAxis, :], -1)

The idea is to align v with one of the dimensions of A, then multiply
elementwise and sum over the common axis. Note that the first (inner)
sum leaves a rank-2 array, so for the second multiplication v gets
extended to rank-2 only.

> PS One more dumb question: I just installed the ScientificPython-2.4.1 rpm
> on my reincarnated Mandrake linux machine running python2.2.  Do I need to
> do something to configure it?  My scripts aren't finding things (e.g.
> indexing.py).

If you took the binary RPM from my site, they might not work correctly
with Mandrake, as they were made for RedHat. The source RPM should
work with all RPM-based Linux distributions. There is nothing that needs
configuring with an RPM.

Also note that Scientific is a package, so the correct way to import the
indexing module is

   import Scientific.indexing

Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-
Rue Charles Sadron                       | Fax:  +33-
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais

More information about the NumPy-Discussion mailing list