With the help of sympy.Derivative() method, we can create an unevaluated derivative of a SymPy expression. It has the same syntax as diff() method. To evaluate an unevaluated derivative, use the doit() method.
Syntax: Derivative(expression, reference variable) Parameters: expression – A SymPy expression whose unevaluated derivative is found. reference variable – Variable with respect to which derivative is found. Returns: Returns an unevaluated derivative of the given expression.
Example #1:
Python3
# import sympy from sympy import * x, y = symbols( 'x y' ) expr = x * * 2 + 2 * y + y * * 3 print ( "Expression : {}" . format (expr)) # Use sympy.Derivative() method expr_diff = Derivative(expr, x) print ( "Derivative of expression with respect to x : {}" . format (expr_diff)) print ( "Value of the derivative : {}" . format (expr_diff.doit())) |
Output:
Expression : x**2 + y**3 + 2*y Derivative of expression with respect to x : Derivative(x**2 + y**3 + 2*y, x) Value of the derivative : 2*x
Example #2:
Python3
# import sympy from sympy import * x, y = symbols( 'x y' ) expr = y * * 2 * x * * 2 + 2 * y * x + x * * 3 * y * * 3 print ( "Expression : {}" . format (expr)) # Use sympy.Derivative() method expr_diff = Derivative(expr, x, y) print ( "Derivative of expression with respect to x : {}" . format (expr_diff)) print ( "Value of the derivative : {} " . format (expr_diff.doit())) |
Output:
Expression : x**3*y**3 + x**2*y**2 + 2*x*y Derivative of expression with respect to x : Derivative(x**3*y**3 + x**2*y**2 + 2*x*y, x, y) Value of the derivative : 9*x**2*y**2 + 4*x*y + 2
Example #3:
Python3
# import sympy from sympy import * # Derivative method for trigonometric functions x, y = symbols( 'x' ) expr = sin(x) + cos(x) print ( "Expression : {}" . format (expr)) # Use sympy.Derivative() method expr_diff = Derivative(expr, x) print ( "Derivative of expression with respect to x : {}" . format (expr_diff)) print ( "Value of the derivative : {}" . format (expr_diff.doit())) |
Output:
Expression : sin(x) + cos(x)
Derivative of expression with respect to x : Derivative(sin(x) + cos(x), x)
Value of the derivative : -sin(x) + cos(x)