Day: April 1, 2021

Python Example – Write a NumPy program to calculate the Frobenius norm and the condition number of a given array

(Python Example for Beginners)   Write a NumPy program to calculate the Frobenius norm and the condition number of a given array.   Sample Solution: Python Code : import numpy as np a = np.arange(1, 10).reshape((3, 3)) print(“Original array:”) print(a) print(“Frobenius norm and the condition number:”) print(np.linalg.norm(a, ‘fro’)) print(np.linalg.cond(a, ‘fro’)) Sample Output: Original array: [[1 …

Python Example – Write a NumPy program to get the qr factorization of a given array

(Python Example for Beginners)   Write a NumPy program to get the qr factorization of a given array.   Sample Solution: Python Code : import numpy as np a = np.array([[4, 12, -14], [12, 37, -53], [-14, -53, 98]], dtype=np.int32) print(“Original array:”) print(a) q, r = np.linalg.qr(a) print(“qr factorization of the said array:”) print( “q=n”, …

Python Example – Write a NumPy program to compute the sum of the diagonal element of a given array

(Python Example for Beginners)   Write a NumPy program to compute the sum of the diagonal element of a given array.   Sample Solution: Python Code : import numpy as np m = np.arange(6).reshape(2,3) print(“Original matrix:”) print(m) result = np.trace(m) print(“Condition number of the said matrix:”) print(result) Sample Output: Original matrix: [[0 1 2] [3 …

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 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 …