�
H�gZ � �� � d dl mZ n
# e$ r eZY nw xY wddlmZ ddlmZ eZn# e $ r d� ZY nw xY wed� � � Z
ed d
�� � Z G d� d� � Zd!d�Z
ej e
_ ee
� � G d� d� � Zd� Zej e_ ee� � ed d�� � Ze d"d�� � Zed� � � Zed� � � Zd� Zd� Zd� Zd� Zd� Zd� Zed#d�� � Zed$d�� � ZdS )%� )�izip� ��xrange� )�defunc �* � | � � � S �N)�next)�_s �m/home/asafur/pinokio/api/open-webui.git/app/env/lib/python3.11/site-packages/mpmath/calculus/extrapolation.py�<lambda>r s � �Q�V�V�X�X� � c � � t |� � dk rt d� � �| � |d |d z
� � | � |d |d z
� � k r|ddd� }t |� � dz dz
}| j }d|z ||z z | � | � |� � � � z }d}t
|dz � � D ]{}|||||z z z
}t t |� � |� � }|||z
| � ||z dz � � |z z z }|d|z | � ||z � � |z z z }�|||fS ) a�
Given a list ``seq`` of the first `N` elements of a slowly convergent
infinite sequence, :func:`~mpmath.richardson` computes the `N`-term
Richardson extrapolate for the limit.
:func:`~mpmath.richardson` returns `(v, c)` where `v` is the estimated
limit and `c` is the magnitude of the largest weight used during the
computation. The weight provides an estimate of the precision
lost to cancellation. Due to cancellation effects, the sequence must
be typically be computed at a much higher precision than the target
accuracy of the extrapolation.
**Applicability and issues**
The `N`-step Richardson extrapolation algorithm used by
:func:`~mpmath.richardson` is described in [1].
Richardson extrapolation only works for a specific type of sequence,
namely one converging like partial sums of
`P(1)/Q(1) + P(2)/Q(2) + \ldots` where `P` and `Q` are polynomials.
When the sequence does not convergence at such a rate
:func:`~mpmath.richardson` generally produces garbage.
Richardson extrapolation has the advantage of being fast: the `N`-term
extrapolate requires only `O(N)` arithmetic operations, and usually
produces an estimate that is accurate to `O(N)` digits. Contrast with
the Shanks transformation (see :func:`~mpmath.shanks`), which requires
`O(N^2)` operations.
:func:`~mpmath.richardson` is unable to produce an estimate for the
approximation error. One way to estimate the error is to perform
two extrapolations with slightly different `N` and comparing the
results.
Richardson extrapolation does not work for oscillating sequences.
As a simple workaround, :func:`~mpmath.richardson` detects if the last
three elements do not differ monotonically, and in that case
applies extrapolation only to the even-index elements.
**Example**
Applying Richardson extrapolation to the Leibniz series for `\pi`::
>>> from mpmath import *
>>> mp.dps = 30; mp.pretty = True
>>> S = [4*sum(mpf(-1)**n/(2*n+1) for n in range(m))
... for m in range(1,30)]
>>> v, c = richardson(S[:10])
>>> v
3.2126984126984126984126984127
>>> nprint([v-pi, c])
[0.0711058, 2.0]
>>> v, c = richardson(S[:30])
>>> v
3.14159265468624052829954206226
>>> nprint([v-pi, c])
[1.09645e-9, 20833.3]
**References**
1. [BenderOrszag]_ pp. 375-376
� z!seq should be of minimum length 3���������������Nr r ) �len�
ValueError�sign�zero�mpf�_ifacr �max�abs)�ctx�seq�N�s�c�maxc�ks r
�
richardsonr$ sX � �D �3�x�x�!�|�|��<�=�=�=�
�x�x��B���B��� � �C�H�H�S��W�S��W�_�$=�$=�=�=��#�#�A�#�h���C���!��A�
�A���A�
�a��!�Q�$��������1���.�.�.�A��D�
�A�a�C�[�[� %� %�� �Q��Q�q�S��\����3�q�6�6�4� � �� �a��c�3�7�7�1�Q�3�q�5�>�>�1�$�
$�$�� �q��s�C�G�G�A�a�C�L�L�!�O�#�$����d�7�Nr NFc � � t |� � dk rt d� � �|rt |� � }nd}g }t |� � dz
}|dz r|dz }| j }| j
}|r%ddlm} |� � } | � |� � t ||� � D ]�}
g }t |
dz � � D ]�}|dk rd||
dz ||
z
}}
nC|dk r ||
}
n||
dz
|dz
}
||dz
||
dz
|dz
z
}|s7|rd| � d� � z |z }n|
dz r|dd� c c S |c c S |� |
||z z � � ��|� |� � ��|S ) a�
Given a list ``seq`` of the first `N` elements of a slowly
convergent infinite sequence `(A_k)`, :func:`~mpmath.shanks` computes the iterated
Shanks transformation `S(A), S(S(A)), \ldots, S^{N/2}(A)`. The Shanks
transformation often provides strong convergence acceleration,
especially if the sequence is oscillating.
The iterated Shanks transformation is computed using the Wynn
epsilon algorithm (see [1]). :func:`~mpmath.shanks` returns the full
epsilon table generated by Wynn's algorithm, which can be read
off as follows:
* The table is a list of lists forming a lower triangular matrix,
where higher row and column indices correspond to more accurate
values.
* The columns with even index hold dummy entries (required for the
computation) and the columns with odd index hold the actual
extrapolates.
* The last element in the last row is typically the most
accurate estimate of the limit.
* The difference to the third last element in the last row
provides an estimate of the approximation error.
* The magnitude of the second last element provides an estimate
of the numerical accuracy lost to cancellation.
For convenience, so the extrapolation is stopped at an odd index
so that ``shanks(seq)[-1][-1]`` always gives an estimate of the
limit.
Optionally, an existing table can be passed to :func:`~mpmath.shanks`.
This can be used to efficiently extend a previous computation after
new elements have been appended to the sequence. The table will
then be updated in-place.
**The Shanks transformation**
The Shanks transformation is defined as follows (see [2]): given
the input sequence `(A_0, A_1, \ldots)`, the transformed sequence is
given by
.. math ::
S(A_k) = \frac{A_{k+1}A_{k-1}-A_k^2}{A_{k+1}+A_{k-1}-2 A_k}
The Shanks transformation gives the exact limit `A_{\infty}` in a
single step if `A_k = A + a q^k`. Note in particular that it
extrapolates the exact sum of a geometric series in a single step.
Applying the Shanks transformation once often improves convergence
substantially for an arbitrary sequence, but the optimal effect is
obtained by applying it iteratively:
`S(S(A_k)), S(S(S(A_k))), \ldots`.
Wynn's epsilon algorithm provides an efficient way to generate
the table of iterated Shanks transformations. It reduces the
computation of each element to essentially a single division, at
the cost of requiring dummy elements in the table. See [1] for
details.
**Precision issues**
Due to cancellation effects, the sequence must be typically be
computed at a much higher precision than the target accuracy
of the extrapolation.
If the Shanks transformation converges to the exact limit (such
as if the sequence is a geometric series), then a division by
zero occurs. By default, :func:`~mpmath.shanks` handles this case by
terminating the iteration and returning the table it has
generated so far. With *randomized=True*, it will instead
replace the zero by a pseudorandom number close to zero.
(TODO: find a better solution to this problem.)
**Examples**
We illustrate by applying Shanks transformation to the Leibniz
series for `\pi`::
>>> from mpmath import *
>>> mp.dps = 50
>>> S = [4*sum(mpf(-1)**n/(2*n+1) for n in range(m))
... for m in range(1,30)]
>>>
>>> T = shanks(S[:7])
>>> for row in T:
... nprint(row)
...
[-0.75]
[1.25, 3.16667]
[-1.75, 3.13333, -28.75]
[2.25, 3.14524, 82.25, 3.14234]
[-2.75, 3.13968, -177.75, 3.14139, -969.937]
[3.25, 3.14271, 327.25, 3.14166, 3515.06, 3.14161]
The extrapolated accuracy is about 4 digits, and about 4 digits
may have been lost due to cancellation::
>>> L = T[-1]
>>> nprint([abs(L[-1] - pi), abs(L[-1] - L[-3]), abs(L[-2])])
[2.22532e-5, 4.78309e-5, 3515.06]
Now we extend the computation::
>>> T = shanks(S[:25], T)
>>> L = T[-1]
>>> nprint([abs(L[-1] - pi), abs(L[-1] - L[-3]), abs(L[-2])])
[3.75527e-19, 1.48478e-19, 2.96014e+17]
The value for pi is now accurate to 18 digits. About 18 digits may
also have been lost to cancellation.
Here is an example with a geometric series, where the convergence
is immediate (the sum is exactly 1)::
>>> mp.dps = 15
>>> for row in shanks([0.5, 0.75, 0.875, 0.9375, 0.96875]):
... nprint(row)
[4.0]
[8.0, 1.0]
**References**
1. [GravesMorris]_
2. [BenderOrszag]_ pp. 368-375
r z!seq should be of minimum length 2r r )�Random�
Nr )
r r �one�eps�randomr&