Matrix vector multiplication

69 views
Skip to first unread message

Lenni Lemoy

unread,
Nov 1, 2023, 10:15:18 PM11/1/23
to sympy
Hi everybody,

I just started using SymPy, and tried to multiply a matrix M by a vector x holding sympy.symbols. 

I expected that M*x will be a vector again, but it is a matrix holding elements M_i,j*x_j. 

Is that the intended behavior or am I doing something wrong? I looked for another method I could use but found nothing adequate. Currently, I use np.sum(M*x, axis=1) to obtain the vector result, but this seems a little awkward.

Thanks already for any info on this! :)

Aaron Meurer

unread,
Nov 1, 2023, 11:03:44 PM11/1/23
to sy...@googlegroups.com
What is the type of your vector x? If it is a Matrix, this is just the
way that SymPy works. It represents "vectors" as a 1 x n matrix. If it
is an object from the sympy.vector module then I'm not sure.

I would recommend not using NumPy to manipulate SymPy objects like
that. NumPy should only be used for numeric values and ideally you
should only do so after converting your SymPy expression to a NumPy
function with lambdify(). You lose out on the benefits of using SymPy
if you store SymPy objects in a NumPy array. See
https://docs.sympy.org/dev/explanation/best-practices.html#separate-symbolic-and-numeric-code

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/5dfb60ca-617f-429a-b23c-c59c67cc98c3n%40googlegroups.com.

Lenni Lemoy

unread,
Nov 2, 2023, 6:45:28 AM11/2/23
to sympy
Hi Aaron, thank you for your answer!

Here is more detail on what I did (from my ipython session):
In [1]: from sympy import symbols, eye, Matrix
In [2]: b = np.array([symbols(f"b_{i}") for i in range(3)])
In [3]: M = eye(3)
In [4]: M*b
Out[4]:
array([[b_0, 0, 0],
       [0, b_1, 0],
       [0, 0, b_2]], dtype=object)


I fixed it now using sympy.Matrix instead of the np.array:
In [5]: b = Matrix(b)
In [6]: M*b
Out[6]:
Matrix([
[b_0],
[b_1],
[b_2]])


Thanks again! That was an important hint :)

Aaron Meurer

unread,
Nov 6, 2023, 4:34:22 PM11/6/23
to sy...@googlegroups.com
The problem is, as I noted, you are using NumPy arrays to store SymPy
expressions instead of using SymPy matrices. NumPy arrays do not
define * as matrix multiplication like sympy matrix does, but rather
as elementwise multiplication with broadcasting. It's generally a good
idea to use @ for matrix multiplication instead of *, but even so, you
wouldn't have this issue if you used only SymPy matrices because
SymPy's Matrix does define * as matrix multiplication.

I would recommend not mixing NumPy and SymPy like this. You should do
symbolic operations using only SymPy and SymPy classes. If you want to
use NumPy to do numeric calculations on these expressions, this should
be done explicitly at the end after converting your SymPy expression
into a NumPy function using lambdify().

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/d060941d-279b-4763-b7c1-4d0482f39589n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages