From 9e8ab1e3a217e531218e690df3c96f88e8a40ce6 Mon Sep 17 00:00:00 2001 From: NilsForssen Date: Mon, 7 Feb 2022 10:27:29 +0100 Subject: [PATCH] j --- laboration3/{uppgift_3.py => uppgift_2.py} | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) rename laboration3/{uppgift_3.py => uppgift_2.py} (75%) diff --git a/laboration3/uppgift_3.py b/laboration3/uppgift_2.py similarity index 75% rename from laboration3/uppgift_3.py rename to laboration3/uppgift_2.py index e263aed..2d83dd0 100644 --- a/laboration3/uppgift_3.py +++ b/laboration3/uppgift_2.py @@ -34,12 +34,12 @@ def get_first_column(matrix): def get_nth_column(n, matrix): tot = [] for row in matrix: - tot.append(row[n]) + tot.append(row[n - 1]) return tot def get_all_columns(matrix): - cols = [[] for _ in matrix] + cols = [[] for _ in matrix[0]] for row in matrix: for idx, item in enumerate(row): @@ -50,9 +50,12 @@ def get_all_columns(matrix): def scalar_product(vec1, vec2): prod = 0 - for row in vec1: - for item1, item2 in zip(vec1, vec2): - prod += item1*item2 + for row1 ,row2 in zip(vec1, vec2): + if isinstance(row1, list) and isinstance(row2, list): + for item1, item2 in zip(row1, row2): + prod += item1*item2 + else: + prod += row1*row2 return prod @@ -61,3 +64,8 @@ def matrix_square(matrix): for idx, (row, col) in enumerate(zip(matrix, get_all_columns(matrix))): tot[idx].append(scalar_product(row, col)) return tot + +print(matrix_square([[1, 2, 4], + [3, 0, 6], + [0, 5, 1]] +)) \ No newline at end of file -- 2.30.2