Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. import argparse
  2. import sys
  3.  
  4. mtx = []
  5.  
  6. def banner():
  7.     print("""
  8.               __      _____.__                
  9.  ____   _____/  |_  _/ ____\ |   ______  _  __
  10. /    \_/ __ \  __\ \  __\|  |  /  _ \ \/ \/ /
  11. |   |  \ ___/|  |    |  |  |  |_(  <_> )     /
  12. |___|  /\___  >__|    |__|  |____/\____/ \/\_/  
  13.     \/     \/                                  
  14.    """)
  15.  
  16. def networkflow():
  17.     print("""
  18.     20 |        | 90
  19.       ↓                ↓
  20.    20  |   x3       |    80
  21.  -->--|--->------------|-->--
  22.       A|           C|
  23.        |        |
  24.     x2 |        ↓ x4
  25.        |        |
  26.  30  B|       x1      D|   70
  27.  --<--|-------<--------|--<--
  28.        |            |
  29.    100 ↓          ↓ 50
  30.        |        |  
  31.    """)
  32.  
  33. def networktable():
  34.     print("""
  35.    -------------------------------
  36.    | node |    in     |    out   |
  37.    |------|-----------|----------|
  38.    |   A  |  20 + 20  | x2 + x3  |
  39.    |   B  |  x1 + x2  | 30 + 100 |
  40.    |   C  |  90 + x3  | 80 + x4  |
  41.    |   D  |  x4 + 70  | x1 + 50  |
  42.    -------------------------------
  43.  
  44.    --------------------------------------
  45.    |                    |               |
  46.    ---------------------|---------------|
  47.    | 20 + 20 = x2 + x3  | x2 + x3 = 40  |
  48.    | x1 + x2 = 30 + 100 | x1 + x2 = 130 |
  49.    | 90 + x3 = 80 + x4  | x3 - x4 = -10 |
  50.    | x4 + 70 = x1 + 50  | x1 - x4 = 20  |
  51.    --------------------------------------
  52.  
  53.    ---------------------------
  54.    | x1 + x2 + x3 + x4 = 40  |
  55.    | x1 + x3 + x3 + x4 = 130 |
  56.    | x1 + x2 + x3 - x4 = -10 |
  57.    | x1 + x2 + x3 - x4 = 20  |
  58.    ---------------------------
  59.    """)
  60.  
  61. def ToReducedRowEchelonForm( M):
  62.     if not M: return
  63.     lead = 0
  64.     rowCount = len(M)
  65.     columnCount = len(M[0])
  66.     for r in range(rowCount):
  67.         if lead >= columnCount:
  68.             return
  69.         i = r
  70.         while M[i][lead] == 0:
  71.             i += 1
  72.             if i == rowCount:
  73.                 i = r
  74.                 lead += 1
  75.                 if columnCount == lead:
  76.                     return
  77.         M[i],M[r] = M[r],M[i]
  78.         lv = M[r][lead]
  79.         M[r] = [ mrx / float(lv) for mrx in M[r]]
  80.         for i in range(rowCount):
  81.             if i != r:
  82.                 lv = M[i][lead]
  83.                 M[i] = [ iv - lv*rv for rv,iv in zip(M[r],M[i])]
  84.         lead += 1
  85. def solveMat():
  86.     ToReducedRowEchelonForm( mtx )
  87.    
  88. def solvematrix():
  89.     n_tries = 1000
  90.     while n_tries > 0:
  91.         n_tries -= 1
  92.         try:
  93.             inp = input("> ").strip()
  94.         except KeyboardInterrupt:
  95.             print("interrupted - exiting")
  96.             exit(0)
  97.        
  98.         # we're done here, the matrix has been collected
  99.         if inp == '.':
  100.             break
  101.         if inp == '..':
  102.             try:
  103.                 solveMat()
  104.             except:
  105.                 print("")
  106.  
  107.         elif inp == '':
  108.             print("empty line - try again!")
  109.             continue
  110.  
  111.         try:
  112.             row = list(map(float, inp.split()))
  113.             mtx.append(row)
  114.         except Exception as e:
  115.             print("exception while reading this row:" + str(e))
  116.             print("try again!")
  117.  
  118.         try:
  119.             for rw in mtx:
  120.                 print (', '.join( (str(rv) for rv in rw) ))
  121.         except Exception as e:
  122.             exit(1)
  123.  
  124.  
  125. banner()
  126. parser = argparse.ArgumentParser()
  127. parser._optionals.title = "OPTIONS"
  128. parser.add_argument('-n', '--nflow', action='store_true', help="shows network flow")
  129. parser.add_argument('-t', '--table', action='store_true', help="shows table of network data")
  130. parser.add_argument('-m', '--matrix', action='store_true', help="solve the matrix data")
  131. args = parser.parse_args()
  132.  
  133.  
  134. if args.nflow:
  135.     networkflow()
  136. if args.table:
  137.     networktable()
  138. if args.matrix:
  139.     solvematrix()
  140.  
  141.  
  142. # mtx = [
  143. # [0,1,-1,0,100],
  144. # [1,-1,0,0,300],
  145. # [0,0,1,-1,-500],
  146. # [-1,0,0,1,100]
  147. # ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement