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

On Thu, Nov 2, 2023 at 4:45 AM Lenni Lemoy <lennile...@gmail.com> wrote:
>
> 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 :)
>
>
> asme...@gmail.com schrieb am Donnerstag, 2. November 2023 um 04:03:44 UTC+1:
>>
>> 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
>>
>> On Wed, Nov 1, 2023 at 8:15 PM Lenni Lemoy <lenni...@gmail.com> wrote:
>> >
>> > 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! :)
>> >
>> > --
>> > 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.
>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sympy/d060941d-279b-4763-b7c1-4d0482f39589n%40googlegroups.com.

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BLMjfBED%2BqVfGXosz%2BsxirVe8wybx7Arde7rBcWUacsA%40mail.gmail.com.

Reply via email to