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

Latex printing on jupyter notebooks #261

Merged
merged 7 commits into from
Dec 25, 2018
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion symengine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
ImmutableMatrix, ImmutableDenseMatrix, MutableDenseMatrix,
MatrixBase, Basic, DictBasic, symarray, series, diff, zeros,
eye, diag, ones, Derivative, Subs, expand, has_symbol,
UndefFunction, Function,
UndefFunction, Function, latex,
have_numpy, true, false, Equality, Unequality, GreaterThan,
LessThan, StrictGreaterThan, StrictLessThan, Eq, Ne, Ge, Le,
Gt, Lt, And, Or, Not, Nand, Nor, Xor, Xnor, perfect_power, integer_nthroot,
Expand All @@ -19,6 +19,7 @@
)
from .utilities import var, symbols
from .functions import *
from .printing import init_printing

if have_mpfr:
from .lib.symengine_wrapper import RealMPFR
Expand Down
3 changes: 3 additions & 0 deletions symengine/lib/symengine.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1038,3 +1038,6 @@ cdef extern from "<symengine/sets.h>" namespace "SymEngine":
cdef extern from "<symengine/solve.h>" namespace "SymEngine":
cdef RCP[const Set] solve(rcp_const_basic &f, RCP[const Symbol] &sym) nogil except +
cdef RCP[const Set] solve(rcp_const_basic &f, RCP[const Symbol] &sym, RCP[const Set] &domain) nogil except +

cdef extern from "<symengine/latex.h>" namespace "SymEngine":
string latex(const Basic &x) nogil except +
11 changes: 11 additions & 0 deletions symengine/lib/symengine_wrapper.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ cdef list vec_pair_to_list(symengine.vec_pair& vec):
return result


repr_latex=[False]

cdef class Basic(object):

def __str__(self):
Expand All @@ -782,6 +784,12 @@ cdef class Basic(object):
def __repr__(self):
return self.__str__()

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

def __hash__(self):
return deref(self.thisptr).hash()

Expand Down Expand Up @@ -4932,6 +4940,9 @@ def cse(exprs):
symengine.cse(replacements, reduced_exprs, vec)
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 _flattened_vec(symengine.vec_basic &vec, exprs):
cdef Basic b
Expand Down
10 changes: 10 additions & 0 deletions symengine/printing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from symengine.lib.symengine_wrapper import ccode, sympify, Basic
import symengine.lib.symengine_wrapper

class CCodePrinter:

Expand All @@ -22,3 +23,12 @@ def doprint(self, expr, assign_to=None):
code_line = '{}[{}] = {};'.format(assign_to, i, element)
code_lines.append(code_line)
return '\n'.join(code_lines)


def init_printing(pretty_print=True, use_latex=True):
if pretty_print:
if not use_latex:
raise RuntimeError("Only latex is supported for pretty printing")
symengine.lib.symengine_wrapper.repr_latex[0] = True
else:
symengine.lib.symengine_wrapper.repr_latex[0] = False
2 changes: 1 addition & 1 deletion symengine_version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9b604b2757aa09ae9d2286510eaf4b75d9b2b796
9ef48f32501bc66c5111672b4a1964d98a9fd253