1. Matrix
In NumPy, matrices are subclasses of ndarray and can be created using specialized string formats. Like matrices in mathematical concepts, matrices in NumPy are also two-dimensional. The multiplication operation of matrices is different from the ordinary multiplication operation in NumPy, and the power operation is also different.
1.1. Create matrices (np. mat(). T. I)
#string call np.mat () function create matrix : the rows of the matrix are separated by semicolons, and the elements within the rows are separated by spaces
import numpy as np
A = np.mat("1 2 3;4 5 6;7 8 9")
print(A)
B = np.mat(np.arange(3, 12).reshape(3, 3)) #apply NumPy creating arrays
print(B)
#use T attribute acquisition transpose matrix
print(A.T)
#use I attribute acquisition inverse matrix
print(A.I)
1.2 Create a new matrix from an existing matrix (np, eye(), np. bmat())
#create identity matrix
A = np.eye(3)
print(A)
B = A * 2
print(B)
#using strings np.bmat () create a composite matrix, where matrix variable names can be used instead of numbers
C = np.bmat("A B;B A")
print(C)
2. General functions (np. freepyfunc(), np. zeros)_Like(). flat
The input of a universal function is a set of scalars, and the output is also a set of scalars, which usually correspond to basic mathematical operations such as addition, subtraction, multiplication, division, etc. You can use the freepyfun function in NumPy to create a universal function through a Python function.
import numpy as np
def ultimate_answer(a):
result = np.zeros_like(a) #apply zeros_like create a function and a an array with the same shape and all elements being 0
result.flat = 33 # flat property provides a flat iterator that can set the values of array elements one by one
return result
ufunc = np.frompyfunc(ultimate_answer, 1, 1)
A = ufunc(np.arange(4))
B = ufunc(np.arange(6).reshape(2, 3))
print(A)
print(B)
3. Arithmetic operations (np. add(), np. subtract(), np. multiply(), np. divide(), np. rule)_Divide(), np. floor_Division()
In NumPy, the basic arithmetic operators+- Implicitly associated with * are universal functions add, subtract, and multiply. When using these arithmetic operators on NumPy arrays, the corresponding universal functions will be automatically called. The process involved in division is relatively complex, involving three universal functions: divide and true in array division operations_Divide and Floor_Division, along with two corresponding operators/and//.
a = np.array(([2, 6, 5], [5, 7, 9]))
b = np.array(([1, 2, 3], [4, 5, 6]))
print(a)
print(b)
#plus
add1 = a+b
add2 = np.add(a, b)
print(add1)
print(add2)
#plus
sub1 = a-b
sub2 = np.subtract(a, b)
print(sub1)
print(sub2)
#ride
mul1 = a*b
mul2 = np.multiply(a, b)
print(mul1)
print(mul2)
#division operation of arrays
a = np.array(([2, 6, 5], [5, 7, 9]))
b = np.array(([1, 2, 3], [4, 5, 6]))
print(a)
print(b)
div = np.divide(a, b)
print(div)
tdiv = np.true_divide(a, b)
print(tdiv)
fdiv = np.floor_divide(a, b)
print(fdiv)
c = a // b
print(c)
d = a / b
print(d)
4. Modular operations (np. mod(), np. remainder(), np. fmod())
Modular operation refers to taking modular operation, that is, finding the remainder of m/n, calculating the modulus or remainder, which can be done using the mod, retain, fmod functions, and% operator in NumPy. The main difference between these functions lies in the way they handle negative numbers, and the fmod function is different from other functions in this regard.
import numpy as np
a = np.arange(-5,6)
print(a)
b = np.arange(1,12)
print(b)
c1 = np.mod(a,2)
print(c1)
c2 = np.mod(a,b)
print(c2)
d1 = np.remainder(a,2)
print(d1)
d2 = np.remainder(a,b)
print(d2)
e1 = a % 2
e2 = a % b
print(e1)
print(e2)
f1 = np.fmod(a,2)
print(f1)
f2 = np.fmod(a,b)
print(f2)
5. Fibonacci sequence (np. matrix(), np. cail(), np. rint())
import numpy as np
F = np.matrix([[1, 1], [1, 0]], dtype="int64") #the recursive relationship of the fibonacci sequence can be represented by a matrix
f = []
for i in range(10):
a = (F ** (i))[0, 0]
f.append(a)
print(f)
import numpy as np
n = np.array([-5.6, -4.2, -2.7, 2.7, 4.3, 5.5, 8.1])
print(np.ceil(n)) #round up
print(np.rint(n)) #round to the nearest whole number
6. Lissajous curves (np. linspace(), np. pi, np. sin())
In NumPy, all standard trigonometric functions such as sin, cos, tan, etc. have corresponding universal functions. The Lissajous curve is an interesting way to use trigonometric functions.
x = A sin(at + n/2)
y = B sin(bt)
import numpy as np
from matplotlib.pyplot import plot
from matplotlib.pyplot import show
a = 9
b = 8
#apply linspace function initialization variable t , i.e. from- pi reach pi 201 uniformly distributed points on top
t = np.linspace(-np.pi, np.pi, 201)
#apply sin functions and NumPy constant pi calculate variables x
x = np.sin(a*t + np.pi/2)
#apply sin function calculation variable y
y = np.sin(b * t)
plot(x, y)
show()
7. Square wave, saw tooth wave
Square wave is also a waveform that can be displayed on an oscilloscope. A square wave can be approximated as the superposition of multiple sine waves, and any square wave signal can be represented by an infinite Fourier series.
import numpy as np
from matplotlib.pyplot import plot
from matplotlib.pyplot import show
t = np.linspace(-np.pi, np.pi, 201)
k = np.arange(1, 9.)
k = 2 * k - 1
f = np.zeros_like(t)
for i in range(len(t)):
f[i] = np.sum(np.sin(k * t[i])/k)
f = (4 / np.pi) * f
plot(t, f)
show()
import numpy as np
from matplotlib.pyplot import plot
from matplotlib.pyplot import show
t = np.linspace(-np.pi, np.pi, 201)
k = np.arange(1, 9.)
f = np.zeros_like(t)
for i in range(len(t)):
f[i] = np.sum(np.sin(2 * np.pi * k * t[i])/k)
f = (-2 / np.pi) * f
plot(t, f, lw=1.0)
plot(t, np.abs(f), lw=2.0)
show()
8. Bitwise operations and comparison functions (np. less(), np. bitwise)_Xor(), np. equal(), np. bitwise_And (), np. left_Shift()
Bitwise operation functions can operate on the bits of integers or integer arrays, both of which are general-purpose functions& Amp;, |<<>> The equality operator also has a corresponding part in NumPy,<>== The same goes for comparison operators. With these operators, you can play some advanced techniques in the code to improve its performance. However, they can make the code difficult to understand, so caution should be exercised when using them.
import numpy as np
x = np.arange(-9, 9)
y = -x
print("X:",x)
print("Y:",y)
# XOR or the ^ operator (inequality operator), when the signs of two operands are inconsistent, XOR the result of the operation is a negative number
print("Sign different?", (x ^ y) < 0)
#stay NumPy in, the ^ operator corresponds to bitwise_xor function, < operator corresponds to less function
print("Sign different?", np.less(np.bitwise_xor(x, y), 0))
#stay NumPy middle, & operator corresponds to bitwise_and function, == operator corresponds to equal function
print("Power of 2?n", x, "n", (x & (x - 1)) == 0)
print("Power of 2?n", x, "n", np.equal(np.bitwise_and(x, (x - 1)), 0))
#stay NumPy middle, << operator corresponds to left_shift function
print("Modulus 4n", x, "n", x & ((1 << 2) - 1))
print("Modulus 4n", x, "n", np.bitwise_and(x, np.left_shift(1, 2) - 1))
Summary
Introduced the matrices and general functions in NumPy, including how to create matrices and how general functions work. It also briefly introduces general functions such as arithmetic operation functions, trigonometric functions, bit operation functions, and comparison functions.