Datasets:
Formats:
csv
Languages:
English
Size:
100K - 1M
Tags:
research-artifact
synthetic-data
mathematical-proofs
system-identification
inverse-problems
nonlinear-dynamics
License:
Download tests/test_core.py from PureOne/EVE-Obstruction-Tomography: direct link, hf CLI and curl.
- Browser
- Download file 4.96 kB
-
https://huggingface.co/datasets/PureOne/EVE-Obstruction-Tomography/resolve/main/tests/test_core.py
- Command line
-
hf download hf://datasets/PureOne/EVE-Obstruction-Tomography/tests/test_core.py
-
curl -L -o test_core.py https://huggingface.co/datasets/PureOne/EVE-Obstruction-Tomography/resolve/main/tests/test_core.py
4.96 kB
| """Mathematical identities, counterexamples, and scope guards.""" | |
| import unittest | |
| import numpy as np | |
| from scipy.linalg import expm,block_diag | |
| from scipy.optimize import minimize | |
| from ot.core import (Model,lattice,query_support,kernel_from_queries,reconstruct, | |
| minimum_joint_cost,pair_log,stability_certificate) | |
| class ResearchChecks(unittest.TestCase): | |
| def setUp(self): | |
| self.A=np.array([[-.2,-.7],[.7,-.3]]) | |
| self.C=np.array([[1.,.2]]) | |
| self.Q=np.array([[1.1,.1],[.1,.8]]) | |
| self.model=Model(self.A,self.C,self.Q) | |
| def test_support_polarization_and_optimizer(self): | |
| O=self.model.design([0.,.3,.9]);a=np.array([.2,-.7,.3]);a/=np.linalg.norm(a) | |
| v=O.T@a | |
| oracle=float(v@self.Q@v) | |
| result=minimize(lambda x:float(x@np.linalg.solve(self.Q,x)),np.ones(2), | |
| constraints={'type':'eq','fun':lambda x:float(v@x)-1.}, | |
| method='SLSQP',options={'ftol':1e-12,'maxiter':300}) | |
| self.assertTrue(result.success) | |
| self.assertAlmostEqual(result.fun,1/oracle,places=8) | |
| rows=query_support(O,self.Q) | |
| np.testing.assert_allclose(kernel_from_queries(rows,3),O@self.Q@O.T,atol=1e-14) | |
| def test_joint_infeasibility_has_null_certificate(self): | |
| K=np.ones((2,2));a=np.array([1.,-1.]);y=np.array([1.,-1.]) | |
| self.assertEqual(minimum_joint_cost(K,y),float('inf')) | |
| self.assertAlmostEqual(float(a@K@a),0.) | |
| self.assertNotEqual(float(a@y),0.) | |
| self.assertAlmostEqual(minimum_joint_cost(K,[2.,2.]),4.) | |
| def test_coordinate_gauge_invariance(self): | |
| S=np.array([[2.,.4],[.2,.8]]) | |
| m2=Model(S@self.A@np.linalg.inv(S),self.C@np.linalg.inv(S),S@self.Q@S.T) | |
| times=[0.,.17,.81,1.3] | |
| O=self.model.design(times);P=m2.design(times) | |
| np.testing.assert_allclose(O@self.Q@O.T,P@m2.Q@P.T,atol=2e-14) | |
| def test_unobservable_extension_not_identified(self): | |
| m2=Model(block_diag(self.A,[[13.]]),np.column_stack([self.C,[0.]]),block_diag(self.Q,[[1.]])) | |
| O=self.model.design([0.,.1,.3]);P=m2.design([0.,.1,.3]) | |
| np.testing.assert_allclose(O@self.Q@O.T,P@m2.Q@P.T,atol=1e-14) | |
| def test_feasibility_does_not_calibrate_resource(self): | |
| O=self.model.design([0.,.2,.8]);K=O@self.Q@O.T;y=O@np.array([.4,.8]) | |
| self.assertAlmostEqual(minimum_joint_cost(2*K,y),minimum_joint_cost(K,y)/2,places=10) | |
| np.testing.assert_allclose(np.linalg.pinv(K)@K,np.linalg.pinv(2*K)@(2*K),atol=1e-13) | |
| def test_singleton_data_and_uniform_grid_alias(self): | |
| times=np.arange(5);w1=1.;w2=1.+2*np.pi | |
| np.testing.assert_allclose(np.cos(w1*(times[:,None]-times)),np.cos(w2*(times[:,None]-times)),atol=1e-14) | |
| self.assertGreater(abs(np.cos(w1*np.sqrt(2))-np.cos(w2*np.sqrt(2))),.1) | |
| def test_two_clock_branch_recovery(self): | |
| w=7.3;A=np.array([[0.,-w],[w,0.]]) | |
| F=expm(A);J=expm(np.sqrt(2)*A) | |
| got,info=pair_log(F,J,1.,np.sqrt(2),40.) | |
| np.testing.assert_allclose(got,A,atol=1e-11) | |
| self.assertGreater(info['min_margin'],.1) | |
| def test_branch_solver_rejects_degenerate_spectrum(self): | |
| with self.assertRaises(ValueError):pair_log(np.eye(2),np.eye(2),1.,np.sqrt(2),40.) | |
| def test_noisy_entry_bound(self): | |
| O=self.model.design([0.,.2,.7]);delta=.002 | |
| raw=query_support(O,self.Q,np.random.default_rng(5),delta) | |
| Kh=kernel_from_queries(raw,3);K=O@self.Q@O.T | |
| self.assertLessEqual(np.max(abs(Kh-K)),delta+1e-14) | |
| self.assertLessEqual(np.linalg.norm(Kh-K,2),3*delta+1e-14) | |
| def test_clock_shift_orientation(self): | |
| tau=.2;sigma=tau*np.sqrt(2);times,labels=lattice(2,1,tau,sigma) | |
| O=self.model.design(times) | |
| rec=reconstruct(kernel_from_queries(query_support(O,self.Q),len(O)),labels,2,tau,sigma) | |
| B=O[rec['basis']] | |
| np.testing.assert_allclose(rec['F'],B@expm(self.A*tau)@np.linalg.inv(B),atol=1e-12) | |
| np.testing.assert_allclose(rec['A'],B@self.A@np.linalg.inv(B),atol=1e-11) | |
| def test_independent_and_shared_preparation_differ(self): | |
| # Same scalar marginals, different joint budgets when preparations reset. | |
| Kshared=np.ones((2,2));Kindependent=np.eye(2) | |
| self.assertEqual(minimum_joint_cost(Kshared,[1.,-1.]),float('inf')) | |
| self.assertAlmostEqual(minimum_joint_cost(Kindependent,[1.,-1.]),2.) | |
| def test_chain_precision_pair_exact_difference(self): | |
| eps=.13;s=np.arange(4);O3=np.column_stack([np.ones(4),eps*s,(eps*s)**2/2]) | |
| O2=O3[:,:2];h=O3[:,2] | |
| np.testing.assert_allclose(O3@O3.T-O2@O2.T,np.outer(h,h),atol=1e-15) | |
| self.assertAlmostEqual(np.linalg.norm(np.outer(h,h),2),float(h@h),places=13) | |
| def test_incomplete_queries_rejected(self): | |
| O=self.model.design([0.,.2]);rows=query_support(O,self.Q) | |
| with self.assertRaises(ValueError):kernel_from_queries(rows[:-1],2) | |
| if __name__=='__main__':unittest.main(verbosity=2) | |