Skip to content

feat: add numerical laplace transform#14602

Open
Tushar-R-Tyagi wants to merge 5 commits intoTheAlgorithms:masterfrom
Tushar-R-Tyagi:master
Open

feat: add numerical laplace transform#14602
Tushar-R-Tyagi wants to merge 5 commits intoTheAlgorithms:masterfrom
Tushar-R-Tyagi:master

Conversation

@Tushar-R-Tyagi
Copy link
Copy Markdown

Describe your change:

I have added a numerical implementation of the Laplace transform in the maths/ directory.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Copilot AI review requested due to automatic review settings April 28, 2026 18:34
@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Apr 28, 2026
@Tushar-R-Tyagi
Copy link
Copy Markdown
Author

pre-commit.ci autofix

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread maths/laplace_transformation.py Outdated
Comment thread maths/laplace_transformation.py
Comment thread maths/laplace_transformation.py
Comment thread maths/laplace_transformation.py Outdated
Comment on lines +43 to +44
if s_value < 0:
raise ValueError("s_value must be non-negative for convergence.")
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment thread maths/laplace_transformation.py
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Apr 28, 2026
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Apr 28, 2026
>>> abs(res - 0.5) < 1e-3
True
"""
if delta_t <= 0:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
"""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants