I am trying to understand about sympy's symbolic functions:
import sympy from sympy.abc import x,y,z
with sympy.evaluate(False):
print(sympy.sympify("diff(x,x)").func)
print(sympy.parse_expr("diff(x, x)", local_dict={'diff':sympy.Derivative}).func)
print(sympy.sympify("Derivative(x,x)").func)
pass
This puts out:
Piecewise
<class 'sympy.core.function.Derivative'>
<class 'sympy.core.function.Derivative'>
This example should illustrate that diff is not a symbolic function yet Derivative is.
sympy.sympify("diff(x,x)").func results in Piecewise.
What exactly makes a function in sympy 'symbolic'?
Why don't both of the functions belong to <class 'sympy.core.function.Derivative'>?
I tried to test on a few examples if a function is symbolic using:
list_of_funcs = [sin, cos, tan, sec, csc, cot, sinh, cosh, tanh, sech, csch, coth, asin, acos, atan, asec, acsc, acot, asinh, acosh, atanh, asech, acsch, acoth, log, log, log, exp, <class 'sympy.concrete.summations.Sum'>, <class 'sympy.concrete.products.Product'>, Piecewise, jacobi, Piecewise]
with sympy.evaluate(False):
for f in list_of_funcs:
if issubclass(f, sympy.Basic):
print(f'{f}: True')
It returned True for all yet as far as I understood Piecewise is not symbolic.
Could you help me finding a way to test if a function is symbolic?
Answering this question without going too deep into coding concepts is not easy, but I can give it a try.
SymPy exposes many functions:
cos,sin,exp,Derivative,Integral... we can think of them as symbolic functions. Let's say you provide one or more arguments, then:cos(0)will return the symbolic number 1.cos(x)returnscos(x): this is a symbolic expression of typecos(as you have seen by running thefuncattribute). Similarly, you can create a derivative objectDerivative(expr, x): this is a symbolic expression that represents a derivative, it doesn't actually compute the derivative!Function("f")(x), which will render asf(x): this is a symbolic expression of typef.diff,integrate,series,limit, ... : those are ordinary python functions (for example, created withdef diff(...)) that are going to apply some operation to a symbolic expression. When you calldiff(expr, x)you are asking SymPy to compute the derivative ofexprwith respect tox. What if you wanted to represent the derivative without actually computing it? You writeDerivative(expr, x).So, going back to your example:
this can be easily simplified to:
A few more "relationships":
diffandDerivativeintegrateandIntegrallimitandLimit