Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Clean up __cinit__ methods of matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Jun 7, 2018
1 parent cb52ee3 commit c8fa7bb
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 228 deletions.
24 changes: 9 additions & 15 deletions src/sage/matrix/matrix0.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import sage.rings.integer
from sage.arith.power cimport generic_power
from sage.misc.misc import verbose, get_verbose
from sage.structure.sequence import Sequence
from sage.structure.parent cimport Parent

cimport sage.structure.element
from sage.structure.element cimport ModuleElement, Element, RingElement, Vector
Expand Down Expand Up @@ -93,20 +94,11 @@ cdef class Matrix(sage.structure.element.Matrix):
[1]
[2]
"""
def __cinit__(self):
self.hash = -1

def __init__(self, parent):
def __cinit__(self, parent, *args, **kwds):
"""
The initialization routine of the ``Matrix`` base class ensures
that it sets the attributes ``self._parent``, ``self._base_ring``,
``self._nrows``, ``self._ncols``. It sets the latter ones by
accessing the relevant information on ``parent``, which is often
slower than what a more specific subclass can do.
Subclasses of ``Matrix`` can safely skip calling
``Matrix.__init__`` provided they take care of initializing these
attributes themselves.
``self._nrows``, ``self._ncols``.
The private attributes ``self._is_immutable`` and ``self._cache``
are implicitly initialized to valid values upon memory allocation.
Expand All @@ -118,10 +110,12 @@ cdef class Matrix(sage.structure.element.Matrix):
sage: type(A)
<type 'sage.matrix.matrix0.Matrix'>
"""
self._parent = parent
self._base_ring = parent.base_ring()
self._nrows = parent.nrows()
self._ncols = parent.ncols()
P = <Parent?>parent
self._parent = P
self._base_ring = P._base
self._nrows = P.nrows()
self._ncols = P.ncols()
self.hash = -1

def list(self):
"""
Expand Down
15 changes: 1 addition & 14 deletions src/sage/matrix/matrix_complex_ball_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,10 @@ cdef class Matrix_complex_ball_dense(Matrix_dense):
sage: matrix(CBF, 1, 3, [1, 2, -3])
[ 1.000000000000000 2.000000000000000 -3.000000000000000]
"""
def __cinit__(self,
parent,
entries,
coerce,
copy):
def __cinit__(self):
"""
Create and allocate memory for the matrix.
INPUT:
- ``parent, entries, coerce, copy`` - as for
``__init__``.
EXAMPLES::
sage: from sage.matrix.matrix_complex_ball_dense import Matrix_complex_ball_dense
Expand All @@ -151,10 +142,6 @@ cdef class Matrix_complex_ball_dense(Matrix_dense):
sage: type(a)
<type 'sage.matrix.matrix_complex_ball_dense.Matrix_complex_ball_dense'>
"""
self._parent = parent
self._base_ring = parent.base_ring()
self._nrows = parent.nrows()
self._ncols = parent.ncols()
sig_str("Arb exception")
acb_mat_init(self.value, self._nrows, self._ncols)
sig_off()
Expand Down
2 changes: 1 addition & 1 deletion src/sage/matrix/matrix_complex_double_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ cdef class Matrix_complex_double_dense(Matrix_double_dense):
See the commands qr, lu, and svd for QR, LU, and singular value
decomposition.
"""
def __cinit__(self, parent, entries, copy, coerce):
def __cinit__(self):
global numpy
if numpy is None:
import numpy
Expand Down
13 changes: 2 additions & 11 deletions src/sage/matrix/matrix_cyclo_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,8 @@ echelon_verbose_level = 1


cdef class Matrix_cyclo_dense(Matrix_dense):
def __cinit__(self, parent, entries, coerce, copy):
def __cinit__(self):
"""
Create a new dense cyclotomic matrix.
INPUT:
parent -- a matrix space over a cyclotomic field
entries -- a list of entries or scalar
coerce -- bool; if true entries are coerced to base ring
copy -- bool; ignored due to underlying data structure
EXAMPLES::
sage: from sage.matrix.matrix_cyclo_dense import Matrix_cyclo_dense
Expand All @@ -103,14 +95,13 @@ cdef class Matrix_cyclo_dense(Matrix_dense):
<type 'sage.matrix.matrix_cyclo_dense.Matrix_cyclo_dense'>
Note that the entries of A haven't even been set yet above; that doesn't
happen until init is called::
happen until ``__init__`` is called::
sage: A[0,0]
Traceback (most recent call last):
...
ValueError: matrix entries not yet initialized
"""
Matrix.__init__(self, parent)
self._degree = self._base_ring.degree()
self._n = int(self._base_ring._n())

Expand Down
7 changes: 0 additions & 7 deletions src/sage/matrix/matrix_double_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,6 @@ cdef class Matrix_double_dense(Matrix_dense):
6694819972852100501 # 64-bit
1829383573 # 32-bit
"""
def __cinit__(self, parent, entries, copy, coerce):
"""
Set up a new matrix
"""
Matrix_dense.__init__(self,parent)
return

def __create_matrix__(self):
"""
Create a new uninitialized numpy matrix to hold the data for the class.
Expand Down
5 changes: 1 addition & 4 deletions src/sage/matrix/matrix_gap.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ cdef class Matrix_gap(Matrix_dense):
[0]
"""
ma = MatrixArgs_init(parent, entries)
Matrix_dense.__init__(self, ma.space)
it = ma.iter(coerce)
cdef list mat = []
cdef long i, j
Expand All @@ -132,9 +131,7 @@ cdef class Matrix_gap(Matrix_dense):
else:
P = self.matrix_space(nrows, ncols)

cdef Matrix_gap M = Matrix_gap.__new__(Matrix_gap, P, None, None, None)
Matrix_dense.__init__(M, P)
return M
return Matrix_gap.__new__(Matrix_gap, P)

def __copy__(self):
r"""
Expand Down
15 changes: 5 additions & 10 deletions src/sage/matrix/matrix_generic_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,20 @@ cdef class Matrix_generic_dense(matrix_dense.Matrix_dense):
sage: Matrix_generic_dense(M, (x, y), True, True)
[x y]
"""
matrix.Matrix.__init__(self, parent)
ma = MatrixArgs_init(parent, entries)
self._entries = ma.list(coerce)

cdef Matrix_generic_dense _new(self, Py_ssize_t nrows, Py_ssize_t ncols):
r"""
Return a new dense matrix with no entries set.
"""
cdef Matrix_generic_dense res
res = self.__class__.__new__(self.__class__, 0, 0, 0)

if nrows == self._nrows and ncols == self._ncols:
res._parent = self._parent
MS = self._parent
else:
res._parent = self.matrix_space(nrows, ncols)
res._ncols = ncols
res._nrows = nrows
res._base_ring = self._base_ring
return res
MS = self.matrix_space(nrows, ncols)

cdef type t = <type>type(self)
return <Matrix_generic_dense>t.__new__(t, MS)

cdef set_unsafe(self, Py_ssize_t i, Py_ssize_t j, value):
self._entries[i*self._ncols + j] = value
Expand Down
5 changes: 2 additions & 3 deletions src/sage/matrix/matrix_generic_sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ cdef class Matrix_generic_sparse(matrix_sparse.Matrix_sparse):
Secondly, there is no fast way to access non-zero elements in a given
row/column.
"""
def __cinit__(self, parent, entries=0, coerce=True, copy=True):
def __cinit__(self):
self._entries = {} # crucial so that pickling works
self._zero = self._base_ring.zero()

def __init__(self, parent, entries=None, copy=None, bint coerce=True):
r"""
Expand Down Expand Up @@ -166,8 +167,6 @@ cdef class Matrix_generic_sparse(matrix_sparse.Matrix_sparse):
[0 0]
[0 1]
"""
matrix.Matrix.__init__(self, parent)
self._zero = self.base_ring().zero()
ma = MatrixArgs_init(parent, entries)
self._entries = ma.dict(coerce)

Expand Down
29 changes: 7 additions & 22 deletions src/sage/matrix/matrix_gf2e_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,13 @@ cdef class M4RIE_finite_field:
"""
cdef gf2e *ff

def __cinit__(self):
def __dealloc__(self):
"""
EXAMPLES::
sage: from sage.matrix.matrix_gf2e_dense import M4RIE_finite_field
sage: K = M4RIE_finite_field(); K
<sage.matrix.matrix_gf2e_dense.M4RIE_finite_field object at 0x...>
"""
pass

def __dealloc__(self):
"""
EXAMPLES::
sage: from sage.matrix.matrix_gf2e_dense import M4RIE_finite_field
sage: K = M4RIE_finite_field()
sage: del K
"""
if self.ff:
Expand All @@ -141,17 +132,12 @@ cdef m4ri_word poly_to_word(f):
cdef object word_to_poly(w, F):
return F.fetch_int(w)


cdef class Matrix_gf2e_dense(matrix_dense.Matrix_dense):
def __cinit__(self, parent, entries, copy, coerce, alloc=True):
def __cinit__(self, *args, bint alloc=True, **kwds):
"""
Create new matrix over `GF(2^e)` for 2<=e<=10.
INPUT:
- ``parent`` - a :class:`MatrixSpace`.
- ``entries`` - may be list or a finite field element.
- ``copy`` - ignored, elements are always copied
- ``coerce`` - ignored, elements are always coerced
- ``alloc`` - if ``True`` the matrix is allocated first (default: ``True``)
EXAMPLES::
Expand All @@ -178,14 +164,13 @@ cdef class Matrix_gf2e_dense(matrix_dense.Matrix_dense):
[ a^2 + 1 a^2 + a + 1 a^2 + 1 a^2]
[ a^2 + a a^2 + 1 a^2 + a + 1 a + 1]
"""
matrix_dense.Matrix_dense.__init__(self, parent)

cdef M4RIE_finite_field FF

R = parent.base_ring()

R = self._base_ring
f = R.polynomial()
cdef m4ri_word poly = sum(int(c)*2**i for i,c in enumerate(f))

cdef long i
cdef m4ri_word poly = sum(((<m4ri_word>c) << i) for (i, c) in enumerate(f))

if alloc and self._nrows and self._ncols:
if poly in _m4rie_finite_field_cache:
Expand Down
22 changes: 10 additions & 12 deletions src/sage/matrix/matrix_gfpn_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ cdef Matrix_gfpn_dense new_mtx(Matrix_t* mat, Matrix_gfpn_dense template):
MS = MatrixSpace(B, mat.Nor, mat.Noc, implementation=Matrix_gfpn_dense)

ret = <Matrix_gfpn_dense>Matrix_gfpn_dense.__new__(Matrix_gfpn_dense, MS)
Matrix_dense.__init__(ret, MS)
ret.Data = mat
ret._converter = conv
return ret
Expand Down Expand Up @@ -333,8 +332,9 @@ cdef class Matrix_gfpn_dense(Matrix_dense):
"""
TESTS::
sage: MS = MatrixSpace(GF(64), 0)
sage: from sage.matrix.matrix_gfpn_dense import Matrix_gfpn_dense # optional: meataxe
sage: Matrix_gfpn_dense.__new__(Matrix_gfpn_dense) # optional: meataxe
sage: Matrix_gfpn_dense.__new__(Matrix_gfpn_dense, MS) # optional: meataxe
[]
sage: M = None
sage: M = Matrix_gfpn_dense(MatrixSpace(GF(64,'z'),4), None) # optional: meataxe
Expand Down Expand Up @@ -405,7 +405,6 @@ cdef class Matrix_gfpn_dense(Matrix_dense):
[0 1]
"""
ma = MatrixArgs_init(parent, entries)
Matrix_dense.__init__(self, ma.space)
cdef long fl = ma.base.order()
cdef long nr = ma.nrows
cdef long nc = ma.ncols
Expand Down Expand Up @@ -471,7 +470,7 @@ cdef class Matrix_gfpn_dense(Matrix_dense):
sage: N is M
False
sage: from sage.matrix.matrix_gfpn_dense import Matrix_gfpn_dense # optional: meataxe
sage: M = Matrix_gfpn_dense.__new__(Matrix_gfpn_dense) # optional: meataxe
sage: M = Matrix_gfpn_dense.__new__(Matrix_gfpn_dense, parent(M)) # optional: meataxe
sage: copy(M) # optional: meataxe
Traceback (most recent call last):
...
Expand Down Expand Up @@ -1408,7 +1407,7 @@ cdef class Matrix_gfpn_dense(Matrix_dense):
cdef Matrix_gfpn_dense OUT = self.fetch("left_kernel_matrix")
if OUT is not None:
return OUT
if self.Data == NULL:
if self.Data is NULL:
raise ValueError("The matrix must not be empty")
sig_on()
mat = MatNullSpace(self.Data)
Expand Down Expand Up @@ -1738,18 +1737,17 @@ def mtx_unpickle(f, int nr, int nc, bytes Data, bint m):
ValueError: Inconsistent dimensions in this matrix pickle
sage: mtx_unpickle(MatrixSpace(GF(19),0,5), 0, 5, b'', True) # optional: meataxe
[]
"""
cdef Matrix_gfpn_dense OUT
OUT = Matrix_gfpn_dense.__new__(Matrix_gfpn_dense)
if isinstance(f, (int, long)):
# This is for old pickles created with the group cohomology spkg
Matrix_dense.__init__(OUT, MatrixSpace(GF(f, 'z'), nr, nc, implementation=Matrix_gfpn_dense))
MS = MatrixSpace(GF(f, 'z'), nr, nc, implementation=Matrix_gfpn_dense)
else:
if f.nrows() != nr or f.ncols() != nc:
MS = f
if MS.nrows() != nr or MS.ncols() != nc:
raise ValueError("Inconsistent dimensions in this matrix pickle")
Matrix_dense.__init__(OUT, f)
f = OUT._base_ring.order()
f = MS.base_ring().order()

OUT = <Matrix_gfpn_dense>Matrix_gfpn_dense.__new__(Matrix_gfpn_dense, MS)
OUT.Data = MatAlloc(f, nr, nc)
OUT._is_immutable = not m
OUT._converter = FieldConverter(OUT._base_ring)
Expand Down
14 changes: 1 addition & 13 deletions src/sage/matrix/matrix_integer_dense.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,11 @@ cdef class Matrix_integer_dense(Matrix_dense):
1846857684291126914 # 64-bit
1591707266 # 32-bit
"""
def __cinit__(self, parent, entries, coerce, copy):
def __cinit__(self):
"""
Create and allocate memory for the matrix. Does not actually
initialize any of the memory.
INPUT:
- ``parent, entries, coerce, copy`` - as for
__init__.
EXAMPLES::
sage: from sage.matrix.matrix_integer_dense import Matrix_integer_dense
Expand All @@ -225,11 +218,6 @@ cdef class Matrix_integer_dense(Matrix_dense):
...
RuntimeError: FLINT exception
"""
self._parent = parent
self._base_ring = ZZ
self._nrows = parent.nrows()
self._ncols = parent.ncols()
self._pivots = None
sig_str("FLINT exception")
fmpz_mat_init(self._matrix, self._nrows, self._ncols)
sig_off()
Expand Down
1 change: 0 additions & 1 deletion src/sage/matrix/matrix_integer_sparse.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ from .matrix_sparse cimport Matrix_sparse

cdef class Matrix_integer_sparse(Matrix_sparse):
cdef mpz_vector* _matrix
cdef int _initialized

cdef _mod_int_c(self, mod_int p)
Loading

0 comments on commit c8fa7bb

Please sign in to comment.