
    [h،                          " S  S\ 5      r " S S\5      r " S S\5      r " S S\5      r " S S	\5      r " S
 S\ 5      r\S:X  a  SSKr\R                  " 5         gg)c                   *    \ rS rSrSrS rS rS rSrg)InverseLaplaceTransform   a  
Inverse Laplace transform methods are implemented using this
class, in order to simplify the code and provide a common
infrastructure.

Implement a custom inverse Laplace transform algorithm by
subclassing :class:`InverseLaplaceTransform` and implementing the
appropriate methods. The subclass can then be used by
:func:`~mpmath.invertlaplace` by passing it as the *method*
argument.
c                     Xl         g Nctx)selfr   s     V/var/www/auris/envauris/lib/python3.13/site-packages/mpmath/calculus/inverselaplace.py__init__ InverseLaplaceTransform.__init__   s        c                     [         e)z
Determine the vector of Laplace parameter values needed for an
algorithm, this will depend on the choice of algorithm (de
Hoog is default), the algorithm-specific parameters passed (or
default ones), and desired time.
NotImplementedError)r	   tkwargss      r
   calc_laplace_parameter.InverseLaplaceTransform.calc_laplace_parameter   
     "!r   c                     [         e)z
Compute the time domain solution, after computing the
Laplace-space function evaluations at the abscissa required
for the algorithm. Abscissa computed for one algorithm are
typically not useful for another algorithm.
r   )r	   fps     r
   calc_time_domain_solution1InverseLaplaceTransform.calc_time_domain_solution   r   r   r   N)	__name__
__module____qualname____firstlineno____doc__r   r   r   __static_attributes__ r   r
   r   r      s    
""r   r   c                   $    \ rS rSrS rSS jrSrg)FixedTalbot'   c                 P   U R                   R                  U5      U l        U R                   R                  UR                  SU R                  5      5      U l        SU;   a  US   U l        U R
                  U l        ON[        SU R                   R                  -  5      U l        [        S[        SU R                  -  5      5      U l        U R
                  nU R                   R                  U l
        U R                  U R                   l        UR                  SU R                   R                  SS5      U-  5      U l        U R                   R                  S	U R                   R                  US
-   5      U l        U R                   R!                  US
5      U l        SU R"                  S'   U R                   R!                  US
5      U l        U R                  U R$                  S'   ['        S
U5       Ht  nU R                   R)                  U R                  U   5      U R"                  U'   U R                  U R                  U   -  U R"                  U   S-   -  U R$                  U'   Mv     U R                   R!                  US
5      U l        U R$                  U R                  -  U l        g)a  The "fixed" Talbot method deforms the Bromwich contour towards
`-\infty` in the shape of a parabola. Traditionally the Talbot
algorithm has adjustable parameters, but the "fixed" version
does not. The `r` parameter could be passed in as a parameter,
if you want to override the default given by (Abate & Valko,
2004).

The Laplace parameter is sampled along a parabola opening
along the negative imaginary axis, with the base of the
parabola along the real axis at
`p=\frac{r}{t_\mathrm{max}}`. As the number of terms used in
the approximation (degree) grows, the abscissa required for
function evaluation tend towards `-\infty`, requiring high
precision to prevent overflow.  If any poles, branch cuts or
other singularities exist such that the deformed Bromwich
contour lies to the left of the singularity, the method will
fail.

**Optional arguments**

:class:`~mpmath.calculus.inverselaplace.FixedTalbot.calc_laplace_parameter`
recognizes the following keywords

*tmax*
    maximum time associated with vector of times
    (typically just the time requested)
*degree*
    integer order of approximation (M = number of terms)
*r*
    abscissa for `p_0` (otherwise computed using rule
    of thumb `2M/5`)

The working precision will be increased according to a rule of
thumb. If 'degree' is not specified, the working precision and
degree are chosen to hopefully achieve the dps of the calling
context. If 'degree' is specified, the working precision is
chosen to achieve maximum resulting precision for the
specified degree.

.. math ::

    p_0=\frac{r}{t}

.. math ::

    p_i=\frac{i r \pi}{Mt_\mathrm{max}}\left[\cot\left(
    \frac{i\pi}{M}\right) + j \right] \qquad 1\le i <M

where `j=\sqrt{-1}`, `r=2M/5`, and `t_\mathrm{max}` is the
maximum specified time.

tmaxdegreegQ?   Gz?r      g                             ?N)r   convertr   getr%   r&   dps_goalintdpsmaxdps_origfractionr)   linspacepithetamatrix	cot_thetadeltarangecotp)r	   r   r   Mis        r
   r   "FixedTalbot.calc_laplace_parameter)   s   r !!!$ HH$$VZZ%?@	 v *DK KKDMTXX\\ 12DMb#d4==&8"9:DKKK
 }} C!2!21a!8!:;XX&&sDHHKK1=
A.q XX__Q*


1q!A $TZZ] ;DNN1 FF4::a=0$..2Cb2HIDJJqM  A&DII%r   c                    U R                   R                  U5      U l        U R                  nU R                  nU R
                  nU R                  nU R                  nU R                   R                  US5      n	U R                   R                  US   5      US   -  S-  U	S'   [        SU5       H[  n
U R                   R                  XZ   5      X   -  SSXJ   -  SU R                  U
   S-  -   -  -   SU R                  U
   -  -
  -  X'   M]     U R                   R                  SS5      U R                   R                  U	5      -  U R                  -  nU(       d  U R                  U R                   l        UR                   $ )a  The fixed Talbot time-domain solution is computed from the
Laplace-space function evaluations using

.. math ::

    f(t,M)=\frac{2}{5t}\sum_{k=0}^{M-1}\Re \left[
    \gamma_k \bar{f}(p_k)\right]

where

.. math ::

    \gamma_0 = \frac{1}{2}e^{r}\bar{f}(p_0)

.. math ::

    \gamma_k = e^{tp_k}\left\lbrace 1 + \frac{jk\pi}{M}\left[1 +
    \cot \left( \frac{k \pi}{M} \right)^2 \right] - j\cot\left(
    \frac{k \pi}{M}\right)\right \rbrace \qquad 1\le k<M.

Again, `j=\sqrt{-1}`.

Before calling this function, call
:class:`~mpmath.calculus.inverselaplace.FixedTalbot.calc_laplace_parameter`
to set the parameters and compute the required coefficients.

**References**

1. Abate, J., P. Valko (2004). Multi-precision Laplace
   transform inversion. *International Journal for Numerical
   Methods in Engineering* 60:979-993,
   http://dx.doi.org/10.1002/nme.995
2. Talbot, A. (1979). The accurate numerical inversion of
   Laplace transforms. *IMA Journal of Applied Mathematics*
   23(1):97, http://dx.doi.org/10.1093/imamat/23.1.97
r,   r-   r*   r.   r+   )r   r/   r   r9   r<   r&   r?   r)   r:   expr=   r;   r6   fsumr5   r3   real)r	   r   r   manual_precr9   r<   r@   r?   r)   ansrA   results               r
   r   %FixedTalbot.calc_time_domain_solution   sE   P !!!$ 



KKFFFFhhooa#eAh'1-a/Aq!AXX\\%(+BE1BuxKT^^A%6%9!9::4>>!$$%&CF 
 ""1a(s);;DFFB ==DHHL{{r   )
r;   r&   r<   r1   r5   r?   r)   r   r9   r%   NFr   r   r   r   r   r   r   r    r   r
   r"   r"   '   s    e&REr   r"   c                   *    \ rS rSrS rS rSS jrSrg)Stehfest   c                    U R                   R                  U5      U l        SU;   a(  US   U l        [	        SU R                  -  5      U l        OB[	        SU R                   R                  -  5      U l        [        SU R
                  5      U l        U R                  S-  S:  a  U =R                  S-  sl        U R                  nU R                   R                  U l        U R
                  U R                   l        U R                  5       U l
        U R                   R                  U R                   R                  SUS-   5      5      U R                   R                  -  U R                  -  U l        g)	a  
The Gaver-Stehfest method is a discrete approximation of the
Widder-Post inversion algorithm, rather than a direct
approximation of the Bromwich contour integral.

The method abscissa along the real axis, and therefore has
issues inverting oscillatory functions (which have poles in
pairs away from the real axis).

The working precision will be increased according to a rule of
thumb. If 'degree' is not specified, the working precision and
degree are chosen to hopefully achieve the dps of the calling
context. If 'degree' is specified, the working precision is
chosen to achieve maximum resulting precision for the
specified degree.

.. math ::

    p_k = \frac{k \log 2}{t} \qquad 1 \le k \le M
r&   r(   gq=
ףp@   r*   r-   r,   N)r   r/   r   r&   r2   r1   r3   r4   r5   _coeffVr:   arangeln2r?   )r	   r   r   r@   s       r
   r   Stehfest.calc_laplace_parameter   s   2 !!!$ v *DKT[[ 01DMTXX\\ 12DMb$--0DK ;;?QKK1KKK
 }}AaC!89$((,,FtvvMr   c                 v   U R                   n[        US-  5      nU R                  R                  US5      n[	        SUS-   5       GHn  nU R                  R                  [        XB5      S-   S5      n[	        [        US-   S-  5      [        XB5      S-   5       H  nU R                  R                  Xb5      U R                  R                  SU-  5      -  U R                  R                  X&-
  5      U R                  R                  U5      -  U R                  R                  US-
  5      -  U R                  R                  XF-
  5      -  U R                  R                  SU-  U-
  5      -  -  XV'   M     U R                  R                  SXB-   5      U R                  R                  U5      -  X4S-
  '   GMq     U$ )ztSalzer summation weights (aka, "Stehfest coefficients")
only depend on the approximation order (M) and the precisionr*   r,   )	r&   r2   r   r:   r=   minpowerfacrE   )r	   r@   M2rS   kzjs          r
   rR   Stehfest._coeff  s[    KK1XHHOOAq! q!A#AA
1a0A3!Qw<QA6q-dhhll1Q3.??bd+DHHLLO;ac*++/88<<+<=ac!e,-. 7
 XX^^B-dhhmmA.>>AcF  r   c                 2   U R                   R                  U5      U l        U R                   R                  U R                  U5      U R                   R
                  -  U R                  -  nU(       d  U R                  U R                   l        UR                  $ )as  Compute time-domain Stehfest algorithm solution.

.. math ::

    f(t,M) = \frac{\log 2}{t} \sum_{k=1}^{M} V_k \bar{f}\left(
    p_k \right)

where

.. math ::

    V_k = (-1)^{k + N/2} \sum^{\min(k,N/2)}_{i=\lfloor(k+1)/2 \rfloor}
    \frac{i^{\frac{N}{2}}(2i)!}{\left(\frac{N}{2}-i \right)! \, i! \,
    \left(i-1 \right)! \, \left(k-i\right)! \, \left(2i-k \right)!}

As the degree increases, the abscissa (`p_k`) only increase
linearly towards `\infty`, but the Stehfest coefficients
(`V_k`) alternate in sign and increase rapidly in sign,
requiring high precision to prevent overflow or loss of
significance when evaluating the sum.

**References**

1. Widder, D. (1941). *The Laplace Transform*. Princeton.
2. Stehfest, H. (1970). Algorithm 368: numerical inversion of
   Laplace transforms. *Communications of the ACM* 13(1):47-49,
   http://dx.doi.org/10.1145/361953.361969

)	r   r/   r   fdotrS   rU   r5   r3   rF   )r	   r   r   rG   rI   s        r
   r   "Stehfest.calc_time_domain_solution0  sk    @ !!!$ tvvr*488<<7> ==DHHL {{r   )rS   r&   r1   r5   r?   r   NrK   )r   r   r   r   r   rR   r   r   r    r   r
   rN   rN      s    6Nt0-r   rN   c                   $    \ rS rSrS rSS jrSrg)deHoogib  c                    U R                   R                  U5      U l        UR                  SU R                  5      U l        SU;   a(  US   U l        [        SU R
                  -  5      U l        OB[        U R                   R                  S-  5      U l        [        SU R                  5      U l        U R
                  nU R                   R                  SU R                  * 5      nU R                   R                  UR                  SU5      5      U l        U R                   R                  UR                  SU R                  S-  5      5      U l        S	U R
                  -  S
-   U l        U R                   R                  U l        U R                  U R                   l        UR                  SS	5      U l        U R                   R                  UR                  SU R                  U R                  -  5      5      U l        U R                   R#                  S	U-  S
-   S
5      U l        U R                  U R                   R'                  U R                  5      U R                  U R                   -  -  -
  U l        U R(                  U R                   R*                  U R                   R#                  U R                   R-                  U R                  5      5      -  U R                   -  S-  -   U l        g)a  the de Hoog, Knight & Stokes algorithm is an
accelerated form of the Fourier series numerical
inverse Laplace transform algorithms.

.. math ::

    p_k = \gamma + \frac{jk}{T} \qquad 0 \le k < 2M+1

where

.. math ::

    \gamma = \alpha - \frac{\log \mathrm{tol}}{2T},

`j=\sqrt{-1}`, `T = 2t_\mathrm{max}` is a scaled time,
`\alpha=10^{-\mathrm{dps\_goal}}` is the real part of the
rightmost pole or singularity, which is chosen based on the
desired accuracy (assuming the rightmost singularity is 0),
and `\mathrm{tol}=10\alpha` is the desired tolerance, which is
chosen in relation to `\alpha`.`

When increasing the degree, the abscissa increase towards
`j\infty`, but more slowly than the fixed Talbot
algorithm. The de Hoog et al. algorithm typically does better
with oscillatory functions of time, and less well-behaved
functions. The method tends to be slower than the Talbot and
Stehfest algorithsm, especially so at very high precision
(e.g., `>500` digits precision).

r%   r&   r(   g(\?
   g      $@alphatolr*   r,   scaleTr.   N)r   r/   r   r0   r%   r&   r2   r1   r3   r4   rZ   rh   ri   npr5   rj   rk   r:   r?   loggammar8   rT   )r	   r   r   r@   tmps        r
   r   deHoog.calc_laplace_parameterd  s   D !!!$ JJvtvv.	 v *DKT[[ 01DMT 12DMb$--0DK KK hhnnTDMM>2XX%%fjj#&>?
 88##FJJudjjo$FGDKK-/
 }} ZZ+
!!&**S$**TYY2F"GH1Q*ZZ$((,,txx"8$**TVV:K"LL
**txx{{((//$((//$''":; <<@FF CCE F Fr   c                    U R                   nU R                  nU R                  nU R                  R	                  U5      U l        U R                  R                  XTS-   5      nU R                  R                  SU-  U5      nU R                  R                  US5      n	U R                  R                  US-   S5      n
U R                  R                  US-   S5      nSUSS2S4'   US   US   S-  -  US'   [        SSU-  5       H  nXS-      X   -  XS4'   M     [        SUS-   5       H  nSXM-
  -  S-   nUSUS-   2US-
  4   USU2US-
  4   -
  USUS-   2US-
  4   -   USU2U4'   X:X  a  MG  US-   nSXO-
  -  S-   S-   n[        U5       H-  nXS-   US-
  4   X|S-   US-
  4   -  X|US-
  4   -  XUS-
  4'   M/     M     US   S-  U	S'   [        SUS-   5       H%  nUSUS-
  4   * U	SU-  S-
  '   USU4   * U	SU-  '   M'     SU
S'   U	S   U
S'   SUSS& U R                  R                  U R
                  U-  5      n[        SSU-  5       H5  nX   X   XS-
     -  U-  -   XS-   '   X   X   XS-
     -  U-  -   XS-   '   M7     SU	SU-  S-
     U	SU-     -
  U-  -   S-  nUU R                  R                  SU	SU-     U-  U-  -   U R                  R                  SS5      5      -  nU
SU-     UU
SU-  S-
     -  -   X'   USU-     UUSU-  S-
     -  -   X'   U R                  R                  U R                  U R
                  -  5      U-  X   X   -  R                  -  nU(       d  U R                   U R                  l        U$ )a  Calculate time-domain solution for
de Hoog, Knight & Stokes algorithm.

The un-accelerated Fourier series approach is:

.. math ::

    f(t,2M+1) = \frac{e^{\gamma t}}{T} \sum_{k=0}^{2M}{}^{'}
    \Re\left[\bar{f}\left( p_k \right)
    e^{i\pi t/T} \right],

where the prime on the summation indicates the first term is halved.

This simplistic approach requires so many function evaluations
that it is not practical. Non-linear acceleration is
accomplished via Pade-approximation and an analytic expression
for the remainder of the continued fraction. See the original
paper (reference 2 below) a detailed description of the
numerical approach.

**References**

1. Davies, B. (2005). *Integral Transforms and their
   Applications*, Third Edition. Springer.
2. de Hoog, F., J. Knight, A. Stokes (1982). An improved
   method for numerical inversion of Laplace transforms. *SIAM
   Journal of Scientific and Statistical Computing* 3:357-366,
   http://dx.doi.org/10.1137/0903022

r,   r*   y                Nr-   )r-   r-   y      ?        )r&   rl   rk   r   r/   r   zerosr:   onesr=   expjpipowm1r6   rD   rn   rF   r5   r3   )r	   r   r   rG   r@   rl   rk   eqdABrA   r)   mrrqr^   bremremrI   s                       r
   r    deHoog.calc_time_domain_solution  s   @ KKWWFF!!!$ HHNN2s#HHOOAaC#HHOOB"HHNN2a4#HHMM"Q$" !Q$Q%Aq/$q!A#A1gbemAdG  q!A#AAC1B1RT61Q3;!AbD!A#I,61RT61Q3;GAadAgJ6qSXaZ!^rA!"Q319a!RT	l!:11W:!EAAgJ #  !uQw!q!A#A!QqS&	zAac!eH!Q$xAacF 
 !t!!A HHOODFF1H% q!A#ATADQ3KM)AcFTADQ3KM)AcF 
 Qqs1uX!A#&))1,488>>!a!fQhtm"3"&(("3"3Aq"9; ; !A#Qqs1uX%!A#Qqs1uX% djj/02AE!%K3E3EE ==DHHLr   )rk   rh   r&   r1   r5   rn   rl   r?   rj   r   r%   ri   NrK   rL   r    r   r
   re   re   b  s    LG`er   re   c                   $    \ rS rSrS rSS jrSrg)Coheni  c                 4   U R                   R                  U5      U l        SU;   a(  US   U l        [	        SU R                  -  5      U l        ON[	        U R                   R                  S-  5      U l        [        S[	        SU R
                  -  5      5      U l        U R                  S-   nU R                   R                  U l        U R
                  U R                   l        SU R                  -  nU R                   R                  U R                   R                  S5      -  U R                   R                  U5      -   nU R                   R                  SS	5      U-  nU R                   R                  UR                  S
U5      5      U l        U R                  U-  nU R                   R                  S-  U R                  -  nU R                   R                  US5      U l        X`R                  S'   [!        SU5       H  nXhU-  -   U R                  U'   M     g)a  The Cohen algorithm accelerates the convergence of the nearly
alternating series resulting from the application of the trapezoidal
rule to the Bromwich contour inversion integral.

.. math ::

    p_k = \frac{\gamma}{2 t} + \frac{\pi i k}{t} \qquad 0 \le k < M

where

.. math ::

    \gamma = \frac{2}{3} (d + \log(10) + \log(2 t)),

`d = \mathrm{dps\_goal}`, which is chosen based on the desired
accuracy using the method developed in [1] to improve numerical
stability. The Cohen algorithm shows robustness similar to the de Hoog
et al. algorithm, but it is faster than the fixed Talbot algorithm.

**Optional arguments**

*degree*
    integer order of the approximation (M = number of terms)
*alpha*
    abscissa for `p_0` (controls the discretization error)

The working precision will be increased according to a rule of
thumb. If 'degree' is not specified, the working precision and
degree are chosen to hopefully achieve the dps of the calling
context. If 'degree' is specified, the working precision is
chosen to achieve maximum resulting precision for the
specified degree.

**References**

1. P. Glasserman, J. Ruiz-Mata (2006). Computing the credit loss
distribution in the Gaussian copula model: a comparison of methods.
*Journal of Credit Risk* 2(4):33-66, 10.21314/JCR.2006.057

r&   g      ?gףp=
?   g(\?r,   r*   rg      rh   r.   r-   N)r   r/   r   r&   r2   r1   r3   r4   r5   rm   r6   r0   rh   r8   r:   r?   r=   )	r	   r   r   r@   ttworo   a_tp_trA   s	            r
   r   Cohen.calc_laplace_parameter   s   R !!!$v *DKdkk 12DMt 34DMb#dT]]&:";<DKKK!O
 }}466zhhllTXX\\"--T0BBhh1%+XX%%fjj#&>?
 jj4hhkkB'A&q	q!A#gDFF1I r   c                    U R                   R                  U5      U l        U R                  nUS-   nU R                   R	                  US5      n[        U5       H  nX   R                  Xg'   M     SU R                   R                  S5      -   U-  nUSU-  -   S-  nU R                   R                  * n	U* n
Sn[        U5       HD  nX-
  n
XXlS-      -  -   nSX-   -  X-
  -  U	-  SU-  S-   XR                   R                  -   -  -  n	MF     U R                   R                  U R                  S-  5      U R                  -  US   S-  X-  -
  -  nU(       d  U R                  U R                   l        U$ )a%  Calculate time-domain solution for Cohen algorithm.

The accelerated nearly alternating series is:

.. math ::

    f(t, M) = \frac{e^{\gamma / 2}}{t} \left[\frac{1}{2}
    \Re\left(\bar{f}\left(\frac{\gamma}{2t}\right) \right) -
    \sum_{k=0}^{M-1}\frac{c_{M,k}}{d_M}\Re\left(\bar{f}
    \left(\frac{\gamma + 2(k+1) \pi i}{2t}\right)\right)\right],

where coefficients `\frac{c_{M, k}}{d_M}` are described in [1].

1. H. Cohen, F. Rodriguez Villegas, D. Zagier (2000). Convergence
acceleration of alternating series. *Experiment. Math* 9(1):3-12

r,   r      r*   r-   )r   r/   r   r&   r:   r=   rF   sqrtonerD   rh   r5   r3   )r	   r   r   rG   nr@   ry   rA   rx   bcsr]   rI   s                 r
   r   Cohen.calc_time_domain_solutioni  sR   $ !!!$KKEHHOOAq!qA5::AD  q!!a'QY!OXX\\MBqAAa%L AQUqu%)a!eaiA<L-MNA 
 djj1n-6!A$(QU:JK ==DHHLr   )rh   r&   r1   r5   r?   r   NrK   rL   r    r   r
   r   r     s    G&R-r   r   c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)
 LaplaceTransformInversionMethodsi  c                     [        U 5      U l        [        U 5      U l        [	        U 5      U l        [        U 5      U l        g r   )r"   _fixed_talbotrN   	_stehfestre   _de_hoogr   _cohenr   argsr   s      r
   r   )LaplaceTransformInversionMethods.__init__  s0    ', c{3Z
r   c                    UR                  SS5      n[        U5      [        L aj  UR                  5       nUS:X  a  U R                  nOOUS:X  a  U R
                  nO<US:X  a  U R                  nO)US:X  a  U R                  nO[        SU-  5      eU" U 5      nUR                  " U40 UD6  UR                   Vs/ s H
  oa" U5      PM     nnUR                  Xr5      $ s  snf )ar$  Computes the numerical inverse Laplace transform for a
Laplace-space function at a given time.  The function being
evaluated is assumed to be a real-valued function of time.

The user must supply a Laplace-space function `\bar{f}(p)`,
and a desired time at which to estimate the time-domain
solution `f(t)`.

A few basic examples of Laplace-space functions with known
inverses (see references [1,2]) :

.. math ::

    \mathcal{L}\left\lbrace f(t) \right\rbrace=\bar{f}(p)

.. math ::

    \mathcal{L}^{-1}\left\lbrace \bar{f}(p) \right\rbrace = f(t)

.. math ::

    \bar{f}(p) = \frac{1}{(p+1)^2}

.. math ::

    f(t) = t e^{-t}

>>> from mpmath import *
>>> mp.dps = 15; mp.pretty = True
>>> tt = [0.001, 0.01, 0.1, 1, 10]
>>> fp = lambda p: 1/(p+1)**2
>>> ft = lambda t: t*exp(-t)
>>> ft(tt[0]),ft(tt[0])-invertlaplace(fp,tt[0],method='talbot')
(0.000999000499833375, 8.57923043561212e-20)
>>> ft(tt[1]),ft(tt[1])-invertlaplace(fp,tt[1],method='talbot')
(0.00990049833749168, 3.27007646698047e-19)
>>> ft(tt[2]),ft(tt[2])-invertlaplace(fp,tt[2],method='talbot')
(0.090483741803596, -1.75215800052168e-18)
>>> ft(tt[3]),ft(tt[3])-invertlaplace(fp,tt[3],method='talbot')
(0.367879441171442, 1.2428864009344e-17)
>>> ft(tt[4]),ft(tt[4])-invertlaplace(fp,tt[4],method='talbot')
(0.000453999297624849, 4.04513489306658e-20)

The methods also work for higher precision:

>>> mp.dps = 100; mp.pretty = True
>>> nstr(ft(tt[0]),15),nstr(ft(tt[0])-invertlaplace(fp,tt[0],method='talbot'),15)
('0.000999000499833375', '-4.96868310693356e-105')
>>> nstr(ft(tt[1]),15),nstr(ft(tt[1])-invertlaplace(fp,tt[1],method='talbot'),15)
('0.00990049833749168', '1.23032291513122e-104')

.. math ::

    \bar{f}(p) = \frac{1}{p^2+1}

.. math ::

    f(t) = \mathrm{J}_0(t)

>>> mp.dps = 15; mp.pretty = True
>>> fp = lambda p: 1/sqrt(p*p + 1)
>>> ft = lambda t: besselj(0,t)
>>> ft(tt[0]),ft(tt[0])-invertlaplace(fp,tt[0],method='dehoog')
(0.999999750000016, -6.09717765032273e-18)
>>> ft(tt[1]),ft(tt[1])-invertlaplace(fp,tt[1],method='dehoog')
(0.99997500015625, -5.61756281076169e-17)

.. math ::

    \bar{f}(p) = \frac{\log p}{p}

.. math ::

    f(t) = -\gamma -\log t

>>> mp.dps = 15; mp.pretty = True
>>> fp = lambda p: log(p)/p
>>> ft = lambda t: -euler-log(t)
>>> ft(tt[0]),ft(tt[0])-invertlaplace(fp,tt[0],method='stehfest')
(6.3305396140806, -1.92126634837863e-16)
>>> ft(tt[1]),ft(tt[1])-invertlaplace(fp,tt[1],method='stehfest')
(4.02795452108656, -4.81486093200704e-16)

**Options**

:func:`~mpmath.invertlaplace` recognizes the following optional
keywords valid for all methods:

*method*
    Chooses numerical inverse Laplace transform algorithm
    (described below).
*degree*
    Number of terms used in the approximation

**Algorithms**

Mpmath implements four numerical inverse Laplace transform
algorithms, attributed to: Talbot, Stehfest, and de Hoog,
Knight and Stokes. These can be selected by using
*method='talbot'*, *method='stehfest'*, *method='dehoog'* or
*method='cohen'* or by passing the classes *method=FixedTalbot*,
*method=Stehfest*, *method=deHoog*, or *method=Cohen*. The functions
:func:`~mpmath.invlaptalbot`, :func:`~mpmath.invlapstehfest`,
:func:`~mpmath.invlapdehoog`, and :func:`~mpmath.invlapcohen`
are also available as shortcuts.

All four algorithms implement a heuristic balance between the
requested precision and the precision used internally for the
calculations. This has been tuned for a typical exponentially
decaying function and precision up to few hundred decimal
digits.

The Laplace transform converts the variable time (i.e., along
a line) into a parameter given by the right half of the
complex `p`-plane.  Singularities, poles, and branch cuts in
the complex `p`-plane contain all the information regarding
the time behavior of the corresponding function. Any numerical
method must therefore sample `p`-plane "close enough" to the
singularities to accurately characterize them, while not
getting too close to have catastrophic cancellation, overflow,
or underflow issues. Most significantly, if one or more of the
singularities in the `p`-plane is not on the left side of the
Bromwich contour, its effects will be left out of the computed
solution, and the answer will be completely wrong.

*Talbot*

The fixed Talbot method is high accuracy and fast, but the
method can catastrophically fail for certain classes of time-domain
behavior, including a Heaviside step function for positive
time (e.g., `H(t-2)`), or some oscillatory behaviors. The
Talbot method usually has adjustable parameters, but the
"fixed" variety implemented here does not. This method
deforms the Bromwich integral contour in the shape of a
parabola towards `-\infty`, which leads to problems
when the solution has a decaying exponential in it (e.g., a
Heaviside step function is equivalent to multiplying by a
decaying exponential in Laplace space).

*Stehfest*

The Stehfest algorithm only uses abscissa along the real axis
of the complex `p`-plane to estimate the time-domain
function. Oscillatory time-domain functions have poles away
from the real axis, so this method does not work well with
oscillatory functions, especially high-frequency ones. This
method also depends on summation of terms in a series that
grows very large, and will have catastrophic cancellation
during summation if the working precision is too low.

*de Hoog et al.*

The de Hoog, Knight, and Stokes method is essentially a
Fourier-series quadrature-type approximation to the Bromwich
contour integral, with non-linear series acceleration and an
analytical expression for the remainder term. This method is
typically one of the most robust. This method also involves the
greatest amount of overhead, so it is typically the slowest of the
four methods at high precision.

*Cohen*

The Cohen method is a trapezoidal rule approximation to the Bromwich
contour integral, with linear acceleration for alternating
series. This method is as robust as the de Hoog et al method and the
fastest of the four methods at high precision, and is therefore the
default method.

**Singularities**

All numerical inverse Laplace transform methods have problems
at large time when the Laplace-space function has poles,
singularities, or branch cuts to the right of the origin in
the complex plane. For simple poles in `\bar{f}(p)` at the
`p`-plane origin, the time function is constant in time (e.g.,
`\mathcal{L}\left\lbrace 1 \right\rbrace=1/p` has a pole at
`p=0`). A pole in `\bar{f}(p)` to the left of the origin is a
decreasing function of time (e.g., `\mathcal{L}\left\lbrace
e^{-t/2} \right\rbrace=1/(p+1/2)` has a pole at `p=-1/2`), and
a pole to the right of the origin leads to an increasing
function in time (e.g., `\mathcal{L}\left\lbrace t e^{t/4}
\right\rbrace = 1/(p-1/4)^2` has a pole at `p=1/4`).  When
singularities occur off the real `p` axis, the time-domain
function is oscillatory. For example `\mathcal{L}\left\lbrace
\mathrm{J}_0(t) \right\rbrace=1/\sqrt{p^2+1}` has a branch cut
starting at `p=j=\sqrt{-1}` and is a decaying oscillatory
function, This range of behaviors is illustrated in Duffy [3]
Figure 4.10.4, p. 228.

In general as `p \rightarrow \infty` `t \rightarrow 0` and
vice-versa. All numerical inverse Laplace transform methods
require their abscissa to shift closer to the origin for
larger times. If the abscissa shift left of the rightmost
singularity in the Laplace domain, the answer will be
completely wrong (the effect of singularities to the right of
the Bromwich contour are not included in the results).

For example, the following exponentially growing function has
a pole at `p=3`:

.. math ::

    \bar{f}(p)=\frac{1}{p^2-9}

.. math ::

    f(t)=\frac{1}{3}\sinh 3t

>>> mp.dps = 15; mp.pretty = True
>>> fp = lambda p: 1/(p*p-9)
>>> ft = lambda t: sinh(3*t)/3
>>> tt = [0.01,0.1,1.0,10.0]
>>> ft(tt[0]),invertlaplace(fp,tt[0],method='talbot')
(0.0100015000675014, 0.0100015000675014)
>>> ft(tt[1]),invertlaplace(fp,tt[1],method='talbot')
(0.101506764482381, 0.101506764482381)
>>> ft(tt[2]),invertlaplace(fp,tt[2],method='talbot')
(3.33929164246997, 3.33929164246997)
>>> ft(tt[3]),invertlaplace(fp,tt[3],method='talbot')
(1781079096920.74, -1.61331069624091e-14)

**References**

1. [DLMF]_ section 1.14 (http://dlmf.nist.gov/1.14T4)
2. Cohen, A.M. (2007). Numerical Methods for Laplace Transform
   Inversion, Springer.
3. Duffy, D.G. (1998). Advanced Engineering Mathematics, CRC Press.

**Numerical Inverse Laplace Transform Reviews**

1. Bellman, R., R.E. Kalaba, J.A. Lockett (1966). *Numerical
   inversion of the Laplace transform: Applications to Biology,
   Economics, Engineering, and Physics*. Elsevier.
2. Davies, B., B. Martin (1979). Numerical inversion of the
   Laplace transform: a survey and comparison of methods. *Journal
   of Computational Physics* 33:1-32,
   http://dx.doi.org/10.1016/0021-9991(79)90025-1
3. Duffy, D.G. (1993). On the numerical inversion of Laplace
   transforms: Comparison of three new methods on characteristic
   problems from applications. *ACM Transactions on Mathematical
   Software* 19(3):333-359, http://dx.doi.org/10.1145/155743.155788
4. Kuhlman, K.L., (2013). Review of Inverse Laplace Transform
   Algorithms for Laplace-Space Numerical Approaches, *Numerical
   Algorithms*, 63(2):339-355.
   http://dx.doi.org/10.1007/s11075-012-9625-3

methodcohentalbotstehfestdehoogzunknown invlap algorithm: %s)r0   typestrlowerr   r   r   r   
ValueErrorr   r?   r   )r   fr   r   rulelruler?   r   s           r
   invertlaplace.LaplaceTransformInversionMethods.invertlaplace  s    r zz(G,:JJLE ((*$}}("||zz !?$!FGG9D 	##A00 !FF#FqadF# --b44	 $s   8Cc                 0    SUS'   U R                   " U0 UD6$ )Nr   r   r   r   s      r
   invlaptalbot-LaplaceTransformInversionMethods.invlaptalbot  "    #x  $1&11r   c                 0    SUS'   U R                   " U0 UD6$ )Nr   r   r   r   s      r
   invlapstehfest/LaplaceTransformInversionMethods.invlapstehfest  s"    %x  $1&11r   c                 0    SUS'   U R                   " U0 UD6$ )Nr   r   r   r   s      r
   invlapdehoog-LaplaceTransformInversionMethods.invlapdehoog  r   r   c                 0    SUS'   U R                   " U0 UD6$ )Nr   r   r   r   s      r
   invlapcohen,LaplaceTransformInversionMethods.invlapcohen  s"    "x  $1&11r   r    N)r   r   r   r   r   r   r   r   r   r   r   r    r   r
   r   r     s"     S5l2222r   r   __main__r-   N)
objectr   r"   rN   re   r   r   r   doctesttestmodr    r   r
   <module>r      s}    "f  "Fp) pjA& ALw$ wxx# xzk2v k2`	 zOO r   