1

I have a matrix T:

[ 0.2  0.4  0.4]
[ 0.8  0.2  0. ]
[ 0.8  0.   0.2]

T = numpy.mat("0.2 0.4 0.4;0.8 0.2 0.0;0.8 0.0 0.2")

I have vector v, numpy.array(73543, -36772, 36772)

v = numpy.array([ 73543, -36772, 36772])

How do I multiply the array v by the matrix T correctly in python?

thanks,

Chris

1
  • to clarify, do you want to perform vT or Tv? Commented Feb 21, 2014 at 1:19

2 Answers 2

1

use numpy.dot, which is not quite same as * operator:

In [138]: T.dot(v) #the resulting shape is (1, 3), not (3, 1) if you don't care
Out[138]: matrix([[ 14708.6,  51480. ,  66188.8]])

In [139]: v.dot(T) #same with v * T
Out[139]: matrix([[ 14708.6,  22062.8,  36771.6]])

In [140]: T.dot(v[:, None]) #if you need the shape to be (3, 1) when doing T*v
Out[140]: 
matrix([[ 14708.6],
        [ 51480. ],
        [ 66188.8]])
Sign up to request clarification or add additional context in comments.

Comments

0

Simple:

v * T

numpy overload arithmetic operates in ways that make sense most of the time. In your case, since T is a matrix, it converts v to a matrix as well before doing the multiplication. That turns v into a row-vector. Therefore v*T performs matrix multiplication, but T*v throws an exception because v is the wrong shape. However you can make v the correct shape with v.reshape(3,1) or treat v as a vector of the correct orientation with T.dot(v) or numpy.dot(T,v).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.