Use np.array
to define your 2D arrays.
The extra convenience of specifying data as strings, as in your example, and having a few methods proper to 2D arrays attached to a matrix
object are, at least in my opinion, outbalanced by the minor flexibility of the latter data structure, in particular most expressions that involve a matrix
lead to a matrix
result, even if this is not what you're looking for.
E.g., the double matrix product, i.e., a scalar, when it involves matrix
objects is a 2D matrix and you will see the following recurring idiom
x = np.matrix('1;2;3')
K = np.matrix('1 2 3; 2 4 5; 3 5 6')
V = 0.5 * (x.T*K*x)[0,0]
when you want the strain energy of an elastic system represented as free-standing floating point value.
Note also that, using array
s, you have no more to write
V = 0.5*np.dot(x.T, np.dot(K, x))
but you can use the new syntax
V = 0.5 * x.T@K@x
That said, if you still want a matrix
, the answer by Divakar is exactly what you want and you're right in accepting it.