From efca208739aaf32240265c155a7acf5594e4f8c3 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Mon, 11 Oct 2021 08:24:24 +0530 Subject: [PATCH 01/16] Magnitude and Angle Core function to find Magnitude and Angle of two Given Vector --- linear_algebra/src/lib.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 74aeb9137666..1c09dc275ddc 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -147,7 +147,27 @@ def __mul__(self, other: float | Vector) -> float | Vector: return summe else: # error case raise Exception("invalid operand!") - + + def magnitude(self) -> float: + ''' + Magnitude of a Vector + ''' + return sum([i**2 for i in self.__components])**(1/2) + + def angle(self, other: Vector, deg: bool) -> float: + """ + find angle between two Vector (self, Vector) + """ + try: + num = self * other + den = (self.magnitude())*(n.magnitude()) + if deg: + return math.degrees(math.acos(num/den)) + else: + return math.acos(num/den) + except Exception as e: + raise e + def copy(self) -> Vector: """ copies this vector and returns it. From 2fc782eca355f438c8b26bc567b8702ea699fd03 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Mon, 11 Oct 2021 16:26:12 +0530 Subject: [PATCH 02/16] Magnitude and Angle with Doctest added Doctest to the functions --- linear_algebra/src/lib.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 1c09dc275ddc..3b54a48fa13e 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -151,22 +151,40 @@ def __mul__(self, other: float | Vector) -> float | Vector: def magnitude(self) -> float: ''' Magnitude of a Vector + + >>> x = Vector([2, 3, 4]) + >>> x.magnitude() + 5.385164807134504 + ''' return sum([i**2 for i in self.__components])**(1/2) - def angle(self, other: Vector, deg: bool) -> float: + def angle(self, other: Vector, deg: bool = False) -> float: """ find angle between two Vector (self, Vector) + + >>> y = Vector([3, 4, -1]) + >>> z = Vector([2, -1, 1]) + >>> y.angle(z) + 1.4906464636572374 + >>> y.angle(z, deg = True) + 85.40775111366095 + >>> z = Vector([2, -1]) + >>> y.angle(z) + Traceback (most recent call last): + ... + Exception: invalid operand! """ try: num = self * other - den = (self.magnitude())*(n.magnitude()) + den = (self.magnitude())*(other.magnitude()) if deg: return math.degrees(math.acos(num/den)) else: return math.acos(num/den) except Exception as e: raise e + def copy(self) -> Vector: """ From 0350a9d86390bac301cc0b499b1ccd224e430d22 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Mon, 11 Oct 2021 17:59:32 +0530 Subject: [PATCH 03/16] Update linear_algebra/src/lib.py Co-authored-by: Christian Clauss --- linear_algebra/src/lib.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 3b54a48fa13e..6afc2d895c6d 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -163,9 +163,7 @@ def angle(self, other: Vector, deg: bool = False) -> float: """ find angle between two Vector (self, Vector) - >>> y = Vector([3, 4, -1]) - >>> z = Vector([2, -1, 1]) - >>> y.angle(z) + >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1])) 1.4906464636572374 >>> y.angle(z, deg = True) 85.40775111366095 From cde040f29ba3dd22cb1f4b57fc2c84278543cafa Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Mon, 11 Oct 2021 17:59:58 +0530 Subject: [PATCH 04/16] Update linear_algebra/src/lib.py Co-authored-by: Christian Clauss --- 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 6afc2d895c6d..f4807399948d 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -175,7 +175,7 @@ def angle(self, other: Vector, deg: bool = False) -> float: """ try: num = self * other - den = (self.magnitude())*(other.magnitude()) + den = self.magnitude() * other.magnitude() if deg: return math.degrees(math.acos(num/den)) else: From 02fc704c38bd83e8b45eead0b4b1eafc149052c3 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Tue, 12 Oct 2021 14:00:14 +0530 Subject: [PATCH 05/16] Changes done and Magnitude and Angle Issues --- linear_algebra/src/lib.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index f4807399948d..29209c6ad233 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -152,8 +152,7 @@ def magnitude(self) -> float: ''' Magnitude of a Vector - >>> x = Vector([2, 3, 4]) - >>> x.magnitude() + >>> Vector([2, 3, 4]).magnitude() 5.385164807134504 ''' @@ -165,12 +164,11 @@ def angle(self, other: Vector, deg: bool = False) -> float: >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1])) 1.4906464636572374 - >>> y.angle(z, deg = True) + >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]), deg = True) 85.40775111366095 - >>> z = Vector([2, -1]) - >>> y.angle(z) + >>> Vector([3, 4, -1]).angle(Vector([2, -1])) Traceback (most recent call last): - ... + ... Exception: invalid operand! """ try: @@ -180,8 +178,8 @@ def angle(self, other: Vector, deg: bool = False) -> float: return math.degrees(math.acos(num/den)) else: return math.acos(num/den) - except Exception as e: - raise e + except: + raise def copy(self) -> Vector: From ef01d092d8e4db3d0bc574688c398c91dff863bb Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 12 Oct 2021 11:46:48 +0200 Subject: [PATCH 06/16] black --- linear_algebra/src/lib.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 29209c6ad233..6a18df5e15c3 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -147,16 +147,16 @@ def __mul__(self, other: float | Vector) -> float | Vector: return summe else: # error case raise Exception("invalid operand!") - + def magnitude(self) -> float: - ''' + """ Magnitude of a Vector >>> Vector([2, 3, 4]).magnitude() 5.385164807134504 - ''' - return sum([i**2 for i in self.__components])**(1/2) + """ + return sum([i ** 2 for i in self.__components]) ** (1 / 2) def angle(self, other: Vector, deg: bool = False) -> float: """ @@ -164,24 +164,20 @@ def angle(self, other: Vector, deg: bool = False) -> float: >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1])) 1.4906464636572374 - >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]), deg = True) + >>> Vector([3, 4, -1]).angle(Vector([2, -1, 1]), deg = True) 85.40775111366095 - >>> Vector([3, 4, -1]).angle(Vector([2, -1])) + >>> Vector([3, 4, -1]).angle(Vector([2, -1])) Traceback (most recent call last): ... Exception: invalid operand! """ - try: - num = self * other - den = self.magnitude() * other.magnitude() - if deg: - return math.degrees(math.acos(num/den)) - else: - return math.acos(num/den) - except: - raise + num = self * other + den = self.magnitude() * other.magnitude() + if deg: + return math.degrees(math.acos(num / den)) + else: + return math.acos(num / den) - def copy(self) -> Vector: """ copies this vector and returns it. From cb32d39eb354863868ba0c7f71fa040ec5d954e8 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Tue, 12 Oct 2021 18:14:50 +0530 Subject: [PATCH 07/16] Modified Euler's Method Adding Modified Euler's method, which was the further change to a Euler method and known for better accuracy to the given value --- maths/euler_modified.py | 49 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 maths/euler_modified.py diff --git a/maths/euler_modified.py b/maths/euler_modified.py new file mode 100644 index 000000000000..1c7b70db3d54 --- /dev/null +++ b/maths/euler_modified.py @@ -0,0 +1,49 @@ +import numpy as np +import math + + +def euler_modified(ode_func, y0, x0, step_size, x_end): + """ + Calculate numeric solution at each step to an ODE using Euler's Modified Method + The Euler forward scheme may be straightforward to implement, but it can't give accurate solutions. + So, they Proposed some changes to the base function to improve the accuracy + + https://en.wikipedia.org/wiki/Euler_method + + Arguments: + ode_func -- The ode as a function of x and y + y0 -- the initial value for y + x0 -- the initial value for x + stepsize -- the increment value for x + x_end -- the end value for x + + >>> # the exact solution is math.exp(x) + >>> def f1(x, y): + ... return -2*x*(y**2) + >>> y = euler_modified(f1, 1, 0, 0.2, 1.0) + >>> y[-1] + 0.503338255442106 + >>> def f2(x, y): + ... return -2*y + (x**3)*math.exp(-2*x) + >>> y = euler_modified(f2, 1, 0, 0.1, 0.3) + >>> y[-1] + 0.5525976431951775 + """ + N = int(np.ceil((x_end - x0) / step_size)) + y = np.zeros((N + 1,)) + y[0] = y0 + x = x0 + + for k in range(N): + y_get = y[k] + step_size * ode_func(x, y[k]) + y[k + 1] = y[k] + \ + ((step_size/2)*(ode_func(x, y[k])+ode_func(x+step_size, y_get))) + x += step_size + + return y + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From b581c18f5191cdc62cbaa180a20b1b9667423fdd Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Tue, 12 Oct 2021 18:27:03 +0530 Subject: [PATCH 08/16] Modified Euler's Method (changed the typing of function) Modified function is used for better accuracy --- maths/euler_modified.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 1c7b70db3d54..1be27e131ed2 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -1,8 +1,9 @@ import numpy as np import math +from typing import Callable -def euler_modified(ode_func, y0, x0, step_size, x_end): +def euler_modified(ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float) -> np.array: """ Calculate numeric solution at each step to an ODE using Euler's Modified Method The Euler forward scheme may be straightforward to implement, but it can't give accurate solutions. @@ -20,12 +21,12 @@ def euler_modified(ode_func, y0, x0, step_size, x_end): >>> # the exact solution is math.exp(x) >>> def f1(x, y): ... return -2*x*(y**2) - >>> y = euler_modified(f1, 1, 0, 0.2, 1.0) + >>> y = euler_modified(f1, 1.0, 0.0, 0.2, 1.0) >>> y[-1] 0.503338255442106 >>> def f2(x, y): ... return -2*y + (x**3)*math.exp(-2*x) - >>> y = euler_modified(f2, 1, 0, 0.1, 0.3) + >>> y = euler_modified(f2, 1.0, 0.0, 0.1, 0.3) >>> y[-1] 0.5525976431951775 """ From 83c0aa75bd7c8992b985b5c4d0f2fc7c5805ce45 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Tue, 12 Oct 2021 18:29:22 +0530 Subject: [PATCH 09/16] Link added Added link to an explanation as per Contributions Guidelines --- maths/euler_modified.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 1be27e131ed2..37e862a1f94e 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -9,7 +9,7 @@ def euler_modified(ode_func: Callable, y0: float, x0: float, step_size: float, x The Euler forward scheme may be straightforward to implement, but it can't give accurate solutions. So, they Proposed some changes to the base function to improve the accuracy - https://en.wikipedia.org/wiki/Euler_method + https://math.iitm.ac.in/public_html/sryedida/caimna/ode/euler/ie.html Arguments: ode_func -- The ode as a function of x and y From dfb6b7094be3f0b57b71a9af148cd574fdfaea60 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 15:41:56 +0530 Subject: [PATCH 10/16] Resolving Pre-Commit error --- maths/euler_modified.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 37e862a1f94e..f422926991fd 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -3,13 +3,15 @@ from typing import Callable -def euler_modified(ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float) -> np.array: +def euler_modified( + ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float +) -> np.array: """ Calculate numeric solution at each step to an ODE using Euler's Modified Method The Euler forward scheme may be straightforward to implement, but it can't give accurate solutions. So, they Proposed some changes to the base function to improve the accuracy - https://math.iitm.ac.in/public_html/sryedida/caimna/ode/euler/ie.html + https://en.wikipedia.org/wiki/Euler_method Arguments: ode_func -- The ode as a function of x and y @@ -37,8 +39,9 @@ def euler_modified(ode_func: Callable, y0: float, x0: float, step_size: float, x for k in range(N): y_get = y[k] + step_size * ode_func(x, y[k]) - y[k + 1] = y[k] + \ - ((step_size/2)*(ode_func(x, y[k])+ode_func(x+step_size, y_get))) + y[k + 1] = y[k] + ( + (step_size/2)*(ode_func(x, y[k])+ode_func(x+step_size, y_get)) + ) x += step_size return y From b7e52cafa4b89e486a5cc28443ad1f505b6e535f Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 15:46:17 +0530 Subject: [PATCH 11/16] Pre-Commit Error Resolved --- maths/euler_modified.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index f422926991fd..714f4f54b1f4 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -40,7 +40,7 @@ def euler_modified( for k in range(N): y_get = y[k] + step_size * ode_func(x, y[k]) y[k + 1] = y[k] + ( - (step_size/2)*(ode_func(x, y[k])+ode_func(x+step_size, y_get)) + (step_size / 2) * (ode_func(x, y[k]) + ode_func(x + step_size, y_get)) ) x += step_size From cacf44857c898457e763150e3c35d4a88be0f1d2 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 15:55:11 +0530 Subject: [PATCH 12/16] Pre-Commit Error import statement Change --- maths/euler_modified.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 714f4f54b1f4..3890f4188c75 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -1,7 +1,8 @@ -import numpy as np import math from typing import Callable +import numpy as np + def euler_modified( ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float From 32a1df2c2a81db897f7f0abd75e88e4516062ab4 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 15:58:10 +0530 Subject: [PATCH 13/16] Removed Import Math --- maths/euler_modified.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 3890f4188c75..1afecdb6da7f 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -1,4 +1,3 @@ -import math from typing import Callable import numpy as np From 34f903d0c9b97d03c08c5a8eef6e1d26a16ae25f Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 16:06:34 +0530 Subject: [PATCH 14/16] import math built issue --- maths/euler_modified.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 1afecdb6da7f..687fe1ddf76b 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -7,9 +7,10 @@ def euler_modified( ode_func: Callable, y0: float, x0: float, step_size: float, x_end: float ) -> np.array: """ - Calculate numeric solution at each step to an ODE using Euler's Modified Method - The Euler forward scheme may be straightforward to implement, but it can't give accurate solutions. - So, they Proposed some changes to the base function to improve the accuracy + Calculate solution at each step to an ODE using Euler's Modified Method + The Euler forward scheme may be straightforward to implement, + but it can't give accurate solutions. + So, they Proposed some changes to improve the accuracy https://en.wikipedia.org/wiki/Euler_method @@ -26,6 +27,7 @@ def euler_modified( >>> y = euler_modified(f1, 1.0, 0.0, 0.2, 1.0) >>> y[-1] 0.503338255442106 + >>> import math >>> def f2(x, y): ... return -2*y + (x**3)*math.exp(-2*x) >>> y = euler_modified(f2, 1.0, 0.0, 0.1, 0.3) From b1376bbb2ee942933aa05585e62465d779397d48 Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 16:09:08 +0530 Subject: [PATCH 15/16] adding space pre-commit error --- maths/euler_modified.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index 687fe1ddf76b..ec6c5540c129 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -8,6 +8,7 @@ def euler_modified( ) -> np.array: """ Calculate solution at each step to an ODE using Euler's Modified Method + The Euler forward scheme may be straightforward to implement, but it can't give accurate solutions. So, they Proposed some changes to improve the accuracy From cb7fd81361d5928e26547880cccfadfab85b710e Mon Sep 17 00:00:00 2001 From: Aman kanojiya <50018596+AMANKANOJIYA@users.noreply.github.com> Date: Wed, 13 Oct 2021 16:15:17 +0530 Subject: [PATCH 16/16] statement sorter for doc --- maths/euler_modified.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/euler_modified.py b/maths/euler_modified.py index ec6c5540c129..bf0c07c17f48 100644 --- a/maths/euler_modified.py +++ b/maths/euler_modified.py @@ -8,9 +8,7 @@ def euler_modified( ) -> np.array: """ Calculate solution at each step to an ODE using Euler's Modified Method - - The Euler forward scheme may be straightforward to implement, - but it can't give accurate solutions. + The Euler is straightforward to implement, but can't give accurate solutions. So, they Proposed some changes to improve the accuracy https://en.wikipedia.org/wiki/Euler_method