Is there a method to find the independant variables of a function defined in SymPy?

69 Views Asked by At

I am currently trying to solve a very complex integral, firstly symbolically, and then by evaluating at different limits for optimisation.

For context, my derivative is the following:

dFx = 0.5 * row * Vapr * c *(Cl*cos(theta) + Cd*sin(theta))
Fx = sp.integrate(dFx, r)

Where, within dFx, the variables Vapr, c & theta are all functions of r. When I pprint the function dFx it is larger than the console.

Subsequent to integrating dFx for Fx, when I use:

Fx.subs(r,0.85)

This does not output a numerical answer, but rather then pprint(Fx) I find that the function Fx still has symbols of r in it.

Is there a function, which can tell me what the independent variables of Fx are? Because the equation is so long, I cannot see it all in the console to check that only r remains. i.e. something like

Fx.independants >> r, a, y

Then I would be aware that in fact, Fx is still a function of r, a & y

(For further context, I am trying to solve the Blade Element Momentum Theory equations)

2

There are 2 best solutions below

0
On BEST ANSWER

You are looking for the free_symbols property:

from sympy import symbols
from sympy.abc import x, y
    
z = x**2 + y
eq = sqrt(x) * y / z

eq.free_symbols  # returns {x, y}
0
On

The only reason that I can think of that your Fx would still have r in it after integrating and doing a substitution would be because the integral was not evaluated. Consider:

>>> from sympy import *
>>> from sympy.abc import r
>>> q = integrate(r*exp(r)/(1-r),r).subs(r,1); q
-Integral(r*exp(r)/(r - 1), (r, 1))

But this expression won't have r as a free symbol (it is a structurally bound symbol which can be replaced with anything).

>>> q.free_symbols
{}

In this case you can see that r is still present by using the has method:

>>> q.has(r)  # equivalent to `r in q.atoms(Symbol)`
True

So you know that your integral didn't evaluate or else substitution of r with a value would have eliminated the r.

Also, you can see if a known variable is a free symbol or not without generating all the free symbols: just ask

>>> q.has_free(r)
False

How to evaluate an unevaluable integral is another topic.