Python/Numpy之加减乘除开方平方幂值三角函数等算数运算

时间:2024-11-20 07:41:06
>>> import numpy as np >>> a = np.array([1,2,3]) >>> b = np.array([2,3,4]) # 减法运算 >>> sub1 = a - b >>> sub1 array([-1, -1, -1]) >>> sub2 = np.subtract(a,b) >>> sub2 array([-1, -1, -1]) # 加法运算 >>> add1 = a + b >>> add1 array([3, 5, 7]) >>> add2 = np.add(a,b) >>> add2 array([3, 5, 7]) # 除法运算 >>> div1 = a / b >>> div1 array([0.5 , 0.66666667, 0.75 ]) >>> div2 = np.divide(a,b) >>> div2 array([0.5 , 0.66666667, 0.75 ]) # 乘法运算 >>> mul1 = a * b >>> mul1 array([ 2, 6, 12]) >>> mul2 = np.multiply(a,b) >>> mul2 array([ 2, 6, 12]) # 三角函数 >>> sinx = np.sin(a) >>> sinx array([0.84147098, 0.90929743, 0.14112001]) >>> cosx = np.cos(a) >>> cosx array([ 0.54030231, -0.41614684, -0.9899925 ]) >>> tanx = np.tan(a) >>> tanx array([ 1.55740772, -2.18503986, -0.14254654]) # 幂指数运算 >>> expx = np.exp(a) >>> expx array([ 2.71828183, 7.3890561 , 20.08553692]) >>> logx = np.log(a) >>> logx array([0. , 0.69314718, 1.09861229]) # 开方运算 >>> sqrtx = np.sqrt(a) >>> sqrtx array([1. , 1.41421356, 1.73205081]) # 平方运算 >>> powerx = np.power(a,2) >>> powerx array([1, 4, 9], dtype=int32)