From 91b6ee63993357a3407a86a377749b7dae0db812 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 15 Oct 2020 17:04:06 -0400 Subject: [PATCH 01/37] Create distance_formula.py --- maths/distance_formula.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 maths/distance_formula.py diff --git a/maths/distance_formula.py b/maths/distance_formula.py new file mode 100644 index 000000000000..4155eacef683 --- /dev/null +++ b/maths/distance_formula.py @@ -0,0 +1,15 @@ +def distance_formula(x1,y1,x2,y2): + """ + Calculate the distance between the two coordinates (x1,y1) and (x2,y2). + >>> distance_formula(0, 0, 2, 2) + 2.8284271247461903 + >>> distance_formula(1, 0, 5, 2) + 4.47213595499958 + >>> distance_formula(0, 0, 5, 0) + 5.0 + """ + return ((y2-y1) ** 2 + (x2-x1) ** 2) ** (1/2) + + +if __name__ == "__main__": + print(distance_formula(0,0,2,2)) From eb044a3178a2c78b8d0706267e549d923fdcfd23 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 15 Oct 2020 17:18:56 -0400 Subject: [PATCH 02/37] Remove whitespace --- maths/distance_formula.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/distance_formula.py b/maths/distance_formula.py index 4155eacef683..1f1128bb14e4 100644 --- a/maths/distance_formula.py +++ b/maths/distance_formula.py @@ -2,11 +2,11 @@ def distance_formula(x1,y1,x2,y2): """ Calculate the distance between the two coordinates (x1,y1) and (x2,y2). >>> distance_formula(0, 0, 2, 2) - 2.8284271247461903 + 2.8284271247461903 >>> distance_formula(1, 0, 5, 2) - 4.47213595499958 + 4.47213595499958 >>> distance_formula(0, 0, 5, 0) - 5.0 + 5.0 """ return ((y2-y1) ** 2 + (x2-x1) ** 2) ** (1/2) From 861ce33cce4546ce807b7acd63aa710773157d1e Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 15 Oct 2020 17:25:01 -0400 Subject: [PATCH 03/37] Update distance_formula.py --- maths/distance_formula.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/distance_formula.py b/maths/distance_formula.py index 1f1128bb14e4..936e248dcbb3 100644 --- a/maths/distance_formula.py +++ b/maths/distance_formula.py @@ -12,4 +12,4 @@ def distance_formula(x1,y1,x2,y2): if __name__ == "__main__": - print(distance_formula(0,0,2,2)) + print(distance_formula(0,0,2,2)) From 9102f3e99b0fb6b928a8c37321046fa77c0af7ce Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 15 Oct 2020 17:31:07 -0400 Subject: [PATCH 04/37] Update distance_formula.py --- maths/distance_formula.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/distance_formula.py b/maths/distance_formula.py index 936e248dcbb3..dec448b9bce0 100644 --- a/maths/distance_formula.py +++ b/maths/distance_formula.py @@ -1,4 +1,4 @@ -def distance_formula(x1,y1,x2,y2): +def distance_formula(x1, y1, x2, y2): """ Calculate the distance between the two coordinates (x1,y1) and (x2,y2). >>> distance_formula(0, 0, 2, 2) @@ -8,8 +8,8 @@ def distance_formula(x1,y1,x2,y2): >>> distance_formula(0, 0, 5, 0) 5.0 """ - return ((y2-y1) ** 2 + (x2-x1) ** 2) ** (1/2) + return ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** (1 / 2) if __name__ == "__main__": - print(distance_formula(0,0,2,2)) + print(distance_formula(0, 0, 2, 2)) From 2cd354bf707c5af703f2f08b03dc6f50b087c6e4 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 15 Oct 2020 17:34:06 -0400 Subject: [PATCH 05/37] Update distance_formula.py --- maths/distance_formula.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/distance_formula.py b/maths/distance_formula.py index dec448b9bce0..2d7e6a51d7d8 100644 --- a/maths/distance_formula.py +++ b/maths/distance_formula.py @@ -8,7 +8,7 @@ def distance_formula(x1, y1, x2, y2): >>> distance_formula(0, 0, 5, 0) 5.0 """ - return ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** (1 / 2) + return ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** (1 / 2) if __name__ == "__main__": From 36f90ae3e6ea04556d900474874d93322807ba37 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 16 Oct 2020 09:54:46 -0400 Subject: [PATCH 06/37] Generalize --- maths/distance_formula.py | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/maths/distance_formula.py b/maths/distance_formula.py index 2d7e6a51d7d8..0774ca909d86 100644 --- a/maths/distance_formula.py +++ b/maths/distance_formula.py @@ -1,15 +1,22 @@ -def distance_formula(x1, y1, x2, y2): +import numpy as np + +def euclidean_distance(v1, v2) -> int: """ - Calculate the distance between the two coordinates (x1,y1) and (x2,y2). - >>> distance_formula(0, 0, 2, 2) + Calculate the distance between the two the endpoints of two vectors, v1 and v2. + >>> euclidean_distance(np.array([0, 0]), np.array([2, 2])) 2.8284271247461903 - >>> distance_formula(1, 0, 5, 2) - 4.47213595499958 - >>> distance_formula(0, 0, 5, 0) + >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) + 3.4641016151377544 + >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) 5.0 """ - return ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** (1 / 2) + sumSquared = 0 + for i in range(len(v1)): + sumSquared += (v1[i] - v2[i]) ** 2 + return sumSquared ** (1 / 2) if __name__ == "__main__": - print(distance_formula(0, 0, 2, 2)) + point = np.array([1, 2, 3, 4]) + point2 = np.array([5, 6, 7, 8]) + print(euclidean_distance(point, point2)) From 9186ba98598c85ecf1edb7b382e4128352a9e61e Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 16 Oct 2020 09:57:11 -0400 Subject: [PATCH 07/37] Grammar mistake --- maths/distance_formula.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/distance_formula.py b/maths/distance_formula.py index 0774ca909d86..8e0a340f0714 100644 --- a/maths/distance_formula.py +++ b/maths/distance_formula.py @@ -2,7 +2,7 @@ def euclidean_distance(v1, v2) -> int: """ - Calculate the distance between the two the endpoints of two vectors, v1 and v2. + Calculate the distance between the two = endpoints of two vectors, v1 and v2. >>> euclidean_distance(np.array([0, 0]), np.array([2, 2])) 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) @@ -13,7 +13,6 @@ def euclidean_distance(v1, v2) -> int: sumSquared = 0 for i in range(len(v1)): sumSquared += (v1[i] - v2[i]) ** 2 - return sumSquared ** (1 / 2) if __name__ == "__main__": From 4dd283a39826ff6bd989179ef0f79723f763af18 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 16 Oct 2020 10:04:11 -0400 Subject: [PATCH 08/37] Rename distance_formula.py to euclidean_distance.py --- maths/{distance_formula.py => euclidean_distance.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename maths/{distance_formula.py => euclidean_distance.py} (100%) diff --git a/maths/distance_formula.py b/maths/euclidean_distance.py similarity index 100% rename from maths/distance_formula.py rename to maths/euclidean_distance.py From 01a1ea9c78d9c41a8f53663ba5615cc76eca476c Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 16 Oct 2020 10:10:41 -0400 Subject: [PATCH 09/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 8e0a340f0714..1be268f8312e 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -2,17 +2,17 @@ def euclidean_distance(v1, v2) -> int: """ - Calculate the distance between the two = endpoints of two vectors, v1 and v2. + Calculate the distance between the two = endpoints of two vectors, v1 and v2. v1 and v2 must be the same length. >>> euclidean_distance(np.array([0, 0]), np.array([2, 2])) 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) 3.4641016151377544 >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) - 5.0 + 8.0 """ sumSquared = 0 for i in range(len(v1)): - sumSquared += (v1[i] - v2[i]) ** 2 + sumSquared += (v1[i] - v1[i]) ** 2 return sumSquared ** (1 / 2) if __name__ == "__main__": From 0451a5b5b436d25fc06f6ba9fd738f559840facf Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 16 Oct 2020 10:24:57 -0400 Subject: [PATCH 10/37] v1 - > v2 --- maths/euclidean_distance.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 1be268f8312e..488f7d0d177a 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -2,7 +2,7 @@ def euclidean_distance(v1, v2) -> int: """ - Calculate the distance between the two = endpoints of two vectors, v1 and v2. v1 and v2 must be the same length. + Calculate the distance between the two = endpoints of two vectors, v1 and v2. >>> euclidean_distance(np.array([0, 0]), np.array([2, 2])) 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) @@ -10,12 +10,13 @@ def euclidean_distance(v1, v2) -> int: >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) 8.0 """ + sumSquared = 0 for i in range(len(v1)): - sumSquared += (v1[i] - v1[i]) ** 2 + sumSquared += (v2[i] - v1[i]) ** 2 return sumSquared ** (1 / 2) if __name__ == "__main__": - point = np.array([1, 2, 3, 4]) - point2 = np.array([5, 6, 7, 8]) - print(euclidean_distance(point, point2)) + point = np.array([2, 2]) + point2 = np.array([0, 0]) + print(euclidean_distance(point, point2)) From 3940049dc2ad9d34b5280e3acd9fa26c79ab156e Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 16 Oct 2020 13:48:04 -0400 Subject: [PATCH 11/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 488f7d0d177a..7038b1de9b6c 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -1,6 +1,6 @@ import numpy as np -def euclidean_distance(v1, v2) -> int: +def euclidean_distance(v1, v2) -> double: """ Calculate the distance between the two = endpoints of two vectors, v1 and v2. >>> euclidean_distance(np.array([0, 0]), np.array([2, 2])) From de643925cd7fed99ac8dd01ac0cd4d2306770c02 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:41:58 -0400 Subject: [PATCH 12/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 7038b1de9b6c..86f0a096850c 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -1,20 +1,21 @@ import numpy as np +from typing import Union, List, Tuple -def euclidean_distance(v1, v2) -> double: +Vector = Union[List[float], List[int], Tuple[float], Tuple[int], np.ndarray] +VectorOut = Union[np.float64, int, float] + +def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ - Calculate the distance between the two = endpoints of two vectors, v1 and v2. - >>> euclidean_distance(np.array([0, 0]), np.array([2, 2])) + Calculate the distance between the two endpoints of two vectors. A vector is defined as a list, tuple, or numpy 1D array. 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) 3.4641016151377544 >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) 8.0 + >>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) + 8.0 """ - - sumSquared = 0 - for i in range(len(v1)): - sumSquared += (v2[i] - v1[i]) ** 2 - return sumSquared ** (1 / 2) + return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2))**2)) if __name__ == "__main__": point = np.array([2, 2]) From 5a092eb8b480c512fd46284d9069e094b4e5812d Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:46:15 -0400 Subject: [PATCH 13/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 86f0a096850c..6b75d4648718 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -15,9 +15,9 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: >>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) 8.0 """ - return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2))**2)) + return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2)) if __name__ == "__main__": point = np.array([2, 2]) - point2 = np.array([0, 0]) - print(euclidean_distance(point, point2)) + point_2 = np.array([0, 0]) + print(euclidean_distance(point, point_2)) From b5152b23f15622feca7515fb69ffb13e6dc84ed3 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:51:20 -0400 Subject: [PATCH 14/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 6b75d4648718..857b24b9ed18 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -4,9 +4,11 @@ Vector = Union[List[float], List[int], Tuple[float], Tuple[int], np.ndarray] VectorOut = Union[np.float64, int, float] + def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ - Calculate the distance between the two endpoints of two vectors. A vector is defined as a list, tuple, or numpy 1D array. + Calculate the distance between the two endpoints of two vectors. + A vector is defined as a list, tuple, or numpy 1D array. 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) 3.4641016151377544 @@ -17,6 +19,7 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2)) + if __name__ == "__main__": point = np.array([2, 2]) point_2 = np.array([0, 0]) From a1b9a868b5bdac9295ae1d84a0835130ea825e79 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:54:55 -0400 Subject: [PATCH 15/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 857b24b9ed18..f36d97bffe3d 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -1,6 +1,7 @@ import numpy as np from typing import Union, List, Tuple + Vector = Union[List[float], List[int], Tuple[float], Tuple[int], np.ndarray] VectorOut = Union[np.float64, int, float] From ac0ff3781b231dff513048ebc761c4fa79691170 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:58:11 -0400 Subject: [PATCH 16/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index f36d97bffe3d..02c87345a48a 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -1,6 +1,6 @@ -import numpy as np -from typing import Union, List, Tuple +from typing import List, Tuple, Union +import numpy as np Vector = Union[List[float], List[int], Tuple[float], Tuple[int], np.ndarray] VectorOut = Union[np.float64, int, float] From 6dad371109e657bf37c2654e80b18e92d27abb7d Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Mon, 19 Oct 2020 11:27:25 -0400 Subject: [PATCH 17/37] Update maths/euclidean_distance.py Co-authored-by: Christian Clauss --- maths/euclidean_distance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 02c87345a48a..fc7cd70a990d 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -1,8 +1,8 @@ -from typing import List, Tuple, Union +from typing import Iterable, Union import numpy as np -Vector = Union[List[float], List[int], Tuple[float], Tuple[int], np.ndarray] +Vector = Union[Iterable[float], Iterable[int], np.ndarray] VectorOut = Union[np.float64, int, float] From 36185ab0b4597dec1347f7485f1c038bf0009ed0 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Tue, 20 Oct 2020 11:14:22 -0400 Subject: [PATCH 18/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index fc7cd70a990d..4be5a5e8ce4a 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -10,6 +10,7 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ Calculate the distance between the two endpoints of two vectors. A vector is defined as a list, tuple, or numpy 1D array. + >>> euclidean_distance(np.array([0, 0), np.array([2, 2])) 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) 3.4641016151377544 From d69c65faf768bdc46c844beb9fbcbbffd5f24f8c Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Tue, 20 Oct 2020 11:16:20 -0400 Subject: [PATCH 19/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 4be5a5e8ce4a..af83eadd6692 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -10,7 +10,7 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ Calculate the distance between the two endpoints of two vectors. A vector is defined as a list, tuple, or numpy 1D array. - >>> euclidean_distance(np.array([0, 0), np.array([2, 2])) + >>> euclidean_distance((0, 0), (2, 2)) 2.8284271247461903 >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) 3.4641016151377544 From 68635e29b4b9fae26b92b11f0cb90edd044601dd Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:40:01 -0400 Subject: [PATCH 20/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index af83eadd6692..35ec30dec81d 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -21,7 +21,24 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2)) - +def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: + """ + Calculate the distance between the two endpoints of two vectors without numpy. + A vector is defined as a list, tuple, or numpy 1D array. + >>> euclidean_distance((0, 0), (2, 2)) + 2.8284271247461903 + >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) + 3.4641016151377544 + >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) + 8.0 + >>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) + 8.0 + """ + the_sum = 0 + for i in range(len(vector_1)): + the_sum += (vector_2[i] - vector_1[i]) ** 2 + return sumSquared ** (1 / 2) + if __name__ == "__main__": point = np.array([2, 2]) point_2 = np.array([0, 0]) From aedcb59830404dc65f6701da2214366a73543dab Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:43:02 -0400 Subject: [PATCH 21/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 35ec30dec81d..4b476d6da7be 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -39,6 +39,7 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: the_sum += (vector_2[i] - vector_1[i]) ** 2 return sumSquared ** (1 / 2) + if __name__ == "__main__": point = np.array([2, 2]) point_2 = np.array([0, 0]) From 0581677a2df0b4ac4b073660940eaba51f0e0dc8 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:45:03 -0400 Subject: [PATCH 22/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 4b476d6da7be..308e9581fcd4 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -27,10 +27,6 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: A vector is defined as a list, tuple, or numpy 1D array. >>> euclidean_distance((0, 0), (2, 2)) 2.8284271247461903 - >>> euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])) - 3.4641016151377544 - >>> euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])) - 8.0 >>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) 8.0 """ From a278402bc662cd1dd82174e1963528091ea41440 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:49:59 -0400 Subject: [PATCH 23/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 308e9581fcd4..5644c648c02a 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -34,8 +34,8 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: for i in range(len(vector_1)): the_sum += (vector_2[i] - vector_1[i]) ** 2 return sumSquared ** (1 / 2) - - + + if __name__ == "__main__": point = np.array([2, 2]) point_2 = np.array([0, 0]) From e12d80735e153d3a3d97ae0688840949059ce0c4 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:53:06 -0400 Subject: [PATCH 24/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 5644c648c02a..da6fdcf5a583 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -32,8 +32,8 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: """ the_sum = 0 for i in range(len(vector_1)): - the_sum += (vector_2[i] - vector_1[i]) ** 2 - return sumSquared ** (1 / 2) + the_sum += (vector_2[i] - vector_1[i]) ** 2 + return sumSquared ** (1 / 2) if __name__ == "__main__": From 3ea94fa8ef2e3d7db2324bfd7fb585b117b87155 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:56:03 -0400 Subject: [PATCH 25/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index da6fdcf5a583..3257d08fcaad 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -21,6 +21,7 @@ def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut: """ return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2)) + def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: """ Calculate the distance between the two endpoints of two vectors without numpy. From 21bba6af27234882e7db73ac06c88e9f0e272af0 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Wed, 21 Oct 2020 12:59:40 -0400 Subject: [PATCH 26/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 3257d08fcaad..94e383cd7cbb 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -34,7 +34,7 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: the_sum = 0 for i in range(len(vector_1)): the_sum += (vector_2[i] - vector_1[i]) ** 2 - return sumSquared ** (1 / 2) + return the_sum ** (1 / 2) if __name__ == "__main__": From 39e0b46e1546d8fde3f55d0072ad69a17eb62bde Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:34:25 -0400 Subject: [PATCH 27/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 94e383cd7cbb..a49386adb4d0 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -37,7 +37,15 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: return the_sum ** (1 / 2) +def benchmark() -> None: + """ + Benchmark + """ + from timeit import timeit + print(timeit("euclidean_distance_no_np([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4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 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", setup="from __main__ import euclidean_distance", number = 10000)) + + + if __name__ == "__main__": - point = np.array([2, 2]) - point_2 = np.array([0, 0]) - print(euclidean_distance(point, point_2)) + benchmark() From 0531471e268c5aa123ff63baad7ed09374caedef Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:36:57 -0400 Subject: [PATCH 28/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index a49386adb4d0..b182a3bedbd4 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -42,9 +42,21 @@ def benchmark() -> None: Benchmark """ from timeit import timeit - print(timeit("euclidean_distance_no_np([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], [5, 6, 7, 8, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", setup="from __main__ import euclidean_distance_no_np", number = 10000)) - print(timeit("euclidean_distance([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], [5, 6, 7, 8, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", setup="from __main__ import euclidean_distance", number = 10000)) + print( + timeit( + "euclidean_distance_no_np([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 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3, 4,1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", + setup="from __main__ import euclidean_distance", + number=10000, + ) + ) if __name__ == "__main__": From 764f07cfc7a9aa60c5a4708c375d4385ab7682f0 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:40:06 -0400 Subject: [PATCH 29/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index b182a3bedbd4..43077445cc92 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -45,14 +45,38 @@ def benchmark() -> None: print( timeit( - "euclidean_distance_no_np([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4], [5, 6, 7, 8, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4,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 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, + 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", setup="from __main__ import euclidean_distance", number=10000, ) From 3d0cd8e4862d17379c2b770217b8c1d37cf54bcb Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:45:14 -0400 Subject: [PATCH 30/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 29 +++-------------------------- 1 file changed, 3 insertions(+), 26 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 43077445cc92..6703a08dbc95 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -40,43 +40,20 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: def benchmark() -> None: """ Benchmark + The numpy takes longer than the non numpy for smaller vectors, but as the dimension increases, the numpy is much faster """ from timeit import timeit print( timeit( - "euclidean_distance_no_np([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, - 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, - 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, - 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, - 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, - 2, 3, 4], [5, 6, 7, 8, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", + "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])", setup="from __main__ import euclidean_distance_no_np", number=10000, ) ) print( timeit( - "euclidean_distance([1, 2, 3, 4, 1, 2, 3, 4, 1, 2, - 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, - 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, - 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, - 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, - 2, 3, 4], [5, 6, 7, 8, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, - 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4])", + "euclidean_distance([1, 2, 3], [4, 5, 6])", setup="from __main__ import euclidean_distance", number=10000, ) From 78d46445680b31e08a596c0e07cf0875cb4e9059 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Sun, 25 Oct 2020 16:47:59 -0400 Subject: [PATCH 31/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 6703a08dbc95..7d65b2c9bc66 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -40,7 +40,8 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: def benchmark() -> None: """ Benchmark - The numpy takes longer than the non numpy for smaller vectors, but as the dimension increases, the numpy is much faster + The numpy takes longer than the non numpy for smaller vectors, but as + the dimension increases, the numpy is much faster """ from timeit import timeit From aad0d3a75e0579f29174e0b5d72193a5dc4f3ea7 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 26 Nov 2020 23:35:32 -0500 Subject: [PATCH 32/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 7d65b2c9bc66..00f3dd41987a 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -26,22 +26,17 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: """ Calculate the distance between the two endpoints of two vectors without numpy. A vector is defined as a list, tuple, or numpy 1D array. - >>> euclidean_distance((0, 0), (2, 2)) + >>> euclidean_distance_no_np((0, 0), (2, 2)) 2.8284271247461903 - >>> euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]) + >>> euclidean_distance_no_np([1, 2, 3, 4], [5, 6, 7, 8]) 8.0 """ - the_sum = 0 - for i in range(len(vector_1)): - the_sum += (vector_2[i] - vector_1[i]) ** 2 - return the_sum ** (1 / 2) + return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) def benchmark() -> None: """ - Benchmark - The numpy takes longer than the non numpy for smaller vectors, but as - the dimension increases, the numpy is much faster + Benchmarks """ from timeit import timeit From 4a082548527fe484384f5006281ed4040c0da068 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 26 Nov 2020 23:39:07 -0500 Subject: [PATCH 33/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 00f3dd41987a..d615a429d2fa 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -31,7 +31,7 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: >>> euclidean_distance_no_np([1, 2, 3, 4], [5, 6, 7, 8]) 8.0 """ - return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) + return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) def benchmark() -> None: From 017eb7311db6da919b358dca907d528a7ec49ce8 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 26 Nov 2020 23:45:41 -0500 Subject: [PATCH 34/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index d615a429d2fa..73220ad213fd 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -31,7 +31,7 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: >>> euclidean_distance_no_np([1, 2, 3, 4], [5, 6, 7, 8]) 8.0 """ - return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) + return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) ** (1 / 2) def benchmark() -> None: From f16b2bbf42b27f45ec5eb4387199184d413681ec Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 26 Nov 2020 23:50:01 -0500 Subject: [PATCH 35/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index 73220ad213fd..b36efaf85d58 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -34,27 +34,28 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) ** (1 / 2) -def benchmark() -> None: +if __name__ == "__main__": + def benchmark() -> None: """ Benchmarks """ from timeit import timeit - + print("Without Numpy") print( timeit( "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])", - setup="from __main__ import euclidean_distance_no_np", number=10000, + globals=globals() ) ) + print("With Numpy") print( timeit( "euclidean_distance([1, 2, 3], [4, 5, 6])", - setup="from __main__ import euclidean_distance", number=10000, + globals=globals() ) ) -if __name__ == "__main__": - benchmark() + benchmark() From 8e83ae685cdfe17027a3312aa088f03126a117fa Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Thu, 26 Nov 2020 23:55:14 -0500 Subject: [PATCH 36/37] Update euclidean_distance.py --- maths/euclidean_distance.py | 43 +++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index b36efaf85d58..f4cac89005ff 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -35,27 +35,28 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: if __name__ == "__main__": - def benchmark() -> None: - """ - Benchmarks - """ - from timeit import timeit - print("Without Numpy") - print( - timeit( - "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])", - number=10000, - globals=globals() + + def benchmark() -> None: + """ + Benchmarks + """ + from timeit import timeit + + print("Without Numpy") + print( + timeit( + "euclidean_distance_no_np([1, 2, 3], [4, 5, 6])", + number=10000, + globals=globals(), + ) ) - ) - print("With Numpy") - print( - timeit( - "euclidean_distance([1, 2, 3], [4, 5, 6])", - number=10000, - globals=globals() + print("With Numpy") + print( + timeit( + "euclidean_distance([1, 2, 3], [4, 5, 6])", + number=10000, + globals=globals(), + ) ) - ) - - benchmark() + benchmark() From ccdd50e7700a67ea1835d0fd76c1c14733c66304 Mon Sep 17 00:00:00 2001 From: Andrew <54547089+ac-tam@users.noreply.github.com> Date: Fri, 27 Nov 2020 09:29:11 -0500 Subject: [PATCH 37/37] Update maths/euclidean_distance.py Co-authored-by: Christian Clauss --- maths/euclidean_distance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euclidean_distance.py b/maths/euclidean_distance.py index f4cac89005ff..6e0da6370219 100644 --- a/maths/euclidean_distance.py +++ b/maths/euclidean_distance.py @@ -31,7 +31,7 @@ def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut: >>> euclidean_distance_no_np([1, 2, 3, 4], [5, 6, 7, 8]) 8.0 """ - return sum([(y - x) ** 2 for (y, x) in list(zip(vector_1, vector_2))]) ** (1 / 2) + return sum((v1 - v2) ** 2 for v1, v2 in zip(vector_1, vector_2)) ** (1 / 2) if __name__ == "__main__":