濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > python numpy.power()數(shù)組元素求n次方案例

python numpy.power()數(shù)組元素求n次方案例

熱門標(biāo)簽:南京crm外呼系統(tǒng)排名 汕頭電商外呼系統(tǒng)供應(yīng)商 賓館能在百度地圖標(biāo)注嗎 云南地圖標(biāo)注 400電話 申請(qǐng) 條件 電銷機(jī)器人 金倫通信 crm電銷機(jī)器人 北京外呼電銷機(jī)器人招商 鄭州智能外呼系統(tǒng)中心

如下所示:

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ī)意義上的暴力,過程中通過動(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ù)組的問題

標(biāo)簽:懷化 石家莊 梅州 昆明 西寧 浙江 文山 錫林郭勒盟

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python numpy.power()數(shù)組元素求n次方案例》,本文關(guān)鍵詞  python,numpy.power,數(shù)組,元素,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python numpy.power()數(shù)組元素求n次方案例》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于python numpy.power()數(shù)組元素求n次方案例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    邹平县| 九江县| 阜宁县| 攀枝花市| 翁源县| 淮安市| 那坡县| 安泽县| 铜川市| 弥勒县| 长泰县| 抚顺县| 淮南市| 玉龙| 白城市| 荃湾区| 牡丹江市| 邹城市| 吉木萨尔县| 茶陵县| 枣阳市| 青岛市| 金昌市| 山东省| 东海县| 成都市| 眉山市| 沽源县| 措美县| 正蓝旗| 新化县| 普洱| 盐城市| 乐清市| 定远县| 扎赉特旗| 辽阳市| 鲁甸县| 蒲城县| 阿勒泰市| 仪征市|