From ad6c2156e227cdacc97de38f43a6ac8f10f74d4b Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sat, 16 Oct 2021 18:08:28 -0400 Subject: [PATCH 01/15] 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 02/15] 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 03/15] 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 04/15] 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 05/15] 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 06/15] 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: From 36dd293b7d628cfde76eb6ae68eb3e25c48e4f21 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 29 Oct 2021 04:39:34 -0400 Subject: [PATCH 07/15] Deduplicate euclidean length method for Vector --- linear_algebra/src/lib.py | 30 +++++++++++------------ linear_algebra/src/test_linear_algebra.py | 4 ++- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index dad0a8c0a6a2..350bc16fd482 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -159,18 +159,18 @@ def change_component(self, pos: int, value: float) -> None: 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 - >>> Vector([2, 3, 4]).magnitude() + >>> Vector([2, 3, 4]).euclidean_length() 5.385164807134504 - + >>> Vector([1]).euclidean_length() + 1 + >>> Vector([]).euclidean_length() + Traceback (most recent call last): + ... + Exception: Vector is empty """ + if len(self.__components) == 0: + raise Exception("Vector is empty") squares = [c ** 2 for c in self.__components] return math.sqrt(sum(squares)) @@ -188,7 +188,7 @@ def angle(self, other: Vector, deg: bool = False) -> float: Exception: invalid operand! """ num = self * other - den = self.magnitude() * other.magnitude() + den = self.euclidean_length() * other.euclidean_length() if deg: return math.degrees(math.acos(num / den)) else: @@ -267,8 +267,7 @@ class Matrix: def __init__(self, matrix: list[list[float]], w: int, h: int) -> None: """ - simple constructor for initializing - the matrix with components. + simple constructor for initializing the matrix with components. """ self.__matrix = matrix self.__width = w @@ -276,8 +275,7 @@ def __init__(self, matrix: list[list[float]], w: int, h: int) -> None: def __str__(self) -> str: """ - returns a string representation of this - matrix. + returns a string representation of this matrix. """ ans = "" for i in range(self.__height): @@ -291,7 +289,7 @@ def __str__(self) -> str: def __add__(self, other: Matrix) -> Matrix: """ - implements the matrix-addition. + implements matrix addition. """ if self.__width == other.width() and self.__height == other.height(): matrix = [] @@ -307,7 +305,7 @@ def __add__(self, other: Matrix) -> Matrix: def __sub__(self, other: Matrix) -> Matrix: """ - implements the matrix-subtraction. + implements matrix subtraction. """ if self.__width == other.width() and self.__height == other.height(): matrix = [] diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py index de7041a17038..3ff45335d629 100644 --- a/linear_algebra/src/test_linear_algebra.py +++ b/linear_algebra/src/test_linear_algebra.py @@ -42,12 +42,14 @@ def test_size(self) -> None: x = Vector([1, 2, 3, 4]) self.assertEqual(len(x), 4) - def test_euclidLength(self) -> None: + def test_euclidean_length(self) -> None: """ test for method euclidean_length() """ x = Vector([1, 2]) + y = Vector([1, 2, 3, 4, 5]) self.assertAlmostEqual(x.euclidean_length(), 2.236, 3) + self.assertAlmostEqual(y.euclidean_length(), 7.416, 3) def test_add(self) -> None: """ From 6615be8f652b38740bc1c0a8f3aed9236cf9711d Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 29 Oct 2021 05:01:38 -0400 Subject: [PATCH 08/15] Add more unit tests for Euclidean length method --- linear_algebra/src/lib.py | 2 ++ linear_algebra/src/test_linear_algebra.py | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 350bc16fd482..603db3ce4090 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -164,6 +164,8 @@ def euclidean_length(self) -> float: 5.385164807134504 >>> Vector([1]).euclidean_length() 1 + >>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length() + 9.539392014169456 >>> Vector([]).euclidean_length() Traceback (most recent call last): ... diff --git a/linear_algebra/src/test_linear_algebra.py b/linear_algebra/src/test_linear_algebra.py index 3ff45335d629..724ceef2599a 100644 --- a/linear_algebra/src/test_linear_algebra.py +++ b/linear_algebra/src/test_linear_algebra.py @@ -48,8 +48,12 @@ def test_euclidean_length(self) -> None: """ x = Vector([1, 2]) y = Vector([1, 2, 3, 4, 5]) + z = Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + w = Vector([1, -1, 1, -1, 2, -3, 4, -5]) self.assertAlmostEqual(x.euclidean_length(), 2.236, 3) self.assertAlmostEqual(y.euclidean_length(), 7.416, 3) + self.assertEqual(z.euclidean_length(), 0) + self.assertAlmostEqual(w.euclidean_length(), 7.616, 3) def test_add(self) -> None: """ From c3d5dfde426bad1b6cfaa19f0d5e2e2eb19c6e6f Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 29 Oct 2021 05:14:27 -0400 Subject: [PATCH 09/15] Fix bug in unit test for euclidean_length --- linear_algebra/src/lib.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 603db3ce4090..9a3302785629 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -163,7 +163,7 @@ def euclidean_length(self) -> float: >>> Vector([2, 3, 4]).euclidean_length() 5.385164807134504 >>> Vector([1]).euclidean_length() - 1 + 1.0 >>> Vector([0, -1, -2, -3, 4, 5, 6]).euclidean_length() 9.539392014169456 >>> Vector([]).euclidean_length() From 45662fe20ffed8a7fcb5eb35dc710df1965c0b1c Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sat, 30 Oct 2021 13:27:15 -0400 Subject: [PATCH 10/15] Remove old comments for magnitude method --- linear_algebra/src/lib.py | 1 - 1 file changed, 1 deletion(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 9a3302785629..85dc4b71c4a4 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -44,7 +44,6 @@ class Vector: 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 """ From 66304f91746c08d472c22d86020994b0f6991295 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 31 Oct 2021 18:55:13 -0400 Subject: [PATCH 11/15] Rewrite maths/fibonacci.py --- maths/fibonacci.py | 203 ++++++++++++++++----------------------------- 1 file changed, 73 insertions(+), 130 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index e6519035401e..5479721f8cae 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -1,130 +1,73 @@ -# fibonacci.py -""" -1. Calculates the iterative fibonacci sequence - -2. Calculates the fibonacci sequence with a formula - an = [ Phin - (phi)n ]/Sqrt[5] - reference-->Su, Francis E., et al. "Fibonacci Number Formula." Math Fun Facts. - -""" -import functools -import math -import time -from decimal import Decimal, getcontext - -getcontext().prec = 100 - - -def timer_decorator(func): - @functools.wraps(func) - def timer_wrapper(*args, **kwargs): - start = time.time() - func(*args, **kwargs) - end = time.time() - if int(end - start) > 0: - print(f"Run time for {func.__name__}: {(end - start):0.2f}s") - else: - print(f"Run time for {func.__name__}: {(end - start)*1000:0.2f}ms") - return func(*args, **kwargs) - - return timer_wrapper - - -# define Python user-defined exceptions -class Error(Exception): - """Base class for other exceptions""" - - -class ValueTooLargeError(Error): - """Raised when the input value is too large""" - - -class ValueTooSmallError(Error): - """Raised when the input value is not greater than one""" - - -class ValueLessThanZero(Error): - """Raised when the input value is less than zero""" - - -def _check_number_input(n, min_thresh, max_thresh=None): - """ - :param n: single integer - :type n: int - :param min_thresh: min threshold, single integer - :type min_thresh: int - :param max_thresh: max threshold, single integer - :type max_thresh: int - :return: boolean - """ - try: - if n >= min_thresh and max_thresh is None: - return True - elif min_thresh <= n <= max_thresh: - return True - elif n < 0: - raise ValueLessThanZero - elif n < min_thresh: - raise ValueTooSmallError - elif n > max_thresh: - raise ValueTooLargeError - except ValueLessThanZero: - print("Incorrect Input: number must not be less than 0") - except ValueTooSmallError: - print( - f"Incorrect Input: input number must be > {min_thresh} for the recursive " - "calculation" - ) - except ValueTooLargeError: - print( - f"Incorrect Input: input number must be < {max_thresh} for the recursive " - "calculation" - ) - return False - - -@timer_decorator -def fib_iterative(n): - """ - :param n: calculate Fibonacci to the nth integer - :type n:int - :return: Fibonacci sequence as a list - """ - n = int(n) - if _check_number_input(n, 2): - seq_out = [0, 1] - a, b = 0, 1 - for _ in range(n - len(seq_out)): - a, b = b, a + b - seq_out.append(b) - return seq_out - - -@timer_decorator -def fib_formula(n): - """ - :param n: calculate Fibonacci to the nth integer - :type n:int - :return: Fibonacci sequence as a list - """ - seq_out = [0, 1] - n = int(n) - if _check_number_input(n, 2, 1000000): - sqrt = Decimal(math.sqrt(5)) - phi_1 = Decimal(1 + sqrt) / Decimal(2) - phi_2 = Decimal(1 - sqrt) / Decimal(2) - for i in range(2, n): - temp_out = ((phi_1 ** Decimal(i)) - (phi_2 ** Decimal(i))) * ( - Decimal(sqrt) ** Decimal(-1) - ) - seq_out.append(int(temp_out)) - return seq_out - - -if __name__ == "__main__": - num = 20 - # print(f'{fib_recursive(num)}\n') - # print(f'{fib_iterative(num)}\n') - # print(f'{fib_formula(num)}\n') - fib_iterative(num) - fib_formula(num) +# fibonacci.py +""" +Calculates the Fibonacci sequence using iteration and a simplified form of +Binet's formula + +NOTE 1: the iterative function is more accurate than the Binet's formula +function because the iterative function doesn't use floats + +NOTE 2: the Binet's formula function is much more limited in the size of inputs +that it can handle due to the size limitations of Python floats +""" + +import functools +import time +from math import sqrt + + +def timer_decorator(func): + @functools.wraps(func) + def timer_wrapper(*args, **kwargs): + start = time.time() + func(*args, **kwargs) + end = time.time() + if int(end - start) > 0: + print(f"{func.__name__} runtime: {(end - start):0.4f} s") + else: + print(f"{func.__name__} runtime: {(end - start) * 1000:0.4f} ms") + return func(*args, **kwargs) + + return timer_wrapper + + +@timer_decorator +def fib_iterative(n: int) -> list[int]: + """ + Calculates the first n (0-indexed) Fibonacci numbers using iteration + """ + if n < 0: + raise Exception("n is negative") + if n == 0: + return [0] + fib = [0, 1] + for _ in range(n - 1): + fib.append(fib[-1] + fib[-2]) + return fib + + +@timer_decorator +def fib_binet(n: int) -> list[int]: + """ + Calculates the first n (0-indexed) Fibonacci numbers using a simplified form + of Binet's formula: + https://en.m.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding + + NOTE 1: this function diverges from fib_iterative at around n = 71, likely + due to compounding floating-point arithmetic errors + + NOTE 2: this function doesn't accept n >= 1475 because it overflows + thereafter due to the size limitations of Python floats + """ + if n < 0: + raise Exception("n is negative") + if n >= 1475: + raise Exception("n is too large") + sqrt_5 = sqrt(5) + phi = (1 + sqrt_5) / 2 + return [round(phi ** i / sqrt_5) for i in range(n + 1)] + + +if __name__ == "__main__": + num = 50 + fib_iterative(num) + fib_binet(num) From 3d97fd664470f18783822e099a86cc64409fdc54 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 31 Oct 2021 22:16:15 -0400 Subject: [PATCH 12/15] Rewrite timer and add unit tests --- maths/fibonacci.py | 58 +++++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 5479721f8cae..1de362bcdc5d 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -10,30 +10,39 @@ that it can handle due to the size limitations of Python floats """ -import functools -import time from math import sqrt +from time import time -def timer_decorator(func): - @functools.wraps(func) - def timer_wrapper(*args, **kwargs): - start = time.time() - func(*args, **kwargs) - end = time.time() - if int(end - start) > 0: - print(f"{func.__name__} runtime: {(end - start):0.4f} s") - else: - print(f"{func.__name__} runtime: {(end - start) * 1000:0.4f} ms") - return func(*args, **kwargs) - - return timer_wrapper +def time_func(func, *args, **kwargs): + """ + Times the execution of a function with parameters + """ + start = time() + output = func(*args, **kwargs) + end = time() + if int(end - start) > 0: + print(f"{func.__name__} runtime: {(end - start):0.4f} s") + else: + print(f"{func.__name__} runtime: {(end - start) * 1000:0.4f} ms") + return output -@timer_decorator def fib_iterative(n: int) -> list[int]: """ Calculates the first n (0-indexed) Fibonacci numbers using iteration + >>> fib_iterative(0) + [0] + >>> fib_iterative(1) + [0, 1] + >>> fib_iterative(5) + [0, 1, 1, 2, 3, 5] + >>> fib_iterative(10) + [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] + >>> fib_iterative(-1) + Traceback (most recent call last): + ... + Exception: n is negative """ if n < 0: raise Exception("n is negative") @@ -45,7 +54,6 @@ def fib_iterative(n: int) -> list[int]: return fib -@timer_decorator def fib_binet(n: int) -> list[int]: """ Calculates the first n (0-indexed) Fibonacci numbers using a simplified form @@ -57,6 +65,18 @@ def fib_binet(n: int) -> list[int]: NOTE 2: this function doesn't accept n >= 1475 because it overflows thereafter due to the size limitations of Python floats + >>> fib_iterative(0) + [0] + >>> fib_iterative(1) + [0, 1] + >>> fib_iterative(5) + [0, 1, 1, 2, 3, 5] + >>> fib_iterative(10) + [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] + >>> fib_iterative(-1) + Traceback (most recent call last): + ... + Exception: n is negative """ if n < 0: raise Exception("n is negative") @@ -69,5 +89,5 @@ def fib_binet(n: int) -> list[int]: if __name__ == "__main__": num = 50 - fib_iterative(num) - fib_binet(num) + time_func(fib_iterative, num) + time_func(fib_binet, num) From 2cf941cab259ddedaf7dd928b536f772803d8ece Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 31 Oct 2021 22:20:24 -0400 Subject: [PATCH 13/15] Fix typos in fib_binet unit tests --- maths/fibonacci.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 1de362bcdc5d..47cc83aacaac 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -65,18 +65,22 @@ def fib_binet(n: int) -> list[int]: NOTE 2: this function doesn't accept n >= 1475 because it overflows thereafter due to the size limitations of Python floats - >>> fib_iterative(0) + >>> fib_binet(0) [0] - >>> fib_iterative(1) + >>> fib_binet(1) [0, 1] - >>> fib_iterative(5) + >>> fib_binet(5) [0, 1, 1, 2, 3, 5] - >>> fib_iterative(10) + >>> fib_binet(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - >>> fib_iterative(-1) + >>> fib_binet(-1) Traceback (most recent call last): ... Exception: n is negative + >>> fib_binet(1500) + Traceback (most recent call last): + ... + Exception: n is too large """ if n < 0: raise Exception("n is negative") @@ -88,6 +92,10 @@ def fib_binet(n: int) -> list[int]: if __name__ == "__main__": - num = 50 - time_func(fib_iterative, num) - time_func(fib_binet, num) + # num = 50 + # time_func(fib_iterative, num) + # time_func(fib_binet, num) + + import doctest + + doctest.testmod(verbose=True) From 68f33f18f47ad4b1f7d03805ac3af6b38654b16e Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 31 Oct 2021 22:21:56 -0400 Subject: [PATCH 14/15] Fix typos in fib_binet unit tests --- maths/fibonacci.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 1de362bcdc5d..e2160c0180c9 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -65,18 +65,22 @@ def fib_binet(n: int) -> list[int]: NOTE 2: this function doesn't accept n >= 1475 because it overflows thereafter due to the size limitations of Python floats - >>> fib_iterative(0) + >>> fib_binet(0) [0] - >>> fib_iterative(1) + >>> fib_binet(1) [0, 1] - >>> fib_iterative(5) + >>> fib_binet(5) [0, 1, 1, 2, 3, 5] - >>> fib_iterative(10) + >>> fib_binet(10) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55] - >>> fib_iterative(-1) + >>> fib_binet(-1) Traceback (most recent call last): ... Exception: n is negative + >>> fib_binet(1500) + Traceback (most recent call last): + ... + Exception: n is too large """ if n < 0: raise Exception("n is negative") From 2460ff6b5d2fb3e0332a7d355759e65eae295cd3 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Sun, 31 Oct 2021 22:24:51 -0400 Subject: [PATCH 15/15] Clean main method --- maths/fibonacci.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 47cc83aacaac..e2160c0180c9 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -92,10 +92,6 @@ def fib_binet(n: int) -> list[int]: if __name__ == "__main__": - # num = 50 - # time_func(fib_iterative, num) - # time_func(fib_binet, num) - - import doctest - - doctest.testmod(verbose=True) + num = 50 + time_func(fib_iterative, num) + time_func(fib_binet, num)