如下所示:
數(shù)組的元素分別求n次方。x2可以是數(shù)字,也可以是數(shù)組,但是x1和x2的列數(shù)要相同。
>>> x1 = range(6)
>>> x1
[0, 1, 2, 3, 4, 5]
>>> np.power(x1, 3)
array([ 0, 1, 8, 27, 64, 125])
>>> x2 = [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]
>>> np.power(x1, x2)
array([ 0., 1., 8., 27., 16., 5.])
>>> x2 = np.array([[1, 2, 3, 3, 2, 1], [1, 2, 3, 3, 2, 1]])
>>> x2
array([[1, 2, 3, 3, 2, 1],
[1, 2, 3, 3, 2, 1]])
>>> np.power(x1, x2)
array([[ 0, 1, 8, 27, 16, 5],
[ 0, 1, 8, 27, 16, 5]])
補(bǔ)充:python求n次方的函數(shù)_python實(shí)現(xiàn)pow函數(shù)(求n次冪,求n次方)
類型一:求n次冪
實(shí)現(xiàn) pow(x, n),即計(jì)算 x 的 n 次冪函數(shù)。其中n為整數(shù)。pow函數(shù)的實(shí)現(xiàn)——leetcode
解法1:暴力法
不是常規(guī)意義上的暴力,過程中通過動(dòng)態(tài)調(diào)整底數(shù)的大小來加快求解。代碼如下:
class Solution:
def myPow(self, x: float, n: int) -> float:
judge = True
if n0:
n = -n
judge = False
if n==0:
return 1
final = 1 # 記錄當(dāng)前的乘積值
tmp = x # 記錄當(dāng)前的因子
count = 1 # 記錄當(dāng)前的因子是底數(shù)的多少倍
while n>0:
if n>=count:
final *= tmp
tmp = tmp*x
n -= count
count +=1
else:
tmp /= x
count -= 1
return final if judge else 1/final
解法2:根據(jù)奇偶冪分類(遞歸法,迭代法,位運(yùn)算法)
如果n為偶數(shù),則pow(x,n) = pow(x^2, n/2);
如果n為奇數(shù),則pow(x,n) = x*pow(x, n-1)。
遞歸代碼實(shí)現(xiàn)如下:
class Solution:
def myPow(self, x: float, n: int) -> float:
if n0:
n = -n
return 1/self.help_(x,n)
return self.help_(x,n)
def help_(self,x,n):
if n==0:
return 1
if n%2 == 0: #如果是偶數(shù)
return self.help_(x*x, n//2)
# 如果是奇數(shù)
return self.help_(x*x,(n-1)//2)*x
迭代代碼如下:
class Solution:
def myPow(self, x: float, n: int) -> float:
judge = True
if n 0:
n = -n
judge = False
final = 1
while n>0:
if n%2 == 0:
x *=x
n //= 2
final *= x
n -= 1
return final if judge else 1/final
python位運(yùn)算符簡(jiǎn)介
其實(shí)跟上面的方法類似,只是通過位運(yùn)算符判斷奇偶性并且進(jìn)行除以2的操作(移位操作)。代碼如下:
class Solution:
def myPow(self, x: float, n: int) -> float:
judge = True
if n 0:
n = -n
judge = False
final = 1
while n>0:
if n 1: #代表是奇數(shù)
final *= x
x *= x
n >>= 1 # 右移一位
return final if judge else 1/final
類型二:求n次方
實(shí)現(xiàn) pow(x, n),即計(jì)算 x 的 n 次冪函數(shù)。其中x大于0,n為大于1整數(shù)。
解法:二分法求開方
思路就是逐步逼近目標(biāo)值。以x大于1為例:
設(shè)定結(jié)果范圍為[low, high],其中l(wèi)ow=0, high = x,且假定結(jié)果為r=(low+high)/2;
如果r的n次方大于x,則說明r取大了,重新定義low不變,high= r,r=(low+high)/2;
如果r的n次方小于x,則說明r取小了,重新定義low=r,high不變,r=(low+high)/2;
代碼如下:
class Solution:
def myPow(self, x: float, n: int) -> float:
# x為大于0的數(shù),因?yàn)樨?fù)數(shù)無法開平方(不考慮復(fù)數(shù)情況)
if x>1:
low,high = 0,x
else:
low,high =x,1
while True:
r = (low+high)/2
judge = 1
for i in range(n):
judge *= r
if x >1 and judge>x:break # 對(duì)于大于1的數(shù),如果當(dāng)前值已經(jīng)大于它本身,則無需再算下去
if x 1 and judge
if abs(judge-x)0.0000001: # 判斷是否達(dá)到精度要求
print(pow(x,1/n)) # pow函數(shù)計(jì)算結(jié)果
return r
else:
if judge>x:
high = r
else:
low = r
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- numpy 聲明空數(shù)組詳解
- Numpy將二維數(shù)組添加到空數(shù)組的實(shí)現(xiàn)
- 在NumPy中創(chuàng)建空數(shù)組/矩陣的方法
- NumPy實(shí)現(xiàn)ndarray多維數(shù)組操作
- 如何在向量化NumPy數(shù)組上進(jìn)行移動(dòng)窗口
- 如何將numpy二維數(shù)組中的np.nan值替換為指定的值
- 解決numpy數(shù)組互換兩行及賦值的問題
- python 將numpy維度不同的數(shù)組相加相乘操作
- Python 用NumPy創(chuàng)建二維數(shù)組的案例
- Numpy ndarray 多維數(shù)組對(duì)象的使用
- 淺談Python numpy創(chuàng)建空數(shù)組的問題