From ad6c2156e227cdacc97de38f43a6ac8f10f74d4b Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sat, 16 Oct 2021 18:08:28 -0400 Subject: [PATCH 1/6] Rewrite parts of Vector and Matrix methods --- linear_algebra/src/lib.py | 45 +++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 6a18df5e15c3..dd2a4679b7cb 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -92,10 +92,8 @@ def euclidLength(self) -> float: """ returns the euclidean length of the vector """ - summe: float = 0 - for c in self.__components: - summe += c ** 2 - return math.sqrt(summe) + squares = [c ** 2 for c in self.__components] + return math.sqrt(sum(squares)) def __add__(self, other: Vector) -> Vector: """ @@ -139,12 +137,10 @@ def __mul__(self, other: float | Vector) -> float | Vector: if isinstance(other, float) or isinstance(other, int): ans = [c * other for c in self.__components] return Vector(ans) - elif isinstance(other, Vector) and (len(self) == len(other)): + elif isinstance(other, Vector) and len(self) == len(other): size = len(self) - summe: float = 0 - for i in range(size): - summe += self.__components[i] * other.component(i) - return summe + prods = [self.__components[i] * other.component(i) for i in range(size)] + return sum(prods) else: # error case raise Exception("invalid operand!") @@ -156,7 +152,8 @@ def magnitude(self) -> float: 5.385164807134504 """ - return sum([i ** 2 for i in self.__components]) ** (1 / 2) + squares = [c ** 2 for c in self.__components] + return math.sqrt(sum(squares)) def angle(self, other: Vector, deg: bool = False) -> float: """ @@ -355,20 +352,20 @@ def __mul__(self, other: float | Vector) -> Vector | Matrix: implements the matrix-vector multiplication. implements the matrix-scalar multiplication """ - if isinstance(other, Vector): # vector-matrix + if isinstance(other, Vector): # matrix-vector if len(other) == self.__width: ans = zeroVector(self.__height) for i in range(self.__height): - summe: float = 0 - for j in range(self.__width): - summe += other.component(j) * self.__matrix[i][j] - ans.changeComponent(i, summe) - summe = 0 + prods = [ + self.__matrix[i][j] * other.component(j) + for j in range(self.__width) + ] + ans.changeComponent(i, sum(prods)) return ans else: raise Exception( "vector must have the same size as the " - + "number of columns of the matrix!" + "number of columns of the matrix!" ) elif isinstance(other, int) or isinstance(other, float): # matrix-scalar matrix = [ @@ -384,9 +381,10 @@ def __add__(self, other: Matrix) -> Matrix: if self.__width == other.width() and self.__height == other.height(): matrix = [] for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] + other.component(i, j)) + row = [ + self.__matrix[i][j] + other.component(i, j) + for j in range(self.__width) + ] matrix.append(row) return Matrix(matrix, self.__width, self.__height) else: @@ -399,9 +397,10 @@ def __sub__(self, other: Matrix) -> Matrix: if self.__width == other.width() and self.__height == other.height(): matrix = [] for i in range(self.__height): - row = [] - for j in range(self.__width): - row.append(self.__matrix[i][j] - other.component(i, j)) + row = [ + self.__matrix[i][j] - other.component(i, j) + for j in range(self.__width) + ] matrix.append(row) return Matrix(matrix, self.__width, self.__height) else: From f84d76915d48581423badbedb9ba4c16edbbedc4 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 17 Oct 2021 04:28:10 -0400 Subject: [PATCH 2/6] Refactor determinant method and add unit tests Refactor determinant method to create separate minor and cofactor methods. Add respective unit tests for new methods. Rename methods using snake case to follow Python naming conventions. --- knapsack/README.md | 2 +- linear_algebra/README.md | 20 ++-- linear_algebra/src/lib.py | 131 +++++++++++++--------- linear_algebra/src/test_linear_algebra.py | 96 ++++++++++++---- 4 files changed, 158 insertions(+), 91 deletions(-) diff --git a/knapsack/README.md b/knapsack/README.md index 6041c1e48eb8..f31e5f591412 100644 --- a/knapsack/README.md +++ b/knapsack/README.md @@ -17,7 +17,7 @@ The knapsack problem has been studied for more than a century, with early works ## Documentation This module uses docstrings to enable the use of Python's in-built `help(...)` function. -For instance, try `help(Vector)`, `help(unitBasisVector)`, and `help(CLASSNAME.METHODNAME)`. +For instance, try `help(Vector)`, `help(unit_basis_vector)`, and `help(CLASSNAME.METHODNAME)`. --- diff --git a/linear_algebra/README.md b/linear_algebra/README.md index dc6085090d02..e52c70b8d997 100644 --- a/linear_algebra/README.md +++ b/linear_algebra/README.md @@ -17,20 +17,20 @@ This module contains classes and functions for doing linear algebra. - \_\_str\_\_() : toString method - component(i : int): gets the i-th component (start by 0) - \_\_len\_\_() : gets the size / length of the vector (number of components) - - euclidLength() : returns the eulidean length of the vector. + - euclidean_length() : returns the eulidean length of the vector. - operator + : vector addition - operator - : vector subtraction - operator * : scalar multiplication and dot product - copy() : copies this vector and returns it. - - changeComponent(pos,value) : changes the specified component. + - change_component(pos,value) : changes the specified component. -- function zeroVector(dimension) +- function zero_vector(dimension) - returns a zero vector of 'dimension' -- function unitBasisVector(dimension,pos) +- function unit_basis_vector(dimension,pos) - returns a unit basis vector with a One at index 'pos' (indexing at 0) - function axpy(scalar,vector1,vector2) - computes the axpy operation -- function randomVector(N,a,b) +- function random_vector(N,a,b) - returns a random vector of size N, with random integer components between 'a' and 'b'. ### class Matrix @@ -42,24 +42,24 @@ This module contains classes and functions for doing linear algebra. - \_\_str\_\_() : returns a string representation - operator * : implements the matrix vector multiplication implements the matrix-scalar multiplication. - - changeComponent(x,y,value) : changes the specified component. + - change_component(x,y,value) : changes the specified component. - component(x,y) : returns the specified component. - width() : returns the width of the matrix - height() : returns the height of the matrix - - determinate() : returns the determinate of the matrix if it is square + - determinant() : returns the determinant of the matrix if it is square - operator + : implements the matrix-addition. - operator - _ implements the matrix-subtraction -- function squareZeroMatrix(N) +- function square_zero_matrix(N) - returns a square zero-matrix of dimension NxN -- function randomMatrix(W,H,a,b) +- function random_matrix(W,H,a,b) - returns a random matrix WxH with integer components between 'a' and 'b' --- ## Documentation This module uses docstrings to enable the use of Python's in-built `help(...)` function. -For instance, try `help(Vector)`, `help(unitBasisVector)`, and `help(CLASSNAME.METHODNAME)`. +For instance, try `help(Vector)`, `help(unit_basis_vector)`, and `help(CLASSNAME.METHODNAME)`. --- diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index dd2a4679b7cb..d245952e6597 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -10,13 +10,13 @@ Overview: - class Vector -- function zeroVector(dimension) -- function unitBasisVector(dimension,pos) +- function zero_vector(dimension) +- function unit_basis_vector(dimension,pos) - function axpy(scalar,vector1,vector2) -- function randomVector(N,a,b) +- function random_vector(N,a,b) - class Matrix -- function squareZeroMatrix(N) -- function randomMatrix(W,H,a,b) +- function square_zero_matrix(N) +- function random_matrix(W,H,a,b) """ from __future__ import annotations @@ -37,12 +37,12 @@ class Vector: __str__() : toString method component(i : int): gets the i-th component (start by 0) __len__() : gets the size of the vector (number of components) - euclidLength() : returns the euclidean length of the vector. + euclidean_length() : returns the euclidean length of the vector. operator + : vector addition operator - : vector subtraction operator * : scalar multiplication and dot product copy() : copies this vector and returns it. - changeComponent(pos,value) : changes the specified component. + change_component(pos,value) : changes the specified component. TODO: compare-operator """ @@ -88,7 +88,7 @@ def __len__(self) -> int: """ return len(self.__components) - def euclidLength(self) -> float: + def euclidean_length(self) -> float: """ returns the euclidean length of the vector """ @@ -181,7 +181,7 @@ def copy(self) -> Vector: """ return Vector(self.__components) - def changeComponent(self, pos: int, value: float) -> None: + def change_component(self, pos: int, value: float) -> None: """ input: an index (pos) and a value changes the specified component (pos) with the @@ -192,7 +192,7 @@ def changeComponent(self, pos: int, value: float) -> None: self.__components[pos] = value -def zeroVector(dimension: int) -> Vector: +def zero_vector(dimension: int) -> Vector: """ returns a zero-vector of size 'dimension' """ @@ -201,7 +201,7 @@ def zeroVector(dimension: int) -> Vector: return Vector([0] * dimension) -def unitBasisVector(dimension: int, pos: int) -> Vector: +def unit_basis_vector(dimension: int, pos: int) -> Vector: """ returns a unit basis vector with a One at index 'pos' (indexing at 0) @@ -222,13 +222,13 @@ def axpy(scalar: float, x: Vector, y: Vector) -> Vector: # precondition assert ( isinstance(x, Vector) - and (isinstance(y, Vector)) + and isinstance(y, Vector) and (isinstance(scalar, int) or isinstance(scalar, float)) ) return x * scalar + y -def randomVector(N: int, a: int, b: int) -> Vector: +def random_vector(n: int, a: int, b: int) -> Vector: """ input: size (N) of the vector. random range (a,b) @@ -236,26 +236,29 @@ def randomVector(N: int, a: int, b: int) -> Vector: random integer components between 'a' and 'b'. """ random.seed(None) - ans = [random.randint(a, b) for _ in range(N)] + ans = [random.randint(a, b) for _ in range(n)] return Vector(ans) class Matrix: """ class: Matrix - This class represents a arbitrary matrix. + This class represents an arbitrary matrix. Overview about the methods: __str__() : returns a string representation operator * : implements the matrix vector multiplication - implements the matrix-scalar multiplication. - changeComponent(x,y,value) : changes the specified component. - component(x,y) : returns the specified component. + implements the matrix scalar multiplication + change_component(x, y, value) : changes the specified component + component(x, y) : returns the specified component width() : returns the width of the matrix height() : returns the height of the matrix - operator + : implements the matrix-addition. - operator - _ implements the matrix-subtraction + minor() : returns the minor of the matrix along (x, y) + cofactor() : returns the cofactor of the matrix along (x, y) + determinant() : returns the determinant of the matrix + operator + : implements matrix addition + operator - : implements matrix subtraction """ def __init__(self, matrix: list[list[float]], w: int, h: int) -> None: @@ -282,14 +285,14 @@ def __str__(self) -> str: ans += str(self.__matrix[i][j]) + "|\n" return ans - def changeComponent(self, x: int, y: int, value: float) -> None: + def change_component(self, x: int, y: int, value: float) -> None: """ changes the x-y component of this matrix """ if 0 <= x < self.__height and 0 <= y < self.__width: self.__matrix[x][y] = value else: - raise Exception("changeComponent: indices out of bounds") + raise Exception("change_component: indices out of bounds") def component(self, x: int, y: int) -> float: """ @@ -298,7 +301,7 @@ def component(self, x: int, y: int) -> float: if 0 <= x < self.__height and 0 <= y < self.__width: return self.__matrix[x][y] else: - raise Exception("changeComponent: indices out of bounds") + raise Exception("change_component: indices out of bounds") def width(self) -> int: """ @@ -312,32 +315,48 @@ def height(self) -> int: """ return self.__height - def determinate(self) -> float: - """ - returns the determinate of an nxn matrix using Laplace expansion - """ - if self.__height == self.__width and self.__width >= 2: - total = 0 - if self.__width > 2: - for x in range(0, self.__width): - for y in range(0, self.__height): - total += ( - self.__matrix[x][y] - * (-1) ** (x + y) - * Matrix( - self.__matrix[0:x] + self.__matrix[x + 1 :], - self.__width - 1, - self.__height - 1, - ).determinate() - ) - else: - return ( - self.__matrix[0][0] * self.__matrix[1][1] - - self.__matrix[0][1] * self.__matrix[1][0] - ) - return total + def minor(self, x: int, y: int) -> float: + """ + returns the minor along (x, y) + """ + if self.__height != self.__width: + raise Exception("Matrix is not square") + minor = self.__matrix[:x] + self.__matrix[x + 1 :] + for i in range(len(minor)): + minor[i] = minor[i][:y] + minor[i][y + 1 :] + return Matrix(minor, self.__width - 1, self.__height - 1).determinant() + + def cofactor(self, x: int, y: int) -> float: + """ + returns the cofactor (signed minor) along (x, y) + """ + if self.__height != self.__width: + raise Exception("Matrix is not square") + if 0 <= x < self.__height and 0 <= y < self.__width: + return (-1) ** (x + y) * self.minor(x, y) else: - raise Exception("matrix is not square") + raise Exception("Indices out of bounds") + + def determinant(self) -> float: + """ + returns the determinant of an nxn matrix using Laplace expansion + """ + if self.__height != self.__width: + raise Exception("Matrix is not square") + if self.__height < 1: + raise Exception("Matrix has no elements") + elif self.__height == 1: + return self.__matrix[0][0] + elif self.__height == 2: + return ( + self.__matrix[0][0] * self.__matrix[1][1] + - self.__matrix[0][1] * self.__matrix[1][0] + ) + else: + cofactor_prods = [ + self.__matrix[0][y] * self.cofactor(0, y) for y in range(self.__width) + ] + return sum(cofactor_prods) @overload def __mul__(self, other: float) -> Matrix: @@ -354,13 +373,13 @@ def __mul__(self, other: float | Vector) -> Vector | Matrix: """ if isinstance(other, Vector): # matrix-vector if len(other) == self.__width: - ans = zeroVector(self.__height) + ans = zero_vector(self.__height) for i in range(self.__height): prods = [ self.__matrix[i][j] * other.component(j) for j in range(self.__width) ] - ans.changeComponent(i, sum(prods)) + ans.change_component(i, sum(prods)) return ans else: raise Exception( @@ -407,21 +426,21 @@ def __sub__(self, other: Matrix) -> Matrix: raise Exception("matrix must have the same dimension!") -def squareZeroMatrix(N: int) -> Matrix: +def square_zero_matrix(n: int) -> Matrix: """ returns a square zero-matrix of dimension NxN """ - ans: list[list[float]] = [[0] * N for _ in range(N)] - return Matrix(ans, N, N) + ans: list[list[float]] = [[0] * n for _ in range(n)] + return Matrix(ans, n, n) -def randomMatrix(W: int, H: int, a: int, b: int) -> Matrix: +def random_matrix(width: int, height: int, a: int, b: int) -> Matrix: """ returns a random matrix WxH with integer components between 'a' and 'b' """ random.seed(None) matrix: list[list[float]] = [ - [random.randint(a, b) for _ in range(W)] for _ in range(H) + [random.randint(a, b) for _ in range(width)] for _ in range(height) ] - return Matrix(matrix, W, H) + return Matrix(matrix, width, height) diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py index 0954a2d932b7..de7041a17038 100644 --- a/linear_algebra/src/test_linear_algebra.py +++ b/linear_algebra/src/test_linear_algebra.py @@ -8,13 +8,20 @@ """ import unittest -from .lib import Matrix, Vector, axpy, squareZeroMatrix, unitBasisVector, zeroVector +from .lib import ( + Matrix, + Vector, + axpy, + square_zero_matrix, + unit_basis_vector, + zero_vector, +) class Test(unittest.TestCase): def test_component(self) -> None: """ - test for method component + test for method component() """ x = Vector([1, 2, 3]) self.assertEqual(x.component(0), 1) @@ -23,24 +30,24 @@ def test_component(self) -> None: def test_str(self) -> None: """ - test for toString() method + test for method toString() """ x = Vector([0, 0, 0, 0, 0, 1]) self.assertEqual(str(x), "(0,0,0,0,0,1)") def test_size(self) -> None: """ - test for size()-method + test for method size() """ x = Vector([1, 2, 3, 4]) self.assertEqual(len(x), 4) def test_euclidLength(self) -> None: """ - test for the eulidean length + test for method euclidean_length() """ x = Vector([1, 2]) - self.assertAlmostEqual(x.euclidLength(), 2.236, 3) + self.assertAlmostEqual(x.euclidean_length(), 2.236, 3) def test_add(self) -> None: """ @@ -67,26 +74,26 @@ def test_mul(self) -> None: test for * operator """ x = Vector([1, 2, 3]) - a = Vector([2, -1, 4]) # for test of dot-product + a = Vector([2, -1, 4]) # for test of dot product b = Vector([1, -2, -1]) self.assertEqual(str(x * 3.0), "(3.0,6.0,9.0)") self.assertEqual((a * b), 0) def test_zeroVector(self) -> None: """ - test for the global function zeroVector(...) + test for global function zero_vector() """ - self.assertTrue(str(zeroVector(10)).count("0") == 10) + self.assertTrue(str(zero_vector(10)).count("0") == 10) def test_unitBasisVector(self) -> None: """ - test for the global function unitBasisVector(...) + test for global function unit_basis_vector() """ - self.assertEqual(str(unitBasisVector(3, 1)), "(0,1,0)") + self.assertEqual(str(unit_basis_vector(3, 1)), "(0,1,0)") def test_axpy(self) -> None: """ - test for the global function axpy(...) (operation) + test for global function axpy() (operation) """ x = Vector([1, 2, 3]) y = Vector([1, 0, 1]) @@ -94,7 +101,7 @@ def test_axpy(self) -> None: def test_copy(self) -> None: """ - test for the copy()-method + test for method copy() """ x = Vector([1, 0, 0, 0, 0, 0]) y = x.copy() @@ -102,53 +109,94 @@ def test_copy(self) -> None: def test_changeComponent(self) -> None: """ - test for the changeComponent(...)-method + test for method change_component() """ x = Vector([1, 0, 0]) - x.changeComponent(0, 0) - x.changeComponent(1, 1) + x.change_component(0, 0) + x.change_component(1, 1) self.assertEqual(str(x), "(0,1,0)") def test_str_matrix(self) -> None: + """ + test for Matrix method str() + """ A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) self.assertEqual("|1,2,3|\n|2,4,5|\n|6,7,8|\n", str(A)) - def test_determinate(self) -> None: + def test_minor(self) -> None: + """ + test for Matrix method minor() + """ + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + minors = [[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]] + for x in range(A.height()): + for y in range(A.width()): + self.assertEqual(minors[x][y], A.minor(x, y)) + + def test_cofactor(self) -> None: """ - test for determinate() + test for Matrix method cofactor() """ - A = Matrix([[1, 1, 4, 5], [3, 3, 3, 2], [5, 1, 9, 0], [9, 7, 7, 9]], 4, 4) - self.assertEqual(-376, A.determinate()) + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + cofactors = [[-3, 14, -10], [5, -10, 5], [-2, 1, 0]] + for x in range(A.height()): + for y in range(A.width()): + self.assertEqual(cofactors[x][y], A.cofactor(x, y)) + + def test_determinant(self) -> None: + """ + test for Matrix method determinant() + """ + A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) + self.assertEqual(-5, A.determinant()) def test__mul__matrix(self) -> None: + """ + test for Matrix * operator + """ A = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]], 3, 3) x = Vector([1, 2, 3]) self.assertEqual("(14,32,50)", str(A * x)) self.assertEqual("|2,4,6|\n|8,10,12|\n|14,16,18|\n", str(A * 2)) - def test_changeComponent_matrix(self) -> None: + def test_change_component_matrix(self) -> None: + """ + test for Matrix method change_component() + """ A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) - A.changeComponent(0, 2, 5) + A.change_component(0, 2, 5) self.assertEqual("|1,2,5|\n|2,4,5|\n|6,7,8|\n", str(A)) def test_component_matrix(self) -> None: + """ + test for Matrix method component() + """ A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) self.assertEqual(7, A.component(2, 1), 0.01) def test__add__matrix(self) -> None: + """ + test for Matrix + operator + """ A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3) self.assertEqual("|2,4,10|\n|4,8,10|\n|12,14,18|\n", str(A + B)) def test__sub__matrix(self) -> None: + """ + test for Matrix - operator + """ A = Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]], 3, 3) B = Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]], 3, 3) self.assertEqual("|0,0,-4|\n|0,0,0|\n|0,0,-2|\n", str(A - B)) def test_squareZeroMatrix(self) -> None: + """ + test for global function square_zero_matrix() + """ self.assertEqual( - "|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|" + "\n|0,0,0,0,0|\n", - str(squareZeroMatrix(5)), + "|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n", + str(square_zero_matrix(5)), ) From 3756b2fded6f3cc842cbb1744cc92733d63d9488 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 17 Oct 2021 23:24:49 -0400 Subject: [PATCH 3/6] Reorganize Vector and Matrix methods --- linear_algebra/src/lib.py | 322 +++++++++++++++++++------------------- 1 file changed, 163 insertions(+), 159 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index d245952e6597..be5e7e1c5b67 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -11,12 +11,12 @@ - class Vector - function zero_vector(dimension) -- function unit_basis_vector(dimension,pos) -- function axpy(scalar,vector1,vector2) -- function random_vector(N,a,b) +- function unit_basis_vector(dimension, pos) +- function axpy(scalar, vector1, vector2) +- function random_vector(N, a, b) - class Matrix - function square_zero_matrix(N) -- function random_matrix(W,H,a,b) +- function random_matrix(W, H, a, b) """ from __future__ import annotations @@ -30,20 +30,23 @@ class Vector: This class represents a vector of arbitrary size. You need to give the vector components. - Overview about the methods: - - constructor(components : list) : init the vector - set(components : list) : changes the vector components. - __str__() : toString method - component(i : int): gets the i-th component (start by 0) - __len__() : gets the size of the vector (number of components) - euclidean_length() : returns the euclidean length of the vector. - operator + : vector addition - operator - : vector subtraction - operator * : scalar multiplication and dot product - copy() : copies this vector and returns it. - change_component(pos,value) : changes the specified component. - TODO: compare-operator + Overview of the methods: + + __init__(components: Collection[float] | None): init the vector + __len__(): gets the size of the vector (number of components) + __str__(): returns a string representation + __add__(other: Vector): vector addition + __sub__(other: Vector): vector subtraction + __mul__(other: float): scalar multiplication + __mul__(other: Vector): dot product + set(components: Collection[float]): changes the vector components + copy(): copies this vector and returns it + component(i): gets the i-th component (0-indexed) + change_component(pos: int, value: float): changes specified component + euclidean_length(): returns the euclidean length of the vector + magnitude(): returns the magnitude of the vector + angle(other: Vector, deg: bool): returns the angle between two vectors + TODO: compare-operator """ def __init__(self, components: Collection[float] | None = None) -> None: @@ -55,45 +58,17 @@ def __init__(self, components: Collection[float] | None = None) -> None: components = [] self.__components = list(components) - def set(self, components: Collection[float]) -> None: - """ - input: new components - changes the components of the vector. - replace the components with newer one. - """ - if len(components) > 0: - self.__components = list(components) - else: - raise Exception("please give any vector") - - def __str__(self) -> str: - """ - returns a string representation of the vector - """ - return "(" + ",".join(map(str, self.__components)) + ")" - - def component(self, i: int) -> float: - """ - input: index (start at 0) - output: the i-th component of the vector. - """ - if type(i) is int and -len(self.__components) <= i < len(self.__components): - return self.__components[i] - else: - raise Exception("index out of range") - def __len__(self) -> int: """ returns the size of the vector """ return len(self.__components) - def euclidean_length(self) -> float: + def __str__(self) -> str: """ - returns the euclidean length of the vector + returns a string representation of the vector """ - squares = [c ** 2 for c in self.__components] - return math.sqrt(sum(squares)) + return "(" + ",".join(map(str, self.__components)) + ")" def __add__(self, other: Vector) -> Vector: """ @@ -144,6 +119,50 @@ def __mul__(self, other: float | Vector) -> float | Vector: else: # error case raise Exception("invalid operand!") + def set(self, components: Collection[float]) -> None: + """ + input: new components + changes the components of the vector. + replace the components with newer one. + """ + if len(components) > 0: + self.__components = list(components) + else: + raise Exception("please give any vector") + + def copy(self) -> Vector: + """ + copies this vector and returns it. + """ + return Vector(self.__components) + + def component(self, i: int) -> float: + """ + input: index (0-indexed) + output: the i-th component of the vector. + """ + if type(i) is int and -len(self.__components) <= i < len(self.__components): + return self.__components[i] + else: + raise Exception("index out of range") + + def change_component(self, pos: int, value: float) -> None: + """ + input: an index (pos) and a value + changes the specified component (pos) with the + 'value' + """ + # precondition + assert -len(self.__components) <= pos < len(self.__components) + self.__components[pos] = value + + def euclidean_length(self) -> float: + """ + returns the euclidean length of the vector + """ + squares = [c ** 2 for c in self.__components] + return math.sqrt(sum(squares)) + def magnitude(self) -> float: """ Magnitude of a Vector @@ -175,22 +194,6 @@ def angle(self, other: Vector, deg: bool = False) -> float: else: return math.acos(num / den) - def copy(self) -> Vector: - """ - copies this vector and returns it. - """ - return Vector(self.__components) - - def change_component(self, pos: int, value: float) -> None: - """ - input: an index (pos) and a value - changes the specified component (pos) with the - 'value' - """ - # precondition - assert -len(self.__components) <= pos < len(self.__components) - self.__components[pos] = value - def zero_vector(dimension: int) -> Vector: """ @@ -245,20 +248,21 @@ class Matrix: class: Matrix This class represents an arbitrary matrix. - Overview about the methods: - - __str__() : returns a string representation - operator * : implements the matrix vector multiplication - implements the matrix scalar multiplication - change_component(x, y, value) : changes the specified component - component(x, y) : returns the specified component - width() : returns the width of the matrix - height() : returns the height of the matrix - minor() : returns the minor of the matrix along (x, y) - cofactor() : returns the cofactor of the matrix along (x, y) - determinant() : returns the determinant of the matrix - operator + : implements matrix addition - operator - : implements matrix subtraction + Overview of the methods: + + __init__(): + __str__(): returns a string representation + __add__(other: Matrix): matrix addition + __sub__(other: Matrix): matrix subtraction + __mul__(other: float): scalar multiplication + __mul__(other: Vector): vector multiplication + height() : returns height + width() : returns width + component(x: int, y: int): returns specified component + change_component(x: int, y: int, value: float): changes specified component + minor(x: int, y: int): returns minor along (x, y) + cofactor(x: int, y: int): returns cofactor along (x, y) + determinant() : returns determinant """ def __init__(self, matrix: list[list[float]], w: int, h: int) -> None: @@ -285,29 +289,72 @@ def __str__(self) -> str: ans += str(self.__matrix[i][j]) + "|\n" return ans - def change_component(self, x: int, y: int, value: float) -> None: + def __add__(self, other: Matrix) -> Matrix: """ - changes the x-y component of this matrix + implements the matrix-addition. """ - if 0 <= x < self.__height and 0 <= y < self.__width: - self.__matrix[x][y] = value + if self.__width == other.width() and self.__height == other.height(): + matrix = [] + for i in range(self.__height): + row = [ + self.__matrix[i][j] + other.component(i, j) + for j in range(self.__width) + ] + matrix.append(row) + return Matrix(matrix, self.__width, self.__height) else: - raise Exception("change_component: indices out of bounds") + raise Exception("matrix must have the same dimension!") - def component(self, x: int, y: int) -> float: + def __sub__(self, other: Matrix) -> Matrix: """ - returns the specified (x,y) component + implements the matrix-subtraction. """ - if 0 <= x < self.__height and 0 <= y < self.__width: - return self.__matrix[x][y] + if self.__width == other.width() and self.__height == other.height(): + matrix = [] + for i in range(self.__height): + row = [ + self.__matrix[i][j] - other.component(i, j) + for j in range(self.__width) + ] + matrix.append(row) + return Matrix(matrix, self.__width, self.__height) else: - raise Exception("change_component: indices out of bounds") + raise Exception("matrix must have the same dimension!") - def width(self) -> int: + @overload + def __mul__(self, other: float) -> Matrix: + ... + + @overload + def __mul__(self, other: Vector) -> Vector: + ... + + def __mul__(self, other: float | Vector) -> Vector | Matrix: """ - getter for the width + implements the matrix-vector multiplication. + implements the matrix-scalar multiplication """ - return self.__width + if isinstance(other, Vector): # matrix-vector + if len(other) == self.__width: + ans = zero_vector(self.__height) + for i in range(self.__height): + prods = [ + self.__matrix[i][j] * other.component(j) + for j in range(self.__width) + ] + ans.change_component(i, sum(prods)) + return ans + else: + raise Exception( + "vector must have the same size as the " + "number of columns of the matrix!" + ) + elif isinstance(other, int) or isinstance(other, float): # matrix-scalar + matrix = [ + [self.__matrix[i][j] * other for j in range(self.__width)] + for i in range(self.__height) + ] + return Matrix(matrix, self.__width, self.__height) def height(self) -> int: """ @@ -315,6 +362,30 @@ def height(self) -> int: """ return self.__height + def width(self) -> int: + """ + getter for the width + """ + return self.__width + + def component(self, x: int, y: int) -> float: + """ + returns the specified (x,y) component + """ + if 0 <= x < self.__height and 0 <= y < self.__width: + return self.__matrix[x][y] + else: + raise Exception("change_component: indices out of bounds") + + def change_component(self, x: int, y: int, value: float) -> None: + """ + changes the x-y component of this matrix + """ + if 0 <= x < self.__height and 0 <= y < self.__width: + self.__matrix[x][y] = value + else: + raise Exception("change_component: indices out of bounds") + def minor(self, x: int, y: int) -> float: """ returns the minor along (x, y) @@ -358,73 +429,6 @@ def determinant(self) -> float: ] return sum(cofactor_prods) - @overload - def __mul__(self, other: float) -> Matrix: - ... - - @overload - def __mul__(self, other: Vector) -> Vector: - ... - - def __mul__(self, other: float | Vector) -> Vector | Matrix: - """ - implements the matrix-vector multiplication. - implements the matrix-scalar multiplication - """ - if isinstance(other, Vector): # matrix-vector - if len(other) == self.__width: - ans = zero_vector(self.__height) - for i in range(self.__height): - prods = [ - self.__matrix[i][j] * other.component(j) - for j in range(self.__width) - ] - ans.change_component(i, sum(prods)) - return ans - else: - raise Exception( - "vector must have the same size as the " - "number of columns of the matrix!" - ) - elif isinstance(other, int) or isinstance(other, float): # matrix-scalar - matrix = [ - [self.__matrix[i][j] * other for j in range(self.__width)] - for i in range(self.__height) - ] - return Matrix(matrix, self.__width, self.__height) - - def __add__(self, other: Matrix) -> Matrix: - """ - implements the matrix-addition. - """ - if self.__width == other.width() and self.__height == other.height(): - matrix = [] - for i in range(self.__height): - row = [ - self.__matrix[i][j] + other.component(i, j) - for j in range(self.__width) - ] - matrix.append(row) - return Matrix(matrix, self.__width, self.__height) - else: - raise Exception("matrix must have the same dimension!") - - def __sub__(self, other: Matrix) -> Matrix: - """ - implements the matrix-subtraction. - """ - if self.__width == other.width() and self.__height == other.height(): - matrix = [] - for i in range(self.__height): - row = [ - self.__matrix[i][j] - other.component(i, j) - for j in range(self.__width) - ] - matrix.append(row) - return Matrix(matrix, self.__width, self.__height) - else: - raise Exception("matrix must have the same dimension!") - def square_zero_matrix(n: int) -> Matrix: """ From 04a0845148e5d99cf84f84130bd530458f7f59d5 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 19 Oct 2021 07:14:26 +0000 Subject: [PATCH 4/6] Update linear_algebra/README.md Co-authored-by: John Law --- linear_algebra/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linear_algebra/README.md b/linear_algebra/README.md index e52c70b8d997..d43ababf8f55 100644 --- a/linear_algebra/README.md +++ b/linear_algebra/README.md @@ -17,7 +17,7 @@ This module contains classes and functions for doing linear algebra. - \_\_str\_\_() : toString method - component(i : int): gets the i-th component (start by 0) - \_\_len\_\_() : gets the size / length of the vector (number of components) - - euclidean_length() : returns the eulidean length of the vector. + - euclidean_length() : returns the eulidean length of the vector - operator + : vector addition - operator - : vector subtraction - operator * : scalar multiplication and dot product From 53f9953a2182c0983688b39e27ecc46f68a98abd Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 19 Oct 2021 07:20:13 +0000 Subject: [PATCH 5/6] Fix punctuation and wording --- linear_algebra/README.md | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/linear_algebra/README.md b/linear_algebra/README.md index d43ababf8f55..35b50b5e0f0a 100644 --- a/linear_algebra/README.md +++ b/linear_algebra/README.md @@ -10,50 +10,50 @@ This module contains classes and functions for doing linear algebra. - - This class represents a vector of arbitrary size and related operations. - **Overview about the methods:** + **Overview of the methods:** - - constructor(components : list) : init the vector - - set(components : list) : changes the vector components. + - constructor(components) : init the vector + - set(components) : changes the vector components. - \_\_str\_\_() : toString method - - component(i : int): gets the i-th component (start by 0) + - component(i): gets the i-th component (0-indexed) - \_\_len\_\_() : gets the size / length of the vector (number of components) - euclidean_length() : returns the eulidean length of the vector - operator + : vector addition - operator - : vector subtraction - operator * : scalar multiplication and dot product - - copy() : copies this vector and returns it. - - change_component(pos,value) : changes the specified component. + - copy() : copies this vector and returns it + - change_component(pos,value) : changes the specified component - function zero_vector(dimension) - returns a zero vector of 'dimension' -- function unit_basis_vector(dimension,pos) - - returns a unit basis vector with a One at index 'pos' (indexing at 0) -- function axpy(scalar,vector1,vector2) +- function unit_basis_vector(dimension, pos) + - returns a unit basis vector with a one at index 'pos' (0-indexed) +- function axpy(scalar, vector1, vector2) - computes the axpy operation -- function random_vector(N,a,b) - - returns a random vector of size N, with random integer components between 'a' and 'b'. +- function random_vector(N, a, b) + - returns a random vector of size N, with random integer components between 'a' and 'b' inclusive ### class Matrix - - This class represents a matrix of arbitrary size and operations on it. - **Overview about the methods:** + **Overview of the methods:** - \_\_str\_\_() : returns a string representation - operator * : implements the matrix vector multiplication implements the matrix-scalar multiplication. - - change_component(x,y,value) : changes the specified component. - - component(x,y) : returns the specified component. + - change_component(x, y, value) : changes the specified component. + - component(x, y) : returns the specified component. - width() : returns the width of the matrix - height() : returns the height of the matrix - determinant() : returns the determinant of the matrix if it is square - operator + : implements the matrix-addition. - - operator - _ implements the matrix-subtraction + - operator - : implements the matrix-subtraction - function square_zero_matrix(N) - returns a square zero-matrix of dimension NxN -- function random_matrix(W,H,a,b) - - returns a random matrix WxH with integer components between 'a' and 'b' +- function random_matrix(W, H, a, b) + - returns a random matrix WxH with integer components between 'a' and 'b' inclusive --- ## Documentation From f176b18251533e8a5db4248d1a246df9da646b17 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 19 Oct 2021 07:22:34 +0000 Subject: [PATCH 6/6] Apply suggestions from code review Co-authored-by: John Law --- linear_algebra/src/lib.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index be5e7e1c5b67..dad0a8c0a6a2 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -123,7 +123,7 @@ def set(self, components: Collection[float]) -> None: """ input: new components changes the components of the vector. - replace the components with newer one. + replaces the components with newer one. """ if len(components) > 0: self.__components = list(components) @@ -319,7 +319,7 @@ def __sub__(self, other: Matrix) -> Matrix: matrix.append(row) return Matrix(matrix, self.__width, self.__height) else: - raise Exception("matrix must have the same dimension!") + raise Exception("matrices must have the same dimension!") @overload def __mul__(self, other: float) -> Matrix: @@ -415,7 +415,7 @@ def determinant(self) -> float: if self.__height != self.__width: raise Exception("Matrix is not square") if self.__height < 1: - raise Exception("Matrix has no elements") + raise Exception("Matrix has no element") elif self.__height == 1: return self.__matrix[0][0] elif self.__height == 2: