Alberto
Alberto

Reputation: 372

Sympy: replacing "-1" in expressions?

I am currently working with Sympy 1.0 and Python 2.7, and my aim is to replace every numerical parameter in an expression with a symbol.

For example:

2 * x + 3 * y - 10 * z -> a * x + b * y - c * z

So far I used expression.atoms(Number) to obtain the list of numerical parameters, to later replace them either using .subs or by writing everything to string and using .replace. However, I am facing an issue with expressions such as:

expression = sympy.simpify("x - z")

Where -z is actually -1 * z if I analyze the expression as a tree...but -1 does not appear among the result of expression.atoms(Number), so I cannot replace it.

Even converting the expression to a string to then use .replace does not really help, as -1 * z is always written out as -z.

There is probably some solution that I overlooked, but so far I am stuck. Can you help me? Thank you in advance for your time.

Upvotes: 1

Views: 139

Answers (1)

Alberto
Alberto

Reputation: 372

In fact, it turns out I probably did a moronic error when I first tried to use .subs; as Patrick noted in the comments, .subs works perfectly in this case.

Upvotes: 1

Related Questions