Numpy | Linear Algebra - GeeksforGeeks

15 Nov, 2018
The Linear Algebra module of NumPy offers various methods to apply linear algebra on any numpy array.One can find:
rank, determinant, trace, etc. of an array.
eigen values of matrices
matrix and vector products (dot, inner, outer,etc. product), matrix exponentiation
solve linear or tensor equations and much more!
Output:
Rank of A: 3

Trace of A: 11

Determinant of A: -306.0

Inverse of A:
 [[ 0.17647059 -0.00326797 -0.02287582]
 [ 0.05882353 -0.13071895  0.08496732]
 [-0.11764706  0.1503268   0.05228758]]

Matrix A raised to power 3:
 [[336 162 228]
 [406 162 469]
 [698 702 905]]
 
numpy.linalg.eigh(a, UPLO=’L’) : This function is used to return the eigenvalues and eigenvectors of a complex Hermitian (conjugate symmetric) or a real symmetric matrix.Returns two objects, a 1-D array containing the eigenvalues of a, and a 2-D square array or matrix (depending on the input type) of the corresponding eigenvectors (in columns).
Output :
Array is : [[ 1.+0.j,  0.-2.j],
                [ 0.+2.j,  5.+0.j]]

Eigen value is : [ 0.17157288,  5.82842712]

Eigen value is : [[-0.92387953+0.j , -0.38268343+0.j ],
       [ 0.00000000+0.38268343j,  0.00000000-0.92387953j]]

 numpy.linalg.eig(a) : This function is used to compute the eigenvalues and right eigenvectors of a square array.
Output :
Array is : [[1  0  0],
                 [0  2  0],
                 [0  0  3]]

Eigen value is : [ 1  2  3]

Eigen value is : [[ 1  0  0],
                 [  0  1  0],
                 [  0  0  1]]

 
numpy.dot(vector_a, vector_b, out = None) : returns the dot product of vectors a and b. It can handle 2D arrays but considering them as matrix and will perform matrix multiplication. For N dimensions it is a sum product over the last axis of a and the second-to-last of b :
dot(a, b)[i,j,k,m] = sum(a[i,j,:] * b[k,:,m]) 
Code #1:
Output:
Dot Product of scalar values  :  20
Dot Product  :  (-7+22j)

 How Code #1 works ?
vector_a = 2 + 3j
vector_b = 4 + 5j

now dot product 
= 2(4 + 5j) + 3j(4 - 5j)
                = 8 + 10j + 12j - 15
                = -7 + 22j
 numpy.vdot(vector_a, vector_b) : Returns the dot product of vectors a and b. If first argument is complex the complex conjugate of the first argument(this is where vdot() differs working of dot() method) is used for the calculation of the dot product. It can handle multi-dimensional arrays but working on it as a flattened array.
Code #1:
Output :
Dot Product  :  (23-2j)

 How Code #1 works ?
vector_a = 2 + 3j
vector_b = 4 + 5j

As per method, take conjugate of vector_a i.e. 2 - 3j

now dot product = 2(4 - 5j) + 3j(4 - 5j)
                = 8 - 10j + 12j + 15
                = 23 - 2j
numpy.linalg.solve() : Solve a linear matrix equation, or system of linear scalar equations.Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.
Output:
Solution of linear equations: [ 2.  3.]

 numpy.linalg.lstsq() : Return the least-squares solution to a linear matrix equation.Solves the equation a x = b by computing a vector x that minimizes the Euclidean 2-norm || b – a x ||^2. The equation may be under-, well-, or over- determined (i.e., the number of linearly independent rows of a can be less than, equal to, or greater than its number of linearly independent columns). If a is square and of full rank, then x (but for round-off error) is the “exact” solution of the equation.
Output:
numpy.linalg.det() : Compute the determinant of an array.
Output:
Determinant of A: -306.0

 numpy.trace() : Return the sum along diagonals of the array.If a is 2-D, the sum along its diagonal with the given offset is returned, i.e., the sum of elements a[i,i+offset] for all i.If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-arrays whose traces are returned. The shape of the resulting array is the same as that of a with axis1 and axis2 removed.
Output:
Trace of A: 11

 
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
Java Program for Depth First Search or DFS for a Graph
Python Program for Breadth First Search or BFS for a Graph
Best Time to Buy and Sell Stock
Must Do Coding Questions for Product Based Companies
C Program For Deleting A Node In A Linked List
Axios in React: A Guide for Beginners
Java Program For Inserting A Node In A Linked List
Tailwind CSS vs Bootstrap
Image Sharpening Using Laplacian Filter and High Boost Filtering in MATLAB
Create a real time voice translator using Python