python numpy.power()數(shù)組元素求n次方案例
numpy.power(x1, x2)
數(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ī)意義上的暴力,過(guò)程中通過(guò)動(dòng)態(tài)調(diào)整底數(shù)的大小來(lái)加快求解。代碼如下:
class Solution:def myPow(self, x: float, n: int) -> float:judge = Trueif n<0:n = -njudge = Falseif n==0:return 1final = 1 # 記錄當(dāng)前的乘積值tmp = x # 記錄當(dāng)前的因子count = 1 # 記錄當(dāng)前的因子是底數(shù)的多少倍while n>0:if n>=count:final *= tmptmp = tmp*xn -= countcount +=1else:tmp /= xcount -= 1return 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 n<0:n = -nreturn 1/self.help_(x,n)return self.help_(x,n)def help_(self,x,n):if n==0:return 1if 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 = Trueif n < 0:n = -njudge = Falsefinal = 1while n>0:if n%2 == 0:x *=xn //= 2final *= xn -= 1return final if judge else 1/final
python位運(yùn)算符簡(jiǎn)介
其實(shí)跟上面的方法類似,只是通過(guò)位運(yùn)算符判斷奇偶性并且進(jìn)行除以2的操作(移位操作)。代碼如下:
class Solution:def myPow(self, x: float, n: int) -> float:judge = Trueif n < 0:n = -njudge = Falsefinal = 1while n>0:if n & 1: #代表是奇數(shù)final *= xx *= xn >>= 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,則說(shuō)明r取大了,重新定義low不變,high= r,r=(low+high)/2;
如果r的n次方小于x,則說(shuō)明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ù)無(wú)法開平方(不考慮復(fù)數(shù)情況)if x>1:low,high = 0,xelse:low,high =x,1while True:r = (low+high)/2judge = 1for i in range(n):judge *= rif x >1 and judge>x:break # 對(duì)于大于1的數(shù),如果當(dāng)前值已經(jīng)大于它本身,則無(wú)需再算下去if x <1 and judgeif abs(judge-x)<0.0000001: # 判斷是否達(dá)到精度要求print(pow(x,1/n)) # pow函數(shù)計(jì)算結(jié)果return relse:if judge>x:high = relse:low = r
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. Python 操作 MySQL數(shù)據(jù)庫(kù)3. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程4. 開發(fā)效率翻倍的Web API使用技巧5. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 什么是Python變量作用域9. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式10. python 如何在 Matplotlib 中繪制垂直線
