How do I compute derivative of a lambda expression using Sympy?

Question:

How to compute a lambda expression’s derivative ?

For example:

func = lambda x: x ** 3 - 3 * x ** 2
...
# the code
...
derived_func = lambda x: 3 * x ** 2 - 6 * x
Asked By: Lior Elisberg

||

Answers:

You can call the lambda with a symbol as a parameter, and then differentiate the resulting expression:

from sympy import *
x = symbols('x')
func = lambda x: x ** 3 - 3 * x ** 2
display(func(x).diff(x))

Result: 3*x**2 - 6*x

Answered By: wsdookadr

You’re already taking the derivatives of lambda/anonymous functions by definition, though it’s not usually taught that way.

https://i.stack.imgur.com/PrtPf.png

So the type signature of the derivative function is [num -> num] -> [num -> num].

Answered By: bachzealot
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.