Skip to content

Commit 1ef1c87

Browse files
authored
Merge pull request #370 from eendebakpt/feat/densematrix_latex_representation
[RFC] Add latex representation to DenseMatrix
2 parents a09d9d3 + 4fa1db0 commit 1ef1c87

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ install:
5454
- git clone https://github.com/sympy/symengine symengine-cpp
5555

5656
- if [%COMPILER%]==[MSVC15] call %CONDA_INSTALL_LOCN%\Scripts\activate.bat
57-
- if [%COMPILER%]==[MSVC15] conda update --yes --quiet conda
57+
- if [%COMPILER%]==[MSVC15] conda install --yes --quiet conda python=3.6
5858
- if [%COMPILER%]==[MSVC15] conda config --add channels conda-forge
5959
- if [%COMPILER%]==[MSVC15] if [%BUILD_TYPE%]==[Debug] conda config --add channels symengine/label/debug
6060
- if [%COMPILER%]==[MSVC15] conda install --yes mpir=3.0.0 vc=14

symengine/lib/symengine.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,7 @@ cdef extern from "<symengine/solve.h>" namespace "SymEngine":
10991099
cdef extern from "<symengine/printers.h>" namespace "SymEngine":
11001100
string ccode(const Basic &x) nogil except +
11011101
string latex(const Basic &x) nogil except +
1102+
string latex(const DenseMatrix &x, unsigned max_rows, unsigned max_cols) nogil except +
11021103

11031104
## Defined in 'symengine/cwrapper.cpp'
11041105
cdef struct CRCPBasic:

symengine/lib/symengine_wrapper.pyx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,6 +3244,12 @@ cdef class DenseMatrixBase(MatrixBase):
32443244
def __str__(self):
32453245
return deref(self.thisptr).__str__().decode("utf-8")
32463246

3247+
def _repr_latex_(self):
3248+
if repr_latex[0]:
3249+
return "${}$".format(latex(self))
3250+
else:
3251+
return None
3252+
32473253
def __add__(a, b):
32483254
a = _sympify(a, False)
32493255
b = _sympify(b, False)
@@ -5427,8 +5433,14 @@ def cse(exprs):
54275433
return (vec_pair_to_list(replacements), vec_basic_to_list(reduced_exprs))
54285434

54295435
def latex(expr):
5430-
cdef Basic expr_ = sympify(expr)
5431-
return symengine.latex(deref(expr_.thisptr)).decode("utf-8")
5436+
cdef DenseMatrixBase mat_expr
5437+
cdef Basic basic_expr
5438+
if isinstance(expr, DenseMatrixBase):
5439+
mat_expr = expr
5440+
return symengine.latex(deref(symengine.static_cast_DenseMatrix(mat_expr.thisptr)), 20, 12).decode("utf-8")
5441+
else:
5442+
basic_expr = sympify(expr)
5443+
return symengine.latex(deref(basic_expr.thisptr)).decode("utf-8")
54325444

54335445
cdef _flattened_vec(symengine.vec_basic &vec, exprs):
54345446
cdef Basic b

symengine/tests/test_matrices.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from symengine import symbols
1+
from symengine import symbols, init_printing
22
from symengine.lib.symengine_wrapper import (DenseMatrix, Symbol, Integer,
33
Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros,
44
ones, eye, ImmutableMatrix)
@@ -734,3 +734,10 @@ def test_LUdecomp():
734734
for orig, new in p:
735735
res.row_swap(orig, new)
736736
assert res - testmat == zeros(4)
737+
738+
def test_repr_latex():
739+
testmat = DenseMatrix([[0, 2]])
740+
init_printing(True)
741+
latex_string = testmat._repr_latex_()
742+
assert isinstance(latex_string, str)
743+
init_printing(False)

symengine_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v0.8.1
1+
23abf31763620463500d5fad114d855afd66d011

0 commit comments

Comments
 (0)