0

Say I have two symbolic expressions:

a = symbols("a")
thing = symbols("2*a**2")

If one then calls a.free_symbols() one gets {a}, and for thing.free_symbols() one gets {2*a**2}.

Is there a way to format the object thing such that I get the equivalent expression but with eq_expr.free_symbols() = {a}?

  • Does this help: https://stackoverflow.com/questions/30018977/how-can-i-get-a-list-of-the-symbols-in-a-sympy-expression? – ForceBru Jul 18 '22 at 21:52
  • The question is unclear. If the existing answers are insufficient then explain more clealy what you mean. – Oscar Benjamin Jul 19 '22 at 02:21

1 Answers1

1

First, note that writing brackets in a.free_symbols() gives an error: TypeError: 'set' object is not callable. free_symbols is a property of an expression, not a function.

When you write thing = symbols("2*a**2") (docs), you create a new variable thing, whose name is "2*a**2" and which is not connected to a whatsoever.

You can write thing = 2*a**2 (or thing = sympify("2*a**2") (docs)) to make thing an expression using variable a. In that case, thing.free_symbols would be {a}.

JohanC
  • 71,591
  • 8
  • 33
  • 66