symmath.system

class symmath.system.System
facts

A dictionary mapping symbol names to equivalent expressions. Each value in this dictionary should not contain any references to the symbols used as keys. Consider this attribute read-only, and use equate() to add a fact instead of modifying this dictionary.

equate(a, b)

Eliminate one symbol from the system by using the equation: a = b. This adds a fact to facts and simplifies all existing facts.

An error is raised if this equation over-constrains the system.

>>> x = sym('x')
>>> y = sym('y')
>>> system = System()
>>> system.equate(x + 1, 2*y)
>>> system.equate(2*x, y + 2)
>>> system.equate(x, y)
Traceback (most recent call last):
  ...
SymmathError: System is over-constrained

Classes can define _symmath_equate() to customize what it means objects of that class to be equal to something else.

>>> class Vector2D:
...   def __init__(self, x, y):
...     self.x = x
...     self.y = y
...
...   def _symmath_equate(self, f, other):
...     f(self.x, other.x)
...     f(self.y, other.y)

>>> x = sym('x')
>>> y = sym('y')
>>> a = Vector2D(x + 5, 2*y)
>>> b = Vector2D(4*y, 3*x)

>>> system = System()
>>> system.equate(a, b)
>>> system.eval(x)
1.0
>>> system.eval(y)
1.5
eval(val)

Substitue all known facts into val, recursing into structures that support it. eval() calls symmath.expr.Expr.scalar() to eliminate all expressions. This method raises an exception if the system is not fully solved.

Classes supporting this function define the method _symmath_eval(), which takes a single argument: the eval() method as a bound method, and should return a fully evaluated copy of itself.

rewrite(expr)

Substitue all known facts into expr.

simplify(expr)

Same as rewrite(), but returns the simplified expression instead of modifying expr.