python - 在 jupyter notebook 中自定义打印

标签 python ipython sympy jupyter-notebook

我正在寻找 MathCad 的替代品来进行简单的计算,但我希望表达式看起来就像我使用笔和纸一样,并且让不懂编程的人也能轻松阅读。我尝试了 Sweave、Knitr,但我对它不满意。我最近发现了带有 SymPy 的 Jupyter notebook,它对我来说仍然不如 MathCad 容易,但我会试一试。使用 Jupyter 我在打印公式时遇到问题:我想自动打印等式的两边

我想要的:

enter image description here

我得到的:

enter image description here

我尝试了什么

ccode 不返回 latex 并且总是输入“assign_to”很无聊 enter image description here

最佳答案

如果你需要更方便的方式,你可以定义一个包装函数如下。

class Equation(object):    
    def __init__(self, left, right, mode='latex'):        
        self.mode = mode
        self.left = left
        self.right = right

        self._eq = sym.Eq(left, right)
        self._latex = sym.latex(self._eq)

    def __repr__(self):
        if self.mode == 'latex':
            return self._latex.__repr__()
        elif self.mode == 'sympy':
            return self._eq.__repr__()

    def __str__(self):
        if self.mode == 'latex':
            return self._latex
        elif self.mode == 'sympy':
            return self.eq.__str__()

    def eq(self):
        return self._eq

    def latex(self):
        return self._latex

    @property
    def mode(self):
        return self._mode

    @mode.setter
    def mode(self, val):
        self._mode = val

    @property
    def left(self):
        return self._left
    @left.setter
    def left(self, val):
        self._left = sym

    @property
    def right(self):
        return self._right

    @right.setter
    def right(self, val):
        self._right = val
# end of class 

enter image description here

关于python - 在 jupyter notebook 中自定义打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39116201/

相关文章:

python - 如何在 APScheduler 中动态修改作业?

python - 加速 python -c 调用

Sympy 复数符号的绝对值和自变量

python - 两个具有相同轴或在同一图形上的 matplotlib/pyplot 直方图

python - 查明是否使用了三角函数并跟踪符号变量

python - 创建一组 numpy 数组

python - oct2py - 在 python 中使用线程调用 Octave 函数

python - 如何从pyodbc中的用户获取表名,同时避免SQL注入(inject)?

python - Enthought Canopy 中的 IPython 缓冲区和分页

python - 如何在 IPython shell 中获取或激活虚拟环境?