-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhankel.py
executable file
·225 lines (180 loc) · 7.38 KB
/
hankel.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sympy as sp
import abc
class HankelMatrix:
"""
Simple class for holding methods like evaluating minors or formatting submatrices.
"""
def __init__(self, matrix_symbol=None, element_symbol=None):
if matrix_symbol is None:
matrix_symbol = sp.IndexedBase('H')
self.matrix_symbol = matrix_symbol
if element_symbol is None:
element_symbol = matrix_symbol.copy()
element_symbol.label.name = element_symbol.label.name.lower()
self.element_symbol = element_symbol
def minor(self, rows, columns):
"""
Calculate the minor at the specified rows and columns
:param rows: array of row indices
:param columns: array of column indices
:return: determinant expression
"""
return self.submatrix(rows, columns).det()
def submatrix(self, rows, columns):
"""
Calculate the Hankel submatrix at the specified rows and columns.
"""
return sp.Matrix([[self.element_symbol[r + c] for c in columns] for r in rows])
def format_minor(self, rows, columns, short=True):
"""
Make latex for the minor.
Either short one with just rows-columns specification,
or long one with the values themselves (when short=False).
"""
if short:
mat = sp.Matrix([rows, columns])
return sp.latex(self.matrix_symbol) + sp.latex(mat)
else:
mat = self.submatrix(rows, columns)
return r'\det ' + sp.latex(mat)
class Conjecture(abc.ABC):
"""
Abstract class with some methods for testing and formatting hypotheses.
"""
def __init__(self):
self.valid = None
@abc.abstractmethod
def check(self):
"""
Verify the conjecture.
:return: Boolean result of the verification.
"""
return NotImplemented
def format(self, short):
"""
Format LaTeX code stating the (in)validity of conjecture.
:param short: use short or long form of minors
:return: tex code in str
"""
if self.valid is None:
self.valid = self.check()
return self._format(short)
@abc.abstractmethod
def _format(self, short):
"""
Format LaTeX code stating the (in)validity of conjecture.
The real work is done here. The `format` method is a wrapper.
:param short: use short or long form of minors
:return: tex code in str
"""
return NotImplemented
class Conjecture_1plus2equals3(Conjecture):
"""
The hypothesis states that (minor1 + minor2 == minor3).
"""
def __init__(self, matrix, minor1, minor2, minor3):
super().__init__()
self.matrix = matrix
self.minor1 = minor1
self.minor2 = minor2
self.minor3 = minor3
def check(self):
det1 = self.matrix.minor(*self.minor1)
det2 = self.matrix.minor(*self.minor2)
det3 = self.matrix.minor(*self.minor3)
self.valid = (det1 + det2).equals(det3)
return self.valid
def _format(self, short=True):
tex_eq = r'=' if self.valid else r'\neq'
tex1 = self.matrix.format_minor(*self.minor1, short=short)
tex2 = self.matrix.format_minor(*self.minor2, short=short)
tex3 = self.matrix.format_minor(*self.minor3, short=short)
return '{} + {} {} {}'.format(tex1, tex2, tex_eq, tex3)
class Conjecture_increment(Conjecture):
"""
The hypothesis states that sum of minors with one-at-a-time augmented indices by some value
is independent whether rows are augmented either columns.
"""
def __init__(self, matrix, minor, delta):
super().__init__()
self.matrix = matrix
self.base_minor = minor
self.delta = delta
# Obtain the minors for summation
self.row_minors = []
self.column_minors = []
rows, columns = self.base_minor
for i in range(len(rows)):
incremented_rows = rows[:]
incremented_rows[i] = rows[i] + self.delta
self.row_minors.append((incremented_rows, columns))
incremented_columns = columns[:]
incremented_columns[i] = columns[i] + self.delta
self.column_minors.append((rows, incremented_columns))
def check(self):
expr = 0
for minor in self.row_minors:
expr += self.matrix.minor(*minor)
for minor in self.column_minors:
expr -= self.matrix.minor(*minor)
return expr.equals(0)
def _format(self, short=True):
tex_eq = r'=' if self.valid else r'\neq'
row_sum = ' + '.join([sp.latex(self.matrix.format_minor(*minor, short=short)) for minor in self.row_minors])
column_sum = ' + '.join([sp.latex(self.matrix.format_minor(*minor, short=short)) for minor in self.column_minors])
return '{} {} {}'.format(row_sum, tex_eq, column_sum)
class ConjectureLogger:
"""
Class for logging the conjectures.
"""
def __init__(self, matrix):
"""
:param matrix: The HankelMatrix to state conjectures about.
"""
self.matrix = matrix
self.short_results = []
self.long_results = []
def log(self, conjecture_class, *args):
"""
Test the conjecture about minors and log the results.
"""
conjecture = conjecture_class(self.matrix, *args)
conjecture.check()
self.short_results.append(conjecture.format(True))
self.long_results.append(conjecture.format(False))
def save(self, filename, short=True):
"""
Save the logged results to the file.
"""
with open(filename, 'w') as file:
results = self.short_results if short else self.long_results
file.writelines('\\begin{dmath}' + line + '\\end{dmath}\n' for line in results)
def main():
matrix = HankelMatrix()
with open('sample_hankel.tex', 'w') as file:
file.write('H = ')
file.write(sp.latex(matrix.submatrix(range(5), range(5))))
# Now check some conjectures
logger = ConjectureLogger(matrix)
i, j, k, l, m, n, p, q, t = sp.symbols('i j k l m n p q t', cls=sp.Idx)
logger.log(Conjecture_1plus2equals3, ([0, 1, 2], [0, j, i - 1]), ([0, 1, i], [0, 1, j]),
([0, 1, i - 1], [0, 1, j + 1]))
logger.log(Conjecture_1plus2equals3, ([0, 1, 3], [0, j, i - 1]), ([0, 1, i], [0, 2, j]),
([0, 2, i - 1], [0, 1, j + 1]))
logger.log(Conjecture_1plus2equals3, ([0, 1, l + 1], [0, j, i - 1]), ([0, 1, i], [0, l, j]),
([0, l, i - 1], [0, 1, j + 1]))
logger.log(Conjecture_1plus2equals3, ([k, 1, l + 1], [0, j, i - 1]), ([k, 1, i], [0, l, j]),
([0, l, i - 1], [k, 1, j + 1]))
logger.log(Conjecture_1plus2equals3, ([0, 1, 2, 3], [0, 1, j, i - 1]),
([0, 1, 2, i], [0, 1, 2, j]), ([0, 1, 2, i - 1], [0, 1, 2, j + 1]))
logger.log(Conjecture_1plus2equals3, ([0, 1, 2, 3, 4], [0, 1, 2, j, i - 1]),
([0, 1, 2, 3, i], [0, 1, 2, 3, j]), ([0, 1, 2, 3, i - 1], [0, 1, 2, 3, j + 1]))
logger.log(Conjecture_increment, ([i, j], [l, m]), t)
logger.log(Conjecture_increment, ([i, j, k], [l, m, n]), t)
logger.log(Conjecture_increment, ([i, j, k, l], [m, n, p, q]), t)
logger.save('payload_short.tex', True)
logger.save('payload_long.tex', False)
if __name__ == '__main__':
main()