Python Example for Beginners

Python Example – Write a NumPy program to calculate the QR decomposition of a given matrix

(Python Example for Beginners)   Write a NumPy program to calculate the QR decomposition of a given matrix. From Wikipedia: In linear algebra, a QR decomposition (also called a QR factorization) of a matrix is a decomposition of a matrix A into a product A = QR of an orthogonal matrix Q and an upper …

Python Example – Write a NumPy program to compute the inverse of a given matrix

(Python Example for Beginners)   Write a NumPy program to compute the inverse of a given matrix.   Sample Solution : Python Code : import numpy as np m = np.array([[1,2],[3,4]]) print(“Original matrix:”) print(m) result = np.linalg.inv(m) print(“Inverse of the said matrix:”) print(result) Sample Output: Original matrix: [[1 2] [3 4]] Inverse of the said matrix: …

Python Example – Write a NumPy program to compute the determinant of an array

(Python Example for Beginners)   Write a NumPy program to compute the determinant of an array. From Wikipedia: In linear algebra, the determinant is a value that can be computed from the elements of a square matrix. The determinant of a matrix A is denoted det(A), det A, or |A|. Geometrically, it can be viewed …

Python Example – Write a NumPy program to find a matrix or vector norm

(Python Example for Beginners)   Write a NumPy program to find a matrix or vector norm. Notes on Vector and Matrix Norms from here.   Sample Solution : Python Code : import numpy as np v = np.arange(7) result = np.linalg.norm(v) print(“Vector norm:”) print(result) m = np.matrix(‘1, 2; 3, 4’) result1 = np.linalg.norm(m) print(“Matrix norm:”) print(result1) Sample …

Python Example – Write a NumPy program to compute the condition number of a given matrix

(Python Example for Beginners)   Write a NumPy program to compute the condition number of a given matrix. From Wikipedia, In the field of numerical analysis, the condition number of a function with respect to an argument measures how much the output value of the function can change for a small change in the input …

Python Example – Write a NumPy program to compute the Kronecker product of two given multi-dimension arrays

(Python Example for Beginners)   Write a NumPy program to compute the Kronecker product of two given multi-dimension arrays. Note: In mathematics, the Kronecker product, denoted by ⊗, is an operation on two matrices of arbitrary size resulting in a block matrix. It is a generalization of the outer product (which is denoted by the …

Python Example – Write a NumPy program to compute the inner product of vectors for 1-D array

(Python Example for Beginners)   Write a NumPy program to compute the inner product of vectors for 1-D arrays (without complex conjugation) and in higher dimension.   Sample Solution : Python Code : import numpy as np a = np.array([1,2,5]) b = np.array([2,1,0]) print(“Original 1-d arrays:”) print(a) print(b) print result = np.inner(a, b) print(“Inner product of …

Python Example – Write a NumPy program to evaluate Einstein’s summation convention of two given multidimensional arrays

(Python Example for Beginners)   Write a NumPy program to evaluate Einstein’s summation convention of two given multidimensional arrays. Note: In mathematics, especially in applications of linear algebra to physics, the Einstein notation or Einstein summation convention is a notational convention that implies summation over a set of indexed terms in a formula, thus achieving …

Python Example – Write a NumPy program to compute the outer product of two given vectors

(Python Example for Beginners)   Write a NumPy program to compute the outer product of two given vectors.   Sample Solution : Python Code : import numpy as np p = [[1, 0], [0, 1]] q = [[1, 2], [3, 4]] print(“original matrix:”) print(p) print(q) result = np.outer(p, q) print(“Outer product of the said two vectors:”) …

Python Example – Write a NumPy program to compute the multiplication of two given matrixes

(Python Example for Beginners)   Write a NumPy program to compute the multiplication of two given matrixes. Sample Matrix: [[1, 0], [0, 1]] [[1, 2], [3, 4]]   Sample Solution : Python Code : import numpy as np p = [[1, 0], [0, 1]] q = [[1, 2], [3, 4]] print(“original matrix:”) print(p) print(q) result1 = …