Advertisement
Dari_

chislen methods gauss

Nov 17th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.34 KB | None | 0 0
  1. from math import *
  2.  
  3. global eps
  4.  
  5. def deter(count, initial_matrix):                                    #computation of determinant
  6.     matrix = [[0 for j in range(count)] for i in range(count)]
  7.    
  8.     for i in range(count):
  9.         for j in range(count):
  10.             matrix[i][j] = initial_matrix[i][j]
  11.     det = 1
  12.     for k in range(count):                                           #bringing matrix to the
  13.         for i in range(k, count):                                    #upper-traingular form
  14.             tmp = matrix[i][k]
  15.             if (fabs(tmp) > eps):
  16.                 for j in range(count):
  17.                     matrix[i][j] = matrix[i][j] / tmp
  18.                 det *= tmp
  19.                
  20.                 if (i != k):
  21.                     for j in range(count):
  22.                         matrix[i][j] = matrix[i][j] - matrix[k][j]
  23.                
  24.     for i in range(count):                                           #composition of
  25.         det *= matrix[i][i]                                          #diagonal elements
  26.     return det
  27.  
  28.  
  29. def inverse(count, initial_matrix, temporary):                       #computation of inverse matrix
  30.     matrix = [[0 for j in range(count)] for i in range(count)]
  31.    
  32.     for i in range(count):
  33.         for j in range(count):
  34.             matrix[i][j] = initial_matrix[i][j]
  35.    
  36.     for i in range(count):
  37.         for j in range(count):
  38.             if (i == j):
  39.                 temporary[i][j] = 1
  40.    
  41.     for k in range(count):                                           #bringing matrix to the
  42.         for i in range(k, count):                                    #upper-traingular form
  43.             tmp = matrix[i][k]
  44.             if (fabs(tmp) > eps):
  45.                 for j in range(count):
  46.                     matrix[i][j] = matrix[i][j] / tmp
  47.                     temporary[i][j] = temporary[i][j] / tmp
  48.                
  49.                 if (i != k):
  50.                     for j in range(count):
  51.                         matrix[i][j] = matrix[i][j] - matrix[k][j]
  52.                         temporary[i][j] = temporary[i][j] - temporary[k][j]
  53.  
  54.    
  55.     for k in range(count - 1, -1, -1):                               #bringing matrix to the
  56.         for i in range(count - 1, -1, -1):                           #bottom-traingular form
  57.             tmp = matrix[i][k]
  58.  
  59.             if (fabs(tmp) > eps):
  60.                 for j in range(count):
  61.                     matrix[i][j] = matrix[i][j] / tmp
  62.                     temporary[i][j] = temporary[i][j] / tmp
  63.                
  64.                 if (i != k):
  65.                     for j in range(count):
  66.                         matrix[i][j] = matrix[i][j] - matrix[k][j]
  67.                         temporary[i][j] = temporary[i][j] - temporary[k][j]
  68.  
  69.  
  70. def conditionality(count, initial_matrix):                           #computation of conditionality
  71.     matrix = [[0 for j in range(count)] for i in range(count)]    
  72.    
  73.     for i in range(count):
  74.         for j in range(count):
  75.             matrix[i][j] = initial_matrix[i][j]
  76.    
  77.     conditionality1 = 0                                              #conditionality of matrix
  78.     for i in range(count):
  79.         summa = 0
  80.         for j in range(count):
  81.             summa += fabs(matrix[i][j])
  82.            
  83.         if (not(i) or summa > conditionality1):
  84.             conditionality1 = summa
  85.    
  86.    
  87.     temporary = [[0 for j in range(count)] for i in range(count)]
  88.     inverse(count, initial_matrix, temporary)
  89.    
  90.     conditionality2 = 0                                              #conditionality of inverse matrix
  91.     for i in range(count):
  92.         summa = 0
  93.         for j in range(count):
  94.             summa += fabs(temporary[i][j])
  95.            
  96.         if (not(i) or summa > conditionality2):
  97.             conditionality2 = summa
  98.            
  99.    
  100.     return conditionality2 * conditionality1
  101.  
  102.  
  103.  
  104.  
  105. def gauss(count, initial_matrix, x):                                #gauss method
  106.     matrix = [[0 for j in range(count + 1)] for i in range(count)]
  107.    
  108.     for i in range(count):
  109.         for j in range(count + 1):
  110.             matrix[i][j] = initial_matrix[i][j]
  111.    
  112.     for k in range(count):                                          #reduction to trangular form
  113.         for i in range(k, count):
  114.             tmp = matrix[i][k]
  115.             if (fabs(tmp) > eps):
  116.                 for j in range(count + 1):
  117.                     matrix[i][j] = matrix[i][j] / tmp
  118.                 if (i != k):
  119.                     for j in range(count + 1):
  120.                         matrix[i][j] = matrix[i][j] - matrix[k][j]
  121.  
  122.     for k in range(count-1, -1, -1):                                #computation answers x[i]
  123.         x[k] = matrix[k][count]
  124.         for i in range(k):
  125.             matrix[i][count] = matrix[i][count] - matrix[i][k] * x[k]
  126.    
  127.    
  128.    
  129. def modified_gauss(count, initial_matrix, x):                       #modified gauss method
  130.     matrix = [[0 for j in range(count + 1)] for i in range(count)]
  131.    
  132.     for i in range(count):
  133.         for j in range(count + 1):
  134.             matrix[i][j] = initial_matrix[i][j]
  135.    
  136.     for k in range(count):
  137.         max_elem = fabs(matrix[k][k])
  138.         max_index = k
  139.         for i in range(k + 1, count):
  140.             if (max_elem < fabs(matrix[i][k])):                     #selection of maximum element
  141.                 max_elem = fabs(matrix[i][k])
  142.                 max_index = i
  143.        
  144.         if (max_elem < eps):
  145.             return
  146.        
  147.         for j in range(count + 1):
  148.             tmp = matrix[k][j]
  149.             matrix[k][j] = matrix[max_index][j]
  150.             matrix[max_index][j] = tmp
  151.        
  152.         for i in range(k, count):                                    #reduction to trangular form
  153.             tmp = matrix[i][k]
  154.             if (fabs(tmp) > eps):
  155.                 for j in range(count + 1):
  156.                     matrix[i][j] = matrix[i][j] / tmp
  157.                
  158.                 if (i != k):
  159.                     for j in range(count + 1):
  160.                         matrix[i][j] = matrix[i][j] - matrix[k][j]
  161.                
  162.    
  163.     for k in range(count - 1, -1, -1):                               #computation answers x[i]
  164.         x[k] = matrix[k][count]
  165.         for i in range(k):
  166.             matrix[i][count] = matrix[i][count] - matrix[i][k] * x[k]
  167.        
  168.        
  169.            
  170. eps = 0.000001
  171.  
  172. print("input matrix range")          
  173. count = int(input())
  174. x = [0 for j in range(count)]
  175. print("if you want input from keyboard, press 1,\nif you want to use formula press 2,\nif you want input from file press 3.")
  176. inp = int(input())
  177. if inp == 1:                                                   #from terminal
  178.     matrix = []
  179.     for i in range(count):
  180.         print("input", i + 1, "line")
  181.         matrix.append(list(map(float, input().split())))
  182.  
  183. elif inp == 2:                                                 #using formula
  184.     matrix = [[0 for j in range(count + 1)] for i in range(count)]
  185.     for i in range(count):
  186.         for j in range(count):
  187.             if (i != j):
  188.                 matrix[i][j] = (i + j) / 65
  189.             else:
  190.                 matrix[i][j] = 275 + j / 15 + i / 50
  191.         matrix[i][count] = 750 - i * i * i
  192.        
  193. elif inp == 3:                                                 #from file named 'input.txt'
  194.     fin = open('input.txt', 'r')
  195.     matrix = []
  196.     for i in range(count): #schitat matrix
  197.         matrix.append(list(map(float, (fin.readline()).split())))    
  198.     fin.close()
  199.  
  200. print("\n")
  201. det = deter(count, matrix)
  202. print("determinant D =", det)
  203.  
  204. if (det == 0):
  205.     print("degenerate matrix\n")
  206.  
  207. else:
  208.     print("M =", conditionality(count, matrix), "\n")
  209.    
  210.     print("START OF GAUSS METHOD\n")
  211.     gauss(count, matrix, x)
  212.     for i in range(count):
  213.         print("x[" + str(i + 1) + "] = " + str(x[i]))
  214.     print("")
  215.     print("END OF GAUSS METHOD\n")
  216.    
  217.     print("START OF MODIFIED GAUSS METHOD\n")
  218.     modified_gauss(count, matrix, x)
  219.     for i in range(count):
  220.         print("x[" + str(i + 1) + "] = " + str(x[i]))
  221.     print("")
  222.     print("END OF MODIFIED GAUSS METHOD\n")
  223.    
  224.    
  225.     print("INVERSE MATRIX\n")
  226.     temporary = [[0 for j in range(count)] for i in range(count)]
  227.     inverse(count, matrix, temporary)
  228.     for elem in temporary:
  229.         for el in elem:
  230.             print(el, end = "  ")
  231.         print("")
  232.    
  233.     print("\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement