Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[RFC] Add latex representation to DenseMatrix #370

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

Merged
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ install:
- git clone https://github.com/sympy/symengine symengine-cpp

- if [%COMPILER%]==[MSVC15] call %CONDA_INSTALL_LOCN%\Scripts\activate.bat
- if [%COMPILER%]==[MSVC15] conda update --yes --quiet conda
- if [%COMPILER%]==[MSVC15] conda install --yes --quiet conda python=3.6
- if [%COMPILER%]==[MSVC15] conda config --add channels conda-forge
- if [%COMPILER%]==[MSVC15] if [%BUILD_TYPE%]==[Debug] conda config --add channels symengine/label/debug
- if [%COMPILER%]==[MSVC15] conda install --yes mpir=3.0.0 vc=14
Expand Down
1 change: 1 addition & 0 deletions symengine/lib/symengine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,7 @@ cdef extern from "<symengine/solve.h>" namespace "SymEngine":
cdef extern from "<symengine/printers.h>" namespace "SymEngine":
string ccode(const Basic &x) nogil except +
string latex(const Basic &x) nogil except +
string latex(const DenseMatrix &x, unsigned max_rows, unsigned max_cols) nogil except +

## Defined in 'symengine/cwrapper.cpp'
cdef struct CRCPBasic:
Expand Down
16 changes: 14 additions & 2 deletions symengine/lib/symengine_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3241,6 +3241,12 @@ cdef class DenseMatrixBase(MatrixBase):
def __str__(self):
return deref(self.thisptr).__str__().decode("utf-8")

def _repr_latex_(self):
if repr_latex[0]:
return "${}$".format(latex(self))
else:
return None

def __add__(a, b):
a = _sympify(a, False)
b = _sympify(b, False)
Expand Down Expand Up @@ -5412,8 +5418,14 @@ def cse(exprs):
return (vec_pair_to_list(replacements), vec_basic_to_list(reduced_exprs))

def latex(expr):
cdef Basic expr_ = sympify(expr)
return symengine.latex(deref(expr_.thisptr)).decode("utf-8")
cdef DenseMatrixBase mat_expr
cdef Basic basic_expr
if isinstance(expr, DenseMatrixBase):
mat_expr = expr
return symengine.latex(deref(symengine.static_cast_DenseMatrix(mat_expr.thisptr)), 20, 12).decode("utf-8")
else:
basic_expr = sympify(expr)
return symengine.latex(deref(basic_expr.thisptr)).decode("utf-8")

cdef _flattened_vec(symengine.vec_basic &vec, exprs):
cdef Basic b
Expand Down
9 changes: 8 additions & 1 deletion symengine/tests/test_matrices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from symengine import symbols
from symengine import symbols, init_printing
from symengine.lib.symengine_wrapper import (DenseMatrix, Symbol, Integer,
Rational, function_symbol, I, NonSquareMatrixError, ShapeError, zeros,
ones, eye, ImmutableMatrix)
Expand Down Expand Up @@ -725,3 +725,10 @@ def test_LUdecomp():
for orig, new in p:
res.row_swap(orig, new)
assert res - testmat == zeros(4)

def test_repr_latex():
testmat = DenseMatrix([[0, 2]])
init_printing(True)
latex_string = testmat._repr_latex_()
assert isinstance(latex_string, str)
init_printing(False)
2 changes: 1 addition & 1 deletion symengine_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.8.1
23abf31763620463500d5fad114d855afd66d011