Skip to content

Commit 655f392

Browse files
committed
Make matrix types more consistent
1 parent 751e2e8 commit 655f392

File tree

6 files changed

+267
-118
lines changed

6 files changed

+267
-118
lines changed

src/flint/flint_base/flint_base.pyx

-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ cdef class flint_mat(flint_elem):
136136
"""
137137

138138
def repr(self):
139-
if thectx.pretty:
140-
return str(self)
141139
# XXX
142140
return "%s(%i, %i, [%s])" % (type(self).__name__,
143141
self.nrows(), self.ncols(), (", ".join(map(str, self.entries()))))

src/flint/test/test.py

+55-86
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ def test_fmpz_mat():
557557
assert raises(lambda: M([[1],[2,3]]), ValueError)
558558
assert raises(lambda: M(None), TypeError)
559559
assert raises(lambda: M(2,2,[1,2,3]), ValueError)
560-
assert raises(lambda: M(2,2,2,2), ValueError)
560+
assert raises(lambda: M(2,2,2,2), TypeError)
561561
assert M([[1,2,3],[4,5,6]]) == M(2,3,[1,2,3,4,5,6])
562562
assert raises(lambda: M([[1]]) < M([[2]]), TypeError)
563563
assert (M([[1]]) == 1) is False
@@ -571,15 +571,15 @@ def test_fmpz_mat():
571571
def set_bad(i,j):
572572
D[i,j] = -1
573573
# XXX: Should be IndexError
574-
raises(lambda: set_bad(2,0), ValueError)
575-
raises(lambda: set_bad(0,2), ValueError)
576-
raises(lambda: D[0,2], ValueError)
577-
raises(lambda: D[0,2], ValueError)
574+
raises(lambda: set_bad(2,0), IndexError)
575+
raises(lambda: set_bad(0,2), IndexError)
576+
raises(lambda: D[0,2], IndexError)
577+
raises(lambda: D[0,2], IndexError)
578578
# XXX: Negative indices?
579-
raises(lambda: set_bad(-1,0), ValueError)
580-
raises(lambda: set_bad(0,-1), ValueError)
581-
raises(lambda: D[-1,0], ValueError)
582-
raises(lambda: D[0,-1], ValueError)
579+
raises(lambda: set_bad(-1,0), IndexError)
580+
raises(lambda: set_bad(0,-1), IndexError)
581+
raises(lambda: D[-1,0], IndexError)
582+
raises(lambda: D[0,-1], IndexError)
583583
assert M.hadamard(2) == M([[1,1],[1,-1]])
584584
assert raises(lambda: M.hadamard(3), ValueError)
585585
assert M.hadamard(2).is_hadamard() is True
@@ -1067,7 +1067,7 @@ def test_fmpq_mat():
10671067
assert raises(lambda: Q(2,3,[1,2,3,4,5]), ValueError)
10681068
assert raises(lambda: Q([[1,2,3],[4,[],6]]), TypeError)
10691069
assert raises(lambda: Q(2,3,[1,2,3,4,[],6]), TypeError)
1070-
assert raises(lambda: Q(2,3,[1,2],[3,4]), ValueError)
1070+
assert raises(lambda: Q(2,3,[1,2],[3,4]), TypeError)
10711071
assert bool(Q([[1]])) is True
10721072
assert bool(Q([[0]])) is False
10731073
assert raises(lambda: Q([[1]]) < Q([[0]]), TypeError)
@@ -1078,13 +1078,12 @@ def test_fmpq_mat():
10781078
# XXX: Negative indices should probably be allowed
10791079
def set_bad(i):
10801080
M[i,0] = -1
1081-
raises(lambda: M[-1,0], ValueError)
1082-
raises(lambda: M[0,-1], ValueError)
1083-
raises(lambda: set_bad(-1), ValueError)
1084-
# XXX: Should be IndexError
1085-
raises(lambda: M[2,0], ValueError)
1086-
raises(lambda: M[0,2], ValueError)
1087-
raises(lambda: set_bad(2), ValueError)
1081+
raises(lambda: M[-1,0], IndexError)
1082+
raises(lambda: M[0,-1], IndexError)
1083+
raises(lambda: set_bad(-1), IndexError)
1084+
raises(lambda: M[2,0], IndexError)
1085+
raises(lambda: M[0,2], IndexError)
1086+
raises(lambda: set_bad(2), IndexError)
10881087
assert Q([[1,2,3],[4,5,6]]).transpose() == Q([[1,4],[2,5],[3,6]])
10891088
raises(lambda: M + [], TypeError)
10901089
raises(lambda: M - [], TypeError)
@@ -1488,9 +1487,12 @@ def test_nmod_mat():
14881487
assert A*(B*C) == (A*B)*C
14891488
assert bool(M(2,2,[0,0,0,0],17)) == False
14901489
assert bool(M(2,2,[0,0,0,1],17)) == True
1491-
ctx.pretty = False
1492-
assert repr(M(2,2,[1,2,3,4],17)) == 'nmod_mat(2, 2, [1, 2, 3, 4], 17)'
1493-
ctx.pretty = True
1490+
pretty = ctx.pretty
1491+
try:
1492+
ctx.pretty = False
1493+
assert repr(M(2,2,[1,2,3,4],17)) == 'nmod_mat(2, 2, [1, 2, 3, 4], 17)'
1494+
finally:
1495+
ctx.pretty = pretty
14941496
assert str(M(2,2,[1,2,3,4],17)) == '[1, 2]\n[3, 4]'
14951497
assert repr(M(2,2,[1,2,3,4],17)) == '[1, 2]\n[3, 4]'
14961498
assert M(1,2,[3,4],17) / 3 == M(1,2,[3,4],17) * (~G(3,17))
@@ -1504,7 +1506,7 @@ def test_nmod_mat():
15041506
assert raises(lambda: M(None,17), TypeError)
15051507
assert M(2,3,17) == M(2,3,[0,0,0,0,0,0],17)
15061508
assert raises(lambda: M(2,3,[0,0,0,0,0],17), ValueError)
1507-
assert raises(lambda: M(2,3,[0,1],[1,2],17), ValueError)
1509+
assert raises(lambda: M(2,3,[0,1],[1,2],17), TypeError)
15081510
assert M([[1,2,3],[4,5,6]], 5) == M(2,3,[1,2,3,4,5,6], 5)
15091511
assert raises(lambda: M([[0]],13) < M([[1]],13), TypeError)
15101512
assert (M([[1]],17) == M([[1]],13)) is False
@@ -1522,18 +1524,17 @@ def test_nmod_mat():
15221524
def set_bad(i,j):
15231525
M3[i,j] = 2
15241526
# XXX: negative indices should be allowed
1525-
assert raises(lambda: M3[-1,0], ValueError)
1526-
assert raises(lambda: M3[0,-1], ValueError)
1527-
assert raises(lambda: set_bad(-1,0), ValueError)
1528-
assert raises(lambda: set_bad(0,-1), ValueError)
1529-
# XXX: Should be IndexError
1530-
assert raises(lambda: M3[2,0], ValueError)
1531-
assert raises(lambda: M3[0,2], ValueError)
1532-
assert raises(lambda: set_bad(2,0), ValueError)
1533-
assert raises(lambda: set_bad(0,2), ValueError)
1527+
assert raises(lambda: M3[-1,0], IndexError)
1528+
assert raises(lambda: M3[0,-1], IndexError)
1529+
assert raises(lambda: set_bad(-1,0), IndexError)
1530+
assert raises(lambda: set_bad(0,-1), IndexError)
1531+
assert raises(lambda: M3[2,0], IndexError)
1532+
assert raises(lambda: M3[0,2], IndexError)
1533+
assert raises(lambda: set_bad(2,0), IndexError)
1534+
assert raises(lambda: set_bad(0,2), IndexError)
15341535
def set_bad2():
15351536
M3[0,0] = 1.5
1536-
assert raises(set_bad2, ValueError)
1537+
assert raises(set_bad2, TypeError)
15371538
assert raises(lambda: M3 + [], TypeError)
15381539
assert raises(lambda: M3 - [], TypeError)
15391540
assert raises(lambda: M3 * [], TypeError)
@@ -2643,8 +2644,7 @@ def test_matrices_eq():
26432644

26442645
def test_matrices_constructor():
26452646
for M, S, is_field in _all_matrices():
2646-
# XXX: Inconsistent exception types for different matrix types.
2647-
assert raises(lambda: M(), (ValueError, TypeError))
2647+
assert raises(lambda: M(), TypeError)
26482648

26492649
# Empty matrices
26502650
assert M([]).nrows() == 0
@@ -2720,13 +2720,8 @@ def test_matrices_strrepr():
27202720
A_str = "[1, 2]\n[3, 4]"
27212721
A_repr = _matrix_repr(A)
27222722

2723-
# XXX: inconsistent repr/str for different matrix types
2724-
if type(A) is not flint.nmod_mat:
2725-
assert A.str() == A_str, type(A).__name__
2726-
if type(A) not in (flint.fmpz_mat, flint.fmpq_mat):
2727-
assert A.repr() == A_repr, type(A).__name__
2728-
else:
2729-
assert A.repr() == A_str, type(A).__name__
2723+
assert A.str() == A_str, type(A).__name__
2724+
assert A.repr() == A_repr, type(A).__name__
27302725

27312726
# str always returns a pretty result
27322727
assert str(A) == A_str, type(A).__name__
@@ -2749,14 +2744,13 @@ def test_matrices_getitem():
27492744
assert M1234[0, 1] == S(2)
27502745
assert M1234[1, 0] == S(3)
27512746
assert M1234[1, 1] == S(4)
2752-
# XXX: Should be IndexError
2753-
assert raises(lambda: M1234[0, 2], ValueError)
2754-
assert raises(lambda: M1234[2, 0], ValueError)
2755-
assert raises(lambda: M1234[2, 2], ValueError)
2747+
assert raises(lambda: M1234[0, 2], IndexError)
2748+
assert raises(lambda: M1234[2, 0], IndexError)
2749+
assert raises(lambda: M1234[2, 2], IndexError)
27562750
# XXX: Should negative indices be allowed?
2757-
assert raises(lambda: M1234[-1, 0], ValueError)
2758-
assert raises(lambda: M1234[0, -1], ValueError)
2759-
assert raises(lambda: M1234[-1, -1], ValueError)
2751+
assert raises(lambda: M1234[-1, 0], IndexError)
2752+
assert raises(lambda: M1234[0, -1], IndexError)
2753+
assert raises(lambda: M1234[-1, -1], IndexError)
27602754

27612755

27622756
def test_matrices_setitem():
@@ -2770,19 +2764,19 @@ def test_matrices_setitem():
27702764

27712765
def setbad(obj, key, val):
27722766
obj[key] = val
2773-
# XXX: Inconsistent exception types for different matrix types.
2774-
assert raises(lambda: setbad(M1234, (0,0), None), (TypeError, ValueError))
2767+
2768+
assert raises(lambda: setbad(M1234, (0,0), None), TypeError)
27752769
assert raises(lambda: setbad(M1234, (0,None), 1), TypeError)
27762770
assert raises(lambda: setbad(M1234, (None,0), 1), TypeError)
27772771
assert raises(lambda: setbad(M1234, None, 1), TypeError)
2778-
# XXX: Should be IndexError
2779-
assert raises(lambda: setbad(M1234, (0,2), 1), ValueError)
2780-
assert raises(lambda: setbad(M1234, (2,0), 1), ValueError)
2781-
assert raises(lambda: setbad(M1234, (2,2), 1), ValueError)
2772+
2773+
assert raises(lambda: setbad(M1234, (0,2), 1), IndexError)
2774+
assert raises(lambda: setbad(M1234, (2,0), 1), IndexError)
2775+
assert raises(lambda: setbad(M1234, (2,2), 1), IndexError)
27822776
# XXX: Should negative indices be allowed?
2783-
assert raises(lambda: setbad(M1234, (-1,0), 1), ValueError)
2784-
assert raises(lambda: setbad(M1234, (0,-1), 1), ValueError)
2785-
assert raises(lambda: setbad(M1234, (-1,-1), 1), ValueError)
2777+
assert raises(lambda: setbad(M1234, (-1,0), 1), IndexError)
2778+
assert raises(lambda: setbad(M1234, (0,-1), 1), IndexError)
2779+
assert raises(lambda: setbad(M1234, (-1,-1), 1), IndexError)
27862780

27872781

27882782
def test_matrices_bool():
@@ -2819,7 +2813,6 @@ def test_matrices_add():
28192813
assert raises(lambda: M1234 + M2([[1, 2, 3], [4, 5, 6]]), ValueError)
28202814
assert raises(lambda: M2([[1, 2, 3], [4, 5, 6]]) + M1234, ValueError)
28212815
for M2 in _incompatible_matrix_types(M):
2822-
# XXX: Inconsistent exception types for different matrix types.
28232816
assert raises(lambda: M1234 + M2([[1, 2], [3, 4]]), (TypeError, ValueError))
28242817
assert raises(lambda: M2([[1, 2], [3, 4]]) + M1234, (TypeError, ValueError))
28252818

@@ -2840,7 +2833,6 @@ def test_matrices_sub():
28402833
assert raises(lambda: M1234 - M2([[1, 2, 3], [4, 5, 6]]), ValueError)
28412834
assert raises(lambda: M2([[1, 2, 3], [4, 5, 6]]) - M1234, ValueError)
28422835
for M2 in _incompatible_matrix_types(M):
2843-
# XXX: Inconsistent exception types for different matrix types.
28442836
assert raises(lambda: M1234 - M2([[1, 2], [3, 4]]), (TypeError, ValueError))
28452837
assert raises(lambda: M2([[1, 2], [3, 4]]) - M1234, (TypeError, ValueError))
28462838

@@ -2867,17 +2859,13 @@ def test_matrices_mul():
28672859
assert M2([[1, 2], [3, 4]]) * M1234 == M([[7, 10], [15, 22]])
28682860

28692861
for M2 in _incompatible_matrix_types(M):
2870-
# XXX: Inconsistent exception types for different matrix types.
28712862
assert raises(lambda: M1234 * M2([[1, 2], [3, 4]]), (TypeError, ValueError))
28722863
assert raises(lambda: M2([[1, 2], [3, 4]]) * M1234, (TypeError, ValueError))
28732864

28742865

28752866
def test_matrices_pow():
28762867
for M, S, is_field in _all_matrices():
28772868
M1234 = M([[1, 2], [3, 4]])
2878-
# XXX: nmod_mat should support __pow__
2879-
if type(M1234) is flint.nmod_mat:
2880-
continue
28812869
assert M1234**0 == M([[1, 0], [0, 1]])
28822870
assert M1234**1 == M1234
28832871
assert M1234**2 == M([[7, 10], [15, 22]])
@@ -2889,15 +2877,9 @@ def test_matrices_pow():
28892877
Ms = M([[1, 2], [3, 6]])
28902878
assert raises(lambda: Ms**-1, ZeroDivisionError)
28912879
Mr = M([[1, 2, 3], [4, 5, 6]])
2892-
# XXX: Fix fmpq_mat.__pow__
2893-
if type(Mr) is flint.fmpq_mat:
2894-
assert raises(lambda: Mr**0, AssertionError)
2895-
assert Mr ** 1 == Mr
2896-
assert raises(lambda: Mr**2, ValueError)
2897-
else:
2898-
assert raises(lambda: Mr**0, ValueError)
2899-
assert raises(lambda: Mr**1, ValueError)
2900-
assert raises(lambda: Mr**2, ValueError)
2880+
assert raises(lambda: Mr**0, ValueError)
2881+
assert raises(lambda: Mr**1, ValueError)
2882+
assert raises(lambda: Mr**2, ValueError)
29012883
assert raises(lambda: M1234**None, TypeError)
29022884
assert raises(lambda: None**M1234, TypeError)
29032885

@@ -2938,41 +2920,28 @@ def test_matrices_det():
29382920

29392921
def test_matrices_charpoly():
29402922
for M, S, is_field in _all_matrices():
2941-
# XXX: Add support for nmod_mat charpoly
2942-
if type(M([[0]])) is flint.nmod_mat:
2943-
continue
29442923
P = _poly_type_from_matrix_type(M)
29452924
M1234 = M([[1, 2], [3, 4]])
29462925
assert M1234.charpoly() == P([-2, -5, 1])
29472926
M9 = M([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
29482927
assert M9.charpoly() == P([3, -12, -16, 1])
29492928
Mr = M([[1, 2, 3], [4, 5, 6]])
2950-
# XXX: Fix fmpz_mat and fmpq_mat charpoly to not abort
2951-
if M is not flint.fmpz_mat and M is not flint.fmpq_mat:
2952-
assert raises(lambda: Mr.charpoly(), ValueError)
2929+
assert raises(lambda: Mr.charpoly(), ValueError)
29532930

29542931

29552932
def test_matrices_minpoly():
29562933
for M, S, is_field in _all_matrices():
2957-
# XXX: Add support for nmod_mat minpoly
2958-
if type(M([[0]])) is flint.nmod_mat:
2959-
continue
29602934
P = _poly_type_from_matrix_type(M)
29612935
M1234 = M([[1, 2], [3, 4]])
29622936
assert M1234.minpoly() == P([-2, -5, 1])
29632937
M9 = M([[2, 1, 0], [0, 2, 0], [0, 0, 2]])
29642938
assert M9.minpoly() == P([4, -4, 1])
29652939
Mr = M([[1, 2, 3], [4, 5, 6]])
2966-
# XXX: Fix fmpz_mat and fmpq_mat minpoly to not abort
2967-
if M is not flint.fmpz_mat and M is not flint.fmpq_mat:
2968-
assert raises(lambda: Mr.minpoly(), ValueError)
2940+
assert raises(lambda: Mr.minpoly(), ValueError)
29692941

29702942

29712943
def test_matrices_rank():
29722944
for M, S, is_field in _all_matrices():
2973-
# XXX: fmpq_mat doesn't support rank
2974-
if M is flint.fmpq_mat:
2975-
continue
29762945
M1234 = M([[1, 2], [3, 4]])
29772946
assert M1234.rank() == 2
29782947
Mr = M([[1, 2, 3], [4, 5, 6]])

0 commit comments

Comments
 (0)