IKH

chapter 21

Numpy Mathematical Functions

  • The functions which operates element by element on whole array, are called universal functions.
  • To perform mathematical operations numpy library contains several universal functions (ufunc).
  • np.exp(a) —>Takes e to the power of each value. e value: 2.7182.
  • np.sqrt(a) —>Returns square root of each value.
  • np.log(a) —>Returns logarithm of each value.
  • np.sin(a) —->Returns the sine of each value.
  • np.cos(a) —>Returns the co-sine of each value.
  • np.tan(a) —>Returns the tangent of each value.

Example

Python
import numpy as np
import math
a = np.array([[1,2],[3,4]])
print(f"np.exp(a) value :\n {np.exp(a)}")
print(f"Value of e power 1 ==> {math.exp(1)} ")
print(f"Value of e power 2 ==> {math.exp(2)} ")
print(f"Value of e power 3 ==> {math.exp(3)} ")
print(f"Value of e power 4 ==> {math.exp(4)} ")

Output

PowerShell
np.exp(a) value :
[[ 2.71828183 7.3890561 ]
[20.08553692 54.59815003]]
Value of e power 1 ==> 2.718281828459045
Value of e power 2 ==> 7.38905609893065
Value of e power 3 ==> 20.085536923187668
Value of e power 4 ==> 54.598150033144236

Example

Python
# Mathematical functions
a = np.arange(5)
print(f"exp() on a ==> {np.exp(a)}")

Output

PowerShell
Output nhi h

Example

Python
print(f"sqrt() on a ==> {np.sqrt(a)}")
print(f"sin() on a ==> { np.sin(a)}")
print(f"cos() on a ==> {np.cos(a)}")
print(f"tan() on a ==> {np.tan(a)}")
print(f"log() on a :{np.log(a)}")

Output

PowerShell
exp() on a ==>[ 1.        2.71828183 7.3890561 20.08553692 54.59815003]
sqrt() on a ==>[0.      1.        1.41421356 1.73205081 2.        ]
sin() on a ==>[ 0.        0.84147098 0.90929743 0.14112001 -0.7568025 ]
cos() on a ==>[ 1.        0.54030231 -0.41614684 -0.9899925 -0.65364362]
tan() on a ==>[ 0.        1.55740772 -2.18503986 -0.14254654 1.15782128]
log() on a :[      -inf 0.      0.69314718 1.09861229 1.38629436]

<ipython-input-489-9de2ffb7f031>:8: RuntimeWarning: divide by zero encountered in log
    print(f"log() on a :{np.log(a)}")