1

I have in my code a function, let's say:

def test_func(x):
    return(3*x**2)

I want my code to calculate the derivative der_test_func of test_func, so that der_test_func is a function, too. Means, I should be able to do

>>> print(der_test_func(5))
>>> 30

Or I should be able to put it inside another function so that it gets handled reasonably, e.g.

def func2(x):
    y = 10*der_test_func(x)
    return(y)

>>> print(func2(5))
>>> 300

I found out that sympy does derivations, which looks at first sight great, but it returns expressions, whereas I want somehow to have a function, so that I give input to it and get output. Any suggestions or workarounds on how to create der_test_func? Tnx

NeStack
  • 1,739
  • 1
  • 20
  • 40
  • 3
    Have you seen sympy's Expression.[evalf](https://docs.sympy.org/latest/tutorials/intro-tutorial/basic_operations.html#evalf) ? I can e.g. do `diff(cos(x), x).evalf(subs={x: 3.1})` (assuming `x` is a symbol) – lucidbrot Oct 24 '22 at 17:52
  • 2
    If you want "exact" derivatives of arbitrary python functions: No such library exists, and will likely not exist in the near future. What you should do is turn your function into a sympy expression manually, then take a derivative with sympy. Sympy allows you to turn an expression into a python function (but not vice versa). If you can't use sympy, the only option is to approximate the derivative. [scipy and numpy both have methods to help with this](https://svitla.com/blog/numerical-differentiation-methods-in-python) – Him Oct 24 '22 at 17:53
  • Does this answer your question? [Python How to get the value of one specific point of derivative?](https://stackoverflow.com/questions/51814567/python-how-to-get-the-value-of-one-specific-point-of-derivative) – buran Oct 24 '22 at 17:54

1 Answers1

3

If you pass a SymPy Symbol into your function then it will be converted to a symbolic expression. You can then differentiate that and make a function to call it:

In [11]: def test_func(x):
    ...:     return 3*x**2
    ...: 

In [12]: x = sympy.symbols('x')

In [13]: f = test_func(x)

In [14]: f
Out[14]: 
   2
3⋅x 

In [15]: f.diff(x)
Out[15]: 6⋅x

In [16]: g = sympy.Lambda(x, f.diff(x))

In [17]: g
Out[17]: x ↦ 6⋅x

In [18]: g(5)
Out[18]: 30

This will work for a simple function like the example given but won't work if your function does more complicated things like using if/else or loops etc.

Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
  • Thanks, I accepted this post as the answer! And is there a reason for you to use `sympy.Lambda` instead of `sympy.lamdify`. I read that this is also an option for creating lambda functions in sympy, but I don"t know how the 2 are different from each other. – NeStack Oct 25 '22 at 19:37
  • 1
    You can also use `lambdify`. The difference is that `Lambda` is still symbolic and performs symbolic evaluation whereas `lambdify` would perform numeric evaluation. You didn't specify your ultimate goal so I went with `Lambda` as a simple way to make the resulting derivative callable. – Oscar Benjamin Oct 25 '22 at 23:23