def test_fmpz_mat(): M = flint.fmpz_mat a = M(2, 3, [1, 2, 3, 4, 5, 6]) b = M(2, 3, [4, 5, 6, 7, 8, 9]) assert a == a assert a == M(a) assert a != b assert a.nrows() == 2 assert a.ncols() == 3 assert a.entries() == [1, 2, 3, 4, 5, 6] assert a.table() == [[1, 2, 3], [4, 5, 6]] assert (a + b).entries() == [5, 7, 9, 11, 13, 15] assert raises(a.det, ValueError) assert +a == a assert -a == M(2, 3, [-1, -2, -3, -4, -5, -6]) c = M(2, 2, [1, 2, 3, 4]) assert c.det() == -2 assert raises(lambda: a + c, ValueError) assert (a * 3).entries() == [3, 6, 9, 12, 15, 18] assert (3 * a).entries() == [3, 6, 9, 12, 15, 18] assert (a * long(3)).entries() == [3, 6, 9, 12, 15, 18] assert (long(3) * a).entries() == [3, 6, 9, 12, 15, 18] assert (a * flint.fmpz(3)).entries() == [3, 6, 9, 12, 15, 18] assert (flint.fmpz(3) * a).entries() == [3, 6, 9, 12, 15, 18] assert M.randrank(5, 7, 3, 10).rank() == 3 A = M.randbits(5, 3, 2) B = M.randtest(3, 7, 3) C = M.randtest(7, 2, 4) assert A * (B * C) == (A * B) * C assert bool(M(2, 2, [0, 0, 0, 0])) == False assert bool(M(2, 2, [0, 0, 0, 1])) == True ctx.pretty = False assert repr(M(2, 2, [1, 2, 3, 4])) == 'fmpz_mat(2, 2, [1, 2, 3, 4])' ctx.pretty = True assert str(M(2, 2, [1, 2, 3, 4])) == '[1, 2]\n[3, 4]' assert M(1, 2, [3, 4]) * flint.fmpq(1, 3) == flint.fmpq_mat( 1, 2, [1, flint.fmpq(4, 3)]) assert flint.fmpq(1, 3) * M(1, 2, [3, 4]) == flint.fmpq_mat( 1, 2, [1, flint.fmpq(4, 3)]) assert M(1, 2, [3, 4]) / 3 == flint.fmpq_mat(1, 2, [1, flint.fmpq(4, 3)]) assert M(2, 2, [1, 2, 3, 4 ]).inv().det() == flint.fmpq(1) / M(2, 2, [1, 2, 3, 4]).det() assert M(2, 2, [1, 2, 3, 4]).inv().inv() == M(2, 2, [1, 2, 3, 4]) assert raises(lambda: M.randrank(4, 3, 4, 1), ValueError) assert raises(lambda: M.randrank(3, 4, 4, 1), ValueError) assert M(1, 1, [3])**5 == M(1, 1, [3**5]) assert raises(lambda: M(1, 2)**3, ValueError) assert raises(lambda: M(1, 1)**M(1, 1), TypeError) assert raises(lambda: 1**M(1, 1), TypeError) assert raises(lambda: M([1]), TypeError) assert raises(lambda: M([[1], [2, 3]]), ValueError) assert M([[1, 2, 3], [4, 5, 6]]) == M(2, 3, [1, 2, 3, 4, 5, 6])
def inverse(M, A, indices_from, indices_to): m = fmpq_mat(M, M) d, (r, c) = A for j in range(len(d)): a = guessrational(d[j]) #print("guessed", d[j], "=", a) m[r[j], c[j]] = guessrational(d[j]) t = fmpq_mat(len(indices_from), len(indices_to)) for j in range(len(indices_to)): z = fmpq_mat(M, 1) z[indices_to[j], 0] = 1 o = m.solve(z) for i in range(len(indices_from)): t[i, j] = o[indices_from[i], 0] return t
def test_fmpz_mat(): M = flint.fmpz_mat a = M(2,3,[1,2,3,4,5,6]) b = M(2,3,[4,5,6,7,8,9]) assert a == a assert a == M(a) assert a != b assert a.nrows() == 2 assert a.ncols() == 3 assert a.entries() == [1,2,3,4,5,6] assert a.table() == [[1,2,3],[4,5,6]] assert (a + b).entries() == [5,7,9,11,13,15] assert raises(a.det, ValueError) assert +a == a assert -a == M(2,3,[-1,-2,-3,-4,-5,-6]) c = M(2,2,[1,2,3,4]) assert c.det() == -2 assert raises(lambda: a + c, ValueError) assert (a * 3).entries() == [3,6,9,12,15,18] assert (3 * a).entries() == [3,6,9,12,15,18] assert (a * 3L).entries() == [3,6,9,12,15,18] assert (3L * a).entries() == [3,6,9,12,15,18] assert (a * flint.fmpz(3)).entries() == [3,6,9,12,15,18] assert (flint.fmpz(3) * a).entries() == [3,6,9,12,15,18] assert M.randrank(5,7,3,10).rank() == 3 A = M.randbits(5,3,2) B = M.randtest(3,7,3) C = M.randtest(7,2,4) assert A*(B*C) == (A*B)*C assert bool(M(2,2,[0,0,0,0])) == False assert bool(M(2,2,[0,0,0,1])) == True assert repr(M(2,2,[1,2,3,4])) == 'fmpz_mat(2, 2, [1, 2, 3, 4])' assert str(M(2,2,[1,2,3,4])) == '[1, 2]\n[3, 4]' assert M(1,2,[3,4]) * flint.fmpq(1,3) == flint.fmpq_mat(1, 2, [1, flint.fmpq(4,3)]) assert flint.fmpq(1,3) * M(1,2,[3,4]) == flint.fmpq_mat(1, 2, [1, flint.fmpq(4,3)]) assert M(1,2,[3,4]) / 3 == flint.fmpq_mat(1, 2, [1, flint.fmpq(4,3)]) assert (~M(2,2,[1,2,3,4])).det() == flint.fmpq(1) / M(2,2,[1,2,3,4]).det() assert ~~M(2,2,[1,2,3,4]) == M(2,2,[1,2,3,4]) assert raises(lambda: M.randrank(4,3,4,1), ValueError) assert raises(lambda: M.randrank(3,4,4,1), ValueError) assert M(1,1,[3]) ** 5 == M(1,1,[3**5]) assert raises(lambda: M(1,2) ** 3, ValueError) assert raises(lambda: M(1,1) ** M(1,1), TypeError) assert raises(lambda: 1 ** M(1,1), TypeError)
def matrix_eigenvalues(mat): mat = fmpq_mat(mat) return alg.polynomial_roots(mat.charpoly())