Question 1
1. Topic
Generate a 5*5 matrix whose elements are random integers in the interval [1,10].
2. Code
import numpy as np
a = np.random.randint(1,10,25).reshape(5,5)
print(a)
3. Results
[[5 3 7 6 4] [3 7 4 3 7] [1 7 4 4 3] [1 8 5 6 8] [8 4 4 2 9]]
Question 2
1. Topic
Generate a 4*4 matrix whose elements conform to a normal distribution.
2. Code
import numpy as np
b = np.random.randn(16).reshape(4,4)
print(b)
3. Results
[[-0.33990789 -0.53183933 0.12420977 -0.43573047] [-1.47714092 -0.22094742 -0.11919983 -0.19049501] [-0.54613945 1.30368604 -0.483711 -0.36169592] [-1.50361573 -0.06139576 -1.86228913 1.09515015]]
Question 3
1. Topic
Assign all the elements in the second column of the matrix generated in (1) to -1; assign all the elements in the third row to 1000;
2. Code
a[:,1]=-1
a[2,:]=1000 #或者a[2]=1000
print(a)
3. Results
[[ 5 -1 7 6 4] [ 3 -1 4 3 7] [1000 1000 1000 1000 1000] [ 1 -1 5 6 8] [ 8 -1 4 2 9]]
Question 4
1. Topic
Generate a 4*4 matrix A whose elements are random integers in the interval [-10,10].
Then randomly generate a column vector b consisting of 4 elements, whose elements are random integers in the interval [-5,5].
For the linear equation system Ax=b, find the determinant value corresponding to matrix A, judge whether it has a solution according to whether the determinant value is 0, and if there is a solution, find its solution.
2. Code
import scipy.linalg as sp
A=np.random.randint(-10,10,16).reshape(4,4)
b=np.random.randint(-5,5,4).reshape(-1,1)
print('方程形如Ax=b,其中A=')
print(A)
print('其中b=',b)
print()
dA=np.linalg.det(A)
print("A对应的行列式值为:",dA)
if(abs(dA)>1e-6):
sv=sp.solve(A,b)
print("对应的线性方程组解为:",sv)
else:
print("解不存在。")
3. Results
The equation is of the form Ax=b, where A= [[ 3 -1 7 -4] [ 9 0 4 -3] [ 2 -8 6 -4] [ 7 -9 -4 0]] where b= [[-5 ] [-1] [ 4] [ 1]] The determinant value corresponding to A is: 190.00000000000017 The corresponding linear equation solution is: [[ 3.6 ] [-3.92631579] [14.88421053] [30.97894737]]