python numpy.power()数组元素求n次方案例

作者:NothingLZ 时间:2022-06-26 00:11:22 

如下所示:


numpy.power(x1, x2)

数组的元素分别求n次方。x2可以是数字,也可以是数组,但是x1和x2的列数要相同。


>>> 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]])

补充:python求n次方的函数_python实现pow函数(求n次幂,求n次方)

类型一:求n次幂

实现 pow(x, n),即计算 x 的 n 次幂函数。其中n为整数。pow函数的实现——leetcode

解法1:暴力法

不是常规意义上的暴力,过程中通过动态调整底数的大小来加快求解。代码如下:


class Solution:
def myPow(self, x: float, n: int) -> float:
judge = True
if n<0:
n = -n
judge = False
if n==0:
return 1
final = 1 # 记录当前的乘积值
tmp = x # 记录当前的因子
count = 1 # 记录当前的因子是底数的多少倍
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:根据奇偶幂分类(递归法,迭代法,位运算法)

如果n为偶数,则pow(x,n) = pow(x^2, n/2);

如果n为奇数,则pow(x,n) = x*pow(x, n-1)。

递归代码实现如下:


class Solution:
def myPow(self, x: float, n: int) -> float:
if n<0:
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: #如果是偶数
return self.help_(x*x, n//2)
# 如果是奇数
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位运算符简介

其实跟上面的方法类似,只是通过位运算符判断奇偶性并且进行除以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: #代表是奇数
final *= x
x *= x
n >>= 1 # 右移一位
return final if judge else 1/final

类型二:求n次方

实现 pow(x, n),即计算 x 的 n 次幂函数。其中x大于0,n为大于1整数。

解法:二分法求开方

思路就是逐步逼近目标值。以x大于1为例:

设定结果范围为[low, high],其中low=0, high = x,且假定结果为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的数,因为负数无法开平方(不考虑复数情况)
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 # 对于大于1的数,如果当前值已经大于它本身,则无需再算下去
if x <1 and judge
if abs(judge-x)<0.0000001: # 判断是否达到精度要求
print(pow(x,1/n)) # pow函数计算结果
return r
else:
if judge>x:
high = r
else:
low = r

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。

来源:https://www.cnblogs.com/luozeng/p/8544012.html

标签:python,numpy,power,n次方
0
投稿

猜你喜欢

  • django 框架实现的用户注册、登录、退出功能示例

    2023-04-30 16:38:18
  • Python 统计Jira的bug 并发送邮件功能

    2021-03-24 05:44:24
  • MySQL数据库之UPDATE更新语句精解

    2009-03-20 15:21:00
  • Python pygame 动画游戏循环游戏时钟实现原理

    2022-07-02 06:27:27
  • 微软开源最强Python自动化神器Playwright(不用写一行代码)

    2024-01-02 00:38:00
  • Python中关键字is与==的区别简述

    2022-07-09 10:32:09
  • python实现人机对战的井字棋游戏

    2023-02-11 15:37:45
  • MySQL单表查询进阶教程(最全面!)

    2024-01-22 21:06:42
  • Python一些线程的玩法总结

    2023-03-13 12:02:18
  • Python办公自动化PPT批量转换操作

    2023-11-07 16:54:20
  • EF(EntityFramework) 插入或更新数据报错的解决方法

    2024-01-20 19:21:42
  • 解读python logging模块的使用方法

    2021-02-12 08:39:52
  • 如何给MD5加上salt随机盐值

    2022-01-13 05:32:37
  • MySQL数据类型全解析

    2024-01-27 07:44:58
  • 一文理解MySQL数据库的约束与表的设计

    2024-01-21 08:31:12
  • python__name__原理及用法详解

    2021-04-07 02:47:51
  • 详细解读Python中的json操作

    2022-02-21 00:55:13
  • python检测某个变量是否有定义的方法

    2021-05-17 18:45:01
  • python http通信接口开发示例

    2022-06-07 05:15:29
  • SQL 多条件查询几种实现方法详细介绍

    2024-01-16 23:12:31
  • asp之家 网络编程 m.aspxhome.com