Home > database >  Sympy: can I get an explicit print of a builtin function?
Sympy: can I get an explicit print of a builtin function?

Time:08-26

What I'm trying to do is basically ask sympy to tell me exactly what mathematical formula is behind some of its builtin functions, since I have been struggling quite a lot with naming conventions, and I'd like to check that I'm using the function I'm supposed to.

My case would be for example with the builtin Bessel functions jn: according to Wikipedia, j0(x)=sin(x)/x, and I'm inclined to agree having solved that simple-case differential equation by hand.
However, when I ask sympy if jn(0, x)-sin(x)/x==0 the query returns False.
Running simplify does nothing but take up time, and I wouldn't know what else to do since factor and expand at most move the x to be a common denominator and evalf gives back the function in terms of yet another builtin function.

Since higher-order functions tend to be a lot more complicated than this, it would be really useful to find out which builtins correspond to what I'm looking for.
I'll be grateful for any link to resources that might help.

Using python3.10.6, sympy 1.9, running on Kubuntu 22.04

CodePudding user response:

You can use the built-in help() function to read the documentation associated to a given SymPy object, or you can explore the documentation.

By reading the documentation with help(jn), we can find the description and a few examples, one of which points out to what you'd like to achieve:

print(expand_func(jn(0, x)))
# out: sin(x)/x

Edit: looking back at your question, you used: jn(0, x)-sin(x)/x==0. With SymPy there are two kinds of equality testing:

  • Structural equality, performed with the == operator: you are asking SymPy to compare the expression trees. Clearly, jn(0, x)-sin(x)/x is structurally different then 0, because at this stage jn(0, x) has not been "expanded".
  • Mathematical equality: generally speaking, two expressions are mathematically equivalent if expr1 - expr2 is equal to 0. So, with the above example we can write: expand_func(jn(0, x)) - sin(x)/x which will results to 0. Or, we can use the equals() method: expand_func(jn(0, x)).equals(sin(x) / x) which will return True. Please, read the documentation about equals to better understand its quirks.
  • Related