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

Commit

Permalink
30552: implement global options for matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
mwageringel committed Oct 10, 2020
1 parent 8eec238 commit 7d0b548
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
31 changes: 30 additions & 1 deletion src/sage/matrix/constructor.pyx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# cython: binding=True
"""
General matrix Constructor
General matrix Constructor and display options
"""

#*****************************************************************************
Expand All @@ -15,6 +15,7 @@ General matrix Constructor
#*****************************************************************************

from .args cimport MatrixArgs
from sage.structure.global_options import GlobalOptions


def matrix(*args, **kwds):
Expand Down Expand Up @@ -640,3 +641,31 @@ def matrix(*args, **kwds):
Matrix = matrix

from .special import *

@matrix_method
class options(GlobalOptions):
r"""
Global options for matrices.
@OPTIONS@
EXAMPLES::
sage: matrix.options.max_cols = 6
sage: matrix.options.max_rows = 3
sage: matrix(ZZ, 3, 6)
[0 0 0 0 0 0]
[0 0 0 0 0 0]
[0 0 0 0 0 0]
sage: matrix(ZZ, 3, 7)
3 x 7 dense matrix over Integer Ring...
sage: matrix(ZZ, 4, 6)
4 x 6 dense matrix over Integer Ring...
"""
NAME = 'Matrix'
max_cols = dict(default=49,
description='maximum number of columns to display',
checker=lambda val: val >= 0)
max_rows = dict(default=19,
description='maximum number of rows to display',
checker=lambda val: val >= 0)
30 changes: 18 additions & 12 deletions src/sage/matrix/matrix0.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1723,7 +1723,8 @@ cdef class Matrix(sage.structure.element.Matrix):
100 x 100 dense matrix over Integer Ring]
"""
if self._nrows < max_rows and self._ncols < max_cols:
from .constructor import options
if self._nrows <= options.max_rows() and self._ncols <= options.max_cols():
return self.str()
if self.is_sparse():
s = 'sparse'
Expand Down Expand Up @@ -2059,7 +2060,8 @@ cdef class Matrix(sage.structure.element.Matrix):
-0.35104242112828943 0.5084492941557279]
-0.9541798283979341 -0.8948790563276592]
"""
if self._nrows < max_rows and self._ncols < max_cols:
from .constructor import options
if self._nrows <= options.max_rows() and self._ncols <= options.max_cols():
return self.str(character_art=True)
else:
from sage.typeset.ascii_art import AsciiArt
Expand Down Expand Up @@ -2087,7 +2089,8 @@ cdef class Matrix(sage.structure.element.Matrix):
sage: unicode_art(A)
100 x 100 dense matrix over Integer Ring
"""
if self._nrows < max_rows and self._ncols < max_cols:
from .constructor import options
if self._nrows <= options.max_rows() and self._ncols <= options.max_cols():
return self.str(unicode=True, character_art=True)
else:
from sage.typeset.unicode_art import UnicodeArt
Expand Down Expand Up @@ -5879,9 +5882,6 @@ def unpickle(cls, parent, immutability, cache, data, version):
return A


max_rows = 20
max_cols = 50

def set_max_rows(n):
"""
Sets the global variable max_rows (which is used in deciding how to output a matrix).
Expand All @@ -5890,11 +5890,14 @@ def set_max_rows(n):
sage: from sage.matrix.matrix0 import set_max_rows
sage: set_max_rows(20)
doctest:...: DeprecationWarning: 'set_max_rows' is replaced by 'matrix.options.max_rows'
See https://trac.sagemath.org/30552 for details.
"""

global max_rows
max_rows = n
from sage.misc.superseded import deprecation
deprecation(30552, "'set_max_rows' is replaced by 'matrix.options.max_rows'")
from .constructor import options
options.max_rows = n-1

def set_max_cols(n):
"""
Expand All @@ -5904,8 +5907,11 @@ def set_max_cols(n):
sage: from sage.matrix.matrix0 import set_max_cols
sage: set_max_cols(50)
doctest:...: DeprecationWarning: 'set_max_cols' is replaced by 'matrix.options.max_cols'
See https://trac.sagemath.org/30552 for details.
"""

global max_cols
max_cols = n
from sage.misc.superseded import deprecation
deprecation(30552, "'set_max_cols' is replaced by 'matrix.options.max_cols'")
from .constructor import options
options.max_cols = n-1
2 changes: 1 addition & 1 deletion src/sage/misc/sageinspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@ def sage_getsourcelines(obj):
sage: from sage.misc.sageinspect import sage_getsourcelines
sage: sage_getsourcelines(matrix)[1]
20
21
sage: sage_getsourcelines(matrix)[0][0]
'def matrix(*args, **kwds):\n'
Expand Down
4 changes: 2 additions & 2 deletions src/sage/repl/display/fancy_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def __call__(self, obj, p, cycle):
return False
if not isinstance(obj, Matrix):
return False
from sage.matrix.matrix0 import max_rows, max_cols
if obj.nrows() < max_rows and obj.ncols() < max_cols:
from sage.matrix.constructor import options
if obj.nrows() <= options.max_rows() and obj.ncols() <= options.max_cols():
return False
p.text(
repr(obj) + " (use the '.str()' method to see the entries)"
Expand Down

0 comments on commit 7d0b548

Please sign in to comment.