feat: add numerical laplace transform#14602
feat: add numerical laplace transform#14602Tushar-R-Tyagi wants to merge 5 commits intoTheAlgorithms:masterfrom
Conversation
for more information, see https://pre-commit.ci
|
pre-commit.ci autofix |
There was a problem hiding this comment.
Pull request overview
Adds a numerical (sampled-data) Laplace transform implementation to the maths/ algorithms collection.
Changes:
- Introduces
laplace_transform()that applies an exponential kernel and integrates via the trapezoidal rule. - Adds doctest examples for two basic reference functions.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if s_value < 0: | ||
| raise ValueError("s_value must be non-negative for convergence.") |
There was a problem hiding this comment.
The convergence check if s_value < 0: raise ... is not generally correct for the Laplace transform (e.g., for f(t)=e^{-t}, the transform converges for Re(s) > -1). Also, since this implementation integrates over a finite sampled window, negative s_value won't inherently diverge. Consider removing this restriction, or (if you keep it) documenting it as a deliberate limitation rather than a convergence requirement.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
| >>> abs(res - 0.5) < 1e-3 | ||
| True | ||
| """ | ||
| if delta_t <= 0: |
There was a problem hiding this comment.
Good input validation — catching non-positive delta_t early prevents
silent numerical errors. Consider also validating that s_value is
non-negative since the docstring on line 18 states this implementation
only supports non-negative s values, but there is no guard for it:
if s_value < 0:
raise ValueError("s_value must be non-negative for this implementation.")
| raise ValueError("function_values array cannot be empty.") | ||
|
|
||
| # Time vector corresponding to the function values | ||
| time_vector = np.arange(len(function_values)) * delta_t |
There was a problem hiding this comment.
Using np.arange(len(function_values)) * delta_t works correctly but
np.linspace would be more semantically clear and consistent with the
doctests which already use np.linspace:
time_vector = np.linspace(0, (len(function_values) - 1) * delta_t,
len(function_values))
This makes the time vector construction more readable and explicit.
| @@ -0,0 +1,66 @@ | |||
| """ | |||
| This module provides a numerical implementation of the Laplace Transform. | |||
There was a problem hiding this comment.
The module docstring is minimal — just one line and a Wikipedia link.
Consider expanding it to match the quality of the function docstring:
"""
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
"""
Describe your change:
I have added a numerical implementation of the Laplace transform in the maths/ directory.
Checklist: