Home python python 3 gauss method

# python 3 gauss method

Author

Date

Category

I need to complete a task: Gaussian method in python. Actually I implemented the algorithm itself, there is nothing complicated, here it is:

``````myA = [
[1.0, -2.0, 3.0, -4.0],
[3.0, 3.0, -5.0, -1.0],
[3.0, 0.0, 3.0, -10.0],
[-2.0, 1.0, 2.0, -3.0]
]
myB = [
2.0,
-3.0,
8.0,
5.0]
# --- end of initial data
# --- display the system to the screen
def FancyPrint (A, B, selected):
for row in range (len (B)):
print ("(", end = '')
for col in range (len (A [row])):
print ("\ t {1: 10.2f} {0}". format ("" if (selected is None
or selected! = (row, col)) else "*", A [row] [col]), end = '')
print ("\ t) * (\ tX {0}) = (\ t {1: 10.2f})". format (row + 1, B [row]))
# --- end of displaying the system to the screen
# --- swap two lines of the system
def SwapRows (A, B, row1, row2):
A [row1], A [row2] = A [row2], A [row1]
B [row1], B [row2] = B [row2], B [row1]
# --- end of swap two lines of the system
# --- dividing the system string by a number
def DivideRow (A, B, row, divider):
A [row] = [a / divider for a in A [row]]
B [row] / = divider
# --- end of dividing the system string by a number
# --- addition of a system string with another string multiplied by a number
def CombineRows (A, B, row, source_row, weight):
A [row] = [(a + k * weight) for a, k in zip (A [row], A [source_row])]
B [row] + = B [source_row] * weight
# --- end of addition of the system string with another string, multiplied by
# --- solution of the system by Gauss method (reduction to triangular form)
def Gauss (A, B):
column = 0
while (column & lt; len (B)):
print ("We are looking for the maximum modulo element in the {0} th column:". format (column + 1))
current_row = None
for r in range (column, len (A)):
if current_row is None or abs (A [r] [column]) & gt; abs (A [current_row] [column]):
current_row = r
if current_row is None:
print ("no solutions")
return None
FancyPrint (A, B, (current_row, column))
if current_row! = column:
print ("We rearrange the line with the found element above:")
SwapRows (A, B, current_row, column)
FancyPrint (A, B, (column, column))
print ("Normalize the line with the found element:")
DivideRow (A, B, column, A [column] [column])
FancyPrint (A, B, (column, column))
print ("Processing the lines below:")
for r in range (column + 1, len (A)):
CombineRows (A, B, r, column, -A [r] [column])
FancyPrint (A, B, (column, column))
column + = 1
print ("The matrix is ​​reduced to a triangular form, we consider the solution")
X = [0 for b in B]
for i in range (len (B) - 1, -1, -1):
X [i] = B [i] - sum (x * a for x, a in zip (X [(i + 1):], A [i] [(i + 1):]))
print ("\ n" .join ("X {0} = \ t {1: 10.2f}". format (i + 1, x) for i, x in
enumerate (X)))
return X
# --- end of system solution by Gauss method (reduction to triangular form)
print ("Source system:")
FancyPrint (myA, myB, None)
print ("Solving:")
Gauss (myA, myB)
``````

But I need the matrix of coefficients and right-hand sides to be entered in the program itself, and not in the code. That is, so that the field “Enter the coefficient matrix” and “Enter the matrix of the right parts” appears, I enter the data there and the program calculates.
Tried changing the first two lines to

``````myA = input ("enter coefficient matrix")
myB = input ("Enter the matrix of the right-hand sides")
``````

But it throws an error
Can you tell me how you can fix it? Thanks in advance!

For example, like this (for the matrix `myA `):

``````my_A_rows = int (input ("enter the number of rows of the coefficient matrix:"))
print ("enter a matrix of coefficients (line by line, spaces between coefficients")
myA = [list (map (float, (input (f "string {i + 1}:") .split ())))
for i in range (my_A_rows)]
``````

Test:

``````enter the number of rows of the coefficient matrix: 2
enter a matrix of coefficients (line by line, spaces between the coefficients
line 0: 1 2 3
line 1: 4 5 6
In : myA
Out : [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
``````

For the matrix `myB `:

``````myB = map (float, input ("Enter the matrix of the right sides, spaces between numbers:") .split () )
myB = list (myB)
``````

can be generated using lists:

``````n = 3
print ('enter coefficient matrix')
A = [[int (elem) for elem in input (). Split ()] for i in range (n)]
print (A)
print ('enter vector')
b = [int (elem) for elem in input (). split ()]
print (b)
``````

``````myA = 
myB = 
strok = int (input ('Enter number of lines:'))
kolom = int (input ('Enter the number of columns:'))
myA = [ * kolom] * strok
for s in range (strok):
for k in range (kolom):
myA [s] [k] = float (input ('Enter coefficient values:'))
myB = [] * strok
for s in range (strok):
myB [s] = float (input ('Enter the values ​​for the right-hand sides:'))
``````

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.