Reputation: 626
I would like to preserve the structure of equation in the following order.
*Note that this is in sympy.
Eq=A*X+B*U
where X,U is assigned to other equations.
Sympy expand my symbols with variables assigned elsewhere. So it ends up being something like: Bx+Bx**2+3*A*x What I want to do is preserver the order of A X B U.
Update for clarification: Hi! Thx for your reply.
After trying different options, I've found that by using Matrixsymbols, it is possible to not operate the elements in the matrix. However I still can't make it to show the desired order.
The reason for this is so that I can save this in a jupyter notebook, where the order might help explaining or taking notes for specific procedure.
Upvotes: 1
Views: 1756
Reputation: 19077
When you create an expression with variables, the value of those variables will be used during creation, e.g.
>>> x = cos(3)
>>> eq = x + 2
>>> eq
cos(3) + 2
So if you want eq to look like x + 2
you have to make sure that x
is defined as Symbol x
>>> x = Symbol('x')
>>> eq = x + 2
>>> eq
x + 2
If you have multiple symbols and you want to control the ordering, use unevaluated expressions:
>>> A,B,X,U = symbols('A B X U')
>>> ax = Mul(A, X, evaluate=False)
>>> bu = Mul(B, U, evaluate=False)
>>> eq = Add(ax, bu, evaluate=False)
>>> eq
A*X + B*U
You can substitute in the values that you desire as
>>> eq.subs({U:x+x**2, X:3*x})
3*A*x + B*(x**2 + x)
>>> expand(_)
3*A*x + B*x**2 + B*x
Does that help with what you are trying to do?
Upvotes: 1