Python中numpy求函数的导数实现方法

Published on Aug. 22, 2023, 12:10 p.m.

https://www.py.cn/jishu/jichu/20428.html

https://www.sympy.org/en/index.html

from sympy import *
import numpy as np
x = Symbol('x')
y = x**2 + 1
yprime = y.diff(x)
yprime

$\displaystyle 2 x$

f = lambdify(x, yprime, 'numpy')
f(np.ones(5))
array([2., 2., 2., 2., 2.])
from sympy import *
init_printing(use_unicode=True)
x = symbols("x")
f = log(x)
f

$\displaystyle \log{\left(x \right)}$

diff(f, x)

$\displaystyle \frac{1}{x}$

diff(f, x, 2)

$\displaystyle - \frac{1}{x^{2}}$

Tags: