Download src/geometry_checks.py from PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6: direct link, hf CLI and curl.
- Browser
- Download file 7.08 kB
-
https://huggingface.co/datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/resolve/main/src/geometry_checks.py
- Command line
-
hf download hf://datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/src/geometry_checks.py
-
curl -L -o geometry_checks.py https://huggingface.co/datasets/PureOne/VSCE-Vacuum-State-Cognitive-Engineering-v6/resolve/main/src/geometry_checks.py
7.08 kB
| """Independent matrix, quadrature, construction, and counterexample checks.""" | |
| from pathlib import Path | |
| import json | |
| import unittest | |
| import numpy as np | |
| from scipy.linalg import expm | |
| from scipy.integrate import quad | |
| from geometry import (shape_basis,graph_laplacian,symmetric_inverse_drift, | |
| positive_graph_target,stationary_covariance,weighted_action_split) | |
| class GeometryChecks(unittest.TestCase): | |
| def setUp(self): self.rng=np.random.default_rng(8137) | |
| def test_radial_metric_against_direct_polar_quadrature(self): | |
| # chi=1 inside r=1, cubic smoothstep on 1..2; C1 is enough for norm. | |
| def chi(s): | |
| if s<=1:return 1.,0. | |
| if s>=2:return 0.,0. | |
| u=s-1 | |
| return 1-3*u*u+2*u**3,-6*u+6*u*u | |
| def integrand(s): | |
| c,cp=chi(s) | |
| return np.pi*s*((c+s*cp)**2+c*c) | |
| direct=quad(integrand,0,2,points=[1],epsabs=1e-11)[0] | |
| compact=np.pi*quad(lambda s:s**3*chi(s)[1]**2,1,2,epsabs=1e-11)[0] | |
| self.assertAlmostEqual(direct,compact,places=10) | |
| def test_weighted_action_pair_identity_and_translation(self): | |
| w=np.array([.7,2,4,1.2]);v=self.rng.normal(size=(4,2)) | |
| out=weighted_action_split(v,w) | |
| pairs=sum(w[i]*w[j]*np.sum((v[i]-v[j])**2) | |
| for i in range(4) for j in range(i+1,4))/w.sum() | |
| self.assertAlmostEqual(out['shape_density'],pairs,places=12) | |
| self.assertAlmostEqual(out['lab_density'],out['shape_density']+out['translation_density'],places=12) | |
| shifted=weighted_action_split(v+np.array([9,-4]),w) | |
| self.assertAlmostEqual(shifted['shape_density'],pairs,places=11) | |
| def test_weighted_permutation_endpoint_correction(self): | |
| w=np.array([1.,3.]);a0=np.array([-1.,1.]);a1=-a0 | |
| delta=(w@a1-w@a0)/w.sum() | |
| self.assertEqual(w.sum()*delta**2,4.) | |
| # Nonlinear centroid path is projected to a straight centroid; | |
| # independent quadrature recovers the endpoint lower bound. | |
| dt=quad(lambda t:(delta+2*np.pi*.3*np.cos(2*np.pi*t))**2,0,1)[0] | |
| self.assertGreater(w.sum()*dt,4.) | |
| def test_inverse_lyapunov_with_nonconservative_skew(self): | |
| R=self.rng.normal(size=(4,4));S=R@R.T+np.eye(4) | |
| Q=np.diag([.6,1,1.7,2]);J=self.rng.normal(size=(4,4));J=J-J.T | |
| A=(Q/2+J)@np.linalg.inv(S) | |
| self.assertGreater(np.min(np.linalg.eigvals(A).real),0) | |
| self.assertLess(np.linalg.norm(A@S+S@A.T-Q),2e-13) | |
| self.assertLess(np.linalg.norm(stationary_covariance(A,Q)-S),1e-12) | |
| def test_graph_target_recovery_and_forbidden_target(self): | |
| w=np.array([1.,2.,.7,3.]);U=shape_basis(w) | |
| edges=[(0,1),(1,2),(2,3)];g=np.array([.6,1.2,.9]) | |
| L=graph_laplacian(4,edges,g);R=U/np.sqrt(w)[:,None] | |
| A=.8*np.eye(3)+R.T@L@R;Q=np.diag([1.,1.4,2.]) | |
| S=stationary_covariance(A,Q) | |
| out=positive_graph_target(S,Q,w,.8,edges) | |
| self.assertTrue(out['feasible_at_tolerance']) | |
| self.assertLess(np.linalg.norm(out['lift']-L),2e-13) | |
| v=np.array([1,1,-2.]);U=shape_basis(np.ones(3)) | |
| A=np.eye(2)+U.T@np.outer(v,v)@U | |
| bad=positive_graph_target(np.linalg.inv(A),2*np.eye(2),np.ones(3),1) | |
| self.assertFalse(bad['feasible_at_tolerance']) | |
| self.assertAlmostEqual(bad['lift'][0,1],1.,places=12) | |
| def test_anisotropic_noise_breaks_covariance_monotonicity(self): | |
| Q=np.diag([1,.1]);S0=stationary_covariance(np.eye(2),Q) | |
| S1=stationary_covariance(np.eye(2)+np.ones((2,2)),Q) | |
| self.assertGreater(S1[1,1],S0[1,1]) | |
| expected=np.array([[71,-22],[-22,17]])/240 | |
| self.assertLess(np.linalg.norm(S1-expected),1e-14) | |
| def test_complete_graph_minimax_and_schur_boundary(self): | |
| for n in (2,3,5,8): | |
| edges=[(i,j) for i in range(n) for j in range(i+1,n)] | |
| k,G,q0=1.3,2.4,.6;U=shape_basis(np.ones(n)) | |
| L=graph_laplacian(n,edges,np.full(len(edges),G/len(edges))) | |
| A=k*np.eye(n-1)+U.T@L@U;S=q0/2*np.linalg.inv(A) | |
| exact=q0/(k+2*G/(n-1)) | |
| for i,j in edges: | |
| r=U.T@(np.eye(n)[i]-np.eye(n)[j]) | |
| self.assertAlmostEqual(float(r@S@r),exact,places=12) | |
| block=np.block([[A,r[:,None]],[r[None,:],np.array([[2*exact/q0]])]]) | |
| self.assertGreaterEqual(np.linalg.eigvalsh(block).min(),-1e-12) | |
| for _ in range(20): | |
| L=graph_laplacian(n,edges,G*self.rng.dirichlet(np.ones(len(edges)))) | |
| S=q0/2*np.linalg.inv(k*np.eye(n)+L) | |
| worst=max(float((np.eye(n)[i]-np.eye(n)[j])@S@(np.eye(n)[i]-np.eye(n)[j])) for i,j in edges) | |
| self.assertGreaterEqual(worst,exact-1e-12) | |
| def test_general_martingale_gaussian_moment(self): | |
| A=np.array([[1.,2.],[-.3,1.4]]);Q=np.diag([.7,1.2]) | |
| S=stationary_covariance(A,Q);R=np.linalg.inv(S) | |
| x0=np.array([.2,-.1]) | |
| for t in (.02,.4,2.): | |
| F=expm(-A*t);St=S-F@S@F.T;m=F@x0 | |
| _,logdet=np.linalg.slogdet(np.eye(2)-St@R) | |
| logmoment=-logdet/2+float(m@np.linalg.solve(S-St,m))/2 | |
| self.assertAlmostEqual(logmoment,np.trace(A)*t+x0@R@x0/2,places=10) | |
| def test_covariance_perturbation_and_weighted_resistance(self): | |
| A=np.diag([1.,1.7,2.4]);Q=np.diag([.8,1.3,1.9]) | |
| S=stationary_covariance(A,Q);E=self.rng.normal(size=(3,3))*.015 | |
| eta=np.linalg.norm(E,2);DQ=.02*np.eye(3) | |
| Sp=stationary_covariance(A+E,Q+DQ) | |
| bound=(np.linalg.norm(DQ,2)+2*eta*np.linalg.norm(S,2))/(2*(1-eta)) | |
| self.assertLessEqual(np.linalg.norm(Sp-S,2),bound) | |
| w=np.array([1.,3.,2.]);U=shape_basis(w) | |
| L=graph_laplacian(3,[(0,1),(1,2)],[.7,1.2]);R=U/np.sqrt(w)[:,None] | |
| S=.5*np.linalg.inv(R.T@L@R) | |
| for i,j in ((0,1),(0,2),(1,2)): | |
| b=np.eye(3)[i]-np.eye(3)[j];r=R.T@b | |
| self.assertAlmostEqual(float(r@S@r),float(.5*b@np.linalg.pinv(L)@b),places=11) | |
| def test_observation_connectivity_and_invalid_values(self): | |
| U=shape_basis(np.array([1.,2.,3.,4.])) | |
| connected=np.array([[1,-1,0,0],[0,1,-1,0],[0,0,1,-1]]) | |
| disconnected=np.array([[1,-1,0,0],[0,0,1,-1]]) | |
| self.assertEqual(np.linalg.matrix_rank(connected@U),3) | |
| self.assertEqual(np.linalg.matrix_rank(disconnected@U),2) | |
| with self.assertRaises(ValueError):shape_basis([1,np.nan]) | |
| with self.assertRaises(ValueError):graph_laplacian(3,[(0,1)],[-1]) | |
| if __name__=='__main__': | |
| suite=unittest.defaultTestLoader.loadTestsFromTestCase(GeometryChecks) | |
| result=unittest.TextTestRunner(verbosity=2).run(suite) | |
| out=Path(__file__).resolve().parents[1]/'results/geometry_verification.json' | |
| out.write_text(json.dumps({'status':'passed' if result.wasSuccessful() else 'failed', | |
| 'tests_run':result.testsRun,'failures':len(result.failures), | |
| 'errors':len(result.errors),'scope':'internal matrix and construction checks; no hardware or interval certification'},indent=2)+'\n') | |
| raise SystemExit(0 if result.wasSuccessful() else 1) | |