From fd35ef0fcc09060a0fbd945a50e243631f06ceba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Tue, 3 May 2016 11:36:10 +0200 Subject: [PATCH] python3 print in matrices folder --- src/sage/matrix/benchmark.py | 78 ++++++++++--------- src/sage/matrix/echelon_matrix.pyx | 7 +- src/sage/matrix/matrix0.pyx | 11 +-- src/sage/matrix/matrix2.pyx | 13 ++-- src/sage/matrix/matrix_dense.pyx | 5 +- src/sage/matrix/matrix_gfpn_dense.pyx | 8 +- src/sage/matrix/matrix_integer_dense.pyx | 5 +- .../matrix/matrix_modn_dense_template.pxi | 14 ++-- src/sage/matrix/matrix_rational_dense.pyx | 8 +- src/sage/matrix/matrix_space.py | 10 ++- src/sage/matrix/matrix_sparse.pyx | 6 +- src/sage/matrix/strassen.pyx | 5 +- 12 files changed, 90 insertions(+), 80 deletions(-) diff --git a/src/sage/matrix/benchmark.py b/src/sage/matrix/benchmark.py index f955d3e6a61..d5f9da92bee 100644 --- a/src/sage/matrix/benchmark.py +++ b/src/sage/matrix/benchmark.py @@ -8,7 +8,7 @@ The basic command syntax is as follows:: sage: import sage.matrix.benchmark as b - sage: print "starting"; import sys; sys.stdout.flush(); b.report([b.det_ZZ], 'Test', systems=['sage']) + sage: print("starting"); import sys; sys.stdout.flush(); b.report([b.det_ZZ], 'Test', systems=['sage']) starting... ====================================================================== Test @@ -16,6 +16,8 @@ ... ====================================================================== """ +from __future__ import print_function + from constructor import random_matrix, Matrix from sage.rings.all import ZZ, QQ, GF from sage.misc.misc import cputime @@ -41,7 +43,7 @@ def report(F, title, systems = ['sage', 'magma'], **kwds): EXAMPLES:: sage: import sage.matrix.benchmark as b - sage: print "starting"; import sys; sys.stdout.flush(); b.report([b.det_ZZ], 'Test', systems=['sage']) + sage: print("starting"); import sys; sys.stdout.flush(); b.report([b.det_ZZ], 'Test', systems=['sage']) starting... ====================================================================== Test @@ -52,15 +54,15 @@ def report(F, title, systems = ['sage', 'magma'], **kwds): import os if len(systems) > 2: raise NotImplementedError("at most two systems ('sage' or 'magma')") - print '='*70 - print ' '*10 + title - print '='*70 + print('=' * 70) + print(' ' * 10 + title) + print('=' * 70) os.system('uname -a') - print '\n' + print('\n') for f in F: - print "-"*70 - print f.__doc__.strip() - print ('%15s'*len(systems))%tuple(systems) + print("-"*70) + print(f.__doc__.strip()) + print(('%15s' * len(systems)) % tuple(systems)) w = [] for s in systems: alarm(timeout) @@ -77,8 +79,8 @@ def report(F, title, systems = ['sage', 'magma'], **kwds): w.append(w[0]/w[1]) w = tuple(w) - print ('%15.3f'*len(w))%w - print '='*70 + print(('%15.3f'*len(w)) % w) + print('=' * 70) ####################################################################### @@ -97,7 +99,7 @@ def report_ZZ(**kwds): EXAMPLES:: sage: import sage.matrix.benchmark as b - sage: print "starting"; import sys; sys.stdout.flush(); b.report_ZZ(systems=['sage']) # long time (15s on sage.math, 2012) + sage: print("starting"); import sys; sys.stdout.flush(); b.report_ZZ(systems=['sage']) # long time (15s on sage.math, 2012) starting... ====================================================================== Dense benchmarks over ZZ @@ -147,7 +149,7 @@ def nullspace_ZZ(n=200, min=0, max=2**32, system='sage'): K := Kernel(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -186,7 +188,7 @@ def charpoly_ZZ(n=100, min=0, max=9, system='sage'): K := CharacteristicPolynomial(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -225,7 +227,7 @@ def rank_ZZ(n=700, min=0, max=9, system='sage'): K := Rank(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -263,7 +265,7 @@ def rank2_ZZ(n=400, min=0, max=2**64, system='sage'): K := Rank(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -303,7 +305,7 @@ def smithform_ZZ(n=128, min=0, max=9, system='sage'): K := ElementaryDivisors(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -348,7 +350,7 @@ def matrix_multiply_ZZ(n=300, min=-9, max=9, system='sage', times=1): end for; s := Cputime(t); """%(n,min,max,times) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s'))/times else: @@ -394,7 +396,7 @@ def matrix_add_ZZ(n=200, min=-9, max=9, system='sage', times=50): end for; s := Cputime(t); """%(n,min,max,times) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s'))/times else: @@ -454,7 +456,7 @@ def det_ZZ(n=200, min=1, max=100, system='sage'): d := Determinant(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -494,7 +496,7 @@ def det_QQ(n=300, num_bound=10, den_bound=10, system='sage'): d := Determinant(A); s := Cputime(t); """%(n,-num_bound, num_bound, den_bound) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -541,7 +543,7 @@ def vecmat_ZZ(n=300, min=-9, max=9, system='sage', times=200): end for; s := Cputime(t); """%(n,min,max,times) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s'))/times else: @@ -572,7 +574,7 @@ def report_GF(p=16411, **kwds): EXAMPLES:: sage: import sage.matrix.benchmark as b - sage: print "starting"; import sys; sys.stdout.flush(); b.report_GF(systems=['sage']) + sage: print("starting"); import sys; sys.stdout.flush(); b.report_GF(systems=['sage']) starting... ====================================================================== Dense benchmarks over GF with prime 16411 @@ -617,7 +619,7 @@ def nullspace_GF(n=300, p=16411, system='sage'): K := Kernel(A); s := Cputime(t); """%(n,p) - if verbose: print code + if verbose: print(code) magma.eval(code) return magma.eval('s') else: @@ -656,7 +658,7 @@ def charpoly_GF(n=100, p=16411, system='sage'): K := CharacteristicPolynomial(A); s := Cputime(t); """%(n,p) - if verbose: print code + if verbose: print(code) magma.eval(code) return magma.eval('s') else: @@ -697,7 +699,7 @@ def matrix_add_GF(n=1000, p=16411, system='sage',times=100): end for; s := Cputime(t); """%(n,p,p,times) - if verbose: print code + if verbose: print(code) magma.eval(code) return magma.eval('s') else: @@ -743,7 +745,7 @@ def matrix_multiply_GF(n=100, p=16411, system='sage', times=3): end for; s := Cputime(t); """%(n,p,times) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s'))/times else: @@ -780,7 +782,7 @@ def rank_GF(n=500, p=16411, system='sage'): K := Rank(A); s := Cputime(t); """%(n,p) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -816,7 +818,7 @@ def rank2_GF(n=500, p=16411, system='sage'): K := Rank(A); s := Cputime(t); """%(n,p) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -853,7 +855,7 @@ def det_GF(n=400, p=16411 , system='sage'): d := Determinant(A); s := Cputime(t); """%(n,p) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -915,7 +917,7 @@ def echelon_QQ(n=100, min=0, max=9, system='sage'): K := EchelonForm(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -954,7 +956,7 @@ def inverse_QQ(n=100, min=0, max=9, system='sage'): K := A^(-1); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -1000,7 +1002,7 @@ def matrix_multiply_QQ(n=100, bnd=2, system='sage', times=1): end for; s := Cputime(t); """%(n, A.name(), times) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s'))/times else: @@ -1037,7 +1039,7 @@ def det_hilbert_QQ(n=80, system='sage'): s := Cputime(tinit); delete h; """%n - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) @@ -1071,7 +1073,7 @@ def invert_hilbert_QQ(n=40, system='sage'): s := Cputime(tinit); delete h; """%n - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) @@ -1114,7 +1116,7 @@ def MatrixVector_QQ(n=1000,h=100,system='sage',times=1): end for; s := Cputime(t); """%(n,h,times) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -1162,7 +1164,7 @@ def nullspace_RR(n=300, min=0, max=10, system='sage'): K := Kernel(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: @@ -1202,7 +1204,7 @@ def nullspace_RDF(n=300, min=0, max=10, system='sage'): K := Kernel(A); s := Cputime(t); """%(n,min,max) - if verbose: print code + if verbose: print(code) magma.eval(code) return float(magma.eval('s')) else: diff --git a/src/sage/matrix/echelon_matrix.pyx b/src/sage/matrix/echelon_matrix.pyx index c17afc09601..f2332aecbfd 100644 --- a/src/sage/matrix/echelon_matrix.pyx +++ b/src/sage/matrix/echelon_matrix.pyx @@ -9,6 +9,7 @@ Echelon matrices over finite fields. # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** +from __future__ import print_function from sage.matrix.matrix0 cimport Matrix @@ -38,9 +39,9 @@ def reduced_echelon_matrix_iterator(K, k, n, bint sparse=False, bint copy=True, sage: from sage.matrix.echelon_matrix import reduced_echelon_matrix_iterator sage: it = reduced_echelon_matrix_iterator(GF(2),2,3) sage: for m in it: - ....: print m - ....: print m.pivots() - ....: print "*******" + ....: print(m) + ....: print(m.pivots()) + ....: print("*******") [1 0 0] [0 1 0] (0, 1) diff --git a/src/sage/matrix/matrix0.pyx b/src/sage/matrix/matrix0.pyx index b7a5d04e270..a444c00a22d 100644 --- a/src/sage/matrix/matrix0.pyx +++ b/src/sage/matrix/matrix0.pyx @@ -21,6 +21,7 @@ EXAMPLES:: # (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** +from __future__ import print_function from cpython cimport * @@ -1634,7 +1635,7 @@ cdef class Matrix(sage.structure.element.Matrix): sage: A = matrix([[1,2], [3,4], [5,6]]) sage: A.__repr__() '[1 2]\n[3 4]\n[5 6]' - sage: print A + sage: print(A) [1 2] [3 4] [5 6] @@ -1644,7 +1645,7 @@ cdef class Matrix(sage.structure.element.Matrix): sage: A = random_matrix(ZZ, 100) sage: A.__repr__() '100 x 100 dense matrix over Integer Ring' - sage: print A + sage: print(A) 100 x 100 dense matrix over Integer Ring When a big matrix returned, include a hint on how to get the entries. @@ -4252,9 +4253,9 @@ cdef class Matrix(sage.structure.element.Matrix): self.echelon_form() x = self.fetch('pivots') if x is None: - print self - print self.nrows() - print self.dict() + print(self) + print(self.nrows()) + print(self.dict()) raise RuntimeError("BUG: matrix pivots should have been set but weren't, matrix parent = '%s'"%self.parent()) return tuple(x) diff --git a/src/sage/matrix/matrix2.pyx b/src/sage/matrix/matrix2.pyx index 7753b90c116..ded02be7953 100644 --- a/src/sage/matrix/matrix2.pyx +++ b/src/sage/matrix/matrix2.pyx @@ -26,6 +26,7 @@ TESTS:: # the License, or (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** +from __future__ import print_function from cpython cimport * include "cysignals/signals.pxi" @@ -1283,11 +1284,11 @@ cdef class Matrix(matrix1.Matrix): sage: for algorithm in ("Godsil","Ryser","ButeraPernici"): ....: v = m.rook_vector(complement=True, use_complement=True, algorithm=algorithm) ....: if v != [1, 16, 78, 128, 53]: - ....: print "ERROR with algorithm={} use_complement=True".format(algorithm) + ....: print("ERROR with algorithm={} use_complement=True".format(algorithm)) ....: v = m.rook_vector(complement=True, use_complement=False, algorithm=algorithm) ....: v = m.rook_vector(complement=True, use_complement=False) ....: if v != [1, 16, 78, 128, 53]: - ....: print "ERROR with algorithm={} use_complement=False".format(algorithm) + ....: print("ERROR with algorithm={} use_complement=False".format(algorithm)) REFERENCES: @@ -2240,7 +2241,7 @@ cdef class Matrix(matrix1.Matrix): It's a little difficult to distinguish the variables. To fix this, we temporarily view the indeterminate as `Z`:: - sage: with localvars(f.parent(), 'Z'): print f + sage: with localvars(f.parent(), 'Z'): print(f) Z^2 + (-y^2 - x)*Z - x^2*y + x*y^2 We could also compute f in terms of Z from the start:: @@ -2628,11 +2629,11 @@ cdef class Matrix(matrix1.Matrix): sage: K. = CyclotomicField(3) sage: M = MatrixSpace(K,3,sparse=True) sage: A = M([(1+z)/3,(2+z)/3,z/3,1,1+z,-2,1,5,-1+z]) - sage: print A + sage: print(A) [1/3*z + 1/3 1/3*z + 2/3 1/3*z] [ 1 z + 1 -2] [ 1 5 z - 1] - sage: print A.denominator() + sage: print(A.denominator()) 3 """ if self.nrows() == 0 or self.ncols() == 0: @@ -8603,7 +8604,7 @@ explicitly setting the argument to `True` or `False` will avoid this message.""" sage: L.append((2,Permutation([1, 4, 2, 3, 5]))) sage: M = sum([c * p.to_matrix() for (c,p) in L]) sage: decomp = sage.combinat.permutation.bistochastic_as_sum_of_permutations(M) - sage: print decomp + sage: print(decomp) 2*B[[1, 4, 2, 3, 5]] + 3*B[[3, 1, 4, 2, 5]] + 9*B[[4, 1, 3, 5, 2]] + 6*B[[5, 3, 4, 1, 2]] An exception is raised when the matrix is not bistochastic:: diff --git a/src/sage/matrix/matrix_dense.pyx b/src/sage/matrix/matrix_dense.pyx index 9225064d304..e31a1995c56 100644 --- a/src/sage/matrix/matrix_dense.pyx +++ b/src/sage/matrix/matrix_dense.pyx @@ -7,6 +7,7 @@ TESTS:: sage: m = matrix(R,2,[0,a,b,b^2]) sage: TestSuite(m).run() """ +from __future__ import print_function cimport matrix @@ -137,10 +138,10 @@ cdef class Matrix_dense(matrix.Matrix): sage: M = MatrixSpace(QQ, 2) sage: A = M([1,2,3,4]) sage: B = A.transpose() - sage: print B + sage: print(B) [1 3] [2 4] - sage: print A + sage: print(A) [1 2] [3 4] diff --git a/src/sage/matrix/matrix_gfpn_dense.pyx b/src/sage/matrix/matrix_gfpn_dense.pyx index cbbd472b5d1..90d5198fb88 100644 --- a/src/sage/matrix/matrix_gfpn_dense.pyx +++ b/src/sage/matrix/matrix_gfpn_dense.pyx @@ -26,7 +26,7 @@ AUTHORS: # (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** - +from __future__ import print_function ## Define an environment variable that enables MeatAxe to find ## its multiplication tables. @@ -308,7 +308,7 @@ cdef class Matrix_gfpn_dense(Matrix_dense): EXAMPLES:: sage: M = MatrixSpace(GF(25,'z'),2,3)([1,2,3,4,5,6]) - sage: print M + sage: print(M) [1 2 3] [4 0 1] sage: type(M) # optional: meataxe @@ -549,7 +549,7 @@ cdef class Matrix_gfpn_dense(Matrix_dense): sage: M = MatrixSpace(GF(25,'x'), 3, 20)([20*[0],20*[0],[1]+19*[0]]) sage: N = copy(M) # indirect doctest - sage: print N + sage: print(N) [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] @@ -792,7 +792,7 @@ cdef class Matrix_gfpn_dense(Matrix_dense): # for i in range(r_min, r_max): # p = FfGetPtr(self.Data.Data, i) # for j from 0<=j sage: B = A.transpose() - sage: print B + sage: print(B) [0 3] [1 4] [2 5] - sage: print A + sage: print(A) [0 1 2] [3 4 5] diff --git a/src/sage/matrix/matrix_modn_dense_template.pxi b/src/sage/matrix/matrix_modn_dense_template.pxi index 12611abc045..d4f735dbe3e 100644 --- a/src/sage/matrix/matrix_modn_dense_template.pxi +++ b/src/sage/matrix/matrix_modn_dense_template.pxi @@ -49,8 +49,8 @@ We test corner cases for multiplication:: sage: m10 = matrix(GF(3),1,0,[]) sage: m11 = matrix(GF(3),1,1,[1]) sage: good = [ (v0,m00), (v0,m01), (v1,m10), (v1,m11), (m00,v0), (m10,v0), (m01,v1), (m11,v1), (m00,m00), (m01,m10), (m10,m01), (m11,m11) ] - sage: for v,m in good: - ... print v, 'x', m, '=', v*m + sage: for v, m in good: + ....: print('{} x {} = {}'.format(v, m, v * m) () x [] = () () x [] = (0) (1) x [] = () @@ -66,11 +66,11 @@ We test corner cases for multiplication:: sage: bad = [ (v1,m00), (v1,m01), (v0,m10), (v0,m11), (m00,v1), (m10,v1), (m01,v0), (m11,v0), (m01,m01), (m10,m10), (m11,m01), (m10,m11) ] sage: for v,m in bad: - ... try: - ... v*m - ... print 'Uncaught dimension mismatch!' - ... except (TypeError, ArithmeticError): - ... pass + ....: try: + ....: v*m + ....: print('Uncaught dimension mismatch!') + ....: except (TypeError, ArithmeticError): + ....: pass """ diff --git a/src/sage/matrix/matrix_rational_dense.pyx b/src/sage/matrix/matrix_rational_dense.pyx index 19613e3c5e4..0344f09a56d 100644 --- a/src/sage/matrix/matrix_rational_dense.pyx +++ b/src/sage/matrix/matrix_rational_dense.pyx @@ -50,7 +50,7 @@ TESTS:: # (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** - +from __future__ import print_function from sage.modules.vector_rational_dense cimport Vector_rational_dense @@ -2308,17 +2308,17 @@ cdef class Matrix_rational_dense(matrix_dense.Matrix_dense): sage: type(A) sage: B = A.transpose() - sage: print B + sage: print(B) [0 3] [1 4] [2 5] - sage: print A + sage: print(A) [0 1 2] [3 4 5] ``.T`` is a convenient shortcut for the transpose:: - sage: print A.T + sage: print(A.T) [0 3] [1 4] [2 5] diff --git a/src/sage/matrix/matrix_space.py b/src/sage/matrix/matrix_space.py index 376fa3c26bb..e79784dd5e1 100644 --- a/src/sage/matrix/matrix_space.py +++ b/src/sage/matrix/matrix_space.py @@ -31,7 +31,7 @@ # (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** - +from __future__ import print_function # System imports import sys @@ -792,7 +792,9 @@ def __iter__(self): sage: a = list(MS) sage: len(a) 16 - sage: for m in a: print m, '\n-' + sage: for m in a: + ....: print(m) + ....: print('-') [0 0] [0 0] - @@ -1772,14 +1774,14 @@ def test_trivial_matrices_inverse(ring, sparse=True, checkrank=True): assert(not mn0.is_invertible()) try: d = mn0.determinant() - print d + print(d) res = False except ValueError: res = True assert(res) try: mn0.inverse() - res =False + res = False except ArithmeticError: res = True assert(res) diff --git a/src/sage/matrix/matrix_sparse.pyx b/src/sage/matrix/matrix_sparse.pyx index 82b82c4e425..b16142e2d42 100644 --- a/src/sage/matrix/matrix_sparse.pyx +++ b/src/sage/matrix/matrix_sparse.pyx @@ -9,7 +9,7 @@ Base class for sparse matrices # (at your option) any later version. # http://www.gnu.org/licenses/ #***************************************************************************** - +from __future__ import print_function cimport cython cimport matrix @@ -376,10 +376,10 @@ cdef class Matrix_sparse(matrix.Matrix): sage: M = MatrixSpace(QQ, 2, sparse=True) sage: A = M([1,2,3,4]) sage: B = A.transpose() - sage: print B + sage: print(B) [1 3] [2 4] - sage: print A + sage: print(A) [1 2] [3 4] diff --git a/src/sage/matrix/strassen.pyx b/src/sage/matrix/strassen.pyx index 8c886ceebd3..f36ac13e2c7 100644 --- a/src/sage/matrix/strassen.pyx +++ b/src/sage/matrix/strassen.pyx @@ -13,7 +13,7 @@ multiplication algorithms. # # http://www.gnu.org/licenses/ ################################################################################ - +from __future__ import print_function from matrix_window cimport MatrixWindow @@ -793,7 +793,8 @@ def test(n, m, R, c=2): EXAMPLES:: sage: from sage.matrix.strassen import test - sage: for n in range(5): print n, test(2*n,n,Frac(QQ['x']),2) + sage: for n in range(5): + ....: print("{} {}".format(n, test(2*n,n,Frac(QQ['x']),2))) 0 True 1 True 2 True