Skip to content

Commit 62d049a

Browse files
Tushar-R-Tyagipre-commit-ci[bot]Copilot
authored
feat: add numerical laplace transform (#14602)
* feat: add numerical laplace transform * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update maths/laplace_transformation.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * refactor: add input validation and fix doctest precision * fix: address PR review comments Updated module docstring, added validation for non-negative s_value, and replaced arrange with linspace for clarity. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor docstring and remove unnecessary blank lines Removed extra blank lines and cleaned up docstring. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Refactor error handling for s_value check * Fix indentation for s_value validation * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 3374ede commit 62d049a

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

maths/laplace_transformation.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
Laplace Transform — Numerical Implementation.
3+
4+
Computes the numerical Laplace Transform using the trapezoidal
5+
integration rule. Supports real-valued, non-negative Laplace
6+
parameters only.
7+
8+
Reference: https://en.wikipedia.org/wiki/Laplace_transform
9+
"""
10+
11+
import numpy as np
12+
13+
14+
def laplace_transform(
15+
function_values: np.ndarray, s_value: float, delta_t: float
16+
) -> float:
17+
"""
18+
Calculate the numerical Laplace Transform of a function given its values over time.
19+
20+
This implementation supports only real-valued, non-negative Laplace
21+
parameters ``s``.
22+
23+
Args:
24+
function_values: A numpy array of the function values f(t).
25+
s_value: The real-valued Laplace parameter ``s``. Must be non-negative.
26+
delta_t: The time step between samples.
27+
28+
Returns:
29+
The approximate real-valued value of the Laplace transform at s_value.
30+
31+
Example: For f(t) = 1, the Laplace transform L{1} = 1/s.
32+
If s = 2, L{1} should be 0.5.
33+
34+
>>> t = np.linspace(0, 50, 10000)
35+
>>> f_t = np.ones_like(t) # f(t) = 1
36+
>>> res = laplace_transform(f_t, s_value=2.0, delta_t=50/10000)
37+
>>> abs(res - 0.5) < 1e-3
38+
True
39+
40+
Example: For f(t) = e^(-t), the Laplace transform L{e^-t} = 1/(s+1).
41+
If s = 1, L{e^-t} should be 0.5.
42+
43+
>>> t = np.linspace(0, 50, 10000)
44+
>>> f_t = np.exp(-t)
45+
>>> res = laplace_transform(f_t, s_value=1.0, delta_t=50/10000)
46+
>>> abs(res - 0.5) < 1e-3
47+
True
48+
"""
49+
if delta_t <= 0:
50+
raise ValueError("delta_t must be a positive value.")
51+
if function_values.size == 0:
52+
raise ValueError("function_values array cannot be empty.")
53+
if s_value < 0:
54+
error_msg = (
55+
f"s_value must be non-negative for this implementation, got {s_value}."
56+
)
57+
raise ValueError(error_msg)
58+
59+
# Time vector corresponding to the function values
60+
time_vector = np.linspace(
61+
0, (len(function_values) - 1) * delta_t, len(function_values)
62+
)
63+
64+
# The integrand: f(t) * e^(-s*t)
65+
integrand = function_values * np.exp(-s_value * time_vector)
66+
67+
# Numerical integration using the trapezoidal rule
68+
result = np.trapezoid(integrand, dx=delta_t)
69+
70+
return float(result)
71+
72+
73+
if __name__ == "__main__":
74+
import doctest
75+
76+
doctest.testmod()

0 commit comments

Comments
 (0)