jupyter笔记本中的多行符号方程类
symbolic-equation的Python项目详细描述
符号方程
一个简单的python包,它提供了用于操作符号的Eq类 方程。
Eq类定义了一个等式,并允许应用任意 对方程左侧和/或右侧的操纵。它 (可选)跟踪所有这些操作,并将它们整齐地显示为 交互式解释器会话或Jupyter notebook(使用乳胶表示)中的多行方程。主要用于 SymPy。
symbolic_equation包的开发发生在Github。
安装
要安装最新版本的symbolic_equation,请在终端中运行此命令:
$ pip install symbolic_equation
这是安装symbolic_equation的首选方法,因为它将始终安装最新的稳定版本。
如果没有安装pip,则Python installation guide可以分别引导Python Packaging User Guide 你完成了整个过程。
从Github安装symbolic_equation的最新开发版本。
$ pip install git+https://github.com/goerz/symbolic_equation.git@master#egg=symbolic_equation
示例
>>> fromsymbolic_equationimportEq>>> fromsympyimportsymbols>>> x,y=symbols('x y')>>> eq1=Eq(2*x-y-1,tag='I')>>> eq12*x - y - 1 = 0 ('I') >>> eq2=Eq(x+y-5,tag='II')>>> eq2x + y - 5 = 0 ('II') >>> eq_y=(... (eq1-2*eq2).set_tag("I - 2 II")... .apply(lambdav:v-9,cont=True)... .apply(lambdav:v/(-3),cont=True,tag='y')... )>>> eq_y9 - 3*y = 0 ('I - 2 II') -3*y = -9 y = 3 ('y') >>> eq_x=(... eq1.apply_mtd_to_lhs('subs',eq_y.as_dict,tag=r'y in I')... .apply(lambdav:v/2,cont=True)... .apply(lambdav:v+2,cont=True,tag='x')... )>>> eq_x2*x - 4 = 0 ('y in I') x - 2 = 0 x = 2 ('x')
参考值
>>> help(Eq)# doctest: +NORMALIZE_WHITESPACEHelp on class Eq in module symbolic_equation: <BLANKLINE> class Eq(builtins.object) | Eq(lhs, rhs=None, tag=None, _prev_lhs=None, _prev_rhs=None, _prev_tags=None) | | Symbolic equation. | | This class keeps track of the :attr:`lhs` and :attr:`rhs` of an equation | across arbitrary manipulations. | | Args: | lhs: the left-hand-side of the equation | rhs: the right-hand-side of the equation. If None, defaults to zero. | tag: a tag (equation number) to be shown when printing | the equation | | Class Attributes: | latex_renderer: If not None, a callable that must return a LaTeX | representation (:class:`str`) of `lhs` and `rhs`. | | Methods defined here: | | __add__(self, other) | Add another equation, or a constant. | | __eq__(self, other) | Compare to another equation, or a constant. | | This does not take into account any mathematical knowledge, it merely | checks if the :attr:`lhs` and :attr:`rhs` are exactly equal. If | comparing against a constant, the :attr:`rhs` must be exactly equal to | that constant. | | __init__(self, lhs, rhs=None, tag=None, _prev_lhs=None, _prev_rhs=None, _prev_tags=None) | Initialize self. See help(type(self)) for accurate signature. | | __mul__(self, other) | | __radd__ = __add__(self, other) | | __repr__(self) | Return repr(self). | | __rmul__(self, other) | | __rsub__(self, other) | | __str__(self) | Return str(self). | | __sub__(self, other) | | __truediv__(self, other) | | apply(self, func, *args, cont=False, tag=None, **kwargs) | Apply `func` to both sides of the equation. | | Returns a new equation where the left-hand-side and right-hand side | are replaced by the application of `func`:: | | lhs=func(lhs, *args, **kwargs) | rhs=func(rhs, *args, **kwargs) | | If ``cont=True``, the resulting equation will keep a history of its | previous state (resulting in multiple lines of equations when printed). | | The resulting equation with have the given `tag`. | | apply_mtd(self, mtd, *args, cont=False, tag=None, **kwargs) | Call the method `mtd` on both sides of the equation. | | That is, the left-hand-side and right-hand-side are replaced by:: | | lhs=lhs.<mtd>(*args, **kwargs) | rhs=rhs.<mtd>(*args, **kwargs) | | The `cont` and `tag` parameters are as in :meth:`apply`. | | apply_mtd_to_lhs(self, mtd, *args, cont=False, tag=None, **kwargs) | Call the method `mtd` on the :attr:`lhs` of the equation only. | | Like :meth:`apply_mtd`, but modifying only the left-hand-side. | | apply_mtd_to_rhs(self, mtd, *args, cont=False, tag=None, **kwargs) | Call the method `mtd` on the :attr:`rhs` of the equation. | | Like :meth:`apply_mtd`, but modifying only the right-hand-side. | | apply_to_lhs(self, func, *args, cont=False, tag=None, **kwargs) | Apply `func` to the :attr:`lhs` of the equation only. | | Like :meth:`apply`, but modifying only the left-hand-side. | | apply_to_rhs(self, func, *args, cont=False, tag=None, **kwargs) | Apply `func` to the :attr:`rhs` of the equation only. | | Like :meth:`apply`, but modifying only the right-hand-side. | | copy(self) | Return a copy of the equation | | set_tag(self, tag) | Return a copy of the equation with a new `tag`. | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | as_dict | Mapping of the lhs to the rhs. | | This allows to plug an equation into another expression. | | lhs | The left-hand-side of the equation. | | rhs | The right-hand-side of the equation. | | tag | A tag (equation number) to be shown when printing the equation, or | None | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | __hash__ = None | | latex_renderer = None <BLANKLINE>
在Jupyter笔记本中使用
在Jupyter notebook中,方程将以乳胶形式呈现。 见examples.ipynb。
呈现假定lhs和rhs都有一个乳胶 代表。如果Eq类定义了latex_renderer属性, 该渲染器将用于获取^{tt7}的乳胶表示$ 和rhs。否则:
- 如果lhs或rhs对象具有_latex方法,则该方法将是 调用;或最后一个,
- lhs和rhs将传递给sympy.latex。
与sympy的eq类的关系
sympy包还提供了一个Eq class,表示 两个同情的表达。sympy提供的类和 此包不可互换:sympy的Eq不跟踪 修改或打印为多行公式。而 symbolic_equation.Eq类不是sympy表达式,它可以被转换 通过sympy.sympify函数发送到sympy.Eq实例。
历史记录
0.1.0-dev(2019-05-26)
- 初始版本