jupyter笔记本中的多行符号方程类

symbolic-equation的Python项目详细描述


符号方程

Source code on GithubSymbolicEquation on the Python Package IndexTravis Continuous IntegrationCoverallsBSD License

一个简单的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

呈现假定lhsrhs都有一个乳胶 代表。如果Eq类定义了latex_renderer属性, 该渲染器将用于获取^{tt7}的乳胶表示$ 和rhs。否则:

  • 如果lhsrhs对象具有_latex方法,则该方法将是 调用;或最后一个,
  • lhsrhs将传递给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)

  • 初始版本

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何编写带有连接的动态JPA查询?   子列表上的java分区列表,其中相邻子列表的第一个和最后一个元素相同   java限定符注释方法找不到作为配置类的bean   java同时抛出主异常和子类型,有合适的方法吗?   LString将是一个链表类,它模仿标准的JavaString和StringBuilder类   spring上下文关闭时java停止ConcurrentTaskScheduler   Java继承基类使用派生类方法   java Google的zxing(斑马线)条形码库的位矩阵不是它应该位于的位置   java使用springhateoas反序列化包含(_链接和_嵌入)的JSON   java破坏者如何使用环形缓冲区读取文件?   java在Android中获得用户的日常路线?   javajenkins:依赖于其他工件/项目的构建作业   java家庭替换应用程序/视图网格使用哪种布局?   java如何从签名证书创建信任库和密钥库?