File size: 10,513 Bytes
e47436c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import unittest
import random
from itertools import permutations, product
import sympy as s
from axioryn.core import *
from axioryn.mesh import compile_faces, integrate_scales

Q=s.Rational
I=s.eye(3)

class GainTests(unittest.TestCase):
    def test_balanced_cycle(self):
        e=[GainEdge(0,1,2),GainEdge(1,2,3),GainEdge(2,0,Q(1,6))]
        r=positive_gain_kernel(3,e)
        self.assertTrue(r.feasible)
        self.assertEqual(r.basis,s.Matrix([1,2,6]))
        out=project_gain_scales(r,[1,1,1],[1,1,1],[Q(1,10)]*3,[10]*3)
        self.assertEqual(out['alpha'],s.Matrix([Q(9,41)]))
    def test_unbalanced_cycle(self):
        r=positive_gain_kernel(3,[GainEdge(0,1,2),GainEdge(1,2,3),GainEdge(2,0,Q(1,5))])
        self.assertFalse(r.feasible)
        self.assertNotEqual(r.witness['closed_walk_gain'],1)
    def test_components(self):
        r=positive_gain_kernel(4,[GainEdge(0,1,2)])
        self.assertEqual(r.basis.shape,(4,3))
        self.assertTrue(all(x>0 for x in r.potentials))
    def test_selfloop(self):
        self.assertFalse(positive_gain_kernel(1,[GainEdge(0,0,2)]).feasible)
    def test_nonpositive_rejected(self):
        with self.assertRaises(ValueError): positive_gain_kernel(2,[GainEdge(0,1,-1)])
    def test_float_rejected(self):
        with self.assertRaises(TypeError): positive_gain_kernel(2,[GainEdge(0,1,0.5)])
    def test_face_gain(self):
        self.assertEqual(face_gain([2,4],[1,2]),{'kind':'gain','gain':2})
    def test_zero_face(self):
        self.assertEqual(face_gain([0,0],[0,0])['kind'],'released')
    def test_one_zero(self):
        self.assertEqual(face_gain([0,0],[1,0])['reason'],'one_zero_trace')
    def test_noncollinear(self):
        self.assertEqual(face_gain([1,0],[1,1])['reason'],'noncollinear_traces')
    def test_opposite(self):
        self.assertEqual(face_gain([-1,0],[1,0])['reason'],'opposite_traces')
    def test_random_planted(self):
        rng=random.Random(314159)
        for _ in range(100):
            n=rng.randint(3,15)
            d=[Q(rng.randint(1,7),rng.randint(1,7)) for _ in range(n)]
            e=[GainEdge(j-1,j,d[j]/d[j-1]) for j in range(1,n)]
            for _ in range(n):
                a,b=rng.sample(range(n),2); e.append(GainEdge(a,b,d[b]/d[a]))
            r=positive_gain_kernel(n,e)
            self.assertTrue(r.feasible)
            self.assertEqual(r.basis,s.Matrix([x/d[0] for x in d]))

class InterfaceTests(unittest.TestCase):
    def test_generic(self):
        self.assertEqual(interface_data(I,I,[1,2,2])['active_normal_components'],3)
    def test_rank_two_family(self):
        n=s.Matrix([Q(3,5),Q(4,5),0]); a=n
        b=s.Matrix([2*a[0],a[1]/2,0]); B=I+(b-a)*n.T
        out=interface_data(I,B,n)
        self.assertEqual(out['ratios'],{0:s.Integer(2),1:Q(1,2)})
        self.assertGreater(B.det(),0)
    def test_rank_one_family(self):
        B=s.diag(3,1,1)
        self.assertEqual(interface_data(I,B,[1,0,0])['ratios'],{0:s.Integer(3)})
    def test_high_dimensional_lock(self):
        self.assertEqual(interface_data(s.eye(5),s.eye(5),[1,2,3,0,0])['active_normal_components'],3)
    def test_orientation_excludes_reflection(self):
        n=s.Matrix([Q(1,3),Q(2,3),Q(2,3)])
        with self.assertRaises(ValueError): interface_data(I,I-2*n*n.T,n)
    def test_symbolic_classification(self):
        a1,a2,a3=s.symbols('a1 a2 a3',nonzero=True)
        t=s.symbols('t',nonzero=True)
        a=s.Matrix([a1,a2,a3]); b=s.Matrix([t*a1,a2/t,a3/t])
        self.assertEqual(s.factor(b[1]*b[2]-a2*a3),-a2*a3*(t-1)*(t+1)/t**2)
    def test_quantitative_rational_trials(self):
        rng=random.Random(1729)
        n=s.Matrix([Q(1,3),Q(2,3),Q(2,3)])
        for _ in range(120):
            A=s.diag(*[Q(rng.randint(1,4)) for j in range(3)])
            a=A*n
            b=s.Matrix([a[j]*(1+Q(rng.randint(-3,3),1000)) for j in range(3)])
            B=A+(b-a)*n.T
            out=quantitative_interface_bound(A,B,n)
            self.assertTrue(out['verified'])
    def test_sharp_zero(self):
        out=quantitative_interface_bound(I,I,[Q(1,3),Q(2,3),Q(2,3)])
        self.assertEqual(out['upper_bound_squared'],0)
    def test_explicit_shear_necessity(self):
        n=s.Matrix([Q(1,3),Q(2,3),Q(2,3)])
        B=I+s.Matrix([Q(1,100),0,0])*n.T
        self.assertNotEqual((B*B.T)[0,1],0)
        self.assertTrue(quantitative_interface_bound(I,B,n)['verified'])

class GaugeTests(unittest.TestCase):
    def test_fake_torsion(self):
        B=s.Matrix([[2],[0]])
        c=primitive_real_gauge_quotient(B)['C']
        b=s.Matrix([Q(1,2),3])
        self.assertTrue(all(x.q==1 for x in c*b))
        self.assertEqual(b+B*s.Matrix([-Q(1,4)]),s.Matrix([0,3]))
    def test_primitive(self):
        B=s.Matrix([[2,4],[4,8],[6,12]])
        r=primitive_real_gauge_quotient(B)
        self.assertEqual(r['C']*r['integer_right_inverse'],s.eye(2))
    def test_no_gauge(self):
        r=primitive_real_gauge_quotient(s.zeros(1,0))
        self.assertEqual(r['C'],s.eye(1))
        self.assertNotEqual((r['C']*s.Matrix([Q(1,2)]))[0].q,1)
    def test_rotation_self_loop(self):
        rot=s.Matrix([[0,-1,0],[1,0,0],[0,0,1]])
        B=twisted_incidence(1,[(0,0,rot)])
        r=primitive_real_gauge_quotient(B)
        self.assertEqual(r['C'].rows,1)
        self.assertEqual(B.rank(),2)
    def test_tree_has_no_period_obstruction(self):
        B=twisted_incidence(3,[(0,1,I),(1,2,I)])
        self.assertEqual(primitive_real_gauge_quotient(B)['C'].rows,0)
    def test_two_loop_no_fixed_vector(self):
        q1=s.diag(1,-1,-1); q2=s.diag(-1,1,-1)
        B=twisted_incidence(1,[(0,0,q1),(0,0,q2)])
        self.assertEqual(B.rank(),3)
        self.assertEqual(primitive_real_gauge_quotient(B)['C'].rows,3)
    def test_random_rank_identity(self):
        rng=random.Random(41)
        rots=[]
        for p in permutations(range(3)):
            for sg in product((-1,1),repeat=3):
                m=s.zeros(3)
                for i,j in enumerate(p):m[i,j]=sg[i]
                if m.det()==1:rots.append(m)
        for _ in range(40):
            v=rng.randint(1,4); beta=rng.randint(1,3)
            edges=[(i-1,i,rng.choice(rots)) for i in range(1,v)]
            edges += [(rng.randrange(v),rng.randrange(v),rng.choice(rots)) for i in range(beta)]
            B=twisted_incidence(v,edges)
            f=3*v-B.rank(); r=3*len(edges)-B.rank()
            self.assertEqual(r,3*beta-3+f)
            self.assertLessEqual(r,3*beta)

class OptimizationTests(unittest.TestCase):
    def test_inactive_bounds(self):
        out=box_equality_qp(s.eye(2),[0,0],[[1,1]],[2],[0,0],[3,3])
        self.assertEqual(out['alpha'],s.Matrix([1,1]))
        self.assertEqual(out['cost'],1)
    def test_active_bounds(self):
        out=box_equality_qp(s.eye(2),[Q(9,10),Q(1,10)],[[1,1]],[1],[Q(1,5)]*2,[2,2])
        self.assertEqual(out['alpha'],s.Matrix([Q(4,5),Q(1,5)]))
        self.assertEqual(out['cost'],Q(1,100))
    def test_rank_deficient_equality(self):
        out=box_equality_qp(s.eye(2),[0,0],[[1,1],[2,2]],[2,4],[0,0],[3,3])
        self.assertEqual(out['alpha'],s.Matrix([1,1]))
    def test_infeasible(self):
        self.assertIsNone(box_equality_qp([[1]],[0],[[1]],[3],[0],[2]))
    def test_coupled_integer_cannot_edit_offsets_freely(self):
        out=bounded_integer_projection([[1]],[Q(49,100)],[[1],[2]],[0,0],[Q(2,5)],[Q(7,5)])
        self.assertEqual(out['alpha'],s.Matrix([1]))
        self.assertEqual(out['integer_target'],s.Matrix([1,2]))
        self.assertEqual(out['cost'],Q(2601,20000))
    def test_bounded_integer_infeasible(self):
        self.assertIsNone(bounded_integer_projection([[1]],[Q(1,2)],[[1]],[0],[Q(2,5)],[Q(3,5)]))
    def test_cap(self):
        with self.assertRaises(ValueError):
            bounded_integer_projection([[1]],[0],[[1]],[0],[0],[100],max_targets=2)

class MeshTests(unittest.TestCase):
    def setUp(self):
        self.V=[[0,0,0],[0,1,0],[0,0,1],[-1,0,0],[1,0,0]]
        self.T=[[0,1,2,3],[0,2,1,4]]
    def test_face_aligned_flexibility(self):
        out=compile_faces(self.V,self.T,[I,I])
        self.assertTrue(out['feasible'])
        self.assertEqual(out['basis'].cols,4)
    def test_actual_pl_reconstruction(self):
        out=integrate_scales(self.V,self.T,[I,I],[1,2,3,2,2,3])
        self.assertTrue(out['integrable'])
        self.assertEqual(out['determinants'],[6,12])
        self.assertEqual(out['global_injectivity'],'NOT_CERTIFIED')
    def test_generic_face_constant_directions(self):
        v=[[0,0,0],[1,-1,0],[1,0,-1],[1,1,1],[-1,-1,-1]]
        out=compile_faces(v,self.T,[I,I])
        self.assertTrue(out['feasible'])
        self.assertEqual(out['basis'].cols,3)
    def test_generic_rotated_rejected(self):
        v=[[0,0,0],[1,-1,0],[1,0,-1],[1,1,1],[-1,-1,-1]]
        r=s.Matrix([[Q(3,5),-Q(4,5),0],[Q(4,5),Q(3,5),0],[0,0,1]])
        self.assertFalse(compile_faces(v,self.T,[I,r])['feasible'])
    def test_degenerate_rejected(self):
        with self.assertRaises(ValueError):compile_faces([[0,0,0]]*4,[[0,1,2,3]],[I])
    def test_trace_failure_rejected(self):
        self.assertFalse(integrate_scales(self.V,self.T,[I,I],[1,2,3,2,4,3])['integrable'])


class InjectivityCounterexampleTests(unittest.TestCase):
    @staticmethod
    def prism_data():
        ring=[(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]
        target=[(1,0),(0,1),(-1,0),(0,-1)]*2
        v2=[(0,0)]+ring; u2=[(0,0)]+target
        V=s.Matrix([[x,y,z] for z in (0,1) for x,y in v2])
        U=s.Matrix([[x,y,z] for z in (0,1) for x,y in u2])
        tets=[]
        for j in range(8):
            a,b,c=sorted([0,1+j,1+(j+1)%8])
            tets += [[a,b,c,c+9],[a,b,b+9,c+9],[a,a+9,b+9,c+9]]
        return V,U,tets
    def test_all_twentyfour_tets_positive(self):
        V,U,T=self.prism_data()
        for ids in T:
            X=s.Matrix.hstack(*[(V[i,:]-V[ids[0],:]).T for i in ids[1:]])
            Y=s.Matrix.hstack(*[(U[i,:]-U[ids[0],:]).T for i in ids[1:]])
            self.assertEqual((Y*X.inv()).det(),1)
    def test_double_cover_collision(self):
        V,U,T=self.prism_data()
        # Two points in opposite triangular prism sectors have identical images.
        p=(V[0,:]+V[1,:]+V[2,:])/3+s.Matrix([[0,0,s.Rational(1,2)]])
        q=(V[0,:]+V[5,:]+V[6,:])/3+s.Matrix([[0,0,s.Rational(1,2)]])
        imagep=(U[0,:]+U[1,:]+U[2,:])/3
        imageq=(U[0,:]+U[5,:]+U[6,:])/3
        self.assertNotEqual(p,q)
        self.assertEqual(imagep,imageq)

if __name__=='__main__':unittest.main()