Created
February 28, 2021 07:38
-
-
Save flysand7/13424682b29ac1097ce4dc7830c5fd16 to your computer and use it in GitHub Desktop.
Helper for performing operations on matrix elements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print('enter row count:'); | |
row_count = int(input()); | |
print('enter col count:'); | |
col_count = int(input()); | |
print(f'creating matrix size %dx%d' % (col_count, row_count)); | |
m = []; | |
for i in range(row_count): | |
l = []; | |
m.append(l); | |
for j in range(col_count): | |
l.append(0); | |
def print_matrix(m): | |
for i in range(len(m)): | |
for j in range(len(m[i])): | |
print(f'% 5.5f\t' % m[i][j], end=' '); | |
print(''); | |
row_index = 0; | |
while True: | |
print(f'enter row #%d (separate by spaces):' % (1+row_index)); | |
row_input = input().split(' '); | |
val_count = len(row_input); | |
if val_count != col_count: | |
print(f'number of inputs (%d) doesnt match column count.' % val_count); | |
continue; | |
for val_index in range(len(row_input)): | |
input_string = row_input[val_index]; | |
m[row_index][val_index] = float(input_string); | |
row_index += 1; | |
if row_index == row_count: | |
break; | |
print('your matrix:'); | |
print_matrix(m); | |
while True: | |
print('chose what to do: (h for help)'); | |
op = input(); | |
if op.startswith('h'): | |
print('h - print help'); | |
print('m r n - multiply r by number n, store in r'); | |
print('d r n - divide r by number n, store in r'); | |
print('a r1 r2 ?[m] - add r2 multiplied by m (def.=1) to r1 and store in r1'); | |
print('s r1 r2 - swap r1 and r2'); | |
elif op.startswith('m'): | |
args = op.split(' '); | |
if len(args) != 3: | |
print('expected 3 arguments (h for help)'); | |
continue; | |
row = int(args[1])-1; | |
mul = float(args[2]); | |
for i in range(col_count): | |
m[row][i] = m[row][i] * mul; | |
print_matrix(m); | |
elif op.startswith('d'): | |
args = op.split(' '); | |
if len(args) != 3: | |
print('expected 3 arguments (h for help)'); | |
continue; | |
row = int(args[1])-1; | |
div = float(args[2]); | |
for i in range(col_count): | |
m[row][i] = m[row][i] / div ; | |
print_matrix(m); | |
elif op.startswith('a'): | |
args = op.split(' '); | |
if len(args) < 3 or len(args) > 4: | |
print('expected 2 or 3 args (h for help)'); | |
continue; | |
r1 = int(args[1])-1; | |
r2 = int(args[2])-1; | |
mul = 1; | |
if len(args) == 4: | |
mul = float(args[3]); | |
for i in range(col_count): | |
m[r1][i] = m[r1][i] + m[r2][i] * mul; | |
print_matrix(m); | |
elif op.startswith('s'): | |
args = op.split(' '); | |
if len(args) != 3: | |
print('expected 2 args (h for help)'); | |
continue; | |
r1 = int(args[1])-1; | |
r2 = int(args[2])-1; | |
t = m[r1]; | |
m[r1] = m[r2]; | |
m[r2] = t; | |
print_matrix(m); | |
elif op.startswith('q'): | |
break; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment