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 :)


[email protected] 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 <[email protected]> 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 [email protected].
> > 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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/d060941d-279b-4763-b7c1-4d0482f39589n%40googlegroups.com.

Reply via email to