Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/material/linear-algebra-extra.ipynb
934 views
Kernel: Python 3 (ipykernel)

Extra material about Linear Algebra

import numpy as np xrange=range

Custom implementation of general gaussian method

#Gaussian Elimination def row_lamb( i, lamb, A ): B = np.copy(A) B[i] = lamb*B[i] return B #Combination function def row_comb( i, j, lamb, A ): B = np.copy(A) B[i] = lamb*B[j] + B[i] return B #Swap function def row_swap( i, j, A ): B = np.copy(A) B[[i,j]] = B[[j,i]] return B def Gaussian_Elimination( A0 ): #Making local copy of matrix A = np.copy(A0) #Detecting size of matrix n = len(A) #Sweeping all the columns in order to eliminate coefficients of the i-th variable for i in xrange( 0, n ): #Sweeping all the rows for the i-th column in order to find the first non-zero coefficient for j in xrange( i, n ): if A[i,j] != 0: #Normalization coefficient Norm = 1.0*A[i,j] break #Applying swap operation to put the non-zero coefficient in the i-th row A = row_swap( i, j, A ) #Eliminating the coefficient associated to the i-th variable for j in xrange( i+1, n ): A = row_comb( j, i, -A[j,i]/Norm, A ) #Normalizing n-th variable A = row_lamb( n-1, 1.0/A[n-1,n-1], A ) #Finding solution x = np.zeros( n ) x[n-1] = A[n-1,n] for i in xrange( n-1, -1, -1 ): x[i] = ( A[i,n] - sum(A[i,i+1:n]*x[i+1:n]) )/A[i,i] #Upper diagonal matrix and solutions x return A, x
np.random.seed(3) M = np.random.random( (4,5) ) print ("Augmented Matrix M:\n", M, "\n") #Solving using Gaussian Elimination D, x = Gaussian_Elimination(M) print ("Augmented Diagonal Matrix D:\n", D, "\n") print ("Obtained solution:\n", x, "\n") print ("Exact solution:\n", (M[:,:4]**-1*M[:,4]).T, "\n")
Augmented Matrix M: [[0.5507979 0.70814782 0.29090474 0.51082761 0.89294695] [0.89629309 0.12558531 0.20724288 0.0514672 0.44080984] [0.02987621 0.45683322 0.64914405 0.27848728 0.6762549 ] [0.59086282 0.02398188 0.55885409 0.25925245 0.4151012 ]] Augmented Diagonal Matrix D: [[ 5.50797903e-01 7.08147823e-01 2.90904739e-01 5.10827605e-01 8.92946954e-01] [ 0.00000000e+00 -1.02675749e+00 -2.66135662e-01 -7.79783697e-01 -1.01224976e+00] [ 0.00000000e+00 0.00000000e+00 5.24909828e-01 -6.69967089e-02 2.15310020e-01] [ 0.00000000e+00 0.00000000e+00 -1.70372042e-16 1.00000000e+00 9.32073626e-03]] Obtained solution: [0.27395594 0.8721633 0.41137442 0.00932074] Exact solution: [[ 1.62118801 0.9962667 29.88822639 1.51125934] [ 0.62248281 3.51004303 0.9649251 18.38095262] [ 2.324661 3.26310322 1.041764 1.21007418] [ 0.81260526 8.06535367 1.4905571 1.60114669]]