Skip to main content

iLaplace: A Simple Interface for Powerful Math

iLaplace is a lightweight Python wrapper around advanced symbolic and numerical math tools (sympy, numpy & mpmath) that provides a clean and intuitive interface for computing inverse Laplace transforms using Talbot's method.

I created this library to simplify the process of using the inverse Laplace transform, aiming to provide a MATLAB-like experience within Python. This means the output of my code in Python will be compared against a sample calculation in MathWorks MATLAB.

Why This Library?

  • mpmath is powerful but raw and low-level.
  • sympy gives symbolic transforms, but not numerical answers.
  • Combining them can be verbose and repetitive.

In short: You focus on the math, we handle the machinery.


This library contains two main functions for performing inverse Laplace transform calculations:

  1. inverse_laplace(F, t)
  2. inverse_laplace_fu(F)

Examples for each function and their corresponding MATLAB outputs are provided below.

MATLAB vs. Python Comparison

Here is a sample calculation in MATLAB and its equivalent in Python using this library.

1st Example

MATLAB Example

syms s t
X = (s+3)/((s+1)^2 + (s^2+9));
V = vpa(ilaplace(X, s, t));
t0   = 5.2643;
Volt = double(subs(V, t, t0));
disp(Volt)

The output in MATLAB is -0.020100091042301.

Python Equivalent with iLaplace

import iLaplace as il
import sympy as sp
t, s = sp.symbols('t s')
X = (s+3)/((s+1)**2 + (s**2+9))
t0 = 5.2643
Answer = il.inverse_laplace(X, t0)
print(Answer)

And the output is -0.020100091042300372.

2nd Example

MATLAB Example

syms s t
format long
X = 3 / (s^2 + 3^2);
V = vpa(ilaplace(X, s, t));
t0   = 5.2643;
Volt = double(subs(V, t, t0));
disp(Volt)

And the output in MATLAB is -0.084834643101466.

Python Equivalent

import iLaplace as il
import sympy as sp
import numpy as np
s, t = sp.symbols('s t')
t0 = 5.2643
X = (3) / ((s**2) +  (3**2))
Answer = il.inverse_laplace(X, t0)
print(Answer)

The output is -0.08483425637752962.

3rd Example

MATLAB Example

syms s t
X = (s + 3) / ((s + 3)^2 + 25) ...
    + 1 / (s + 2)^3 ...
    + log((s + 10) / s);  
V = vpa(ilaplace(X, s, t));
t0   = 5.2643;
Volt = double(subs(V, t, t0));
disp(Volt)

And the output in MATLAB is 0.190329629407706.

Python Equivalent

import iLaplace as il
import sympy as sp
t, s = sp.symbols('t s')
t0 = 5.2643
X = ((s + 3) / ((s + 3)**2 + 25)
             + 1 / (s + 2)**3
             + sp.log((s + 10) / s))
Answer = il.inverse_laplace(X, t0)
print(Answer)

The output is 0.19032962256026703.

4th Example

MATLAB Example

syms s t
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X1 = (s + 3) / ((s + 3)^2 + 25) ...
    + 1 / (s + 2)^3 ...
    + log((s + 10) / s);
X2 = (s+3)/((s+1)^2 + (s^2+9));
X3 = 3 / (s^2 + 3^2);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = X1 + X2 + X3;
V = vpa(ilaplace(X, s, t));
t0   = 5.2643;
Volt = double(subs(V, t, t0));
disp(Volt)

And the output in MATLAB is 0.085394895263940.

Python Equivalent

import iLaplace as il
import sympy as sp
t, s = sp.symbols('t s')
t0 = 5.2643
#######################################
X1 = ((s + 3) / ((s + 3)**2 + 25)
             + 1 / (s + 2)**3
             + sp.log((s + 10) / s))
X2 = (s+3)/((s+1)**2 + (s**2+9))
X3 = (3) / ((s**2) +  (3**2))
#######################################
X = X1 + X2 + X3
Answer = il.inverse_laplace(X, t0)
print(Answer)

The output is 0.08539527514043704.


Usage of inverse_laplace_fu

Here is an example of using the inverse_laplace_fu function in iLaplace:

Python Example

import iLaplace as il
import sympy as sp
import math

t, s = sp.symbols('t s')
X = (s+3)/((s+1)**2 + (s**2+9))
Answer = il.inverse_laplace_fu(X)
print(Answer)

def f(t):
    return (5*math.sqrt(19)*math.exp(-t/2)*math.sin(math.sqrt(19)*t/2)/38 + math.exp(-t/2)*math.cos(math.sqrt(19)*t/2)/2)*sp.Heaviside(t)

def g(t):
    return 0.5*math.exp(-0.5*t)*(math.cos(2.1794494717703367761184909919298*t) + 1.1470786693528088295360478904894*math.sin(2.1794494717703367761184909919298*t))

number = 1
d = f(number) - g(number)
print(d)

Output will be:

-0.020100091042300372
(5*sqrt(19)*exp(-t/2)*sin(sqrt(19)*t/2)/38 + exp(-t/2)*cos(sqrt(19)*t/2)/2)*Heaviside(t)
2.77555756156289e-17

License and Attributions

This project is licensed under the MIT License.

It internally utilizes components from the following libraries:

  • mpmath
  • sympy

All third-party libraries retain their original licenses and attributions.

Metadata

Release files for iLaplace 4.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for iLaplace 4.2.1
File Size Uploaded
ilaplace-4.2.1.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for iLaplace 4.2.1
File Interpreter ABI Platform
iLaplace-4.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / ilaplace-4.2.1.tar.gz

Download URL ilaplace-4.2.1.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1d15e3d511bf338dbc185e6b8d49bf3c9b1d7953f54ed8e9550df15e8a9f7c3d
BLAKE2b-256 checksum
How to use checksums
54fb18c7b4ba79750a906da9139c3c013a70b4f89dfc720eb2dba67edf9b8d98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / iLaplace-4.2.1-py3-none-any.whl

Download URL iLaplace-4.2.1-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99a3bcf6412286315bd7f6aa7e3169baa11e8698d9c37e98460ab6a886abd0fe
BLAKE2b-256 checksum
How to use checksums
a54c784a3e9b91c41c30e9d9dc38576fc59848eb80645c37db6a7748785758df
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

4.2.1 This release

2 release files

2.1.3

2 release files

1.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page