Python linear equations example based on Gaussian elimination method, python Linear Equations
This example describes how to calculate a linear equations using the Gaussian elimination method in Python. We will share this with you for your reference. The details are as follows:
#! /Usr/bin/env python # coding = UTF-8 # change the above information as needed def print_matrix (info, m): # output matrix I = 0; j = 0; l = len (m) print info for I in range (0, len (m): for j in range (0, len (m [I]): if (j = l): print '|', print '% 6.4f' % m [I] [j], print printdef swap (a, B): t =; a = B; B = tdef solve (ma, B, n): global m; m = ma # It is mainly used to display global s in the final matrix; I = 0; j = 0; row_pos = 0; col_pos = 0; ik = 0; j K = 0 mik = 0.0; temp = 0.0 n = len (m) # row_pos variable marking row loops, col_pos variable marking column loops print_matrix ("first de matrix", m) while (row_pos <n) and (col_pos <n): print "Location: row_pos = % d, col_pos = % d" % (row_pos, col_pos) # select the principal component mik =-1 for I in range (row_pos, n): if (abs (m [I] [col_pos])> mik ): mik = abs (m [I] [col_pos]) ik = I if (mik = 0.0): col_pos = col_pos + 1 continue print_matrix ("select principal component", m )# Exchange two rows if (ik! = Row_pos): for j in range (col_pos, n): swap (m [row_pos] [j], m [ik] [j]) swap (m [row_pos] [n], m [ik] [n]); # outside the region? Print_matrix ("swap two rows", m) try: # Remove the meta m [row_pos] [n]/= m [row_pos] [col_pos] except t ZeroDivisionError: # Zero division exceptions generally occur when there is no solution or an infinite number of solutions ...... Return 0; j = n-1 while (j> = col_pos ): m [row_pos] [j]/= m [row_pos] [col_pos] j = j-1 for I in range (0, n): if (I = row_pos ): continue m [I] [n]-= m [row_pos] [n] * m [I] [col_pos] j = n-1 while (j> = col_pos ): m [I] [j]-= m [row_pos] [j] * m [I] [col_pos] j = j-1 print_matrix ("", m) row_pos = row_pos + 1; col_pos = col_pos + 1 for I in range (row_pos, n): if (abs (m [I] [n]) = 0.0): retu Rn 0 return 1if _ name _ = '_ main _': matrix = [2.0, 0.0,-2.0, 0.0], [0.0, 2.0, -1.0, 0.0], [0.0, 1.0, 0.0, 10.0] I = 0; j = 0; n = 0 # output equations print_matrix ("first matrix ", matrix) # solve the equations and output the solve information of the equations. ret = solve (matrix, 0, 0) if (ret! = 0): print "equations with solutions \ n" else: print "no unique or unsolvable equations \ n" # output equations and Their Solutions print_matrix ("equations and Their Solutions ", matrix) for I in range (0, len (m): print "x [% d] = % 6.4f" % (I, m [I] [len (m)])
Running result:
First-start matrix 2.0000 0.0000-2.0000 | 0.20.0.0000 2.0000-1.0000 | 0.20.0.0000 1.0000 0.0000 | 10.0000 first-start de matrix 2.0000 0.0000-2.0000 | 0.20.0.0000 2.0000-1.0000 | 0.20.0.0000 1.0000 0.0000 | 10.0000 position: row_pos = 0, col_pos = 0 select the principal component 2.0000 0.0000-2.0000 | 0.20.0.0000 2.0000-1.0000 | 0.20.0.0000 1.0000 | 0.0000 exchange two rows 10.0000 2.0000-0.0000 | 0.20.0.0000 2.0000-2.0000 | 0.20.0.0000 1.0000 1.0000 | 0.0000 million CNY 1.0000 0.0000-1.0000 | 0.20.0.0000 2.0000-1.0000 | 0.20.0.0000 1.0000 0.0000 | 10.0000 location: row_pos = 1, col_pos = 1 select Principal Component 1.0000 0.0000-1.0000 | 0.20.0.0000 2.0000-1.0000 | 0.20.0.0000 1.0000 | 0.0000 exchange two rows 10.0000 1.0000-0.0000 | 0.20.0.0000 1.0000-2.0000 | 0.20.0.0000 1.0000 1.0000 | 0.0000 million CNY 1.0000 0.0000-1.0000 | 0.20.0.0000 1.0000-0.5000 | 0.20.0.0000 0.0000 0.5000 | 10.0000 location: row_pos = 2, col_pos = 2 select Principal Component 1.0000 0.0000-1.0000 | 0.20.0.0000 1.0000-0.5000 | 0.20.0.0000 0.0000 | 0.5000 exchange two rows 10.0000 1.0000-0.0000 | 0.20.0.0000 1.0000-1.0000 | 0.20.0.0000 0.5000 0.0000 | 0.5000 million CNY 1.0000 0.0000 0.0000 | 000000000.0000 1.0000 0.0000 | 10.00000.0000 0.0000 | 1.0000 equations with solutions and solutions 20.0000 1.0000 0.0000 | 000000000.0000 0.0000 1.0000 | 10.00000.0000 0.0000 0.0000 | 00000000x [0] = 00000000x [1] = 10.20.x [2] = 20.0000
PS: Here are some recommended computing tools for your reference:
Calculation tool for online mona1 functions (equations:
Http://tools.jb51.net/jisuanqi/equ_jisuanqi
Scientific calculator online use _ advanced calculator online computing:
Http://tools.jb51.net/jisuanqi/jsqkexue
Online calculator _ standard calculator:
Http://tools.jb51.net/jisuanqi/jsq