ADVERTISEMENT
ADVERTISEMENT

What are the Mathematical Functions in NumPy? 

NumPy's mathematical functions provide array operations. Arithmetic, trigonometry, rounding, exponents and logarithms, complex number operations, and statistics (mean, median, etc.) are examples. Scientific computing requires fast, element-wise array functions. These functions are extremely fast and efficient. Understant the various NumPy's mathematical functions with examples

Different Types of Mathematical Functions in NumPy

Different categories of mathematical functions provided by NumPy:

  1. Arithmetic Operations
  2. Trigonometric Functions
  3. Hyperbolic Functions
  4. Rounding Functions
  5. Exponents and Logarithms
  6. Complex Number Functions
  7. Statistical Functions
  8. Other Special Functions

Arithmetic Functions

NumPy allows element-wise arithmetic operations such as addition (np.add), subtraction (np.subtract), multiplication (np.multiply), division (np.divide), and exponentiation (np.power).

  • np.add
  • np.subtract
  • np.multiply
  • np.divide
  • np.power
  • np.mod
import numpy as np

# Create a simple array
arr1 = np.array([1, 2, 3, 4])
arr2 = np.array([5, 6, 7, 8])

print("Array 1:", arr1)
print("Array 2:", arr2)

# Arithmetic operations
print("Addition:", np.add(arr1, arr2))      # Output: [ 6  8 10 12]
print("Subtraction:", np.subtract(arr1, arr2)) # Output: [-4 -4 -4 -4]
print("Multiplication:", np.multiply(arr1, arr2)) # Output: [ 5 12 21 32]
print("Division:", np.divide(arr1, arr2))   # Output: [0.2        0.33333333 0.42857143 0.5       ]
print("Exponentiation:", np.power(arr1, 2)) # Output: [ 1  4  9 16]

Trignometric Functions

NumPy provides all the standard trigonometric functions like np.sin, np.cos, np.tan, and functions for converting between degrees and radians (np.deg2rad, np.rad2deg).

  • np.sin
  • np.cos
  • np.tan
import numpy as np

# Create an array of angles in degrees
degrees = np.array([0, 30, 45, 60, 90])

# Convert degrees to radians, as np.sin, np.cos and np.tan expect angles in radians
radians = np.radians(degrees)

print("Angles in degrees:", degrees)
print("Angles in radians:", radians)

# Apply the trigonometric functions
print("Sine of angles:", np.sin(radians))       # Output: [0.         0.5        0.70710678 0.8660254  1.        ]
print("Cosine of angles:", np.cos(radians))     # Output: [1.00000000e+00 8.66025404e-01 7.07106781e-01 5.00000000e-01 6.12323400e-17]
print("Tangent of angles:", np.tan(radians))    # Output: [0.00000000e+00 5.77350269e-01 1.00000000e+00 1.73205081e+00 1.63312394e+16]

Hyperbolic Functions

NumPy also provides hyperbolic trig functions such as np.sinh, np.cosh, and np.tanh.

  • np.sinh
  • np.cosh
  • np.tanh
import numpy as np

# Create a simple array
arr = np.array([0, 1, 2, 3, 4])

print("Array:", arr)

# Hyperbolic functions
print("Sinh:", np.sinh(arr))  # Output: [ 0.          1.17520119  3.62686041 10.01787493 27.2899172 ]
print("Cosh:", np.cosh(arr))  # Output: [ 1.          1.54308063  3.76219569 10.067662   27.30823284]
print("Tanh:", np.tanh(arr))  # Output: [0.         0.76159416 0.96402758 0.99505475 0.9993293 ]

Rounding Functions

np.around (rounds to the given number of decimals), np.floor (rounds down), and np.ceil (rounds up).

  • np.around
  • np.floor
  • np.ceil
import numpy as np

# Create a simple array
arr = np.array([0.1, 1.5, 2.7, 3.3, 4.9])

print("Array:", arr)

# Rounding functions
print("Round:", np.round(arr))       # Output: [0. 2. 3. 3. 5.]
print("Floor:", np.floor(arr))       # Output: [0. 1. 2. 3. 4.]
print("Ceil:", np.ceil(arr))         # Output: [1. 2. 3. 4. 5.]

Exponents and Logariths Functions

np.exp (calculates the exponent of all elements in the input array), np.log (natural logarithm), np.log2 (base-2 logarithm), and np.log10 (base-10 logarithm).

  • np.exp
  • np.log
  • np.log2
  • np.log10
import numpy as np

# Create a simple array
arr = np.array([1, 2, 3, 4])

print("Array:", arr)

# Exponents and logarithms
print("Exponentiation:", np.exp(arr))          # Output: [ 2.71828183  7.3890561  20.08553692 54.59815003]
print("Natural logarithm:", np.log(arr))       # Output: [0.         0.69314718 1.09861229 1.38629436]
print("Base 2 logarithm:", np.log2(arr))       # Output: [0.         1.         1.5849625  2.        ]
print("Base 10 logarithm:", np.log10(arr))     # Output: [0.         0.30103    0.47712125 0.60205999]

Complex Number Functions

 np.angle (returns the angle of the complex argument), np.real (returns the real part of the complex argument), np.imag (returns the imaginary part of the complex argument), np.conj (returns the complex conjugate, which is obtained by changing the sign of its imaginary part).

  • np.angle
  • np.real
  • np.imag
  • np.conj
import numpy as np

# Create an array of complex numbers
complex_arr = np.array([1+2j, 2-3j, 4+0j, -3-3j])

print("Complex Array:", complex_arr)

# Complex number functions
print("Real part:", np.real(complex_arr))   # Output: [ 1.  2.  4. -3.]
print("Imaginary part:", np.imag(complex_arr)) # Output: [ 2. -3.  0. -3.]
print("Conjugate:", np.conj(complex_arr))   # Output: [1.-2.j 2.+3.j 4.-0.j -3.+3.j]

Statistical Functions

 np.sum (adds all the elements), np.min (finds the minimum value), np.max (finds the maximum value), np.mean (calculates the arithmetic mean), np.median (computes the median), np.std (computes the standard deviation), np.var (computes variance), among others.

  • np.sum
  • np.min
  • np.max
  • np.mean
  • np.median
  • np.std
  • np.var
import numpy as np

# Create a simple array
arr = np.array([1, 2, 3, 4, 5])

print("Array:", arr)

# Statistical functions
print("Sum:", np.sum(arr))          # Output: 15
print("Minimum:", np.min(arr))      # Output: 1
print("Maximum:", np.max(arr))      # Output: 5
print("Mean:", np.mean(arr))        # Output: 3.0
print("Median:", np.median(arr))    # Output: 3.0
print("Standard deviation:", np.std(arr))   # Output: 1.4142135623730951
print("Variance:", np.var(arr))     # Output: 2.0

Other Special Functions

 np.sqrt (square root), np.cbrt (cube root), np.abs (absolute value), np.sign (computes the sign of each element), np.mod (element-wise modulus operation), etc.

  • np.sqrt
  • np.cbrt
  • np.abs
  • np.sign
import numpy as np

# Create an array
arr = np.array([0.1, 0.2, 0.5, 0.7, 1.0])

print("Array:", arr)

# Special functions
print("Absolute values:", np.abs(arr - 0.5))        # Output: [0.4 0.3 0.  0.2 0.5]
print("Square root:", np.sqrt(arr))                 # Output: [0.31622777 0.4472136  0.70710678 0.83666003 1.        ]
print("Exponential minus one:", np.expm1(arr))      # Output: [0.10517092 0.22140276 0.64872127 1.01375271 1.71828183]
print("Natural logarithm plus one:", np.log1p(arr)) # Output: [0.09531018 0.18232156 0.40546511 0.53062825 0.69314718]

 


ADVERTISEMENT

ADVERTISEMENT