text
stringlengths
0
312
if not isinstance(M[0], list):
M = [M]
rows = len(M)
cols = len(M[0])
MT = []
while len(MT) < dim:
MT.append([])
while len(MT[-1]) < dim:
MT[-1].append(0.0)
for i in range(rows):
for j in range(cols):
MT[j][i] = M[i][j]
print("Transpose Array")
for i in range(rows):
row = '|'
for b in range(cols):
row = row + ' ' + str(MT[i][b])
print(row + ' ' + '|')
# write a program to add two matrix
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
B = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if rowsA != rowsB or colsA != colsB:
raise ArithmeticError('Matrices are NOT the same size.')
C = []
while len(C) < rowsA:
C.append([])
while len(C[-1]) < colsB:
C[-1].append(0.0)
for i in range(rowsA):
for j in range(colsB):
C[i][j] = A[i][j] + B[i][j]
print("Added Array")
for i in range(rowsA):
row = '|'
for b in range(colsA):
row = row + ' ' + str(C[i][b])
print(row + ' ' + '|')
# write a program to subtract two matrix
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
B = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if rowsA != rowsB or colsA != colsB:
raise ArithmeticError('Matrices are NOT the same size.')
C = []
while len(C) < rowsA:
C.append([])
while len(C[-1]) < colsB:
C[-1].append(0.0)
for i in range(rowsA):
for j in range(colsB):
C[i][j] = A[i][j] - B[i][j]
print("Subtracted Array")
for i in range(rowsA):
row = '|'
for b in range(colsA):
row = row + ' ' + str(C[i][b])
print(row + ' ' + '|')
# write a program to multiply two matrix
rowsA = len(A)
colsA = len(A[0])
rowsB = len(B)
colsB = len(B[0])
if colsA != rowsB:
raise ArithmeticError('Number of A columns must equal number of B rows.')
C = []
while len(C) < rowsA:
C.append([])
while len(C[-1]) < colsB:
C[-1].append(0.0)
for i in range(rowsA):