Saturday, November 16, 2024
Google search engine
HomeLanguagesReturn the result of the power to which the negative input value...

Return the result of the power to which the negative input value is raised with scimath in Python

In this article, we will discuss how to Return the result of the power to which the negative input value is raised with scimath in Python and NumPy.

Example

Input: [-1,-2,-3]

Output: [1.-0.j 4.-0.j 9.-0.j]

Explanation: It return x to the power p, (x**p), If x contains negative values, the output is converted to the complex domain.

NumPy.lib.scimath.power method

The lib.scimath.power() from the NumPy package is used to return the power to which the negative input value is raised. The result is (x**p) returns x to the power p. The result is translated to the complex domain if x contains negative values.

Syntax: numpy.lib.scimath.power(x,p)

Parameters:

  • x:  Input array or scalar.
  • p: The number of times x is multiplied.

Return: return x to the power p, (x**p), If x contains negative values, the output is converted to the complex domain.

Example 1:

In this example, we are importing the NumPy package and an array is created using the np.array() method. Information about the array such as shape, datatype, and dimension can be found using the .shape, .dtype, and .ndim attributes. An array of negative values are created in this example and it is raised to a power 2 using the lib.scimath.power() method. As we can see x contains negative values, the output is converted to the complex domain.

Python3




# import packages
import numpy as np
  
# Creating an array
array = np.array([-1,-2,-3])
print(array)
  
# shape of the array is
print("Shape of the array is : ",array.shape)
  
# dimension of the array
print("The dimension of the array is : ",array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
  
# computing power of negative input values
print(np.lib.scimath.power(array,2))


Output:

[-1 -2 -3]
Shape of the array is :  (3,)
The dimension of the array is :  1
Datatype of our Array is :  int64
[1.-0.j 4.-0.j 9.-0.j]

Example 2:

In this example, an array of positive values are passed in the lib.scimath.power() method.

Python3




# import packages
import numpy as np
  
# Creating an array
array = np.array([1,2,3])
print(array)
  
# shape of the array is
print("Shape of the array is : ",array.shape)
  
# dimension of the array
print("The dimension of the array is : ",array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
  
# computing power of negative input values
print(np.lib.scimath.power(array,2))


Output:

[1 2 3]
Shape of the array is :  (3,)
The dimension of the array is :  1
Datatype of our Array is :  int64
[1 4 9]

Example 3:

In this example instead of giving a positive ‘p-value’, NEGATIVE power is given.

Python3




# import packages
import numpy as np
  
# Creating an array
array = np.array([25,36])
print(array)
  
# shape of the array is
print("Shape of the array is : ",array.shape)
  
# dimension of the array
print("The dimension of the array is : ",array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
  
# computing power of negative power
print(np.lib.scimath.power(array,-2))


Output:

[25 36]
Shape of the array is :  (2,)
The dimension of the array is :  1
Datatype of our Array is :  int64
[0.0016    0.0007716]

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments