Goulib.expr module

simple symbolic math expressions

Goulib.expr.add_function(f, s=None, r=None, l=None)[source]

add a function to those allowed in Expr.

Parameters:
  • f – function
  • s – string representation, should be formula-like
  • r – repr representation, should be cut&pastable in a calculator, or in python ...
  • l – LaTeX representation
Goulib.expr.add_module(module)[source]
Goulib.expr.eval(node, ctx={})[source]

safe eval of ast node : only _functions and _operators listed above can be used

Parameters:
  • node – ast.AST to evaluate
  • ctx – dict of varname : value to substitute in node
Returns:

number or expression string

Goulib.expr.get_function_source(f)[source]

returns cleaned code of a function or lambda currently only supports: - lambda x:formula_of_(x) - def anything(x): return formula_of_(x)

Goulib.expr.plouffe(f, epsilon=1e-06)[source]
class Goulib.expr.Expr(f)[source]

Bases: Goulib.plot.Plot

Math expressions that can be evaluated like standard functions combined using standard operators and plotted in IPython/Jupyter notebooks

Parameters:f – function or operator, Expr to copy construct, or formula string
__init__(f)[source]
Parameters:f – function or operator, Expr to copy construct, or formula string
isNum
isconstant
Returns:True if Expr evaluates to a constant number or bool
__call__(x=None, **kwargs)[source]

evaluate the Expr at x OR compose self(x())

__float__()[source]
__repr__()[source]
__str__()[source]
latex()[source]
Returns:string LaTex formula
apply(f, right=None)[source]

function composition self o f = f(self(x))

applx(f, var='x')[source]

function composition f o self = self(f(x))

__eq__(other)[source]
__ne__(other)[source]
__lt__(other)[source]
__le__(other)[source]
__ge__(other)[source]
__gt__(other)[source]
__add__(right)[source]
__sub__(right)[source]
__neg__()[source]
__mul__(right)[source]
__rmul__(right)[source]
__truediv__(right)[source]
__pow__(right)[source]
__div__(right)
__invert__()[source]
__and__(right)[source]
__or__(right)[source]
__xor__(right)[source]
__lshift__(dx)[source]
__rshift__(dx)[source]
complexity()[source]

measures the complexity of Expr :return: int, sum of the precedence of used ops

__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__format__()

default object formatter

__getattribute__

Return getattr(self, name).

__hash__ = None
__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

html(**kwargs)
plot(**kwargs)

renders on IPython Notebook (alias to make usage more straightforward)

png(**kwargs)
render(fmt='svg', **kwargs)
save(filename, **kwargs)
svg(**kwargs)
class Goulib.expr.TextVisitor(dialect)[source]

Bases: ast.NodeVisitor

Parameters:dialect – int index in _operators of symbols to use
__init__(dialect)[source]
Parameters:dialect – int index in _operators of symbols to use
prec(op)[source]

calculate the precedence of op

__class__

alias of type

__delattr__

Implement delattr(self, name).

__dir__() → list

default dir() implementation

__eq__

Return self==value.

__format__()

default object formatter

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

visit(node)

Visit a node.

visit_Call(n)[source]
visit_Name(n)[source]
visit_NameConstant(node)[source]
visit_UnaryOp(n)[source]
visit_BinOp(n)[source]
visit_Compare(n)[source]
visit_Num(n)[source]
generic_visit(n)[source]