python numpy. power of Array Elements for n Solution Example

  • 2021-09-24 23:06:00
  • OfStack

As shown below:


numpy.power(x1, x2)

The elements of the array are evaluated to the n power respectively. x2 can be either a number or an array, but x1 and x2 have the same number of columns.


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

Supplement: python to find the function of n power _ python to realize pow function (find n power, find n power)

Type 1: n power

Realize pow (x, n), that is, calculate n power function of x. Where n is an integer. Realization of pow Function--leetcode

Solution 1: Violence

It is not violence in the conventional sense, and the solution is accelerated by dynamically adjusting the size of the base in the process. The code is as follows:


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 #  Record the current product value 
tmp = x #  Record the current factor 
count = 1 #  Record how many times the current factor is the base 
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

Solution 2: Classify according to odd and even powers (recursive method, iterative method, bit operation method)

If n is even, pow (x, n) = pow (x ^ 2, n/2);

If n is odd, pow (x, n) = x*pow (x, n-1).

The recursive code is implemented as follows:


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: # If it is an even number 
return self.help_(x*x, n//2)
#  If it is odd 
return self.help_(x*x,(n-1)//2)*x

The iteration code is as follows:


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

Brief introduction of python bit operator

In fact, it is similar to the above method, except that the parity is judged by bit operator and divided by 2 (shift operation). The code is as follows:


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: # Represents odd numbers 
final *= x
x *= x
n >>= 1 #  Right shift 1 Bit 
return final if judge else 1/final

Type 2: Find n power

Realize pow (x, n), that is, calculate n power function of x. Where x is greater than 0 and n is an integer greater than 1.

Solution: 2-point method to find the prescription

The idea is to gradually approach the target value. Take x greater than 1 as an example:

Set the result range to [low, high], where low=0, high = x, and assume the result to be r= (low+high)/2;

If the power of n of r is greater than x, it means that r is larger, and the redefinition of low remains unchanged, high=r, r= (low+high)/2;

If the power of n of r is less than x, it means that r is small, redefine low=r, high remains unchanged, r= (low+high)/2;

The code is as follows:


class Solution:
def myPow(self, x: float, n: int) -> float:
# x Is greater than 0 Number of , Because negative numbers cannot be squared ( Do not consider the plural case )
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 #  For cases greater than 1 If the current value is greater than itself, there is no need to calculate it 
if x <1 and judge
if abs(judge-x)<0.0000001: #  Judge whether the accuracy requirements are met 
print(pow(x,1/n)) # pow Function calculation result 
return r
else:
if judge>x:
high = r
else:
low = r

Related articles: