📜  Python中的numpy.cbrt

📅  最后修改于: 2020-06-17 04:38:06             🧑  作者: Mango

numpy.cbrt(arr, out = None, ufunc ‘cbrt’) : 此数学函数可帮助用户计算所有x为数组元素的x的立方根。

参数:

arr:[array_like]输入数组或对象他们的元素,我们需要平方。

返回:

对于所有x即数组元素,其立方根为x的数组

 

代码1:

# 解释cbrt()函数的Python程序 
   
import numpy as np 
   
arr1 = [1, 27000, 64, -1000] 
print ("cbrt Value of arr1 : \n", np.cbrt(arr1)) 
   
arr2 = [1024 ,-128] 
print ("\ncbrt Value of arr2 : ", np.cbrt(arr2))

输出:

cbrt Value of arr1 : 
 [  1.  30.   4. -10.]

cbrt Value of arr2 :  [ 10.0793684  -5.0396842]

代码2:使用复数

# 解释cbrt()函数的Python程序 
   
import numpy as np 
   
a = 4 + 3j
print("cbrt(4 + 3j) : ", np.cbrt(a)) 
   
b = 16 + 13j
print("\ncbrt value(16 + 13j) : ", np.cbrt(b))

输出:

TypeError: ufunc 'cbrt' not supported for the input types,
and the inputs could not be safely coerced to any supported
types according to the casting rule ''safe''

代码3:图形表示

# 解释cbrt()函数的Python程序 
   
import numpy as np 
import matplotlib.pyplot as plt 
   
a = np.linspace(start = -5, stop = 150, 
                num = 10, endpoint = True) 
                   
print("图示 : \n", np.cbrt(a)) 
   
plt.title("blue : with cbrt\nred : without cbrt") 
plt.plot(a, np.cbrt(a)) 
   
plt.scatter(a, a, color = 'red') 
plt.show()

输出:

图示 : 
 [-1.70997595  2.30347441  3.08793243  3.60027433  3.99768384  4.3287262
  4.61565763  4.87076238  5.10162421  5.31329285]