Mathematical functions for array and matrix operations in Numpy

Click “Xiaobai Xue Vision” above and choose to add “star” or “pin

Heavy stuff, delivered as soon as possible

Numpy is a powerful Python calculation library. It provides a wide range of mathematical functions that can perform various operations on arrays and matrices. This article will sort out some basic and commonly used mathematical operations.

  • Basic mathematical operations: Numpy provides many basic mathematical functions for performing operations such as addition, subtraction, multiplication, and division on arrays. These functions include numpy.add(), numpy.subtract(), numpy.multiply(), and numpy.divide().

  • Linear algebra functions: Numpy also provides a number of linear algebra functions for performing operations such as matrix multiplication, determinants, and inversions. These functions include numpy.dot(), numpy.linalg.det(), and numpy.linalg.inv().

  • Statistical and probability functions: Numpy provides a number of statistical and probability functions for performing operations such as mean, median, standard deviation, and correlation. These functions include numpy.mean(), numpy.median(), numpy.std(), and numpy.corrcoef().

  • Trigonometric and logarithmic functions: Numpy also provides a number of trigonometric and logarithmic functions for performing operations such as sine, cosine, tangent and logarithm. These functions include numpy.sin(), numpy.cos(), numpy.tan(), and numpy.log().

Basic mathematical operations

We’ll cover basic math operations:

Addition

Use numpy.add() to add two array elements one at a time. For example, to add two arrays a and b, you can use the following code:

import numpy as np
 a = np.array([1, 2, 3])
 b = np.array([4, 5, 6])
 c = np.add(a, b)
 print(c) # Output: [5, 7, 9]

You can also use the + operator:

c = a + b
 print(c) # Output: [5, 7, 9]

Subtraction

numpy.subtract() can be used to subtract an array from another element. For example, to subtract array b from array a, you would use the following code:

import numpy as np
 a = np.array([1, 2, 3])
 b = np.array([4, 5, 6])
 c = np.subtract(a, b)
 print(c) # Output: [-3, -3, -3]

You can also use the – operator:

c = a - b
 print(c) # Output: [-3, -3, -3]

Multiplication

The numpy.multiply() function can be used to multiply two arrays element-wise. For example, to multiply two arrays a and b, you can use the following code:

import numpy as np
 a = np.array([1, 2, 3])
 b = np.array([4, 5, 6])
 c = np.multiply(a, b)
 print(c) # Output: [4, 10, 18]

You can also use the * operator:

c = a * b
 print(c) # Output: [4, 10, 18]

One thing to note is that this is element-wise multiplication, and dot product multiplication uses dot, which will be introduced later. Therefore, this operation requires that the dimensions of the two variables are the same. If they are different, the broadcast operation will be performed first.

Division

The numpy.divide() function can be used to divide an array by another element. For example, to divide array a by array b, you would use the following code:

import numpy as np
 a = np.array([1, 2, 3])
 b = np.array([4, 5, 6])
 c = np.divide(a, b)
 print(c) # Output: [0.25, 0.4, 0.5]

You can also use the / operator:

c = a / b
 print(c) # Output: [0.25, 0.4, 0.5]

Again: all of the above functions are applied element wise on the input array, so they return an array with the same shape as the input.

Linear algebra function

The most common linear algebra function has

Dot product

The numpy.dot() function can be used to calculate the dot product of two arrays. For example, to calculate the dot product of two 1-D arrays a and b, you can use the following code:

import numpy as np
 a = np.array([1, 2, 3])
 b = np.array([4, 5, 6])
 c = np.dot(a, b)
 print(c) #Output: 32

Or use the @ operator directly

c = a @ b
 print(c) #Output: 32

Matrix multiplication

The numpy.matmul() function can be used to perform matrix multiplication of two arrays. For example, to perform matrix multiplication of two 2-D arrays a and b, you can use the following code:

import numpy as np
 a = np.array([[1, 2], [3, 4]])
 b = np.array([[5, 6], [7, 8]])
 c = np.matmul(a, b)
 print(c)
 #Output:
 # [[19 22]
 # [43 50]]

Matrix multiplication can be performed using the @ operator:

c = a @ b
 print(c)
 #Output:
 # [[19 22]
 # [43 50]]

Transpose

The numpy.transpose() function can be used to transpose an array. For example, to transpose a 2-D array a, you would use the following code:

import numpy as np
 a = np.array([[1, 2], [3, 4]])
 b = np.transpose(a)
 print(b)
 #Output:
 # [[1 3]
 # [2 4]]

You can also use the .T attribute directly to transpose the array:

b = a.T
 print(b)
 #Output:
 # [[1 3]
 # [2 4]]

Determinant

The numpy.linalg.det() function can be used to calculate the determinant of a square array. For example, to calculate the determinant of a two-dimensional array a, you can use the following code:

import numpy as np
 a = np.array([[1, 2], [3, 4]])
 d = np.linalg.det(a)
 print(d) # Output: -2.000000000000000

Note that the input array must be a square array, i.e. it must have the same number of rows and columns.

Inverse

The numpy.linalg.inv() function can be used to calculate the inverse inverse of a square array. For example, to calculate the inverse of a 2-D array a, you can use the following code:

import numpy as np
 a = np.array([[1, 2], [3, 4]])
 b = np.linalg.inv(a)
 print(b)
 #Output:
 # [[-twenty one. ]
 # [ 1.5 -0.5]]

It should be noted that the input array must be a square matrix and the determinant must be non-zero. Otherwise, numpy will raise LinAlgError.

The above are our commonly used linear algebra functions. There are more functions to calculate linear algebra operations on matrices and arrays. You can view the Numpy documentation.

Trigonometric and logarithmic functions

Numpy contains some of the most commonly used trigonometric functions including Numpy .sin(), Numpy .cos(), Numpy .tan(), Numpy .arcsin(), Numpy .arccos(), Numpy .arctan() or Numpy .log(). Example of numpy.sin():

import numpy as np
 a = np.array([0, 30, 45, 60, 90])
 b = np.sin(a)
 print(b)
 # Output: [ 0. 0.5 0.70710678 0.8660254 1. ]

numpy.logComputes the natural logarithm as the reciprocal of an exponential function, so log(exp(x)) = x. The natural logarithm is the logarithm with base e.

import numpy as np
 np.log([1, np.e, np.e**2, 0])
 #array([ 0., 1., 2., -Inf])

The above is a summary of commonly used mathematical functions in Numpy. I hope it will be helpful to you. In addition, Numpy’s documentation is very detailed. If you want to find any functions, you can directly query: https://numpy.org/doc/

Author: Mario Rodriguez

Download 1: OpenCV-Contrib extension module Chinese version tutorial

Reply in the background of the "Xiaobai Xue Vision" public account: Chinese tutorial on extension module, you can download the first Chinese version of OpenCV extension module tutorial on the entire network, covering extension module installation, SFM algorithm, stereo vision, target tracking, biological vision, super Resolution processing and more than twenty chapters.


Download 2: Python visual practical project 52 lectures
Reply in the background of the "Xiaobai Xue Vision" public account: Python visual practical projects, you can download them, including image segmentation, mask detection, lane line detection, vehicle counting, adding eyeliner, license plate recognition, character recognition, emotion detection, text content extraction, 31 practical vision projects, including facial recognition, support rapid school computer vision.


Download 3: 20 lectures on OpenCV practical projects
Reply in the background of the "Xiaobai Xue Vision" public account: OpenCV practical projects 20 lectures, you can download 20 practical projects based on OpenCV to achieve advanced OpenCV learning.


Communication group

Welcome to join the public account reader group to communicate with peers. Currently, there are WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions, etc. (will be gradually subdivided in the future). Please scan the WeChat ID below to join the group, and note: "nickname + school/company + research direction", for example: "Zhang San + Shanghai Jiao Tong University + visual SLAM". Please note according to the format, otherwise it will not be approved. After successful addition, you will be invited to join the relevant WeChat group according to the research direction. Please do not send advertisements in the group, otherwise you will be asked to leave the group, thank you for your understanding~