File size: 2,030 Bytes
4aa7bc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""Quantitative extremal-eigenspace inheritance and theta examples."""
from __future__ import annotations
import numpy as np
from scipy.linalg import eigh


def inherited_gap_bound(lam: float, gap: float, residue: np.ndarray, path_gram: np.ndarray) -> dict:
    if not np.isfinite(lam) or not np.isfinite(gap) or lam < 0 or gap <= 0:
        raise ValueError('A nonnegative threshold and positive next gap are required.')
    R=np.asarray(residue,dtype=float); S=np.asarray(path_gram,dtype=float)
    if R.ndim != 2 or R.shape[0] != R.shape[1] or S.shape != R.shape:
        raise ValueError('Compatible square residue and path-Gram matrices are required.')
    if not np.all(np.isfinite(R)) or not np.all(np.isfinite(S)):
        raise ValueError('Matrices must have finite entries.')
    if R.shape == (0,0):
        return dict(rho=0.0, gap_lower_bound=0.0)
    if not np.allclose(R,R.T) or not np.allclose(S,S.T):
        raise ValueError('Matrices must be symmetric.')
    if np.linalg.eigvalsh(S)[0] <= 0:
        raise ValueError('The path Gram matrix must be positive definite.')
    if np.linalg.eigvalsh(R)[0] < -1e-10:
        raise ValueError('The residue must be positive semidefinite.')
    rho=max(0.0,float(eigh(R,S,eigvals_only=True)[-1]))
    return dict(rho=rho, gap_lower_bound=gap*rho/(lam+gap+rho))


def theta_bound(lengths, k: int) -> dict:
    a,b,c=map(float,lengths)
    if not all(np.isfinite(x) for x in (a,b,c)) or min(a,b,c)<=0 or not isinstance(k,int) or k<2:
        raise ValueError('Positive lengths and k>=2 are required.')
    L=a+b+c; mu=np.pi*(k-1)/L; lam=mu*mu
    # Opened interval: new leaf -- b -- V -- a -- U -- c -- new leaf.
    mismatch=np.sqrt(2/L)*np.array([1-np.cos(mu*(a+b)), np.cos(mu*L)-np.cos(mu*b)])
    R=np.outer(mismatch,mismatch)
    S=np.array([[a+b,-a],[-a,a+c]])
    g0=np.pi**2*(2*k-1)/L**2
    result=inherited_gap_bound(lam,g0,R,S)
    result.update(threshold=lam, next_tree_gap=g0, residue=R.tolist(), path_gram=S.tolist(), k=k, lengths=[a,b,c])
    return result