Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import sys
- mtx = []
- def banner():
- print("""
- __ _____.__
- ____ _____/ |_ _/ ____\ | ______ _ __
- / \_/ __ \ __\ \ __\| | / _ \ \/ \/ /
- | | \ ___/| | | | | |_( <_> ) /
- |___| /\___ >__| |__| |____/\____/ \/\_/
- \/ \/
- """)
- def networkflow():
- print("""
- 20 | | 90
- ↓ ↓
- 20 | x3 | 80
- -->--|--->------------|-->--
- A| C|
- | |
- x2 | ↓ x4
- | |
- 30 B| x1 D| 70
- --<--|-------<--------|--<--
- | |
- 100 ↓ ↓ 50
- | |
- """)
- def networktable():
- print("""
- -------------------------------
- | node | in | out |
- |------|-----------|----------|
- | A | 20 + 20 | x2 + x3 |
- | B | x1 + x2 | 30 + 100 |
- | C | 90 + x3 | 80 + x4 |
- | D | x4 + 70 | x1 + 50 |
- -------------------------------
- --------------------------------------
- | | |
- ---------------------|---------------|
- | 20 + 20 = x2 + x3 | x2 + x3 = 40 |
- | x1 + x2 = 30 + 100 | x1 + x2 = 130 |
- | 90 + x3 = 80 + x4 | x3 - x4 = -10 |
- | x4 + 70 = x1 + 50 | x1 - x4 = 20 |
- --------------------------------------
- ---------------------------
- | x1 + x2 + x3 + x4 = 40 |
- | x1 + x3 + x3 + x4 = 130 |
- | x1 + x2 + x3 - x4 = -10 |
- | x1 + x2 + x3 - x4 = 20 |
- ---------------------------
- """)
- def ToReducedRowEchelonForm( M):
- if not M: return
- lead = 0
- rowCount = len(M)
- columnCount = len(M[0])
- for r in range(rowCount):
- if lead >= columnCount:
- return
- i = r
- while M[i][lead] == 0:
- i += 1
- if i == rowCount:
- i = r
- lead += 1
- if columnCount == lead:
- return
- M[i],M[r] = M[r],M[i]
- lv = M[r][lead]
- M[r] = [ mrx / float(lv) for mrx in M[r]]
- for i in range(rowCount):
- if i != r:
- lv = M[i][lead]
- M[i] = [ iv - lv*rv for rv,iv in zip(M[r],M[i])]
- lead += 1
- def solveMat():
- ToReducedRowEchelonForm( mtx )
- def solvematrix():
- n_tries = 1000
- while n_tries > 0:
- n_tries -= 1
- try:
- inp = input("> ").strip()
- except KeyboardInterrupt:
- print("interrupted - exiting")
- exit(0)
- # we're done here, the matrix has been collected
- if inp == '.':
- break
- if inp == '..':
- try:
- solveMat()
- except:
- print("")
- elif inp == '':
- print("empty line - try again!")
- continue
- try:
- row = list(map(float, inp.split()))
- mtx.append(row)
- except Exception as e:
- print("exception while reading this row:" + str(e))
- print("try again!")
- try:
- for rw in mtx:
- print (', '.join( (str(rv) for rv in rw) ))
- except Exception as e:
- exit(1)
- banner()
- parser = argparse.ArgumentParser()
- parser._optionals.title = "OPTIONS"
- parser.add_argument('-n', '--nflow', action='store_true', help="shows network flow")
- parser.add_argument('-t', '--table', action='store_true', help="shows table of network data")
- parser.add_argument('-m', '--matrix', action='store_true', help="solve the matrix data")
- args = parser.parse_args()
- if args.nflow:
- networkflow()
- if args.table:
- networktable()
- if args.matrix:
- solvematrix()
- # mtx = [
- # [0,1,-1,0,100],
- # [1,-1,0,0,300],
- # [0,0,1,-1,-500],
- # [-1,0,0,1,100]
- # ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement