Skip to content

Commit 8ef62a0

Browse files
committed
add _repr_latex_ to DenseMatrix
1 parent 5d49ad3 commit 8ef62a0

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3241,6 +3241,34 @@ cdef class DenseMatrixBase(MatrixBase):
32413241
def __str__(self):
32423242
return deref(self.thisptr).__str__().decode("utf-8")
32433243

3244+
def _repr_latex_(self):
3245+
MAX_NUMBER_OF_ROWS = 24
3246+
MAX_NUMBER_OF_COLUMNS = 16
3247+
3248+
values = list(self)
3249+
ncols=self.shape[1]
3250+
nrows = self.shape[0]
3251+
if nrows > MAX_NUMBER_OF_ROWS:
3252+
nrows_display = MAX_NUMBER_OF_ROWS - 2
3253+
else:
3254+
nrows_display= nrows
3255+
ncols_display = min(ncols, MAX_NUMBER_OF_COLUMNS)
3256+
latex = r'$\displaystyle \left[\begin{matrix}'
3257+
3258+
newline = r'\\'
3259+
if ncols_display<ncols:
3260+
newline = ' & \cdots ' + newline
3261+
for row in range(nrows_display):
3262+
vv = values[row*ncols:(row*ncols)+ncols_display]
3263+
latex += ' & '.join([str(v) for v in vv])
3264+
if row < self.shape[0]-1:
3265+
latex += newline
3266+
if nrows_display < nrows:
3267+
latex += ' & '.join([r'\vdots' for v in vv])
3268+
3269+
latex += r'\end{matrix}\right]$'
3270+
return latex
3271+
32443272
def __add__(a, b):
32453273
a = _sympify(a, False)
32463274
b = _sympify(b, False)

0 commit comments

Comments
 (0)