Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement matrix truncation for latex to enhance the display of large matrices #26526

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
37 changes: 34 additions & 3 deletions sympy/printing/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ class LatexPrinter(Printer):
"parenthesize_super": True,
"min": None,
"max": None,
"full_output": False,
"diff_operator": "d",
"adjoint_style": "dagger",
}
Expand Down Expand Up @@ -1686,10 +1687,37 @@ def _print_Piecewise(self, expr):
return tex % r" \\".join(ecpairs)

def _print_matrix_contents(self, expr):
lines = []

for line in range(expr.rows): # horrible, should be 'rows'
lines.append(" & ".join([self._print(i) for i in expr[line, :]]))
visible_rows = 10
visible_cols = 10
compact = not(self._settings['full_output'])

row_compactify = compact and expr.rows > visible_rows
col_compactify = compact and expr.cols > visible_cols

row_bound = visible_rows if compact and row_compactify else expr.rows
col_bound = visible_cols if compact and col_compactify else expr.cols

lines = [[] for _ in range(row_bound)]

for i in range(row_bound):
lines[i] = [self._print(expr[i, j]) for j in range(col_bound)]

if col_compactify:
# horizental dots and add last element in the ith row
lines[i][-2:] = [r'\cdots', self._print(expr[i, -1])]

if row_compactify:
last_row = expr[-1, 0:col_bound]
lines[-2:] = [r'\vdots'] * col_bound, [self._print(i) for i in last_row]

if col_compactify:
lines[-1][-2:] = [r'\cdots', self._print(expr[-1, -1])]

if col_compactify and row_compactify:
lines[-2][-2] = r'\ddots' # diagonal dots

lines = [' & '.join(line) for line in lines]

mat_str = self._settings['mat_str']
if mat_str is None:
Expand Down Expand Up @@ -3049,6 +3077,9 @@ def latex(expr, **settings):
max: Integer or None, optional
Sets the upper bound for the exponent to print floating point numbers in
fixed-point format.
full_output: boolean, optional
``False``. If set to False, output is truncated. Else, the output
is printed normally
diff_operator: string, optional
String to use for differential operator. Default is ``'d'``, to print in italic
form. ``'rd'``, ``'td'`` are shortcuts for ``\mathrm{d}`` and ``\text{d}``.
Expand Down
37 changes: 31 additions & 6 deletions sympy/printing/tests/test_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,12 +1550,37 @@ def test_latex_Matrix():
assert latex(M, mat_delim=None, mat_str='bmatrix') == \
r'\begin{bmatrix}x + 1 & y\\y & x - 1\end{bmatrix}'

M2 = Matrix(1, 11, range(11))
assert latex(M2) == \
r'\left[\begin{array}{ccccccccccc}' \
r'0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10\end{array}\right]'


#diagonal dots
M1 = latex(Matrix(15, 15, lambda i,j : i + j))
assert M1 == \
r'\left[\begin{array}{ccccccccccccccc}0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & \cdots & 14'\
r'\\1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \cdots & 15'\
r'\\2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & \cdots & 16'\
r'\\3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \cdots & 17'\
r'\\4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & \cdots & 18'\
r'\\5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & \cdots & 19'\
r'\\6 & 7 & 8 & 9 & 10 & 11 & 12 & 13 & \cdots & 20'\
r'\\7 & 8 & 9 & 10 & 11 & 12 & 13 & 14 & \cdots & 21'\
r'\\\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \ddots & \vdots'\
r'\\14 & 15 & 16 & 17 & 18 & 19 & 20 & 21 & \cdots & 28\end{array}\right]'

#horizontal dots only
M2 = latex(Matrix(5, 15, lambda i,j : i + j))
assert M2 == r'\left[\begin{array}{ccccccccccccccc}'\
r'0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & \cdots & '\
r'14\\1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \cdots & '\
r'15\\2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & \cdots & '\
r'16\\3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \cdots & '\
r'17\\4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & \cdots & '\
r'18\end{array}\right]'

#vertical dots only
M3 = latex(Matrix(15, 5, lambda i,j : i + j))
assert M3 == r'\left[\begin{matrix}0 & 1 & 2 & 3 & 4'\
r'\\1 & 2 & 3 & 4 & 5\\2 & 3 & 4 & 5 & 6\\3 & 4 & 5 & 6 & 7'\
r'\\4 & 5 & 6 & 7 & 8\\5 & 6 & 7 & 8 & 9\\6 & 7 & 8 & 9 & 10'\
r'\\7 & 8 & 9 & 10 & 11\\\vdots & \vdots & \vdots & \vdots &'\
r' \vdots\\14 & 15 & 16 & 17 & 18\end{matrix}\right]'
def test_latex_matrix_with_functions():
t = symbols('t')
theta1 = symbols('theta1', cls=Function)
Expand Down