scipy为python提供了矩阵的运算,还有功能:最优化、线性代数、积分、插值、拟合、特殊函数、快速傅里叶变换、信号和图像处理、常微分方程的求解等等。安装scipy之前必须安装numpy。
例子如下,python3在pycharm中编译:
from scipy.optimize import fsolve #导入求解方程组的函数def f(x): x1 = x[0] x2 = x[1] return [2*x1 - x2**2 - 1, x1**2 - x2 - 2]result = fsolve(f, [1,1]) #输入初始值并求解print(result)# 数值积分from scipy import integratedef g(x): return (1 - x**2)**0.5pi_2, err = integrate.quad(g, -1, 1)print(pi_2 * 2)
from scipy.optimize import fsolve # 导入求解方程组的函数
def f(x):
x1 = x[0]
x2 = x[1]
return [2*x1 - x2**2 - 1, x1**2 - x2 - 2]
result = fsolve(f, [1, 1]) # 输入初始值并求解
print(result)
# 数值积分
from scipy import integrate
def g(x):
return (1 - x**2)**0.5
pi_2, err = integrate.quad(g, -1, 1)
print(pi_2 * 2)