2-2 Numpy-Matrix

 1 # !usr/bin/env python

2 # Author:@vilicute
3
4 import numpy as np
5 # Creation of matrix
6 matr1 = np.mat("4 2 3;4 5 6;7 8 9" )
7 matr2 = np.matrix([[4,5,6],[7,8,9],[1 ,2,3]])
8 print(" matr1= ",matr1)
9 print(" matr2= ",matr2)
10
11 arr1 = np.eye(3)
12 arr2 = arr1*3
13 arr3 = np.random.randint(0,10,size = [3,3])
14 arr4 = np.random.randint(6,10,size = [3,3])
15 matr3 = np.bmat("arr1 arr3;arr4 arr2")
16 print(" matr3= ",matr3)
17
18
19 # Matrix operation
20 matr_numul = matr1*4
21 matr_add = matr1 + matr2
22 matr_sub = matr1 - matr2
23 matr_mul = matr1 * matr2
24 matr_multiply = np.multiply(matr1, matr2)
25 print(" Multiply: " , matr_numul)
26 print(" Add: " , matr_add)
27 print(" Subtract: " , matr_sub)
28 print(" Multiply: " , matr_mul)
29 print(" Multiply corresponding elements: ", matr_multiply)
30
31 print(" Transpose: ", matr1.T)
32 print(" Conjugate transpose: ", matr1.H)
33 print(" Inverse: " , matr1.I)
34 print(" Two-dimensional array view: ", matr1.A)

35 ‘‘‘
36 matr1=
37 [[4 2 3]
38 [4 5 6]
39 [7 8 9]]
40 matr2=
41 [[4 5 6]
42 [7 8 9]
43 [1 2 3]]
44 matr3=
45 [[1. 0. 0. 4. 8. 1 .]
46 [0. 1. 0. 5. 3. 3. ]
47 [0. 0. 1. 5. 1. 1. ]
48 [6. 8. 8. 3. 0. 0. ]
49 [9. 8. 8. 0. 3. 0. ]
50 [9. 7. 7. 0. 0. 3. ]]
51 Multiplication:
52 [[16 8 12]
53 [16 20 24]
54 [28 32 36]]
55 Add:
56 [[ 8 7 9]
57 [11 13 15]
58 [8 10 12]]
59 Subtract:
60 [[ 0 -3 -3]
61 [-3 -3 -3]
62 [6 6 6]]
63 Multiply:
64 [[ 33 42 51]
65 [57 72 87]
66 [93 117 141]]
67 Multiply corresponding elements:
68 [[16 10 18]
69 [28 40 54]
70 [7 16 27]]
71 Transpose:
72 [[4 4 7]
73 [2 5 8]
74 [3 6 9]]
75 Conjugate transpose:
76 [[4 4 7]
77 [2 5 8]
78 [3 6 9]]
79 Inversion:
80 [[ 0.33333333 -0.66666667 0.33333333]
81 [-0.66666667 -1.66666667 1.33333333]
82 [0.33333333 2. -1.33333333]]
83 Two-dimensional array view:
84 [[4 2 3]
85 [4 5 6]
86 [7 8 9]]
87 '''

 1 # !usr/bin/env python

2 # Author:@vilicute
3
4 import numpy as np
5 # Creation of matrix
6 matr1 = np.mat("4 2 3;4 5 6;7 8 9" )
7 matr2 = np.matrix([[4,5,6],[7,8,9],[1 ,2,3]])
8 print(" matr1= ",matr1)
9 print(" matr2= ",matr2)
10
11 arr1 = np.eye(3)
12 arr2 = arr1*3
13 arr3 = np.random.randint(0,10,size = [3,3])
14 arr4 = np.random.randint(6,10,size = [3,3])
15 matr3 = np.bmat("arr1 arr3;arr4 arr2")
16 print(" matr3= ",matr3)
17
18
19 # Matrix operation
20 matr_numul = matr1*4
21 matr_add = matr1 + matr2
22 matr_sub = matr1 - matr2
23 matr_mul = matr1 * matr2
24 matr_multiply = np.multiply(matr1, matr2)
25 print(" Multiply: " , matr_numul)
26 print(" Add: " , matr_add)
27 print(" Subtract: " , matr_sub)
28 print(" Multiply: " , matr_mul)
29 print(" Multiply corresponding elements: ", matr_multiply)
30
31 print(" Transpose: ", matr1.T)
32 print(" Conjugate transpose: ", matr1.H)
33 print(" Inverse: " , matr1.I)
34 print(" Two-dimensional array view: ", matr1.A)

35 ‘‘‘
36 matr1=
37 [[4 2 3]
38 [4 5 6]
39 [7 8 9]]
40 matr2=
41 [[4 5 6]
42 [7 8 9]
43 [1 2 3]]
44 matr3=
45 [[1. 0. 0. 4. 8. 1 .]
46 [0. 1. 0. 5. 3. 3. ]
47 [0. 0. 1. 5. 1. 1. ]
48 [6. 8. 8. 3. 0. 0. ]
49 [9. 8. 8. 0. 3. 0. ]
50 [9. 7. 7. 0. 0. 3. ]]
51 Multiplication:
52 [[16 8 12]
53 [16 20 24]
54 [28 32 36]]
55 Add:
56 [[ 8 7 9]
57 [11 13 15]
58 [8 10 12]]
59 Subtract:
60 [[ 0 -3 -3]
61 [-3 -3 -3]
62 [6 6 6]]
63 Multiply:
64 [[ 33 42 51]
65 [57 72 87]
66 [93 117 141]]
67 Multiply corresponding elements:
68 [[16 10 18]
69 [28 40 54]
70 [7 16 27]]
71 Transpose:
72 [[4 4 7]
73 [2 5 8]
74 [3 6 9]]
75 Conjugate transpose:
76 [[4 4 7]
77 [2 5 8]
78 [3 6 9]]
79 Inversion:
80 [[ 0.33333333 -0.66666667 0.33333333]
81 [-0.66666667 -1.66666667 1.33333333]
82 [0.33333333 2. -1.33333333]]
83 Two-dimensional array view:
84 [[4 2 3]
85 [4 5 6]
86 [7 8 9]]
87 '''

Leave a Comment

Your email address will not be published.