Hallo guys, Berjumpa dengan Miniblog dari Inzaghi's Blog! Jika sebelumnya sudah membahas tentang cara menampilkan Rumus Plot Trigonometri menggunakan NumPy, SciPy, dan Matplotlib, kali ini kita akan menampilkan Rumus Operasi Matriks menggunakan SciPy dan NumPy.
Berikut ini adalah Program Python untuk Menampilkan Rumus Operasi Matriks menggunakan SciPy dan NumPy.
1. Import Library
import numpy as npimport scipy.linalg as la
2. Mendefinisikan Vektor dan Matriks
# Define the vectors and matrixvec1 = np.array([-1., 4., -9.])mat1 = np.array([[1., 3., 5.], [7., -9., 2.], [4., 6., 8.]])
3. Menghitung Vektor
# 1. Multiply vec1 by a constant (np.pi/4)vec2 = (np.pi/4) * vec1# 2. Apply the cosine function to vec2vec2 = np.cos(vec2)# 3. Add vec1 and 2 times vec2vec3 = vec1 + 2 * vec2# 4. Calculate the Euclidean norm of vec3euclidean_norm = la.norm(vec3)
4. Menghitung Matriks
# 5. Perform matrix-vector multiplication between mat1 and vec3vec4 = np.dot(mat1, vec3)# 6. Compute the transpose of mat1mat1_transpose = mat1.T# 7. Compute the determinant of mat1mat1_det = la.det(mat1)# 8. Compute the trace of mat1mat1_trace = np.trace(mat1)
5. Mencari Elemen Terkecil dari Vektor dan Matriks
# 9. Find the smallest element in vec1smallest_element_vec1 = np.min(vec1)# 10. Find the index of the smallest element in vec1index_smallest_element_vec1 = np.argmin(vec1)# 11. Find the smallest element in mat1smallest_element_mat1 = np.min(mat1)
6. Memeriksa dan mem-Verifikasi Penjumlahan Matriks
# 12. Verify if mat1 is a magic squarerow_sums = np.sum(mat1, axis=1)col_sums = np.sum(mat1, axis=0)diagonal_sum1 = np.trace(mat1)diagonal_sum2 = np.trace(np.fliplr(mat1))# Check if all sums are the sameis_magic_square = (len(set(row_sums)) == 1 and len(set(col_sums)) == 1 androw_sums[0] == col_sums[0] == diagonal_sum1 == diagonal_sum2)
7. Mencetak Vektor dan Matriks
print("1. Resulting vec2:", vec2)print("2. Resulting vec2 after applying cosine:", vec2)print("3. Resulting vec3:", vec3)print("4. Euclidean norm of vec3:", euclidean_norm)print("5. Resulting vec4:", vec4)print("6. Transpose of mat1:\n", mat1_transpose)print("7. Determinant of mat1:", mat1_det)print("8. Trace of mat1:", mat1_trace)print("9. Smallest element in vec1:", smallest_element_vec1)print("10. Index of the smallest element in vec1:", index_smallest_element_vec1)print("11. Smallest element in mat1:", smallest_element_mat1)print("12. Is mat1 a magic square?", is_magic_square)
Output :
1. Resulting vec2: [ 0.70710678 -1. 0.70710678]
2. Resulting vec2 after applying cosine: [ 0.70710678 -1. 0.70710678]
3. Resulting vec3: [ 0.41421356 2. -7.58578644]
4. Euclidean norm of vec3: 7.855935892848037
5. Resulting vec4: [-31.51471863 -30.27207794 -47.02943725]
6. Transpose of mat1:
[[ 1. 7. 4.]
[ 3. -9. 6.]
[ 5. 2. 8.]]
7. Determinant of mat1: 161.99999999999997
8. Trace of mat1: 0.0
9. Smallest element in vec1: -9.0
10. Index of the smallest element in vec1: 2
11. Smallest element in mat1: -9.0
12. Is mat1 a magic square? False
8. Konstruksi dan Generasi Matriks dan Submatriks
# 13. Construct a 10x10 matrix named M filled with random numbersM = np.random.rand(10, 10)# 14. Generate the four 5x5 sub-matrices# Upper Left Quarter (MUL)MUL = M[:5, :5]# Upper Right Quarter (MUR)MUR = M[:5, 5:]# Lower Left Quarter (MLL)MLL = M[5:, :5]# Lower Right Quarter (MLR)MLR = M[5:, 5:]
9. Menampilkan Submatriks
# Display the sub-matricesprint("Upper Left Quarter (MUL):\n", MUL)print("\nUpper Right Quarter (MUR):\n", MUR)print("\nLower Left Quarter (MLL):\n", MLL)print("\nLower Right Quarter (MLR):\n", MLR)
Output :
Upper Left Quarter (MUL):
[[0.39613543 0.06659083 0.1353606 0.03575032 0.78565856]
[0.83856055 0.07156087 0.61407801 0.45584335 0.18366205]
[0.67648104 0.47251966 0.20641843 0.48942973 0.23163408]
[0.80090565 0.77390097 0.85108114 0.72952116 0.73008316]
[0.43021931 0.34048256 0.5292398 0.43191999 0.93864353]]
Upper Right Quarter (MUR):
[[0.73586742 0.54266173 0.10776348 0.79440221 0.51642669]
[0.13423576 0.60392076 0.33561124 0.68250265 0.46730018]
[0.58423521 0.76363396 0.21959778 0.8774659 0.09996112]
[0.0157255 0.36378884 0.38811041 0.93804427 0.52344093]
[0.07392527 0.07770546 0.04661559 0.47845337 0.74419622]]
Lower Left Quarter (MLL):
[[0.27387088 0.13349402 0.03315354 0.04740001 0.64161455]
[0.12377607 0.4194496 0.06505218 0.6973751 0.60539034]
[0.9890294 0.35747239 0.36983188 0.11420733 0.78528483]
[0.54299472 0.95740632 0.23606324 0.42562606 0.92802738]
[0.28227684 0.86046471 0.96920774 0.78395278 0.03091222]]
Lower Right Quarter (MLR):
[[0.66121549 0.40610742 0.56539897 0.40851901 0.10947693]
[0.26124489 0.22191262 0.0951539 0.34172111 0.61695321]
[0.39979312 0.52719262 0.03263554 0.06680715 0.45924553]
[0.97422601 0.03391992 0.1604091 0.85942077 0.9710585 ]
[0.35692984 0.14346116 0.6271265 0.51228915 0.04226928]]
10. Hasil di Jupyter Notebook
Mohon maaf apabila ada kesalahan sedikit pun pada Kode Program ini.
Terima Kasih 😀😊😘👌👍 :)