From abcbec3eab8071895c08e5884b2462480533b7ca Mon Sep 17 00:00:00 2001 From: kumarcr711-cloud Date: Tue, 28 Apr 2026 20:29:40 +0200 Subject: [PATCH 01/11] feat: add numerical laplace transform --- maths/laplace_transformation.py | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 maths/laplace_transformation.py diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py new file mode 100644 index 000000000000..669a33a7fb16 --- /dev/null +++ b/maths/laplace_transformation.py @@ -0,0 +1,63 @@ +""" +The Laplace Transform is defined as: L{f(t)} = integral from 0 to infinity of e^(-st) * f(t) dt. + +Wiki: https://en.wikipedia.org/wiki/Laplace_transform + +""" + +import numpy as np + + +def laplace_transform( + function_values: np.ndarray, s_value: float, delta_t: float +) -> float: + """ + Calculate the numerical Laplace Transform of a function given its values over time. + + Args: + function_values: A numpy array of the function values f(t). + s_value: The complex frequency parameter 's' (modeled here as a float). + delta_t: The time step between samples. + + Returns: + The approximate value of the Laplace transform at s_value. + + Example: For f(t) = 1, the Laplace transform L{1} = 1/s. + If s = 2, L{1} should be 0.5. + + >>> t = np.linspace(0, 50, 10000) + >>> f_t = np.ones_like(t) # f(t) = 1 + >>> res = laplace_transform(f_t, s_value=2.0, delta_t=50/10000) + >>> abs(res - 0.5) < 1e-3 + True + + Example: For f(t) = e^(-t), the Laplace transform L{e^-t} = 1/(s+1). + If s = 1, L{e^-t} should be 0.5. + + >>> t = np.linspace(0, 50, 10000) + >>> f_t = np.exp(-t) + >>> res = laplace_transform(f_t, s_value=1.0, delta_t=50/10000) + >>> abs(res - 0.5) < 1e-3 + True + """ + if s_value < 0: + raise ValueError("s_value must be non-negative for convergence.") + + # Time vector corresponding to the function values + time_vector = np.arange(len(function_values)) * delta_t + + # The integrand: f(t) * e^(-s*t) + integrand = function_values * np.exp(-s_value * time_vector) + + # Numerical integration using the trapezoidal rule + result = np.trapezoid(integrand, dx=delta_t) + + return float(result) + + +if __name__ == "__main__": + import doctest + + + doctest.testmod() + From fca04e3162a73a2cdaf7e7fc7c5d485563c955ac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 28 Apr 2026 18:35:08 +0000 Subject: [PATCH 02/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/laplace_transformation.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index 669a33a7fb16..086ed5c5d682 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -24,13 +24,13 @@ def laplace_transform( Example: For f(t) = 1, the Laplace transform L{1} = 1/s. If s = 2, L{1} should be 0.5. - + >>> t = np.linspace(0, 50, 10000) >>> f_t = np.ones_like(t) # f(t) = 1 >>> res = laplace_transform(f_t, s_value=2.0, delta_t=50/10000) >>> abs(res - 0.5) < 1e-3 True - + Example: For f(t) = e^(-t), the Laplace transform L{e^-t} = 1/(s+1). If s = 1, L{e^-t} should be 0.5. @@ -45,19 +45,17 @@ def laplace_transform( # Time vector corresponding to the function values time_vector = np.arange(len(function_values)) * delta_t - + # The integrand: f(t) * e^(-s*t) integrand = function_values * np.exp(-s_value * time_vector) - + # Numerical integration using the trapezoidal rule result = np.trapezoid(integrand, dx=delta_t) - + return float(result) if __name__ == "__main__": import doctest - doctest.testmod() - From 92e3835726723f7ef770be0257072344c60ba7c7 Mon Sep 17 00:00:00 2001 From: Tushar Tyagi Date: Tue, 28 Apr 2026 20:41:43 +0200 Subject: [PATCH 03/11] Update maths/laplace_transformation.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- maths/laplace_transformation.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index 086ed5c5d682..46a7736ea1bc 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -14,13 +14,16 @@ def laplace_transform( """ Calculate the numerical Laplace Transform of a function given its values over time. + This implementation supports only real-valued, non-negative Laplace + parameters ``s``. + Args: function_values: A numpy array of the function values f(t). - s_value: The complex frequency parameter 's' (modeled here as a float). + s_value: The real-valued Laplace parameter ``s``. Must be non-negative. delta_t: The time step between samples. Returns: - The approximate value of the Laplace transform at s_value. + The approximate real-valued value of the Laplace transform at s_value. Example: For f(t) = 1, the Laplace transform L{1} = 1/s. If s = 2, L{1} should be 0.5. From f65afeb6557a473c904239e1a2b42ac86ee9f440 Mon Sep 17 00:00:00 2001 From: kumarcr711-cloud Date: Tue, 28 Apr 2026 20:47:39 +0200 Subject: [PATCH 04/11] refactor: add input validation and fix doctest precision --- maths/laplace_transformation.py | 42 +++++++++++++-------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index 669a33a7fb16..f335d2979b67 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -1,7 +1,7 @@ """ -The Laplace Transform is defined as: L{f(t)} = integral from 0 to infinity of e^(-st) * f(t) dt. +This module provides a numerical implementation of the Laplace Transform. -Wiki: https://en.wikipedia.org/wiki/Laplace_transform +https://en.wikipedia.org/wiki/Laplace_transform """ @@ -12,52 +12,44 @@ def laplace_transform( function_values: np.ndarray, s_value: float, delta_t: float ) -> float: """ - Calculate the numerical Laplace Transform of a function given its values over time. + Calculate the numerical Laplace Transform of a function given its values. Args: function_values: A numpy array of the function values f(t). - s_value: The complex frequency parameter 's' (modeled here as a float). - delta_t: The time step between samples. + s_value: The real-valued Laplace parameter 's'. + delta_t: The positive time step between samples. Returns: - The approximate value of the Laplace transform at s_value. + The approximate real-valued Laplace transform at s_value. - Example: For f(t) = 1, the Laplace transform L{1} = 1/s. - If s = 2, L{1} should be 0.5. - - >>> t = np.linspace(0, 50, 10000) - >>> f_t = np.ones_like(t) # f(t) = 1 + >>> t = np.linspace(0, 50, 10000, endpoint=False) + >>> f_t = np.ones_like(t) >>> res = laplace_transform(f_t, s_value=2.0, delta_t=50/10000) >>> abs(res - 0.5) < 1e-3 True - - Example: For f(t) = e^(-t), the Laplace transform L{e^-t} = 1/(s+1). - If s = 1, L{e^-t} should be 0.5. - >>> t = np.linspace(0, 50, 10000) + >>> t = np.linspace(0, 50, 10000, endpoint=False) >>> f_t = np.exp(-t) >>> res = laplace_transform(f_t, s_value=1.0, delta_t=50/10000) >>> abs(res - 0.5) < 1e-3 True """ - if s_value < 0: - raise ValueError("s_value must be non-negative for convergence.") + if delta_t <= 0: + raise ValueError("delta_t must be a positive value.") + if function_values.size == 0: + raise ValueError("function_values array cannot be empty.") # Time vector corresponding to the function values time_vector = np.arange(len(function_values)) * delta_t - + # The integrand: f(t) * e^(-s*t) integrand = function_values * np.exp(-s_value * time_vector) - - # Numerical integration using the trapezoidal rule - result = np.trapezoid(integrand, dx=delta_t) - - return float(result) + + # Numerical integration using the trapezoid rule + return float(np.trapezoid(integrand, dx=delta_t)) if __name__ == "__main__": import doctest - doctest.testmod() - From cb4f81f99f515dfbe74d6aa03c4766346eec05bd Mon Sep 17 00:00:00 2001 From: Tushar Tyagi Date: Mon, 4 May 2026 19:51:49 +0200 Subject: [PATCH 05/11] fix: address PR review comments Updated module docstring, added validation for non-negative s_value, and replaced arrange with linspace for clarity. --- maths/laplace_transformation.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index 089d681cee4f..537481b9bac0 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -1,7 +1,13 @@ """ -This module provides a numerical implementation of the Laplace Transform. -https://en.wikipedia.org/wiki/Laplace_transform +Laplace Transform — Numerical Implementation. + +Computes the numerical Laplace Transform using the trapezoidal +integration rule. Supports real-valued, non-negative Laplace +parameters only. + +Reference: https://en.wikipedia.org/wiki/Laplace_transform + """ @@ -47,9 +53,11 @@ def laplace_transform( raise ValueError("delta_t must be a positive value.") if function_values.size == 0: raise ValueError("function_values array cannot be empty.") + if s_value < 0: + raise ValueError(f"s_value must be non-negative for this implementation, got{s_value}.") # Time vector corresponding to the function values - time_vector = np.arange(len(function_values)) * delta_t + time_vector = np.linspace(0, (len(function_values) - 1) * delta_t, len(function_values)) # The integrand: f(t) * e^(-s*t) integrand = function_values * np.exp(-s_value * time_vector) From af4446f9b48bd28448792a7bd3851e5fdcc7942c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 17:52:03 +0000 Subject: [PATCH 06/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/laplace_transformation.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index 537481b9bac0..a5af46699108 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -54,10 +54,14 @@ def laplace_transform( if function_values.size == 0: raise ValueError("function_values array cannot be empty.") if s_value < 0: - raise ValueError(f"s_value must be non-negative for this implementation, got{s_value}.") + raise ValueError( + f"s_value must be non-negative for this implementation, got{s_value}." + ) # Time vector corresponding to the function values - time_vector = np.linspace(0, (len(function_values) - 1) * delta_t, len(function_values)) + time_vector = np.linspace( + 0, (len(function_values) - 1) * delta_t, len(function_values) + ) # The integrand: f(t) * e^(-s*t) integrand = function_values * np.exp(-s_value * time_vector) From 70ea742cd6bde907816754c944fbab8626216d5e Mon Sep 17 00:00:00 2001 From: Tushar Tyagi Date: Mon, 4 May 2026 20:02:37 +0200 Subject: [PATCH 07/11] Refactor docstring and remove unnecessary blank lines Removed extra blank lines and cleaned up docstring. --- maths/laplace_transformation.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index a5af46699108..ac35145e123b 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -1,5 +1,4 @@ """ - Laplace Transform — Numerical Implementation. Computes the numerical Laplace Transform using the trapezoidal @@ -7,8 +6,6 @@ parameters only. Reference: https://en.wikipedia.org/wiki/Laplace_transform - - """ import numpy as np @@ -76,3 +73,4 @@ def laplace_transform( import doctest doctest.testmod() + From 06166fb28c69e6332ceeb3ee7b1db743f0780db7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 18:02:51 +0000 Subject: [PATCH 08/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/laplace_transformation.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index ac35145e123b..5b2c991b2653 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -73,4 +73,3 @@ def laplace_transform( import doctest doctest.testmod() - From 5d24b1b92446eac1b0430273759215235cd695a8 Mon Sep 17 00:00:00 2001 From: Tushar Tyagi Date: Mon, 4 May 2026 20:12:53 +0200 Subject: [PATCH 09/11] Refactor error handling for s_value check --- maths/laplace_transformation.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index 5b2c991b2653..bfa7fd58e947 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -50,10 +50,9 @@ def laplace_transform( raise ValueError("delta_t must be a positive value.") if function_values.size == 0: raise ValueError("function_values array cannot be empty.") - if s_value < 0: - raise ValueError( - f"s_value must be non-negative for this implementation, got{s_value}." - ) + if s_value < 0: + error_msg = f"s_value must be non-negative for this implementation, got {s_value}." + raise ValueError(error_msg) # Time vector corresponding to the function values time_vector = np.linspace( From c88d4cd262d0f2554eda9df24cf42adee1b9d89d Mon Sep 17 00:00:00 2001 From: Tushar Tyagi Date: Mon, 4 May 2026 20:14:53 +0200 Subject: [PATCH 10/11] Fix indentation for s_value validation --- maths/laplace_transformation.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index bfa7fd58e947..dcf8e266e914 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -50,9 +50,9 @@ def laplace_transform( raise ValueError("delta_t must be a positive value.") if function_values.size == 0: raise ValueError("function_values array cannot be empty.") - if s_value < 0: - error_msg = f"s_value must be non-negative for this implementation, got {s_value}." - raise ValueError(error_msg) + if s_value < 0: + error_msg = f"s_value must be non-negative for this implementation, got {s_value}." + raise ValueError(error_msg) # Time vector corresponding to the function values time_vector = np.linspace( From 664791a911fd7a222bb041761ef54d1ddc0d1357 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 4 May 2026 18:15:05 +0000 Subject: [PATCH 11/11] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/laplace_transformation.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/maths/laplace_transformation.py b/maths/laplace_transformation.py index dcf8e266e914..36cc73e00e23 100644 --- a/maths/laplace_transformation.py +++ b/maths/laplace_transformation.py @@ -51,7 +51,9 @@ def laplace_transform( if function_values.size == 0: raise ValueError("function_values array cannot be empty.") if s_value < 0: - error_msg = f"s_value must be non-negative for this implementation, got {s_value}." + error_msg = ( + f"s_value must be non-negative for this implementation, got {s_value}." + ) raise ValueError(error_msg) # Time vector corresponding to the function values