sympy主要用于公式推导,scipy主要是进行数值计算
sympy.solve
(1) x 2 − 2 x + 1 = 0 x^2 - 2x + 1 = 0 \tag{1} x2−2x+1=0(1)
Code
from sympy import *
x = symbols('x')
result = solve('x**2 - 2*x + 1',x)
print(result)
Result
[1]
无法求出数值结果的也可运算
(2) x 2 − 2 x + 1 + y = 0 x^2 - 2x + 1 + y= 0 \tag{2} x2−2x+1+y=0(2)
Code
from sympy import *
x,y = symbols('x,y')
result = solve('x**2 - 2*x + 1 + y',x,y)
print(result)
Result
[{y: -x**2 + 2*x - 1}]
scipy.linalg.solve / numpy.linalg.solve
Code:
(3) 3 x 1 + x 2 − 2 x 3 = 10 x 1 − x 2 + 4 x 3 = − 2 2 x 1 + 3 x 3 = 9 \begin{aligned} 3x_1 + x_2 - 2x_3 &= 10 \\ x_1 - x_2 + 4x_3 &= -2 \\ 2x_1 + 3x_3 &= 9 \tag{3} \end{aligned} 3x1+x2−2x3x1−x2+4x32x1+3x3=10=−2=9(3)
import numpy as np
from scipy.linalg import solve
A = np.array([[3, 1, -2],[1, -1, 4],[2, 0, 3]])
B = np.array([10, -2, 9])
result = solve(A, B)
print(result)
Result:
[ 0.75 12.75 2.5 ]
scipy.optimize.solve / scipy.optimize.root
(4) 2 x 1 2 + 3 x 2 − 3 x 3 − 7 = 0 x 1 + 4 x 2 2 + 8 x 3 − 10 = 0 x 1 − 2 x 2 3 − 2 x 3 2 + 1 = 0 \begin{aligned} 2x_1^2 + 3x_2 - 3x_3 - 7 &= 0 \\ x_1 + 4x_2^2 + 8 x_3 - 10 &= 0 \\ x_1 - 2 x _2^3 - 2 x_3^2 + 1 &= 0 \tag{4} \end{aligned} 2x12+3x2−3x3−7x1+4x22+8x3−10x1−2x23−2x32+1=0=0=0(4)
Code
import numpy as np
from scipy.optimize import root,fsolve
def f(x):
return np.array([2*x[0]**2 + 3*x[1] -3*x[2]**3 - 7,
x[0] + 4*x[1]**2 + 8*x[2] - 10,
x[0] - 2*x[1]**3 -2*x[2]**2 + 1])
result_root = root(f,[0,0,0])
result_fsolve = fsolve(f,[0,0,0])
print(result_root)
print("---------------------------")
print(result_fsolve)
Result
fjac: array([[ 0.96686457, 0.08921948, 0.23919195],
[ 0.07663272, 0.79230209, -0.60529731],
[ 0.24351659, -0.60357045, -0.75921168]])
fun: array([-1.64249059e-10, -1.37286271e-09, 1.57796576e-09])
message: 'The solution converged.'
nfev: 26
qtf: array([-1.67013674e-09, -5.92841205e-08, -1.20357384e-08])
r: array([ 6.34170416, 2.71007612, -1.39511094, 7.99929572, 1.70538181,
-4.90041988])
status: 1
success: True
x: array([1.52964909, 0.973546 , 0.58489796])
---------------------------
[1.52964909 0.973546 0.58489796]
sympy.dsolve / scipy.integrate.odeint
主要针对非线性规划
scipy.optimize.minimize / 利用scipy.optimize.solve 求解KTT
minimize 官方文档
(7) m i n 2 + x 1 1 + x 2 − 3 x 1 + 4 x 3 0.1 ≤ x 1 ≤ 0.9 0.1 ≤ x 2 ≤ 0.9 0.1 ≤ x 3 ≤ 0.9 \begin{aligned} min \quad & \frac{2+x_1}{1+x_2} - 3x_1+4x_3 \\ &0.1 \leq x_1 \leq 0.9 \\ &0.1 \leq x_2 \leq 0.9 \\ &0.1 \leq x_3 \leq 0.9 \tag{7} \end{aligned} min1+x22+x1−3x1+4x30.1≤x1≤0.90.1≤x2≤0.90.1≤x3≤0.9(7)
from scipy.optimize import minimize
import numpy as np
f = lambda x : (2 + x[0]) / (1 + x[1]) - 3*x[0] + 4*x[2]
def con(args):
x1min, x1max, x2min, x2max, x3min, x3max = args
cons = ({'type': 'ineq', 'fun': lambda x: x[0] - x1min},
{'type': 'ineq', 'fun': lambda x: -x[0] + x1max},
{'type': 'ineq', 'fun': lambda x: x[1] - x2min},
{'type': 'ineq', 'fun': lambda x: -x[1] + x2max},
{'type': 'ineq', 'fun': lambda x: x[2] - x3min},
{'type': 'ineq', 'fun': lambda x: -x[2] + x3max})
return cons
if __name__ == '__main__':
args1 = (0.1, 0.9, 0.1, 0.9, 0.1, 0.9) # x1min, x1max, x2min, x2max
cons = con(args1)
x0 = np.array([0.5, 0.5, 0.5])
result = minimize(f, x0, method='SLSQP', constraints=cons)
print(result)
例子来源
如果求解是简单的线性规划,可以直接用scipy.optimize.linprog
scipy.optimize.linprog 官方文档
用 cvxpy 包
有时间再写(逃