From 8b11d8f748c14ed23a0433c97e603d1f3d734f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Chapoton?= Date: Sat, 16 Sep 2023 18:41:14 +0200 Subject: [PATCH] remove unused vars in matrix/ --- src/sage/matrix/matrix0.pyx | 10 ++++---- src/sage/matrix/matrix2.pyx | 29 ++++++++++-------------- src/sage/matrix/matrix_cyclo_dense.pyx | 25 +++++++------------- src/sage/matrix/matrix_integer_dense.pyx | 25 +++++++------------- src/sage/matrix/matrix_mod2_dense.pyx | 6 +---- src/sage/matrix/matrix_modn_sparse.pyx | 8 ++----- src/sage/matrix/matrix_sparse.pyx | 6 ++--- 7 files changed, 38 insertions(+), 71 deletions(-) diff --git a/src/sage/matrix/matrix0.pyx b/src/sage/matrix/matrix0.pyx index cfdc96ec146..d781fc822b0 100644 --- a/src/sage/matrix/matrix0.pyx +++ b/src/sage/matrix/matrix0.pyx @@ -524,7 +524,7 @@ cdef class Matrix(sage.structure.element.Matrix): This is fast since it is a cdef function and there is no bounds checking. """ - raise NotImplementedError("this must be defined in the derived class (type=%s)"%type(self)) + raise NotImplementedError("this must be defined in the derived class (type=%s)" % type(self)) cdef get_unsafe(self, Py_ssize_t i, Py_ssize_t j): """ @@ -1396,7 +1396,6 @@ cdef class Matrix(sage.structure.element.Matrix): """ cdef list row_list cdef list col_list - cdef object index cdef Py_ssize_t row_list_len, col_list_len cdef list value_list cdef bint value_list_one_dimensional = 0 @@ -2241,7 +2240,7 @@ cdef class Matrix(sage.structure.element.Matrix): tmp = [align*(b-a) for a,b in zip([0] + col_divs, col_divs + [nc])] format = '|'.join(tmp) - return "\\left" + matrix_delimiters[0] + "\\begin{array}{%s}\n"%format + s + "\n\\end{array}\\right" + matrix_delimiters[1] + return "\\left" + matrix_delimiters[0] + "\\begin{array}{%s}\n" % format + s + "\n\\end{array}\\right" + matrix_delimiters[1] ################################################### ## Basic Properties @@ -2345,7 +2344,6 @@ cdef class Matrix(sage.structure.element.Matrix): if self._nrows != self._ncols: raise ArithmeticError("self must be a square matrix") - F = f.base_ring() vars = f.parent().gens() n = len(self.rows()) ans = [] @@ -3746,7 +3744,7 @@ cdef class Matrix(sage.structure.element.Matrix): """ cdef dict d = {} cdef list queue = list(range(self._ncols)) - cdef int l, sign, i, j + cdef int l, sign, i if skew: # testing the diagonal entries to be zero @@ -4638,7 +4636,7 @@ cdef class Matrix(sage.structure.element.Matrix): 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()) + raise RuntimeError("BUG: matrix pivots should have been set but weren't, matrix parent = '%s'" % self.parent()) return tuple(x) def rank(self): diff --git a/src/sage/matrix/matrix2.pyx b/src/sage/matrix/matrix2.pyx index 1e871d9e553..0623b33c171 100644 --- a/src/sage/matrix/matrix2.pyx +++ b/src/sage/matrix/matrix2.pyx @@ -541,7 +541,7 @@ cdef class Matrix(Matrix1): ... ValueError: matrix equation has no solutions - A ValueError is raised if the input is invalid:: + A :class:`ValueError` is raised if the input is invalid:: sage: A = matrix(QQ,4,2, [0, -1, 1, 0, -2, 2, 1, 0]) sage: B = matrix(QQ,2,2, [1, 0, 1, -1]) @@ -2184,7 +2184,7 @@ cdef class Matrix(Matrix1): Compute the determinant of the upper-left level x level submatrix of self. Does not handle degenerate cases, level MUST be >= 2 """ - cdef Py_ssize_t n, i + cdef Py_ssize_t i if level == 2: return self.get_unsafe(0,0) * self.get_unsafe(1,1) - self.get_unsafe(0,1) * self.get_unsafe(1,0) else: @@ -3316,8 +3316,7 @@ cdef class Matrix(Matrix1): 3 """ if self.nrows() == 0 or self.ncols() == 0: - return ZZ(1) - R = self.base_ring() + return ZZ.one() x = self.list() try: d = x[0].denominator() @@ -3905,7 +3904,7 @@ cdef class Matrix(Matrix1): """ tm = verbose("computing right kernel matrix over a domain for %sx%s matrix" % (self.nrows(), self.ncols()), level=1) - d, u, v = self.smith_form() + d, _, v = self.smith_form() basis = [] cdef Py_ssize_t i, nrows = self._nrows for i in range(self._ncols): @@ -6098,7 +6097,7 @@ cdef class Matrix(Matrix1): # subrings of fields of which an algebraic closure is implemented. if format is None: try: - F = self.base_ring().fraction_field().algebraic_closure() + self.base_ring().fraction_field().algebraic_closure() return 'all' except (NotImplementedError, AttributeError): return 'galois' @@ -6922,7 +6921,6 @@ cdef class Matrix(Matrix1): from warnings import warn warn("Using generic algorithm for an inexact ring, which may result in garbage from numerical precision issues.") - V = [] from sage.categories.homset import hom eigenspaces = self.eigenspaces_left(format='galois', algebraic_multiplicity=True) evec_list=[] @@ -8068,7 +8066,7 @@ cdef class Matrix(Matrix1): if self.fetch('in_echelon_form'): return self.fetch('pivots') - tm = verbose('generic in-place Gauss elimination on %s x %s matrix using %s algorithm'%(self._nrows, self._ncols, algorithm)) + _ = verbose('generic in-place Gauss elimination on %s x %s matrix using %s algorithm' % (self._nrows, self._ncols, algorithm)) self.check_mutability() cdef Matrix A @@ -8367,7 +8365,6 @@ cdef class Matrix(Matrix1): """ if subdivide not in [True, False]: raise TypeError("subdivide must be True or False, not %s" % subdivide) - R = self.base_ring() ident = self.matrix_space(self.nrows(), self.nrows()).one() E = self.augment(ident) extended = E.echelon_form(**kwds) @@ -12227,7 +12224,7 @@ cdef class Matrix(Matrix1): Returns a pair (F, C) such that the rows of C form a symplectic basis for self and F = C \* self \* C.transpose(). - Raises a ValueError if not over a field, or self is not + Raises a :class:`ValueError` if not over a field, or self is not anti-symmetric, or self is not alternating. Anti-symmetric means that `M = -M^t`. Alternating means @@ -15396,7 +15393,7 @@ cdef class Matrix(Matrix1): if p == 2: A = self.change_ring(CDF) A = A.conjugate().transpose() * A - U, S, V = A.SVD() + S = A.SVD()[1] return max(S.list()).real().sqrt() A = self.apply_map(abs, R=RDF) @@ -15887,7 +15884,6 @@ cdef class Matrix(Matrix1): dd, uu, vv = mm.smith_form(transformation=True) else: dd = mm.smith_form(transformation=False) - mone = self.new_matrix(1, 1, [1]) d = dd.new_matrix(1,1,[t[0,0]]).block_sum(dd) if transformation: u = uu.new_matrix(1,1,[1]).block_sum(uu) * u @@ -16081,18 +16077,17 @@ cdef class Matrix(Matrix1): sage: U * A == H True """ - left, H, pivots = self._echelon_form_PID() + left, H, _ = self._echelon_form_PID() if not include_zero_rows: i = H.nrows() - 1 while H.row(i) == 0: i -= 1 - H = H[:i+1] + H = H[:i + 1] if transformation: - left = left[:i+1] + left = left[:i + 1] if transformation: return H, left - else: - return H + return H def _echelon_form_PID(self): r""" diff --git a/src/sage/matrix/matrix_cyclo_dense.pyx b/src/sage/matrix/matrix_cyclo_dense.pyx index f1ed9af8403..93f1f21a4f8 100644 --- a/src/sage/matrix/matrix_cyclo_dense.pyx +++ b/src/sage/matrix/matrix_cyclo_dense.pyx @@ -40,7 +40,7 @@ AUTHORS: # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. -# http://www.gnu.org/licenses/ +# https://www.gnu.org/licenses/ #***************************************************************************** from cysignals.signals cimport sig_on, sig_off @@ -51,7 +51,7 @@ from sage.structure.element cimport Element from sage.misc.randstate cimport randstate, current_randstate from sage.libs.gmp.randomize cimport * -from sage.libs.flint.types cimport fmpz_t, fmpq +from sage.libs.flint.types cimport fmpz_t from sage.libs.flint.fmpz cimport fmpz_init, fmpz_clear, fmpz_set_mpz, fmpz_one, fmpz_get_mpz, fmpz_add, fmpz_mul, fmpz_sub, fmpz_mul_si, fmpz_mul_si, fmpz_mul_si, fmpz_divexact, fmpz_lcm from sage.libs.flint.fmpq cimport fmpq_is_zero, fmpq_set_mpq, fmpq_canonicalise from sage.libs.flint.fmpq_mat cimport fmpq_mat_entry_num, fmpq_mat_entry_den, fmpq_mat_entry @@ -327,7 +327,7 @@ cdef class Matrix_cyclo_dense(Matrix_dense): cdef Py_ssize_t k, c cdef NumberFieldElement x cdef NumberFieldElement_quadratic xq - cdef mpz_t quo, tmp + cdef mpz_t tmp cdef fmpz_t denom, ftmp cdef ZZ_c coeff @@ -1036,7 +1036,6 @@ cdef class Matrix_cyclo_dense(Matrix_dense): """ cdef Py_ssize_t i cdef Matrix_rational_dense mat = self._matrix - cdef fmpq * entry cdef mpq_t tmp sig_on() @@ -1198,8 +1197,6 @@ cdef class Matrix_cyclo_dense(Matrix_dense): if self._nrows <= 3: return max(1, 3*B, 6*B**2, 4*B**3) - # This is an approximation to 2^(5/6*log_2(5) - 2/3*log_2(6)) - alpha = RealNumber('1.15799718800731') # This is 2*e^(1-(2(7\gamma-4))/(13(3-2\gamma))), where \gamma # is Euler's constant. delta = RealNumber('5.418236') @@ -1333,20 +1330,20 @@ cdef class Matrix_cyclo_dense(Matrix_dense): [4 0 0] [0 0 0] """ - tm = verbose("Computing characteristic polynomial of cyclotomic matrix modulo %s."%p) + tm = verbose("Computing characteristic polynomial of cyclotomic matrix modulo %s." % p) # Reduce self modulo all primes over p - R, denom = self._reductions(p) + R, _ = self._reductions(p) # Compute the characteristic polynomial of each reduced matrix F = [A.charpoly('x') for A in R] # Put the characteristic polynomials together as the rows of a mod-p matrix k = R[0].base_ring() - S = matrix(k, len(F), self.nrows()+1, [f.list() for f in F]) + S = matrix(k, len(F), self.nrows() + 1, [f.list() for f in F]) # multiply by inverse of reduction matrix to lift _, L = self._reduction_matrix(p) X = L * S # Now the columns of the matrix X define the entries of the # charpoly modulo p. - verbose("Finished computing charpoly mod %s."%p, tm) + verbose("Finished computing charpoly mod %s." % p, tm) return X def _charpoly_multimodular(self, var='x', proof=None): @@ -1676,7 +1673,6 @@ cdef class Matrix_cyclo_dense(Matrix_dense): [ 1 0 7/19] [ 0 1 3/19] """ - cdef int i cdef Matrix_cyclo_dense res cdef bint is_square @@ -1827,14 +1823,11 @@ cdef class Matrix_cyclo_dense(Matrix_dense): Traceback (most recent call last): ... ValueError: echelon form mod 7 not defined - """ - cdef Matrix_cyclo_dense res cdef int i # Initialize variables - is_square = self._nrows == self._ncols - ls, denom = self._reductions(p) + ls, _ = self._reductions(p) # Find our first echelon form, and the associated list # of pivots @@ -1936,13 +1929,11 @@ cdef class Matrix_cyclo_dense(Matrix_dense): X = R._generator_matrix() d = self._degree MS = MatrixSpace(QQ, d, d) - mlst = self.list() for c in self._matrix.columns(): v = c.list() for n in range(d-1): c = c * X v += c.list() - temp = MS(v) rmul = MS([v[d*i+j] for j in range(d) for i in range(d)]) # We take the transpose l.append(rmul * A._rational_matrix()) diff --git a/src/sage/matrix/matrix_integer_dense.pyx b/src/sage/matrix/matrix_integer_dense.pyx index bf8819bd30a..8c276f2ca29 100644 --- a/src/sage/matrix/matrix_integer_dense.pyx +++ b/src/sage/matrix/matrix_integer_dense.pyx @@ -520,7 +520,6 @@ cdef class Matrix_integer_dense(Matrix_dense): # TODO: *maybe* redo this to use mpz_import and mpz_export # from sec 5.14 of the GMP manual. ?? cdef int i, j, len_so_far, m, n - cdef char *a cdef char *s cdef char *t cdef char *tmp @@ -1537,17 +1536,13 @@ cdef class Matrix_integer_dense(Matrix_dense): """ cdef Integer h cdef Matrix_integer_dense left = self - cdef mod_int *moduli - cdef int i, n, k + cdef int i, k nr = left._nrows nc = right._ncols - snc = left._ncols - cdef Matrix_integer_dense result - h = left.height() * right.height() * left.ncols() verbose('multiplying matrices of height %s and %s'%(left.height(),right.height())) mm = MultiModularBasis(h) @@ -1601,7 +1596,6 @@ cdef class Matrix_integer_dense(Matrix_dense): from .matrix_modn_dense_double import MAX_MODULUS as MAX_MODULUS_DOUBLE cdef Py_ssize_t i, j - cdef mpz_t* self_row cdef float* res_row_f cdef Matrix_modn_dense_float res_f @@ -2015,7 +2009,7 @@ cdef class Matrix_integer_dense(Matrix_dense): if ans is not None: return ans - cdef Matrix_integer_dense H_m,w,U + cdef Matrix_integer_dense H_m, U cdef Py_ssize_t nr, nc, n, i, j nr = self._nrows nc = self._ncols @@ -3757,7 +3751,6 @@ cdef class Matrix_integer_dense(Matrix_dense): if not self.is_square(): raise ValueError("self must be a square matrix") - cdef Py_ssize_t n = self.nrows() cdef Integer det = Integer() cdef fmpz_t e @@ -4884,7 +4877,7 @@ cdef class Matrix_integer_dense(Matrix_dense): [ 0 0 545], [0, 1, 2] ) """ - cdef Py_ssize_t i, j, piv, n = self._nrows, m = self._ncols + cdef Py_ssize_t i, j, n = self._nrows, m = self._ncols from .constructor import matrix @@ -5380,9 +5373,8 @@ cdef class Matrix_integer_dense(Matrix_dense): n = ns + na cdef Matrix_integer_dense Z - Z = self.new_matrix(nrows = m, ncols = n) - cdef Py_ssize_t i, j, p, qs, qa - p, qs, qa = 0, 0, 0 + Z = self.new_matrix(nrows=m, ncols=n) + cdef Py_ssize_t i, j for i from 0 <= i < m: for j from 0 <= j < ns: fmpz_set(fmpz_mat_entry(Z._matrix,i,j), @@ -5455,7 +5447,7 @@ cdef class Matrix_integer_dense(Matrix_dense): Return matrix obtained from self by deleting all zero columns along with the positions of those columns. - OUTPUT: matrix list of integers + OUTPUT: (matrix, list of integers) EXAMPLES:: @@ -5468,10 +5460,9 @@ cdef class Matrix_integer_dense(Matrix_dense): [-1 5], [1] ) """ - C = self.columns() - zero_cols = [i for i,v in enumerate(self.columns()) if v.is_zero()] + zero_cols = [i for i, v in enumerate(self.columns()) if v.is_zero()] s = set(zero_cols) - nonzero_cols = [i for i in range(self.ncols()) if not (i in s)] + nonzero_cols = [i for i in range(self.ncols()) if i not in s] return self.matrix_from_columns(nonzero_cols), zero_cols def _insert_zero_columns(self, cols): diff --git a/src/sage/matrix/matrix_mod2_dense.pyx b/src/sage/matrix/matrix_mod2_dense.pyx index 777ce0b409f..6365eb271aa 100644 --- a/src/sage/matrix/matrix_mod2_dense.pyx +++ b/src/sage/matrix/matrix_mod2_dense.pyx @@ -500,7 +500,6 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse i = i + self._nrows if from_list: return self.rows(copy=False)[i] - cdef Py_ssize_t j cdef Vector_mod2_dense z = Vector_mod2_dense.__new__(Vector_mod2_dense) global VectorSpace if VectorSpace is None: @@ -881,8 +880,6 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse sage: A^(-1) [] """ - cdef int k = 0 - cdef mzd_t *I cdef Matrix_mod2_dense A if self._nrows != self._ncols: @@ -1033,7 +1030,7 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse self.cache('rank', 0) self.cache('pivots', ()) return self - cdef int k, n, full + cdef int k, full full = int(reduced) @@ -1759,7 +1756,6 @@ cdef class Matrix_mod2_dense(matrix_dense.Matrix_dense): # dense or sparse """ cdef Py_ssize_t i, j, k, n cdef char *s - cdef char *t if self._nrows == 0 or self._ncols == 0: data = '' diff --git a/src/sage/matrix/matrix_modn_sparse.pyx b/src/sage/matrix/matrix_modn_sparse.pyx index 84cfbedb226..80a74bf1761 100644 --- a/src/sage/matrix/matrix_modn_sparse.pyx +++ b/src/sage/matrix/matrix_modn_sparse.pyx @@ -225,7 +225,7 @@ cdef class Matrix_modn_sparse(Matrix_sparse): if d is not None: return d - cdef Py_ssize_t i, j, k + cdef Py_ssize_t i, j d = {} cdef IntegerMod_int n for i from 0 <= i < self._nrows: @@ -310,10 +310,6 @@ cdef class Matrix_modn_sparse(Matrix_sparse): v = &(right.rows[i]) for j in range(v.num_nonzero): ( nonzero_positions_in_columns[v.positions[j]]).add(i) - # pre-computes the list of nonzero columns of right - cdef list right_indices - right_indices = [j for j in range(right._ncols) - if nonzero_positions_in_columns[j]] ans = self.new_matrix(self._nrows, right._ncols) @@ -423,7 +419,7 @@ cdef class Matrix_modn_sparse(Matrix_sparse): self.check_mutability() cdef Py_ssize_t i, r, c, min, min_row, start_row - cdef int a0, a_inverse, b, do_verb + cdef int a_inverse, b, do_verb cdef c_vector_modint tmp start_row = 0 pivots = [] diff --git a/src/sage/matrix/matrix_sparse.pyx b/src/sage/matrix/matrix_sparse.pyx index 6c4199340c9..15f4cb093ee 100644 --- a/src/sage/matrix/matrix_sparse.pyx +++ b/src/sage/matrix/matrix_sparse.pyx @@ -927,18 +927,18 @@ cdef class Matrix_sparse(matrix.Matrix): if not isinstance(columns, (list, tuple)): columns = list(columns) - cdef Py_ssize_t nrows, ncols,k,r,i,j + cdef Py_ssize_t nrows, ncols, k, i, j ncols = PyList_GET_SIZE(columns) nrows = PyList_GET_SIZE(rows) cdef Matrix_sparse A = self.new_matrix(nrows = nrows, ncols = ncols) - tmp = [el for el in columns if el >= 0 and el < self._ncols] + tmp = [el for el in columns if 0 <= el < self._ncols] columns = tmp if ncols != PyList_GET_SIZE(columns): raise IndexError("column index out of range") - tmp = [el for el in rows if el >= 0 and el < self._nrows] + tmp = [el for el in rows if 0 <= el < self._nrows] rows = tmp if nrows != PyList_GET_SIZE(rows): raise IndexError("row index out of range")