SoFunction
Updated on 2024-12-14

NumPy Math Functions and Algebraic Operations Implementation Code

I. Introduction to the experiment

1.1 Experimental content

If you use Python for scientific computing, you'll have come across NumPy, an extended library for numerical computation in Python with powerful multi-dimensional array processing and matrix operations. In addition, NumPy has a large number of built-in functions that make it easy to quickly construct mathematical models.

1.2 Experimental knowledge points

  • NumPy Installation
  • Introduction to NumPy Numeric Types

1.3 Experimental environment

  • Python3
  • Jupyter Notebook

1.4 Suitable population groups

This course is of average difficulty and is a beginner-level course for users with a basic knowledge of Python who are interested in using NumPy for scientific computing.

II. Mathematical functions

Using python's own operators, you can add, subtract, multiply, and divide, as well as round up, round down, and calculate powers in math. When you import the included math module, it contains some common math functions such as absolute value, factorial, square, and so on. However, these functions are still relatively basic. If you want to complete more complex math calculations, it will be stretched to the limit.

numpy provides us with more math functions to help us do some numerical calculations better. Here's a look at each in turn.

2.1 Trigonometric Functions

First, take a look at the trigonometric functions provided by numpy. These methods are:

(x)
(x)
(x)
(x)
(x)
(x)
(x1,x2)
(x)
(x)
numpy.deg2rad(x)
numpy.rad2deg(x)

For example, we can convert radians to degrees using numpy.rad2deg(x) as mentioned above.

Sample code:

import numpy as np

np.rad2deg()

2.2 Hyperbolic functions

In mathematics, hyperbolic functions are a class of functions similar to the common trigonometric functions. Hyperbolic functions often appear in the solutions of certain important linear differential equations, and they are computed using numpy as:

(x)
(x)
(x)
(x)
(x)
(x)

2.3 Numerical modification

Numerical modification, also known as number modification, is the process of determining a consistent number of digits according to certain rules before performing a specific numerical operation, and then rounding off the extra trailing digits after certain numbers [via Wikipedia]. For example, we often hear "rounding up or down" is a type of numerical modification.

(a)
numpy.round_(a)
(x)
(x, y)
(x)
(x)
(x)

Choose a few random floating point numbers and see the difference between the above methods.

2.4 Summation, product and difference

The following methods are used to perform sums, products, and differencing within or between elements of an array.

(a, axis, dtype, keepdims)
(a, axis, dtype, keepdims)
(a, axis, dtype, keepdims)
(a, axis, dtype, keepdims)
(a, axis, dtype)
(a, axis, dtype)
(a, axis, dtype)
(a, axis, dtype)
(a, n, axis)
numpy.ediff1d(ary, to_end, to_begin)
(f)
(a, b, axisa, axisb, axisc, axis)
(y, x, dx, axis)

2.5 Indices and logarithms

If you need to do exponential or logarithmic solving, you can use these methods.

(x) :Calculates the indices of all elements in the input array。
numpy.expm1(x) :Calculated for all elements in the array exp(x) - 1.
numpy.exp2(x) :For all input arrays in the p, count 2 ** p。
(x) :count自然对数。
numpy.log10(x) :count常用对数。
numpy.log2(x) :count二进制对数。
numpy.log1p(x) : log(1 + x) 。
(x1, x2) : log2(2**x1 + 2**x2) 。
numpy.logaddexp2(x1, x2) : log(exp(x1) + exp(x2)) 。

2.6 Arithmetic Operations

Of course, numpy also provides some methods for arithmetic operations that are a bit more flexible than the python operators, mainly because they can be used directly on arrays.

(x1, x2)
(x)
(x)
(x1, x2)
(x1, x2)
(x1, x2)
(x1, x2)
(x1, x2)
(x1, x2)
(x1)
(x1, x2)

2.7 Matrix and vector products

Solving for vectors, matrices, dot products of tensors, etc. is also a very powerful aspect of numpy.

(a,b)
(a,b)
(a,b)
(a,b)
(a,b)
(a,b)
(a,b)

2.8 Other

In addition to the above well-categorized methods, there are a number of methods for mathematical operations in numpy, which are summarized below:

(z, deg)
(val)
(val)
(x)
(a, v, mode)
(x)
(x)
(x)
(x)
(x)
(x)
(x1, x2)
(x1, x2)
numpy.nan_to_num(x)
(x, xp, fp, left, right, period)

III. Algebraic operations

Above, we introduced the math functions commonly used in numpy in 8 categories. These methods make complex computations easier to express. In addition, numpy also contains methods for algebraic operations, especially when it comes to matrices, solving eigenvalues, eigenvectors, inverse matrices, and so on, which are very convenient.

(a)
(a ,mode)
(a ,full_matrices,compute_uv)
(a)
(a, UPLO)
(a)
(a, UPLO)
(x ,ord,axis,keepdims)
(x ,p)
(a)
.matrix_rank(M ,tol)
(a)
(a ,offset,axis1,axis2,dtype,out)
(a,b)
(a,b ,axes)
(a,b ,rcond)
(a)
(a ,rcond)
(a ,ind)

This is the whole content of this article.