From abcbec3eab8071895c08e5884b2462480533b7ca Mon Sep 17 00:00:00 2001 From: kumarcr711-cloud Date: Tue, 28 Apr 2026 20:29:40 +0200 Subject: [PATCH 1/4] 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 2/4] [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 3/4] 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 4/4] 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() -