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

Travis Oliphant oliphant at ee.byu.edu
Tue Aug 27 16:38:03 CDT 2002

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

I'm not exactly sure what you mean by a rank three n-dim tensor (in
Numeric the rank is the number of dimensions in the array).

But, I think you can accomplish what you desire in two different ways:

1) If A has N dimensions and you want to perform the reduction over axis
I'll label a1 and a2 (that is a1 is the axis for the v^t*A  sum while a2
is the axis for the A*v sum).  Then I think this should work (if a2 > a1).

ex1 = [Numeric.NewAxis]*(N-1)
ex1[a1] = slice(None)
ex2 = [Numeric.NewAxis]*N
ex2[a2] = slice(None)

Nar = Numeric.add.reduce
result = Nar(v[ex1]*Nar(A*v[ex2],axis=a2),axis=a1)

# I think you need a recent version of Numeric for the axis keyword
#   to be defined here.  Otherwise, just pass a2 and a1 as arguments
#   without the keyword.

2) Using dot (requires transposing the matrix) as dot only operates over
certain dimensions --- I would not try this.

-Travis Oliphant

More information about the Numpy-discussion mailing list