Fix B7 rank and B6 HKS gates behind existing B1 recovery
This commit is contained in:
1 parent
bccf1fee9f
commit
086c56dc36
27 files changed
+61726
-46
No files matched your search
@@ -0,0 +1,64 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from scipy.linalg import expm
|
||||
from phase2_b6_math import assemble,hks,truncate,tsqr_update,partial_from_triangular
|
||||
|
||||
TET=np.array([[0.,0,0],[1,0,0],[0,1,0],[0,0,1]])
|
||||
|
||||
class Acceptance(unittest.TestCase):
|
||||
def test_exact_stiffness_mass_disconnected(self):
|
||||
K,m,_,nc=assemble(TET,[[0,1,2,3]])
|
||||
expected=np.array([[3,-1,-1,-1],[-1,1,0,0],[-1,0,1,0],[-1,0,0,1]])/6
|
||||
np.testing.assert_allclose(K.toarray(),expected,atol=1e-15);np.testing.assert_allclose(m,np.full(4,1/24))
|
||||
x=np.vstack([TET,TET+[3,0,0],TET+[6,0,0]])
|
||||
H,r=hks(x,np.arange(12).reshape(3,4))
|
||||
self.assertEqual(r['zero_modes'],3);self.assertEqual(r['positive_modes_retained'],9)
|
||||
self.assertEqual(r['next_eigenvalue'],None)
|
||||
np.testing.assert_allclose(H[:4],H[4:8],rtol=1e-12)
|
||||
np.testing.assert_allclose(H[:4],H[8:],rtol=1e-12)
|
||||
self.assertTrue(np.all(H>=6))
|
||||
def test_node_permutation(self):
|
||||
original,_=hks(TET,[[0,1,2,3]])
|
||||
p=np.array([2,0,3,1]);inverse=np.argsort(p)
|
||||
reordered,_=hks(TET[p],inverse[np.array([[0,1,2,3]])])
|
||||
np.testing.assert_allclose(reordered[inverse],original,rtol=1e-11,atol=1e-12)
|
||||
def test_cluster_boundary(self):
|
||||
vals=np.array([1,2,3,4,5,6,7,8,8+1e-7,9])
|
||||
self.assertEqual(truncate(vals,False),9)
|
||||
with self.assertRaises(ValueError):truncate(vals[:9],False)
|
||||
self.assertEqual(truncate([1,2,3],True),3)
|
||||
def test_exact_and_near_rotations(self):
|
||||
rng=np.random.default_rng(6);phi=rng.normal(size=(9,2));Q=np.linalg.qr(rng.normal(size=(2,2)))[0]
|
||||
direct=lambda P,B:np.einsum('ij,jk,ik->i',P,expm(-.7*B),P)
|
||||
for vals in [[2,2],[2,2.000001]]:
|
||||
B=np.diag(vals);a=direct(phi,B);b=direct(phi@Q,Q.T@B@Q)
|
||||
np.testing.assert_allclose(a,b,rtol=1e-13,atol=1e-13)
|
||||
np.testing.assert_allclose(a,direct(phi*np.array([-1,1]),B),rtol=1e-13)
|
||||
self.assertGreater(np.max(np.abs(direct(phi@Q,B)-direct(phi,B))),1e-9)
|
||||
def test_sparse_projected_solver(self):
|
||||
n=7;x=np.array([(i,j,k) for i in range(n) for j in range(n) for k in range(n)],float)
|
||||
ix=lambda i,j,k:(i*n+j)*n+k
|
||||
tet=[]
|
||||
for i in range(n-1):
|
||||
for j in range(n-1):
|
||||
for k in range(n-1):
|
||||
corners=[ix(i,j,k),ix(i+1,j,k),ix(i+1,j+1,k),ix(i,j+1,k),ix(i,j,k+1),ix(i+1,j,k+1),ix(i+1,j+1,k+1),ix(i,j+1,k+1)]
|
||||
tet.extend([[corners[a] for a in t] for t in [(0,1,2,6),(0,2,3,6),(0,3,7,6),(0,7,4,6),(0,4,5,6),(0,5,1,6)]])
|
||||
H,r=hks(x,np.array(tet));self.assertEqual(H.shape,(343,3));self.assertFalse(r['spectrum_exhausted'])
|
||||
self.assertGreater(r['next_eigenvalue'],r['positive_values'][-1])
|
||||
def test_degenerate_and_mass_failure(self):
|
||||
with self.assertRaises(ValueError):assemble(TET,[[0,1,2,2]])
|
||||
with self.assertRaises(ValueError):assemble(np.vstack([TET,[8,9,10]]),[[0,1,2,3]])
|
||||
def test_weighted_partial_qr(self):
|
||||
rng=np.random.default_rng(7);x=rng.normal(size=(200,19));z=rng.normal(size=200)
|
||||
y=3*x[:,0]+z;H=np.column_stack([2*x[:,1]+z,np.ones(200),rng.normal(size=200)])
|
||||
w=np.r_[np.full(50,.5/50),np.full(150,.5/150)]
|
||||
block=np.column_stack([np.ones(200),x,y,H])*np.sqrt(w[:,None]);R=None
|
||||
for chunk in np.array_split(block,7):R=tsqr_update(R,chunk)
|
||||
actual=partial_from_triangular(R)
|
||||
self.assertAlmostEqual(actual['signed_r'][0],1.,places=12);self.assertIsNone(actual['signed_r'][1])
|
||||
residual=block[:,20:]-block[:,:20]@np.linalg.lstsq(block[:,:20],block[:,20:],rcond=None)[0]
|
||||
expected=np.dot(residual[:,0],residual[:,3])/np.linalg.norm(residual[:,0])/np.linalg.norm(residual[:,3])
|
||||
self.assertAlmostEqual(actual['signed_r'][2],expected,places=12)
|
||||
|
||||
if __name__=='__main__':unittest.main()
|
||||
Reference in new issue
Block a user