0

I would like to use the symbols

from sympy import *
phi,p,R,n,x1,k,f = symbols('phi,p,R,n,x1,k,f')

to write an abstract function

f = R(phi) - p * n * R(phi)**2 * x1 - k * cos(R(phi)) (1)

and compute its first derivative over phi. Is something like this possible in sympy?

Function composition is available for sympy functions and it seems to work directly in the diff as an argument, but the line (1) results in

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-0771d7cb2d7b> in <module>
----> 1 f = R(phi) - p * n * R(phi)**2 * x1 - l * cos(R(phi))

TypeError: 'Symbol' object is not callable
tmaric
  • 5,347
  • 4
  • 42
  • 75

1 Answers1

2

Define R as a function (R=Function('R') or R=symbols('R', cls=Function). Or, since phi and R are indistinguishable in your example, you can just let symbol R represent what you know to be a function of phi and use idiff:

>>> idiff(f - (R - p * n * R**2 * x1 - k * cos(R)),(R, f),phi)  # dR/dphi
Derivative(f, phi)/(-2*R*n*p*x1 + k*sin(R) + 1)
>>> idiff(f - (R - p * n * R**2 * x1 - k * cos(R)),(f,R),phi)  # df/dphi
(-2*R*n*p*x1 + k*sin(R) + 1)*Derivative(R, phi)
smichr
  • 16,948
  • 2
  • 27
  • 34