diff options
-rw-r--r-- | 4/libsolve2.py | 8 | ||||
-rw-r--r-- | 4/render.py | 45 | ||||
-rw-r--r-- | 4/two.py | 28 | ||||
-rw-r--r-- | 4/two_basis.py | 47 |
4 files changed, 128 insertions, 0 deletions
diff --git a/4/libsolve2.py b/4/libsolve2.py index d723ba3..c4c9106 100644 --- a/4/libsolve2.py +++ b/4/libsolve2.py @@ -3,6 +3,7 @@ from typing import Iterable, Union from fractions import Fraction from itertools import zip_longest +POLY_COLOR = '95' def is_scalar(obj): return isinstance(obj, (Fraction, int)) @@ -295,5 +296,12 @@ class Matrix: break return copy + @property + def T(self): + return Matrix([ + [row[i] for row in self.rows] + for i in range(self.shape[1]) + ]) + x = Poly([0, 1]) diff --git a/4/render.py b/4/render.py new file mode 100644 index 0000000..9285c72 --- /dev/null +++ b/4/render.py @@ -0,0 +1,45 @@ +from libsolve2 import Poly, PolyRenderBase + + +class UnicodeRender(PolyRenderBase): + def __init__(self): + # may not work. see + # https://en.wikipedia.org/wiki/Unicode_subscripts_and_superscripts + # to test with your font + self.sup = '⁰¹²³⁴⁵⁶⁷⁸⁹' # must be <sup>0123456789</sup> + + def render(self, poly: Poly) -> str: + rev = list(reversed(poly.vec)) + if all(r == 0 for r in rev): + return '0' + d = poly.deg() + res = [] + for i in range(len(rev)): + if rev[i] == 0: + continue + if i == 0: + if rev[i] < 0: + res.append('-') + else: + if rev[i] < 0: + res.append(' - ') + else: + res.append(' + ') + + if abs(rev[i]) != 1 or i == d: + res.append(str(abs(rev[i]))) + if i != d: + res.append(poly.letter) + if d - i != 1: + res.append(self._int2sup(d - i)) + return ''.join(res) + + def hint(self, poly: Poly) -> int: + return len(self.render(poly)) + + def _int2sup(self, n): + res = [] + while n > 0: + res.append(self.sup[n % 10]) + n //= 10 + return ''.join(reversed(res)) diff --git a/4/two.py b/4/two.py new file mode 100644 index 0000000..1302f6e --- /dev/null +++ b/4/two.py @@ -0,0 +1,28 @@ +from libsolve2 import * +from fractions import Fraction + +def f_eye(dim): + return Matrix([ + [0 if i != j else 1 for i in range(dim)] + for j in range(dim) + ]) + +m = Matrix([ + [ 4, -3, -2, 0, 0, 0], + [ 0, 2, -1, 0, 0, 0], + [ 0, 2, 5, 0, 0, 0], + [ 1, 1, 1, 2, -1, 0], + [-2, -3, -3, 4, 6, 0], + [ 1, 8, 7, -5, -3, 3] +]) + +print('char polynomial is ', (m - f_eye(6) * x).det()) +print('(x - 3)^2(x - 4)^4 =', (x - 3)*(x - 3)*(x - 4)*(x - 4)*(x - 4)*(x - 4)) + +root1 = m - f_eye(6) * 3 +print(f'rank of m - 3E = {root1.rank()} => one cell of size 2') + +root2 = m - f_eye(6) * 4 +print(f'rank of m - 4E = {root2.rank()} => two cells') +print(f'rank of (m - 4E)^2 = {(root2@root2).rank()} => two cells of size 2') + diff --git a/4/two_basis.py b/4/two_basis.py new file mode 100644 index 0000000..d1d71a2 --- /dev/null +++ b/4/two_basis.py @@ -0,0 +1,47 @@ +from libsolve2 import * +from fractions import Fraction as F + +def f_eye(dim): + return Matrix([ + [0 if i != j else 1 for i in range(dim)] + for j in range(dim) + ]) + +eye = f_eye(6) + +m = Matrix([ + [ 4, -3, -2, 0, 0, 0], + [ 0, 2, -1, 0, 0, 0], + [ 0, 2, 5, 0, 0, 0], + [ 1, 1, 1, 2, -1, 0], + [-2, -3, -3, 4, 6, 0], + [ 1, 8, 7, -5, -3, 3] +]) + +for_root1 = m - eye * 3 + +print('(m - 3E)^2 =') +print((for_root1@for_root1).triangled(swap=True)) +print() +print() + +for_root2 = m - eye * 4 + +print('(m - 4E)^2 =') +print((for_root2@for_root2).triangled(swap=True)) + +def make_vec(iter): + return Matrix([[i] for i in iter]) + +print() +print('Проверим, лежит ли кто-нибудь из ФСР в ядре:') +print(((for_root2) @ make_vec([0, -F(1, 3), F(2, 3), 0, 0, 1])).T) +print(((for_root2) @ make_vec([0, -F(2, 3), F(4, 3), 0, 1, 0])).T) +print(((for_root2) @ make_vec([0, -1, 2, 1, 0, 0])).T) +print(((for_root2) @ make_vec([1, 0, 0, 0, 0, 0])).T) + +print('кажется, ни один вектор не лежит в ядре.') +print() +print('а вот эти уже лежат в ядре') +print((for_root2 @ make_vec([0, -1, 2, 1, 0, 0])).T) +print((for_root2 @ make_vec([1, 0, 0, 0, 0, 0])).T) |