Skip to content

feat: add numerical laplace transform#14602

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

feat: add numerical laplace transform#14602
Tushar-R-Tyagi wants to merge 12 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.")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added validation for s_value < 0 with a ValueError. Thanks for this catch.

Comment thread maths/laplace_transformation.py Outdated
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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I agree, Linspace is much clearer. I have replaced arange with linspace to make the time vector construction more readable.

Comment thread maths/laplace_transformation.py Outdated
@@ -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
"""

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for this suggestion. I have updated the module just like you recommended.

@psyduck1203
Copy link
Copy Markdown

Hi @Tushar-R-Tyagi, nice fix!

I noticed the issue (#14606) mentions enforcing validation of non-negative s_value. Also checked the code reviews from @jgohel9902. It would make sense to also handle edge cases like:

  • non-numeric inputs (e.g., strings)
  • complex numbers (since only real-valued parameters are supported)

Happy to extend any help if needed.

Tushar-R-Tyagi and others added 2 commits May 4, 2026 19:51
Updated module docstring, added validation for non-negative s_value, and replaced arrange with linspace for clarity.
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label May 4, 2026
Tushar-R-Tyagi and others added 2 commits May 4, 2026 20:02
@Tushar-R-Tyagi
Copy link
Copy Markdown
Author

ruff check .

Copy link
Copy Markdown

@algorithms-keeper algorithms-keeper Bot left a comment

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

@@ -0,0 +1,74 @@
"""
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

An error occurred while parsing the file: maths/laplace_transformation.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
tokenizer error: no matching outer block for dedent

"""
^

@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label May 4, 2026
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.

4 participants