freealg 0.7.7__tar.gz → 0.7.8__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {freealg-0.7.7 → freealg-0.7.8}/PKG-INFO +1 -1
- freealg-0.7.8/freealg/__version__.py +1 -0
- freealg-0.7.8/freealg/_algebraic_form/_homotopy.py +280 -0
- freealg-0.7.8/freealg/_algebraic_form/_moments.py +450 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/algebraic_form.py +11 -19
- {freealg-0.7.7 → freealg-0.7.8}/freealg.egg-info/PKG-INFO +1 -1
- {freealg-0.7.7 → freealg-0.7.8}/freealg.egg-info/SOURCES.txt +1 -0
- freealg-0.7.7/freealg/__version__.py +0 -1
- freealg-0.7.7/freealg/_algebraic_form/_homotopy.py +0 -138
- {freealg-0.7.7 → freealg-0.7.8}/AUTHORS.txt +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/CHANGELOG.rst +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/LICENSE.txt +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/MANIFEST.in +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/README.rst +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/__init__.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/__init__.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_constraints.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_continuation_algebraic.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_decompress.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_decompress2.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_discriminant.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_edge.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_algebraic_form/_sheets_util.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/__init__.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_chebyshev.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_damp.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_decompress.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_density_util.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_jacobi.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_linalg.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_pade.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_plot_util.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_sample.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_series.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/_support.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_free_form/free_form.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/__init__.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/_continuation_genus0.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/_continuation_genus1.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/_elliptic_functions.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/_sphere_maps.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/_torus_maps.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_geometric_form/geometric_form.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/_util.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/__init__.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_chiral_block.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_deformed_marchenko_pastur.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_deformed_wigner.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_kesten_mckay.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_marchenko_pastur.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_meixner.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_wachter.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/distributions/_wigner.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/visualization/__init__.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/visualization/_glue_util.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg/visualization/_rgb_hsv.py +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg.egg-info/dependency_links.txt +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg.egg-info/not-zip-safe +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg.egg-info/requires.txt +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/freealg.egg-info/top_level.txt +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/pyproject.toml +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/requirements.txt +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/setup.cfg +0 -0
- {freealg-0.7.7 → freealg-0.7.8}/setup.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.8"
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# =======
|
|
2
|
+
# Imports
|
|
3
|
+
# =======
|
|
4
|
+
|
|
5
|
+
import numpy
|
|
6
|
+
from ._moments import AlgebraicStieltjesMoments
|
|
7
|
+
|
|
8
|
+
__all__ = ['stieltjes_poly']
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
# =====================
|
|
12
|
+
# select root
|
|
13
|
+
# =====================
|
|
14
|
+
|
|
15
|
+
def select_root(roots, z, target):
|
|
16
|
+
"""
|
|
17
|
+
Select the root among Herglotz candidates at a given z closest to a
|
|
18
|
+
given target
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
roots : array_like of complex
|
|
23
|
+
Candidate roots for m at the given z.
|
|
24
|
+
z : complex
|
|
25
|
+
Evaluation point. The Stieltjes/Herglotz branch satisfies
|
|
26
|
+
sign(Im(m)) = sign(Im(z)) away from the real axis.
|
|
27
|
+
target : complex
|
|
28
|
+
Previous continuation value used to enforce continuity, or
|
|
29
|
+
target value.
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
w : complex
|
|
34
|
+
Selected root corresponding to the Stieltjes branch.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
z = complex(z)
|
|
38
|
+
roots = numpy.asarray(roots, dtype=numpy.complex128).ravel()
|
|
39
|
+
|
|
40
|
+
if roots.size == 0:
|
|
41
|
+
raise ValueError("roots must contain at least one candidate root.")
|
|
42
|
+
|
|
43
|
+
desired_sign = numpy.sign(z.imag)
|
|
44
|
+
|
|
45
|
+
# Apply a soft Herglotz sign filter: prefer roots with Im(w) having the
|
|
46
|
+
# same sign as Im(z), allowing tiny numerical violations near the axis.
|
|
47
|
+
imag_roots = numpy.imag(roots)
|
|
48
|
+
|
|
49
|
+
good = roots[numpy.sign(imag_roots) == desired_sign]
|
|
50
|
+
if good.size == 0:
|
|
51
|
+
good = roots[(imag_roots * desired_sign) > -1e-12]
|
|
52
|
+
|
|
53
|
+
candidates = good if good.size > 0 else roots
|
|
54
|
+
idx = int(numpy.argmin(numpy.abs(candidates - target)))
|
|
55
|
+
return candidates[idx]
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# ==============
|
|
59
|
+
# stieltjes poly
|
|
60
|
+
# ==============
|
|
61
|
+
|
|
62
|
+
class StieltjesPoly(object):
|
|
63
|
+
"""
|
|
64
|
+
Stieltjes-branch evaluator for an algebraic equation P(z, m) = 0.
|
|
65
|
+
|
|
66
|
+
This class represents the Stieltjes-branch solution m(z) of an algebraic
|
|
67
|
+
equation defined by a polynomial relation
|
|
68
|
+
|
|
69
|
+
P(z, m) = 0,
|
|
70
|
+
|
|
71
|
+
where P is a polynomial in z and m with monomial-basis coefficients.
|
|
72
|
+
The coefficient matrix ``a`` is fixed at construction time, and all
|
|
73
|
+
quantities depending only on ``a`` are precomputed. Evaluation at a
|
|
74
|
+
complex point ``z`` is performed via :meth:`evaluate`. The instance is
|
|
75
|
+
also callable; :meth:`__call__` supports scalar or vector inputs and
|
|
76
|
+
applies :meth:`evaluate` elementwise.
|
|
77
|
+
|
|
78
|
+
The Stieltjes branch is selected by initializing in the appropriate
|
|
79
|
+
half-plane using an asymptotic Stieltjes estimate and then performing
|
|
80
|
+
homotopy continuation along a straight-line path in the complex plane.
|
|
81
|
+
|
|
82
|
+
Parameters
|
|
83
|
+
----------
|
|
84
|
+
a : ndarray, shape (L, K)
|
|
85
|
+
Coefficient matrix defining P(z, m) in the monomial basis. For fixed
|
|
86
|
+
z, the coefficients of the polynomial in m are assembled from powers
|
|
87
|
+
of z.
|
|
88
|
+
eps : float or None, optional
|
|
89
|
+
If Im(z) == 0, use z + i*eps as the boundary evaluation point.
|
|
90
|
+
If None and Im(z) == 0, eps is set to 1e-8 * max(1, |z|).
|
|
91
|
+
height : float, default = 2.0
|
|
92
|
+
Imaginary height used for the starting point z0 in the same
|
|
93
|
+
half-plane as the evaluation point.
|
|
94
|
+
steps : int, default = 100
|
|
95
|
+
Number of continuation steps along the homotopy path.
|
|
96
|
+
order : int, default = 15
|
|
97
|
+
Number of moments in Stieltjes estimate
|
|
98
|
+
|
|
99
|
+
Methods
|
|
100
|
+
-------
|
|
101
|
+
evaluate(z)
|
|
102
|
+
Evaluate the Stieltjes-branch solution m(z) at a single complex point.
|
|
103
|
+
|
|
104
|
+
__call__(z)
|
|
105
|
+
If ``z`` is scalar, returns ``evaluate(z, ...)``.
|
|
106
|
+
If ``z`` is array-like, returns an array of the same shape, where each
|
|
107
|
+
entry is computed by calling ``evaluate`` on the corresponding element.
|
|
108
|
+
|
|
109
|
+
Notes
|
|
110
|
+
-----
|
|
111
|
+
If an input ``z`` value is real (Im(z) == 0), the evaluation is interpreted
|
|
112
|
+
as a boundary value by replacing that element with z + i*eps. If ``eps`` is
|
|
113
|
+
None, eps is chosen per element as 1e-8 * max(1, |z|).
|
|
114
|
+
"""
|
|
115
|
+
|
|
116
|
+
def __init__(self, a, eps=None, height=2.0, steps=100, order=15):
|
|
117
|
+
a = numpy.asarray(a)
|
|
118
|
+
if a.ndim != 2:
|
|
119
|
+
raise ValueError("a must be a 2D array.")
|
|
120
|
+
|
|
121
|
+
self.a = a
|
|
122
|
+
self.a_l, _ = a.shape
|
|
123
|
+
self.eps = eps
|
|
124
|
+
self.height = height
|
|
125
|
+
self.steps = steps
|
|
126
|
+
self.order = order
|
|
127
|
+
|
|
128
|
+
# Objects depending only on a
|
|
129
|
+
self.mom = AlgebraicStieltjesMoments(a)
|
|
130
|
+
self._zpows_exp = numpy.arange(self.a_l)
|
|
131
|
+
self.rad = 1.0 + self.height * self.mom.radius(self.order)
|
|
132
|
+
|
|
133
|
+
def _poly_coeffs_m(self, z_val):
|
|
134
|
+
z_powers = z_val ** self._zpows_exp
|
|
135
|
+
return (z_powers @ self.a)[::-1]
|
|
136
|
+
|
|
137
|
+
def _poly_roots(self, z_val):
|
|
138
|
+
coeffs = numpy.asarray(self._poly_coeffs_m(z_val),
|
|
139
|
+
dtype=numpy.complex128)
|
|
140
|
+
return numpy.roots(coeffs)
|
|
141
|
+
|
|
142
|
+
def evaluate(self, z, eps=None, height=2.0, steps=100, order=15):
|
|
143
|
+
"""
|
|
144
|
+
Evaluate the Stieltjes-branch solution m(z) at a single point.
|
|
145
|
+
|
|
146
|
+
Parameters are as in the original function, except ``a`` is fixed at
|
|
147
|
+
construction time.
|
|
148
|
+
"""
|
|
149
|
+
z = complex(z)
|
|
150
|
+
|
|
151
|
+
if steps < 1:
|
|
152
|
+
raise ValueError("steps must be a positive integer.")
|
|
153
|
+
|
|
154
|
+
# Boundary-value interpretation on the real axis
|
|
155
|
+
if z.imag == 0.0:
|
|
156
|
+
if self.eps is None:
|
|
157
|
+
eps_loc = 1e-8 * max(1.0, abs(z))
|
|
158
|
+
else:
|
|
159
|
+
eps_loc = float(self.eps)
|
|
160
|
+
z_eval = z + 1j * eps_loc
|
|
161
|
+
else:
|
|
162
|
+
z_eval = z
|
|
163
|
+
|
|
164
|
+
half_sign = numpy.sign(z_eval.imag)
|
|
165
|
+
if half_sign == 0.0:
|
|
166
|
+
half_sign = 1.0
|
|
167
|
+
|
|
168
|
+
# If z is outside radius of convergence, no homotopy
|
|
169
|
+
# necessary
|
|
170
|
+
if numpy.abs(z) > self.rad:
|
|
171
|
+
target = self.mom.stieltjes(z, self.order)
|
|
172
|
+
return select_root(self._poly_roots(z), z, target)
|
|
173
|
+
|
|
174
|
+
z0 = 1j * float(half_sign) * self.rad
|
|
175
|
+
target = self.mom.stieltjes(z0, self.order)
|
|
176
|
+
|
|
177
|
+
# Initialize at z0
|
|
178
|
+
w_prev = select_root(self._poly_roots(z0), z0, target)
|
|
179
|
+
|
|
180
|
+
# Straight-line homotopy continuation
|
|
181
|
+
for tau in numpy.linspace(0.0, 1.0, int(self.steps) + 1)[1:]:
|
|
182
|
+
z_tau = z0 + tau * (z_eval - z0)
|
|
183
|
+
w_prev = select_root(self._poly_roots(z_tau), z_tau, w_prev)
|
|
184
|
+
|
|
185
|
+
return w_prev
|
|
186
|
+
|
|
187
|
+
def __call__(self, z):
|
|
188
|
+
# Scalar fast-path
|
|
189
|
+
if numpy.isscalar(z):
|
|
190
|
+
return self.evaluate(z)
|
|
191
|
+
|
|
192
|
+
# Array-like: evaluate elementwise, preserving shape
|
|
193
|
+
z_arr = numpy.asarray(z)
|
|
194
|
+
out = numpy.empty(z_arr.shape, dtype=numpy.complex128)
|
|
195
|
+
|
|
196
|
+
# Iterate over indices so we can pass Python scalars into evaluate()
|
|
197
|
+
for idx in numpy.ndindex(z_arr.shape):
|
|
198
|
+
out[idx] = self.evaluate(z_arr[idx])
|
|
199
|
+
|
|
200
|
+
return out
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
# def stieltjes_poly(z, a, eps=None, height=2., steps=100, order=15):
|
|
204
|
+
# """
|
|
205
|
+
# Evaluate the Stieltjes-branch solution m(z) of an algebraic equation.
|
|
206
|
+
|
|
207
|
+
# The coefficients `a` define a polynomial relation
|
|
208
|
+
# P(z, m) = 0,
|
|
209
|
+
# where P is a polynomial in z and m with monomial-basis coefficients
|
|
210
|
+
# arranged so that for fixed z, the coefficients of the polynomial in m
|
|
211
|
+
# can be assembled from powers of z.
|
|
212
|
+
|
|
213
|
+
# Parameters
|
|
214
|
+
# ----------
|
|
215
|
+
# z : complex
|
|
216
|
+
# Evaluation point. Must be a single value.
|
|
217
|
+
# a : ndarray, shape (L, K)
|
|
218
|
+
# Coefficient matrix defining P(z, m) in the monomial basis.
|
|
219
|
+
# eps : float or None, optional
|
|
220
|
+
# If Im(z) == 0, use z + i*eps as the boundary evaluation point.
|
|
221
|
+
# If None and Im(z) == 0, eps is set to 1e-8 * max(1, |z|).
|
|
222
|
+
# height : float, default = 2.0
|
|
223
|
+
# Imaginary height used for the starting point z0 in the same
|
|
224
|
+
# half-plane as the evaluation point.
|
|
225
|
+
# steps : int, default = 100
|
|
226
|
+
# Number of continuation steps along the homotopy path.
|
|
227
|
+
# order : int, default = 15
|
|
228
|
+
# Number of moments in Stieltjes estimate
|
|
229
|
+
|
|
230
|
+
# Returns
|
|
231
|
+
# -------
|
|
232
|
+
# w : complex
|
|
233
|
+
# Value of the Stieltjes-branch solution m(z) (or m(z+i*eps) if z is
|
|
234
|
+
# real).
|
|
235
|
+
# """
|
|
236
|
+
|
|
237
|
+
# z = complex(z)
|
|
238
|
+
# a = numpy.asarray(a)
|
|
239
|
+
|
|
240
|
+
# if a.ndim != 2:
|
|
241
|
+
# raise ValueError('a must be a 2D array.')
|
|
242
|
+
|
|
243
|
+
# if steps < 1:
|
|
244
|
+
# raise ValueError("steps must be a positive integer.")
|
|
245
|
+
|
|
246
|
+
# a_l, _ = a.shape
|
|
247
|
+
# mom = AlgebraicStieltjesMoments(a)
|
|
248
|
+
|
|
249
|
+
# def poly_coeffs_m(z_val):
|
|
250
|
+
# z_powers = z_val ** numpy.arange(a_l)
|
|
251
|
+
# return (z_powers @ a)[::-1]
|
|
252
|
+
|
|
253
|
+
# def poly_roots(z_val):
|
|
254
|
+
# coeffs = numpy.asarray(poly_coeffs_m(z_val), dtype=numpy.complex128)
|
|
255
|
+
# return numpy.roots(coeffs)
|
|
256
|
+
|
|
257
|
+
# # If user asked for a real-axis value, interpret as boundary value from C+.
|
|
258
|
+
# if z.imag == 0.0:
|
|
259
|
+
# if eps is None:
|
|
260
|
+
# eps = 1e-8 * max(1.0, abs(z))
|
|
261
|
+
# z_eval = z + 1j * float(eps)
|
|
262
|
+
# else:
|
|
263
|
+
# z_eval = z
|
|
264
|
+
|
|
265
|
+
# half_sign = numpy.sign(z_eval.imag)
|
|
266
|
+
# if half_sign == 0.0:
|
|
267
|
+
# half_sign = 1.0
|
|
268
|
+
|
|
269
|
+
# z0 = 1j * float(half_sign) * (1. + height * mom.radius(order))
|
|
270
|
+
# target = mom.stieltjes(z0, order)
|
|
271
|
+
|
|
272
|
+
# # Initialize at z0 via asymptotic / Im-sign selection.
|
|
273
|
+
# w_prev = select_root(poly_roots(z0), z0, target)
|
|
274
|
+
|
|
275
|
+
# # Straight-line homotopy from z0 to z_eval.
|
|
276
|
+
# for tau in numpy.linspace(0.0, 1.0, int(steps) + 1)[1:]:
|
|
277
|
+
# z_tau = z0 + tau * (z_eval - z0)
|
|
278
|
+
# w_prev = select_root(poly_roots(z_tau), z_tau, w_prev)
|
|
279
|
+
|
|
280
|
+
# return w_prev
|
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
import numpy
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
# =========
|
|
5
|
+
# Moments
|
|
6
|
+
# =========
|
|
7
|
+
|
|
8
|
+
class MomentsESD(object):
|
|
9
|
+
"""
|
|
10
|
+
Moments :math:`\\mu_n(t)` generated from eigenvalues, under
|
|
11
|
+
free decompression, where
|
|
12
|
+
|
|
13
|
+
.. math::
|
|
14
|
+
|
|
15
|
+
m_n = \\mu_n(0) = \\mathbb{E}[\\lambda^n],
|
|
16
|
+
|
|
17
|
+
and :math:`\\lambda` denotes an eigenvalue sample.
|
|
18
|
+
|
|
19
|
+
Parameters
|
|
20
|
+
----------
|
|
21
|
+
|
|
22
|
+
eig : array_like
|
|
23
|
+
1D array of eigenvalues (or samples). Internally it is converted to a
|
|
24
|
+
floating-point :class:`numpy.ndarray`.
|
|
25
|
+
|
|
26
|
+
Attributes
|
|
27
|
+
----------
|
|
28
|
+
|
|
29
|
+
eig : numpy.ndarray
|
|
30
|
+
Eigenvalue samples.
|
|
31
|
+
|
|
32
|
+
Methods
|
|
33
|
+
-------
|
|
34
|
+
|
|
35
|
+
m
|
|
36
|
+
Compute the raw moment :math:`m_n = \\mathbb{E}[\\lambda^n]`.
|
|
37
|
+
|
|
38
|
+
coeffs
|
|
39
|
+
Compute the coefficient vector :math:`a_n`.
|
|
40
|
+
|
|
41
|
+
__call__
|
|
42
|
+
Evaluate :math:`\\mu_n(t)` for a given :math:`n` and :math:`t`.
|
|
43
|
+
|
|
44
|
+
Notes
|
|
45
|
+
-----
|
|
46
|
+
|
|
47
|
+
The recursion memoizes:
|
|
48
|
+
|
|
49
|
+
* Moments ``_m[n] = m_n``.
|
|
50
|
+
* Coefficients ``_a[n] = a_n`` where ``a_n`` has length ``n`` and contains
|
|
51
|
+
:math:`(a_{n,0}, \\dots, a_{n,n-1})`.
|
|
52
|
+
|
|
53
|
+
The coefficient row :math:`a_n` is computed using an intermediate quantity
|
|
54
|
+
:math:`R_{n,k}` formed via discrete convolutions of previous rows.
|
|
55
|
+
|
|
56
|
+
Examples
|
|
57
|
+
--------
|
|
58
|
+
|
|
59
|
+
.. code-block:: python
|
|
60
|
+
|
|
61
|
+
>>> import numpy as np
|
|
62
|
+
>>> eig = np.array([1.0, 2.0, 3.0])
|
|
63
|
+
>>> mu = Moments(eig)
|
|
64
|
+
>>> mu(3, t=0.0) # equals m_3
|
|
65
|
+
12.0
|
|
66
|
+
>>> mu(3, t=0.1)
|
|
67
|
+
14.203...
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
# ====
|
|
71
|
+
# init
|
|
72
|
+
# ====
|
|
73
|
+
|
|
74
|
+
def __init__(self, eig):
|
|
75
|
+
"""
|
|
76
|
+
Initialization.
|
|
77
|
+
"""
|
|
78
|
+
|
|
79
|
+
self.eig = numpy.asarray(eig, dtype=float)
|
|
80
|
+
|
|
81
|
+
# Memoized moments m_n
|
|
82
|
+
self._m = {0: 1.0}
|
|
83
|
+
|
|
84
|
+
# Memoized coefficients a[n] = array of length n
|
|
85
|
+
# (a_{n,0},...,a_{n,n-1})
|
|
86
|
+
self._a = {0: numpy.array([1.0])}
|
|
87
|
+
|
|
88
|
+
# ----------
|
|
89
|
+
# moments
|
|
90
|
+
# ----------
|
|
91
|
+
|
|
92
|
+
def m(self, n):
|
|
93
|
+
"""
|
|
94
|
+
Compute raw moment :math:`m_n`.
|
|
95
|
+
|
|
96
|
+
Parameters
|
|
97
|
+
----------
|
|
98
|
+
|
|
99
|
+
n : int
|
|
100
|
+
Order of the moment.
|
|
101
|
+
|
|
102
|
+
Returns
|
|
103
|
+
-------
|
|
104
|
+
|
|
105
|
+
m_n : float
|
|
106
|
+
The raw moment :math:`m_n = \\mathbb{E}[\\lambda^n]`, estimated by
|
|
107
|
+
the sample mean of ``eig**n``.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
if n not in self._m:
|
|
111
|
+
self._m[n] = numpy.mean(self.eig ** n)
|
|
112
|
+
return self._m[n]
|
|
113
|
+
|
|
114
|
+
# -------------
|
|
115
|
+
# coefficients
|
|
116
|
+
# -------------
|
|
117
|
+
|
|
118
|
+
def coeffs(self, n):
|
|
119
|
+
"""
|
|
120
|
+
Get coefficients :math:`a_n` for :math:`\\mu_n(t)`.
|
|
121
|
+
|
|
122
|
+
Parameters
|
|
123
|
+
----------
|
|
124
|
+
|
|
125
|
+
n : int
|
|
126
|
+
Order of :math:`\\mu_n(t)`.
|
|
127
|
+
|
|
128
|
+
Returns
|
|
129
|
+
-------
|
|
130
|
+
|
|
131
|
+
a_n : numpy.ndarray
|
|
132
|
+
Array of shape ``(n,)`` containing :math:`(a_{n,0}, \\dots, a_{n,n-1})`.
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
if n in self._a:
|
|
136
|
+
return self._a[n]
|
|
137
|
+
|
|
138
|
+
# Ensure previous rows exist
|
|
139
|
+
for r in range(1, n):
|
|
140
|
+
if r not in self._a:
|
|
141
|
+
self._compute_row(r)
|
|
142
|
+
|
|
143
|
+
self._compute_row(n)
|
|
144
|
+
return self._a[n]
|
|
145
|
+
|
|
146
|
+
def _compute_row(self, n):
|
|
147
|
+
"""
|
|
148
|
+
Compute and memoize the coefficient row :math:`a_n`.
|
|
149
|
+
|
|
150
|
+
Parameters
|
|
151
|
+
----------
|
|
152
|
+
|
|
153
|
+
n : int
|
|
154
|
+
Row index to compute.
|
|
155
|
+
|
|
156
|
+
Notes
|
|
157
|
+
-----
|
|
158
|
+
|
|
159
|
+
For :math:`n=1`, the row is
|
|
160
|
+
|
|
161
|
+
.. math::
|
|
162
|
+
|
|
163
|
+
a_{1,0} = m_1.
|
|
164
|
+
|
|
165
|
+
For :math:`n \\ge 2`, let :math:`R_n` be a length ``n-1`` array defined
|
|
166
|
+
by convolution of previous rows:
|
|
167
|
+
|
|
168
|
+
.. math::
|
|
169
|
+
|
|
170
|
+
R_n = \\sum_{i=1}^{n-1} (a_i * a_{n-i})\\big|_{0:(n-2)}.
|
|
171
|
+
|
|
172
|
+
Then for :math:`k = 0, \\dots, n-2`,
|
|
173
|
+
|
|
174
|
+
.. math::
|
|
175
|
+
|
|
176
|
+
a_{n,k} = \\frac{1 + k/2}{(n-1-k)} R_{n,k},
|
|
177
|
+
|
|
178
|
+
and the last coefficient is chosen so that :math:`\\mu_n(0)=m_n`:
|
|
179
|
+
|
|
180
|
+
.. math::
|
|
181
|
+
|
|
182
|
+
a_{n,n-1} = m_n - \\sum_{k=0}^{n-2} a_{n,k}.
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
if n in self._a:
|
|
186
|
+
return
|
|
187
|
+
|
|
188
|
+
if n == 1:
|
|
189
|
+
self._a[1] = numpy.array([self.m(1)])
|
|
190
|
+
return
|
|
191
|
+
|
|
192
|
+
# Ensure all smaller rows exist
|
|
193
|
+
for r in range(1, n):
|
|
194
|
+
if r not in self._a:
|
|
195
|
+
self._compute_row(r)
|
|
196
|
+
|
|
197
|
+
a_n = numpy.zeros(n, dtype=float)
|
|
198
|
+
|
|
199
|
+
# Compute R_{n,k} via convolutions:
|
|
200
|
+
# R_n = sum_{i=1}^{n-1} convolve(a[i], a[n-i]) truncated to length n-1
|
|
201
|
+
R = numpy.zeros(n - 1, dtype=float)
|
|
202
|
+
for i in range(1, n):
|
|
203
|
+
conv = numpy.convolve(self._a[i], self._a[n - i])
|
|
204
|
+
R += conv[: n - 1]
|
|
205
|
+
|
|
206
|
+
k = numpy.arange(n - 1, dtype=float)
|
|
207
|
+
factors = (1.0 + 0.5 * k) / (n - 1 - k)
|
|
208
|
+
a_n[: n - 1] = factors * R
|
|
209
|
+
|
|
210
|
+
# k = n-1 from the initial condition mu_n(0) = m_n
|
|
211
|
+
a_n[n - 1] = self.m(n) - a_n[: n - 1].sum()
|
|
212
|
+
|
|
213
|
+
self._a[n] = a_n
|
|
214
|
+
|
|
215
|
+
# ----------
|
|
216
|
+
# evaluate
|
|
217
|
+
# ----------
|
|
218
|
+
|
|
219
|
+
def __call__(self, n, t=0.0):
|
|
220
|
+
"""
|
|
221
|
+
Evaluate :math:`\\mu_n(t)`.
|
|
222
|
+
|
|
223
|
+
Parameters
|
|
224
|
+
----------
|
|
225
|
+
|
|
226
|
+
n : int
|
|
227
|
+
Order of :math:`\\mu_n(t)`.
|
|
228
|
+
|
|
229
|
+
t : float, default=0.0
|
|
230
|
+
Deformation parameter.
|
|
231
|
+
|
|
232
|
+
Returns
|
|
233
|
+
-------
|
|
234
|
+
|
|
235
|
+
mu_n : float
|
|
236
|
+
The value of :math:`\\mu_n(t)`.
|
|
237
|
+
|
|
238
|
+
Notes
|
|
239
|
+
-----
|
|
240
|
+
|
|
241
|
+
This function evaluates
|
|
242
|
+
|
|
243
|
+
.. math::
|
|
244
|
+
|
|
245
|
+
\\mu_n(t) = \\sum_{k=0}^{n-1} a_{n,k} \\, e^{k t}.
|
|
246
|
+
|
|
247
|
+
For ``n == 0``, it returns ``1.0``.
|
|
248
|
+
"""
|
|
249
|
+
|
|
250
|
+
if n == 0:
|
|
251
|
+
return 1.0
|
|
252
|
+
|
|
253
|
+
a_n = self.coeffs(n)
|
|
254
|
+
k = numpy.arange(n, dtype=float)
|
|
255
|
+
return numpy.dot(a_n, numpy.exp(k * t))
|
|
256
|
+
|
|
257
|
+
# ===========================
|
|
258
|
+
# Algebraic Stieltjes Moments
|
|
259
|
+
# ===========================
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
class AlgebraicStieltjesMoments(object):
|
|
263
|
+
"""
|
|
264
|
+
Given coefficients a[i,j] for P(z,m)=sum_{i,j} a[i,j] z^i m^j,
|
|
265
|
+
compute the large-|z| branch
|
|
266
|
+
m(z) = sum_{k>=0} mu_series[k] / z^{k+1}.
|
|
267
|
+
|
|
268
|
+
Convention here: choose mu0 (the leading coefficient) by solving the
|
|
269
|
+
leading-diagonal equation and (by default) picking the root closest
|
|
270
|
+
to -1, i.e. m(z) ~ -1/z.
|
|
271
|
+
|
|
272
|
+
The returned 'moments(N)' are normalized density moments:
|
|
273
|
+
mu_density[k] = mu_series[k] / mu_series[0]
|
|
274
|
+
so mu_density[0] = 1.
|
|
275
|
+
"""
|
|
276
|
+
|
|
277
|
+
def __init__(self, a, mu0=None):
|
|
278
|
+
self.a = numpy.asarray(a)
|
|
279
|
+
# Ensure valid
|
|
280
|
+
self.a[-1, 0] = 0.0
|
|
281
|
+
if self.a.ndim != 2:
|
|
282
|
+
raise ValueError("a must be a 2D NumPy array with a[i,j]=a_{ij}.")
|
|
283
|
+
|
|
284
|
+
self.I = self.a.shape[0] - 1
|
|
285
|
+
self.J = self.a.shape[1] - 1
|
|
286
|
+
|
|
287
|
+
nz = numpy.argwhere(self.a != 0)
|
|
288
|
+
if nz.size == 0:
|
|
289
|
+
raise ValueError("All coefficients are zero.")
|
|
290
|
+
|
|
291
|
+
# r = max(i-j) over nonzero terms
|
|
292
|
+
self.r = int(numpy.max(nz[:, 0] - nz[:, 1]))
|
|
293
|
+
|
|
294
|
+
# Group coefficients by diagonal offset s = r - (i-j) >= 0
|
|
295
|
+
# diag[s] is list of (j, a_ij) for which i-j = r-s
|
|
296
|
+
self.diag = {}
|
|
297
|
+
for i, j in nz:
|
|
298
|
+
i = int(i)
|
|
299
|
+
j = int(j)
|
|
300
|
+
coeff = self.a[i, j]
|
|
301
|
+
s = self.r - (i - j)
|
|
302
|
+
if s >= 0:
|
|
303
|
+
self.diag.setdefault(int(s), []).append((j, coeff))
|
|
304
|
+
|
|
305
|
+
# Choose mu0 (series leading coefficient). This should be
|
|
306
|
+
# -1 for m(z) ~ -1/z, but it may only hold approximately.
|
|
307
|
+
if mu0 is None:
|
|
308
|
+
self.mu0 = self._solve_mu0()
|
|
309
|
+
else:
|
|
310
|
+
self.mu0 = mu0
|
|
311
|
+
|
|
312
|
+
# Precompute mu0^p up to p=J
|
|
313
|
+
self.mu0pow = [1]
|
|
314
|
+
for _ in range(self.J):
|
|
315
|
+
self.mu0pow.append(self.mu0pow[-1] * self.mu0)
|
|
316
|
+
|
|
317
|
+
# Linear coefficient A0 = sum_{i-j=r} j a_ij mu0^{j-1}
|
|
318
|
+
self.A0 = 0
|
|
319
|
+
for j, coeff in self.diag.get(0, []):
|
|
320
|
+
if j > 0:
|
|
321
|
+
self.A0 += j * coeff * self.mu0pow[j - 1]
|
|
322
|
+
if self.A0 == 0:
|
|
323
|
+
raise ValueError("A0 is zero for this mu0; the sequential recursion is degenerate.")
|
|
324
|
+
|
|
325
|
+
# Stored series moments mu_series[0..]
|
|
326
|
+
self._mu = [self.mu0]
|
|
327
|
+
|
|
328
|
+
# Convolution table c[j][n] = coefficient of w^n in (S(w))^j,
|
|
329
|
+
# where S(w) = sum_{t>=0} mu_series[t] w^t and m(z)=w S(w), w=1/z.
|
|
330
|
+
#
|
|
331
|
+
# We store c as lists growing in n: c[j][n] for j=0..J.
|
|
332
|
+
self._c = [[0] for _ in range(self.J + 1)]
|
|
333
|
+
self._c[0][0] = 1
|
|
334
|
+
for j in range(1, self.J + 1):
|
|
335
|
+
self._c[j][0] = self.mu0pow[j]
|
|
336
|
+
|
|
337
|
+
def _solve_mu0(self):
|
|
338
|
+
# Leading diagonal polynomial L(m) = sum_{i-j=r} a_ij m^j.
|
|
339
|
+
# That means i = j + r, so coefficient is a[j+r, j] if in bounds.
|
|
340
|
+
coeffs = numpy.zeros(self.J + 1, dtype=numpy.complex128)
|
|
341
|
+
for j in range(self.J + 1):
|
|
342
|
+
i = j + self.r
|
|
343
|
+
if 0 <= i <= self.I:
|
|
344
|
+
coeffs[j] = self.a[i, j]
|
|
345
|
+
|
|
346
|
+
if not numpy.any(coeffs != 0):
|
|
347
|
+
raise ValueError("Leading diagonal polynomial is identically zero; cannot determine mu0.")
|
|
348
|
+
|
|
349
|
+
deg = int(numpy.max(numpy.nonzero(coeffs)[0]))
|
|
350
|
+
roots = numpy.roots(coeffs[:deg + 1][::-1]) # descending powers for numpy.roots
|
|
351
|
+
|
|
352
|
+
# Targetting mu0 = -1 for ~ -1/z asymptotics
|
|
353
|
+
mu0 = roots[numpy.argmin(numpy.abs(roots + 1))]
|
|
354
|
+
|
|
355
|
+
if abs(mu0.imag) < 1e-12:
|
|
356
|
+
mu0 = mu0.real
|
|
357
|
+
return mu0
|
|
358
|
+
|
|
359
|
+
def _ensure(self, N):
|
|
360
|
+
# Compute mu_series up to index N (inclusive)
|
|
361
|
+
while len(self._mu) <= N:
|
|
362
|
+
k = len(self._mu) # compute mu_k
|
|
363
|
+
|
|
364
|
+
# Compute f[j] = coefficient of w^k in (S_trunc(w))^j,
|
|
365
|
+
# where S_trunc uses mu_0..mu_{k-1} only (i.e. mu_k treated as 0).
|
|
366
|
+
# Key fact: in the true c[j,k], mu_k can only appear linearly as j*mu_k*mu0^{j-1}.
|
|
367
|
+
f = [0] * (self.J + 1)
|
|
368
|
+
f[0] = 0
|
|
369
|
+
for j in range(1, self.J + 1):
|
|
370
|
+
ssum = 0
|
|
371
|
+
# sum_{t=1..k-1} mu_t * c[j-1, k-t]
|
|
372
|
+
for t in range(1, k):
|
|
373
|
+
ssum += self._mu[t] * self._c[j - 1][k - t]
|
|
374
|
+
# recurrence: c[j,k] = mu0*c[j-1,k] + sum_{t=1..k-1} mu_t*c[j-1,k-t] + mu_k*c[j-1,0]
|
|
375
|
+
# with mu_k=0 for f, and c[j-1,k]=f[j-1]
|
|
376
|
+
f[j] = self.mu0 * f[j - 1] + ssum
|
|
377
|
+
|
|
378
|
+
# Build the linear equation for mu_k:
|
|
379
|
+
# A0*mu_k + rest = 0
|
|
380
|
+
rest = 0
|
|
381
|
+
|
|
382
|
+
# s=0 diagonal contributes coeff*(f[j]) (the mu_k-free part)
|
|
383
|
+
for j, coeff in self.diag.get(0, []):
|
|
384
|
+
if j == 0:
|
|
385
|
+
# only affects k=0, but we never come here with k=0
|
|
386
|
+
continue
|
|
387
|
+
rest += coeff * f[j]
|
|
388
|
+
|
|
389
|
+
# lower diagonals s=1..k contribute coeff*c[j,k-s] (already known since k-s < k)
|
|
390
|
+
for s in range(1, k + 1):
|
|
391
|
+
entries = self.diag.get(s)
|
|
392
|
+
if not entries:
|
|
393
|
+
continue
|
|
394
|
+
n = k - s
|
|
395
|
+
for j, coeff in entries:
|
|
396
|
+
if j == 0:
|
|
397
|
+
if n == 0:
|
|
398
|
+
rest += coeff
|
|
399
|
+
else:
|
|
400
|
+
rest += coeff * self._c[j][n]
|
|
401
|
+
|
|
402
|
+
mu_k = -rest / self.A0
|
|
403
|
+
self._mu.append(mu_k)
|
|
404
|
+
|
|
405
|
+
# Now append the new column k to c using the full convolution recurrence:
|
|
406
|
+
# c[j,k] = sum_{t=0..k} mu_t * c[j-1,k-t]
|
|
407
|
+
for j in range(self.J + 1):
|
|
408
|
+
self._c[j].append(0)
|
|
409
|
+
|
|
410
|
+
self._c[0][k] = 0
|
|
411
|
+
for j in range(1, self.J + 1):
|
|
412
|
+
val = 0
|
|
413
|
+
for t in range(0, k + 1):
|
|
414
|
+
val += self._mu[t] * self._c[j - 1][k - t]
|
|
415
|
+
self._c[j][k] = val
|
|
416
|
+
|
|
417
|
+
# --- API ---
|
|
418
|
+
|
|
419
|
+
def __call__(self, k):
|
|
420
|
+
self._ensure(k)
|
|
421
|
+
return self._mu[k] / self._mu[0]
|
|
422
|
+
|
|
423
|
+
def moments(self, N):
|
|
424
|
+
# normalized density moments so moment 0 is 1
|
|
425
|
+
self._ensure(N)
|
|
426
|
+
mu0 = self._mu[0]
|
|
427
|
+
return numpy.array([self._mu[k] / mu0 for k in range(N + 1)])
|
|
428
|
+
|
|
429
|
+
def radius(self, N):
|
|
430
|
+
# Estimate the radius of convergence of the Stieltjes
|
|
431
|
+
# series
|
|
432
|
+
if N < 3:
|
|
433
|
+
raise RuntimeError("Order is too small, choose a larger value of N")
|
|
434
|
+
self._ensure(N)
|
|
435
|
+
return max([numpy.abs(self._mu[j] / self._mu[j-1]) for j in range(2,N+1)])
|
|
436
|
+
|
|
437
|
+
def stieltjes(self, z, N):
|
|
438
|
+
# Estimate Stieltjes transform (root) using moment
|
|
439
|
+
# expansion
|
|
440
|
+
z = numpy.asarray(z)
|
|
441
|
+
mu = self.moments(N)
|
|
442
|
+
return -numpy.sum(z[..., numpy.newaxis]**(-numpy.arange(N+1)-1) * mu,
|
|
443
|
+
axis=-1)
|
|
444
|
+
|
|
445
|
+
def target_pt(self, N=15):
|
|
446
|
+
# Obtain an estimate of the Stieltjes transform at a
|
|
447
|
+
# single point z where the estimate is likely reliable
|
|
448
|
+
z = 1j + 2j * self.radius(N)
|
|
449
|
+
return z, self.stieltjes(z, N)
|
|
450
|
+
|
|
@@ -20,8 +20,9 @@ from ._continuation_algebraic import sample_z_joukowski, \
|
|
|
20
20
|
from ._edge import evolve_edges, merge_edges
|
|
21
21
|
from ._decompress import decompress_newton
|
|
22
22
|
from ._decompress2 import decompress_coeffs
|
|
23
|
-
from ._homotopy import
|
|
23
|
+
from ._homotopy import StieltjesPoly
|
|
24
24
|
from ._discriminant import compute_singular_points
|
|
25
|
+
from ._moments import MomentsESD
|
|
25
26
|
from .._free_form._support import supp
|
|
26
27
|
from .._free_form._plot_util import plot_density
|
|
27
28
|
|
|
@@ -144,6 +145,7 @@ class AlgebraicForm(object):
|
|
|
144
145
|
self.A = None
|
|
145
146
|
self.eig = None
|
|
146
147
|
self.stieltjes = None
|
|
148
|
+
self.moments = None
|
|
147
149
|
self.support = support
|
|
148
150
|
self.delta = delta # Offset above real axis to apply Plemelj formula
|
|
149
151
|
|
|
@@ -178,6 +180,7 @@ class AlgebraicForm(object):
|
|
|
178
180
|
# Use empirical Stieltjes function
|
|
179
181
|
self.stieltjes = lambda z: \
|
|
180
182
|
numpy.mean(1.0/(self.eig-z[:, numpy.newaxis]), axis=-1)
|
|
183
|
+
self.moments = MomentsESD(self.eig)
|
|
181
184
|
|
|
182
185
|
# Support
|
|
183
186
|
if support is None:
|
|
@@ -291,6 +294,7 @@ class AlgebraicForm(object):
|
|
|
291
294
|
status['res_99_9'] = float(res_99_9)
|
|
292
295
|
status['fit_metrics'] = fit_metrics
|
|
293
296
|
self.status = status
|
|
297
|
+
self.stieltjes = StieltjesPoly(self.a_coeffs)
|
|
294
298
|
|
|
295
299
|
if verbose:
|
|
296
300
|
print(f'fit residual max : {res_max:>0.4e}')
|
|
@@ -395,13 +399,7 @@ class AlgebraicForm(object):
|
|
|
395
399
|
x = self._generate_grid(1.25)
|
|
396
400
|
|
|
397
401
|
# Preallocate density to zero
|
|
398
|
-
rho =
|
|
399
|
-
|
|
400
|
-
for idx, x_i in enumerate(x):
|
|
401
|
-
m_i = stieltjes_poly(x_i, self.a_coeffs)
|
|
402
|
-
rho[idx] = m_i.imag
|
|
403
|
-
|
|
404
|
-
rho = rho / numpy.pi
|
|
402
|
+
rho = self.stieltjes(x).imag / numpy.pi
|
|
405
403
|
|
|
406
404
|
# if self.method == 'jacobi':
|
|
407
405
|
# rho[mask] = jacobi_density(x[mask], self.psi, self.support,
|
|
@@ -669,12 +667,9 @@ class AlgebraicForm(object):
|
|
|
669
667
|
# Decompression ratio equal to e^{t}.
|
|
670
668
|
alpha = numpy.atleast_1d(size) / self.n
|
|
671
669
|
|
|
672
|
-
def m_fn(z):
|
|
673
|
-
return stieltjes_poly(z, self.a_coeffs)
|
|
674
|
-
|
|
675
670
|
# Lower and upper bound on new support
|
|
676
|
-
hilb_lb = (1.0 /
|
|
677
|
-
hilb_ub = (1.0 /
|
|
671
|
+
hilb_lb = (1.0 / self.stieltjes(self.lam_m + self.delta * 1j).item()).real
|
|
672
|
+
hilb_ub = (1.0 / self.stieltjes(self.lam_p + self.delta * 1j).item()).real
|
|
678
673
|
lb = self.lam_m - (numpy.max(alpha) - 1) * hilb_lb
|
|
679
674
|
ub = self.lam_p - (numpy.max(alpha) - 1) * hilb_ub
|
|
680
675
|
|
|
@@ -695,9 +690,7 @@ class AlgebraicForm(object):
|
|
|
695
690
|
z_query = x + 1j * self.delta
|
|
696
691
|
|
|
697
692
|
# Initial condition at t=0 (physical branch)
|
|
698
|
-
|
|
699
|
-
stieltjes = numpy.vectorize(m_fn)
|
|
700
|
-
w0_list = stieltjes(z_query)
|
|
693
|
+
w0_list = self.stieltjes(z_query)
|
|
701
694
|
|
|
702
695
|
# Times
|
|
703
696
|
t = numpy.log(alpha)
|
|
@@ -727,9 +720,8 @@ class AlgebraicForm(object):
|
|
|
727
720
|
for i in range(alpha.size):
|
|
728
721
|
coeffs_i = decompress_coeffs(self.a_coeffs,
|
|
729
722
|
numpy.log(alpha[i]))
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
rho[i, j] = m_j.imag
|
|
723
|
+
stieltjes_i = StieltjesPoly(coeffs_i)
|
|
724
|
+
rho[i, :] = stieltjes_i.imag
|
|
733
725
|
|
|
734
726
|
rho = rho / numpy.pi
|
|
735
727
|
|
|
@@ -24,6 +24,7 @@ freealg/_algebraic_form/_decompress2.py
|
|
|
24
24
|
freealg/_algebraic_form/_discriminant.py
|
|
25
25
|
freealg/_algebraic_form/_edge.py
|
|
26
26
|
freealg/_algebraic_form/_homotopy.py
|
|
27
|
+
freealg/_algebraic_form/_moments.py
|
|
27
28
|
freealg/_algebraic_form/_sheets_util.py
|
|
28
29
|
freealg/_algebraic_form/algebraic_form.py
|
|
29
30
|
freealg/_free_form/__init__.py
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.7.7"
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
# =======
|
|
2
|
-
# Imports
|
|
3
|
-
# =======
|
|
4
|
-
|
|
5
|
-
import numpy
|
|
6
|
-
|
|
7
|
-
__all__ = ['stieltjes_poly']
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
# =====================
|
|
11
|
-
# stieltjes select root
|
|
12
|
-
# =====================
|
|
13
|
-
|
|
14
|
-
def stieltjes_select_root(roots, z, w_prev=None):
|
|
15
|
-
"""
|
|
16
|
-
Select the Stieltjes-branch root among candidates at a given z.
|
|
17
|
-
|
|
18
|
-
Parameters
|
|
19
|
-
----------
|
|
20
|
-
roots : array_like of complex
|
|
21
|
-
Candidate roots for m at the given z.
|
|
22
|
-
z : complex
|
|
23
|
-
Evaluation point. The Stieltjes/Herglotz branch satisfies
|
|
24
|
-
sign(Im(m)) = sign(Im(z)) away from the real axis.
|
|
25
|
-
w_prev : complex or None, optional
|
|
26
|
-
Previous continuation value used to enforce continuity. If None,
|
|
27
|
-
the asymptotic target -1/z is used.
|
|
28
|
-
|
|
29
|
-
Returns
|
|
30
|
-
-------
|
|
31
|
-
w : complex
|
|
32
|
-
Selected root corresponding to the Stieltjes branch.
|
|
33
|
-
"""
|
|
34
|
-
|
|
35
|
-
z = complex(z)
|
|
36
|
-
roots = numpy.asarray(roots, dtype=numpy.complex128).ravel()
|
|
37
|
-
|
|
38
|
-
if roots.size == 0:
|
|
39
|
-
raise ValueError("roots must contain at least one candidate root.")
|
|
40
|
-
|
|
41
|
-
desired_sign = numpy.sign(z.imag)
|
|
42
|
-
|
|
43
|
-
if w_prev is None:
|
|
44
|
-
target = -1.0 / z
|
|
45
|
-
else:
|
|
46
|
-
target = complex(w_prev)
|
|
47
|
-
|
|
48
|
-
# Apply a soft Herglotz sign filter: prefer roots with Im(w) having the
|
|
49
|
-
# same sign as Im(z), allowing tiny numerical violations near the axis.
|
|
50
|
-
imag_roots = numpy.imag(roots)
|
|
51
|
-
|
|
52
|
-
good = roots[numpy.sign(imag_roots) == desired_sign]
|
|
53
|
-
if good.size == 0:
|
|
54
|
-
good = roots[(imag_roots * desired_sign) > -1e-12]
|
|
55
|
-
|
|
56
|
-
candidates = good if good.size > 0 else roots
|
|
57
|
-
idx = int(numpy.argmin(numpy.abs(candidates - target)))
|
|
58
|
-
return candidates[idx]
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
# ==============
|
|
62
|
-
# stieltjes poly
|
|
63
|
-
# ==============
|
|
64
|
-
|
|
65
|
-
def stieltjes_poly(z, a, eps=None, height=1e+4, steps=100):
|
|
66
|
-
"""
|
|
67
|
-
Evaluate the Stieltjes-branch solution m(z) of an algebraic equation.
|
|
68
|
-
|
|
69
|
-
The coefficients `a` define a polynomial relation
|
|
70
|
-
P(z, m) = 0,
|
|
71
|
-
where P is a polynomial in z and m with monomial-basis coefficients
|
|
72
|
-
arranged so that for fixed z, the coefficients of the polynomial in m
|
|
73
|
-
can be assembled from powers of z.
|
|
74
|
-
|
|
75
|
-
Parameters
|
|
76
|
-
----------
|
|
77
|
-
z : complex
|
|
78
|
-
Evaluation point. Must be a single value.
|
|
79
|
-
a : ndarray, shape (L, K)
|
|
80
|
-
Coefficient matrix defining P(z, m) in the monomial basis.
|
|
81
|
-
eps : float or None, optional
|
|
82
|
-
If Im(z) == 0, use z + i*eps as the boundary evaluation point.
|
|
83
|
-
If None and Im(z) == 0, eps is set to 1e-8 * max(1, |z|).
|
|
84
|
-
height : float, optional
|
|
85
|
-
Imaginary height used for the starting point z0 in the same
|
|
86
|
-
half-plane as the evaluation point.
|
|
87
|
-
steps : int, optional
|
|
88
|
-
Number of continuation steps along the homotopy path.
|
|
89
|
-
|
|
90
|
-
Returns
|
|
91
|
-
-------
|
|
92
|
-
w : complex
|
|
93
|
-
Value of the Stieltjes-branch solution m(z) (or m(z+i*eps) if z is
|
|
94
|
-
real).
|
|
95
|
-
"""
|
|
96
|
-
|
|
97
|
-
z = complex(z)
|
|
98
|
-
a = numpy.asarray(a)
|
|
99
|
-
|
|
100
|
-
if a.ndim != 2:
|
|
101
|
-
raise ValueError('a must be a 2D array.')
|
|
102
|
-
|
|
103
|
-
if steps < 1:
|
|
104
|
-
raise ValueError("steps must be a positive integer.")
|
|
105
|
-
|
|
106
|
-
a_l, _ = a.shape
|
|
107
|
-
|
|
108
|
-
def poly_coeffs_m(z_val):
|
|
109
|
-
z_powers = z_val ** numpy.arange(a_l)
|
|
110
|
-
return (z_powers @ a)[::-1]
|
|
111
|
-
|
|
112
|
-
def poly_roots(z_val):
|
|
113
|
-
coeffs = numpy.asarray(poly_coeffs_m(z_val), dtype=numpy.complex128)
|
|
114
|
-
return numpy.roots(coeffs)
|
|
115
|
-
|
|
116
|
-
# If user asked for a real-axis value, interpret as boundary value from C+.
|
|
117
|
-
if z.imag == 0.0:
|
|
118
|
-
if eps is None:
|
|
119
|
-
eps = 1e-8 * max(1.0, abs(z))
|
|
120
|
-
z_eval = z + 1j * float(eps)
|
|
121
|
-
else:
|
|
122
|
-
z_eval = z
|
|
123
|
-
|
|
124
|
-
half_sign = numpy.sign(z_eval.imag)
|
|
125
|
-
if half_sign == 0.0:
|
|
126
|
-
half_sign = 1.0
|
|
127
|
-
|
|
128
|
-
z0 = 1j * float(half_sign) * float(height)
|
|
129
|
-
|
|
130
|
-
# Initialize at z0 via asymptotic / Im-sign selection.
|
|
131
|
-
w_prev = stieltjes_select_root(poly_roots(z0), z0, w_prev=None)
|
|
132
|
-
|
|
133
|
-
# Straight-line homotopy from z0 to z_eval.
|
|
134
|
-
for tau in numpy.linspace(0.0, 1.0, int(steps) + 1)[1:]:
|
|
135
|
-
z_tau = z0 + tau * (z_eval - z0)
|
|
136
|
-
w_prev = stieltjes_select_root(poly_roots(z_tau), z_tau, w_prev=w_prev)
|
|
137
|
-
|
|
138
|
-
return w_prev
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|