Datasets:
Formats:
json
Languages:
English
Size:
< 1K
Tags:
mathematics
nonlinear-systems
model-reduction
state-representation
exterior-algebra
exact-arithmetic
Download tests/test_continuation.py from PureOne/AELYRAITH-III: direct link, hf CLI and curl.
- Browser
- Download file 9.87 kB
-
https://huggingface.co/datasets/PureOne/AELYRAITH-III/resolve/main/tests/test_continuation.py
- Command line
-
hf download hf://datasets/PureOne/AELYRAITH-III/tests/test_continuation.py
-
curl -L -o test_continuation.py https://huggingface.co/datasets/PureOne/AELYRAITH-III/resolve/main/tests/test_continuation.py
9.87 kB
| import unittest | |
| import itertools | |
| import numpy as np | |
| import sympy as s | |
| from src.continuation import * | |
| class AlgebraTests(unittest.TestCase): | |
| def test_wedge_pairing(self): | |
| a=s.Matrix([[1,2,3]]);b=s.Matrix([[2,-1,4]]) | |
| x=s.Matrix([[2,3,1]]);y=s.Matrix([[-1,4,2]]) | |
| self.assertEqual((wedge(a,b)*wedge(x,y).T)[0], | |
| (a*x.T)[0]*(b*y.T)[0]-(a*y.T)[0]*(b*x.T)[0]) | |
| def test_compound_composition(self): | |
| A=s.Matrix([[1,2,0],[2,1,3],[1,0,1]]) | |
| B=s.Matrix([[1,0,2],[0,2,1],[3,1,1]]) | |
| self.assertEqual(compound2(A*B),compound2(A)*compound2(B)) | |
| def test_compound_row_transport(self): | |
| A=s.Matrix([[1,2,0],[2,1,3],[1,0,1]]) | |
| a=s.Matrix([[2,1,4]]);b=s.Matrix([[3,0,2]]) | |
| self.assertEqual(wedge(a*A,b*A),wedge(a,b)*compound2(A)) | |
| def test_bad_wedge(self): | |
| with self.assertRaises(ValueError):wedge(s.ones(2,1),s.ones(1,2)) | |
| def test_empty_library(self): | |
| with self.assertRaises(ValueError):compile_exterior([],[]) | |
| def test_nonpositive_rejected(self): | |
| with self.assertRaises(ValueError):compile_exterior([s.eye(3)],[s.ones(1,3)]) | |
| def test_constant_output_empty_signature(self): | |
| T=s.ones(3)/3 | |
| c=compile_exterior([T],[s.ones(1,3)]) | |
| self.assertEqual(c.R.rows,0);self.assertTrue(c.verify()) | |
| def test_hadamard(self): | |
| for n in [4,8,16,32]: | |
| H=hadamard_exact(n);self.assertEqual(H*H.T,n*s.eye(n)) | |
| def test_bad_family_size(self): | |
| with self.assertRaises(ValueError):separation_family(6) | |
| def test_bad_perturbation(self): | |
| with self.assertRaises(ValueError):separation_family(4,s.Rational(1,8)) | |
| class FamilyTests(unittest.TestCase): | |
| def test_positive_instrument(self): | |
| for n in [4,8,16]: | |
| m=separation_family(n) | |
| self.assertTrue(min(m.A)>0 and min(m.B)>0) | |
| self.assertEqual(s.ones(1,n)*(m.A+m.B),s.ones(1,n)) | |
| def test_branch_mass_floor(self): | |
| for n in [4,8,16]: | |
| m=separation_family(n) | |
| for T in [m.A,m.B]: | |
| self.assertGreaterEqual(min(s.ones(1,n)*T),s.Rational(3,8)) | |
| def test_affine_full_exact(self): | |
| for n in [4,8]: | |
| m=separation_family(n) | |
| W=affine_closure([m.A,m.B],m.F[[0,1],:]) | |
| self.assertEqual(W.rows,n) | |
| def test_exterior_two_dimensional(self): | |
| for n in [4,8]: | |
| m=separation_family(n);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| self.assertEqual(c.R.rows,2);self.assertEqual(c.depth,1) | |
| def test_exact_nonlinear_updates(self): | |
| m=separation_family(8) | |
| x=s.Matrix(range(1,9))/36 | |
| for label,T in enumerate([m.A,m.B]): | |
| self.assertEqual(m.signature(normalize(T,x)),m.compact_step(m.signature(x),label)) | |
| def test_all_words_six(self): | |
| m=separation_family(4);x=s.Matrix([1,2,3,4])/10 | |
| for word in itertools.product(range(2),repeat=6): | |
| y=x;z=m.signature(x) | |
| for a in word: | |
| y=normalize([m.A,m.B][a],y);z=m.compact_step(z,a) | |
| self.assertEqual(z,m.signature(y)) | |
| def test_fiber_noninjective(self): | |
| m=separation_family(8);nu=s.ones(8,1)/8 | |
| x=nu+m.F[4,:].T/s.Integer(64) | |
| self.assertNotEqual(x,nu);self.assertEqual(m.signature(x),m.signature(nu)) | |
| def test_local_rank_two(self): | |
| m=separation_family(4);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| self.assertEqual(jacobian_rank(c,s.ones(4,1)/4),2) | |
| def test_lp_representative_update(self): | |
| m=separation_family(4);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| x=s.Matrix([1,2,3,4])/10 | |
| z=list(c.signature_exact(x)) | |
| for a,T in enumerate([m.A,m.B]): | |
| np.testing.assert_allclose(c.update_float(z,a), | |
| np.array(c.signature_exact(normalize(T,x)),dtype=float).ravel(),atol=1e-10) | |
| def test_invalid_signature_dimension(self): | |
| m=separation_family(4);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| with self.assertRaises(ValueError): c.fiber_matrix([1,2,3]) | |
| def test_invalid_state(self): | |
| m=separation_family(4);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| with self.assertRaises(ValueError): c.signature_exact(s.ones(4,1)) | |
| class RewriteTests(unittest.TestCase): | |
| def test_rank_one_and_equal_spectra(self): | |
| for n in [4,8,16]: | |
| a=separation_family(n);b=separation_family(n,s.Rational(1,10000)) | |
| self.assertEqual((b.B-a.B).rank(),1) | |
| self.assertEqual(a.LB.charpoly().as_expr(),b.LB.charpoly().as_expr()) | |
| self.assertEqual((a.LA+a.LB).charpoly().as_expr(),(b.LA+b.LB).charpoly().as_expr()) | |
| self.assertEqual(s.ones(1,n)*a.B,s.ones(1,n)*b.B) | |
| def test_uniform_stationary(self): | |
| for n in [4,8,16]: | |
| m=separation_family(n,s.Rational(1,10000));nu=s.ones(n,1)/n | |
| self.assertEqual((m.A+m.B)*nu,nu) | |
| def test_reconstruction_inversion(self): | |
| for n in [4,8,16]: | |
| m=separation_family(n,s.Rational(1,1000));x=s.Matrix(range(1,n+1))/(n*(n+1)//2) | |
| y=x;ys=[] | |
| for _ in range(n-3): | |
| y=normalize(m.B,y);ys.append((m.F[1,:]*y)[0]) | |
| got=reconstruct_hidden_from_B_readouts(n,ys,m.delta) | |
| self.assertEqual(got,(m.F*x)[3:,:]) | |
| def test_rewrite_distinguishes_old_memory(self): | |
| n=8;m=separation_family(n,s.Rational(1,1000));nu=s.ones(n,1)/n | |
| x=nu+m.F[3,:].T/(4*n); y=nu-m.F[3,:].T/(4*n) | |
| self.assertEqual(m.signature(x),m.signature(y)) | |
| self.assertNotEqual((m.F[1,:]*normalize(m.B,x))[0],(m.F[1,:]*normalize(m.B,y))[0]) | |
| def test_new_local_rank_full(self): | |
| m=separation_family(4,s.Rational(1,1000)) | |
| c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| self.assertEqual(jacobian_rank(c,s.ones(4,1)/4),3) | |
| def test_perturbation_output_bound(self): | |
| m=separation_family(8,s.Rational(1,1000)) | |
| for j in range(8): | |
| x=s.eye(8)[:,j];v=(m.F[1,:]*normalize(m.B,x))[0] | |
| self.assertLessEqual(abs(v),8*m.delta/3) | |
| def test_span_admission_not_necessary(self): | |
| n=3;e=s.ones(1,n);R=s.Matrix.vstack(*[wedge(s.eye(n)[j,:],e) for j in [0,1]]) | |
| T=s.Matrix([[3,2,1],[1,4,2],[2,1,5]])/20 | |
| self.assertEqual(R.rows,2) | |
| self.assertGreater(s.Matrix.vstack(R,R*compound2(T)).rank(),R.rows) | |
| # Full simplex memory makes any positive update possible regardless of this failure. | |
| self.assertEqual(s.Matrix.vstack(e,s.eye(n)[0,:],s.eye(n)[1,:]).rank(),n) | |
| def test_vertex_promotion_failure(self): | |
| t=s.symbols('t',real=True) | |
| d=1/(1+t)-3/(2+t)+s.Rational(13,25) | |
| self.assertEqual(d.subs(t,0),s.Rational(1,50));self.assertEqual(d.subs(t,1),s.Rational(1,50)) | |
| self.assertLess(float(d.subs(t,(s.sqrt(3)-1)/2)),0) | |
| def test_certificate_tamper(self): | |
| m=separation_family(4);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| c.transports[0][0,0]+=1 | |
| self.assertFalse(c.verify()) | |
| class SnapshotTests(unittest.TestCase): | |
| def test_factor_two_sharp(self): | |
| for r in [1,2,3,4]: | |
| c=factor_two_fixture(r);n=2*r+1 | |
| self.assertEqual(jacobian_rank(c,s.ones(n,1)/n),r) | |
| self.assertEqual(snapshot_space(c).rows-1,2*r) | |
| def test_family_snapshot_three(self): | |
| for n in [4,8]: | |
| m=separation_family(n);c=compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| self.assertEqual(snapshot_space(c).rows-1,3) | |
| def test_denominator_validation(self): | |
| with self.assertRaises(ValueError): | |
| compile_ratios([s.ones(3)/3],[Ratio(s.Matrix([[1,0,0]]),s.Matrix([[0,1,1]]),())]) | |
| def test_snapshot_initializes_compact(self): | |
| m=separation_family(8);x=s.Matrix(range(1,9))/36;z=m.F*x | |
| self.assertEqual(m.signature(x),s.Matrix([z[1],z[2]/(1+m.tau*z[3])])) | |
| class CertificateIntegrityTests(unittest.TestCase): | |
| def cert(self): | |
| m=separation_family(4) | |
| return compile_exterior([m.A,m.B],[m.F[1,:]]) | |
| def test_provenance_tampering_rejected(self): | |
| c=self.cert();r=c.ratios[1] | |
| c.ratios[1]=Ratio(r.numerator,r.denominator,(1,)) | |
| self.assertFalse(c.verify()) | |
| def test_missing_seed_coverage_rejected(self): | |
| c=self.cert() | |
| extra=Ratio(s.eye(4)[0,:],s.ones(1,4),()) | |
| c.seeds=c.seeds+(extra,) | |
| self.assertFalse(c.verify()) | |
| def test_transport_truncation_rejected(self): | |
| c=self.cert();c.transports.pop() | |
| self.assertFalse(c.verify()) | |
| def test_denominator_tampering_rejected(self): | |
| c=self.cert();r=c.ratios[0] | |
| c.ratios[0]=Ratio(r.numerator,-r.denominator,r.word) | |
| self.assertFalse(c.verify()) | |
| def test_seed_word_rejected(self): | |
| with self.assertRaises(ValueError): | |
| compile_ratios([s.ones(3)/3],[Ratio(s.Matrix([[1,0,0]]),s.ones(1,3),(0,))]) | |
| def test_mode_aware_same_evidence_control(self): | |
| m=separation_family(8);x=s.Matrix(range(1,9))/36 | |
| for word in itertools.product([0,1],repeat=5): | |
| y=x;initial=m.F*x | |
| # The ordinary controller starts from three affine scalars, then | |
| # retains one sufficient scalar plus a mode after the first label. | |
| stored=(initial[1],initial[2],initial[3]);mode="initial" | |
| for label in word: | |
| if label==1: | |
| mode="reset";stored=s.Integer(0);pred=s.Integer(0) | |
| else: | |
| if mode=="initial": | |
| v=stored[1]/(1+m.tau*stored[2]) | |
| elif mode=="reset": | |
| v=s.Integer(0) | |
| else: | |
| v=stored | |
| pred=m.gamma*v;stored=m.kappa*v;mode="one-scalar" | |
| y=normalize([m.A,m.B][label],y) | |
| self.assertEqual(pred,(m.F[1,:]*y)[0]) | |
| if __name__=='__main__': unittest.main() | |