說到程式語言python,有一個著名的格言"餘生太短,只用python"。如果要分析為什麼會存在這麼一句格言?python的語法並不簡單,有複雜難懂的部分,之所以又這樣一句格言,是因為python中有很多強大的模塊,就像一個武器庫。
Python正式由於這些模塊的出現,只要引入這個模塊,調用這個模塊的集成函數,問題迎刃而解;不需要從頭開始,節省了大量的時間。
前面講了兩個處理數學問題的模塊math模塊和cmath模塊,今天要說的這個模塊numpy是一個更加強大的數學模塊,因為前面兩個模塊中的數學函數的參數是一個數,而numpy這個模塊的數學函數的參數可以是數組或矩陣,它還可以進行數組和矩陣的運算。
由於numpy模塊是第三方庫模塊,因此需要進行安裝。在cmd命令行窗口中輸入"pip install numpy",就可以進行安裝。
如果要使用numpy模塊,需要進入這個模塊。在python命令行窗口中輸入"from numpy import *",引入numpy模塊,就可以使用該模塊中的數學函數了。
三角函數(Trigonometric)
FunctionDescribe
sin(x[, out])正弦值
cos(x[, out])餘弦值
tan(x[, out]) 正切值
arcsin(x[, out]) 反正弦
arccos(x[, out]) 反餘弦
arctan(x[, out]) 反正切
hypot(x1, x2[, out]) 求直角三角形斜邊
arctan2(x1, x2[, out]) Element-wise arc tangent of x1/x2 choosing the quadrant correctly.
degrees(x[, out]) 弧度求角度
radians(x[, out]) 角度求弧度
unwrap(p[, discont, axis]) Unwrap by changing deltas between values to 2*pi complement.
deg2rad(x[, out]) 角度求弧度
rad2deg(x[, out]) 弧度求角度
雙曲線函數(Hyperbolic)
Function Describe
sinh(x[, out]) 雙曲線正弦
cosh(x[, out]) 雙曲線餘弦
tanh(x[, out]) 雙曲線正切
arcsinh(x[, out]) 反雙曲線正弦
arccosh(x[, out]) 反雙曲線餘弦
arctanh(x[, out]) 反雙曲線正切
四捨五入(Rounding)
FunctionDescribe
around(a[, decimals, out]) 以給定的小數位進行四捨五入
round_(a[, decimals, out])以給定的小數位進行四捨五入,等同於around
rint(x[, out]) 四捨五入到整數
fix(x[, y])向0取整,正數向下取整,負數向上取整
floor(x[, out])向下取整
ceil(x[, out]) 向上取整
trunc(x[, out]) 取整數部分
和、積、差異(Sums、Products、Differences)
FunctionDescribe
prod(a[, axis, dtype, out, keepdims])求積
sum(a[, axis, dtype, out, keepdims])求和
nanprod(a[, axis, dtype, out, keepdims])求積,預設值為1
nansum(a[, axis, dtype, out, keepdims])求和,預設值為0
cumprod(a[, axis, dtype, out])累積
cumsum(a[, axis, dtype, out])累和
nancumprod(a[, axis, dtype, out])累積,預設為1
nancumsum(a[, axis, dtype, out]) 累和,預設為0
diff(a[, n, axis]) out[n] = a[n+1] - a[n]
ediff1d(ary[, to_end, to_begin])The differences between consecutive elements of an array.
gradient(f, *varargs, **kwargs)Return the gradient of an N-dimensional array.
cross(a, b[, axisa, axisb, axisc, axis])Return the cross product of two (arrays of) vectors.
trapz(y[, x, dx, axis]) Integrate along the given axis using the composite trapezoidal rule.
指數、對數函數(Exponents&Logarithm)
FunctionDescribe
exp(x[, out])指數
expm1(x[, out])exp(x) - 1
exp2(x[, out])2**x
log(x[, out])對數
log10(x[, out])以10為底對數
log2(x[, out]) 以2為底對數.
log1p(x[, out])exp(x) - 1
logaddexp(x1, x2[, out])Logarithm of the sum of exponentiations of the inputs.
logaddexp2(x1, x2[, out]) Logarithm of the sum of exponentiations of the inputs in base-2.
算術運算
FunctionDescribe
add(x1, x2[, out])加法
reciprocal(x[, out]) 倒數
negative(x[, out]) 負數
multiply(x1, x2[, out]) 乘法
divide(x1, x2[, out]) 除法
power(x1, x2[, out]) 冪運算
subtract(x1, x2[, out]) 減法
true_divide(x1, x2[, out]) 真除法 /
floor_divide(x1, x2[, out]) 向下取整除法 //
fmod(x1, x2[, out]) 求餘
mod(x1, x2[, out]) 求餘,餘數為正
modf(x[, out1, out2]) 分別返回整數和餘數
remainder(x1, x2[, out]) 和mod相同
其他(Miscellaneous)
FunctionDescribe
convolve(a, v[, mode]) Returns the discrete, linear convolution of two one-dimensional sequences.
clip(a, a_min, a_max[, out])求某一範圍的值
sqrt(x[, out]) 開平方
cbrt(x[, out]) 開立方
square(x[, out]) 求平方
absolute(x[, out]) 絕對值
fabs(x[, out]) 絕對值
sign(x[, out]) 標記數字的正負零
maximum(x1, x2[, out]) 求最大值
minimum(x1, x2[, out]) 求最小值
fmax(x1, x2[, out]) 求最大值
fmin(x1, x2[, out]) 求最小值
nan_to_num(x)替換空值
real_if_close(a[, tol])If complex input returns a real array if complex parts are close to zero.
interp(x, xp, fp[, left, right, period])One-dimensional linear interpolation.
應該說,numpy模塊是比math和cmath模塊擁有更強大數學函數的模塊。
(該文章為原創,抄襲必究)