Goulib.expr module

simple symbolic math expressions

Goulib.expr.eval(node, **kwargs)[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, **kwargs)[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, **kwargs)[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]

Return repr(self).

__str__()[source]

Return str(self).

latex()[source]
Returns:string LaTex formula
points(xmin=-1, xmax=1, step=0.1)[source]
Returns:x,y lists of float : points for a line plot
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]

Return self==value.

__ne__(other)[source]

Return self!=value.

__lt__(other)[source]

Return self<value.

__le__(other)[source]

Return self<=value.

__ge__(other)[source]

Return self>=value.

__gt__(other)[source]

Return self>value.

__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 builtins.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)
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_constant(c, name, s=None, r=None, l=None)[source]

add a constant to those recognized in Expr.

Parameters:
  • c – constant
  • 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]
class Goulib.expr.TextVisitor(dialect, operators={<class '_ast.USub'>: (<built-in function neg>, 1150, '-', '-', '-'), <class '_ast.Gt'>: (<built-in function gt>, 600, ' > ', ' > ', ' \gtr '), <class '_ast.Pow'>: (<function pow>, 1400, '^', '**', '^'), <class '_ast.Num'>: (None, 9000), <class '_ast.Or'>: (<built-in function or_>, 300, ' or ', ' or ', ' \vee '), <class '_ast.Div'>: (<built-in function truediv>, 1201, '/', '/', '\frac{%s}{%s}'), <class '_ast.BitXor'>: (<built-in function xor>, 800, ' xor ', ' xor ', ' xor '), <class '_ast.FloorDiv'>: (<built-in function floordiv>, 1201, '//', '//', '\left\lfloor\frac{%s}{%s}\right\rfloor'), <class '_ast.Sub'>: (<built-in function sub>, 1101, '-', '-', '-'), <class '_ast.LShift'>: (<built-in function lshift>, 1000, ' << ', ' << ', ' \ll '), <class '_ast.Not'>: (<built-in function not_>, 500, 'not ', 'not ', '\neg'), <class '_ast.Lt'>: (<built-in function lt>, 600, ' < ', ' < ', ' \ltr '), <class '_ast.GtE'>: (<built-in function ge>, 600, ' >= ', ' >= ', ' \gec '), <class '_ast.Mod'>: (<built-in function mod>, 1200, ' mod ', '%', ' \bmod '), <class '_ast.Name'>: (None, 9000), <class '_ast.Eq'>: (<built-in function eq>, 600, '=', ' == ', ' = '), <class '_ast.Mult'>: (<built-in function mul>, 1200, '*', '*', ' \cdot '), <class '_ast.RShift'>: (<built-in function rshift>, 1000, ' >> ', ' >> ', ' \gg '), <class '_ast.UAdd'>: (<built-in function pos>, 1150, '+', '+', '+'), <class '_ast.LtE'>: (<built-in function le>, 600, ' <= ', ' <= ', ' \leq '), <class '_ast.Add'>: (<built-in function add>, 1100, '+', '+', '+'), <class '_ast.And'>: (<built-in function and_>, 400, ' and ', ' and ', ' \wedge '), <class '_ast.Call'>: (None, 9000), <class '_ast.Invert'>: (<built-in function not_>, 1300, '~', '~', '\sim ')}, functions={'abs': (<built-in function abs>, 9999, None, None, '\lvert{%s}\rvert'), 'acos': (<built-in function acos>, 9999, None, None, '\arccos'), 'acosh': (<built-in function acosh>, 9999, None, None, '\cosh^{-1}'), 'asin': (<built-in function asin>, 9999, None, None, '\arcsin'), 'asinh': (<built-in function asinh>, 9999, None, None, '\sinh^{-1}'), 'atan': (<built-in function atan>, 9999, None, None, '\arctan'), 'atan2': (<built-in function atan2>, 9999, None, None, None), 'atanh': (<built-in function atanh>, 9999, None, None, '\tanh^{-1}'), 'ceil': (<built-in function ceil>, 9999, None, None, '\left\lceil{%s}\right\rceil'), 'copysign': (<built-in function copysign>, 9999, None, None, None), 'cos': (<built-in function cos>, 9999, None, None, None), 'cosh': (<built-in function cosh>, 9999, None, None, None), 'degrees': (<built-in function degrees>, 9999, None, None, '%s\cdot\frac{360}{2\pi}'), 'erf': (<built-in function erf>, 9999, None, None, None), 'erfc': (<built-in function erfc>, 9999, None, None, None), 'exp': (<built-in function exp>, 9999, None, None, 'e^{%s}'), 'expm1': (<built-in function expm1>, 9999, None, None, 'e^{%s}-1'), 'fabs': (<built-in function fabs>, 9999, None, None, '\lvert{%s}\rvert'), 'factorial': (<built-in function factorial>, 9999, '%s!', 'fact', '%s!'), 'factorial2': (<function factorial2 at 0x7f41c01322f0>, 9999, '%s!', 'fact', '%s!!'), 'floor': (<built-in function floor>, 9999, None, None, '\left\lfloor{%s}\right\rfloor'), 'fmod': (<built-in function fmod>, 9999, None, None, None), 'frexp': (<built-in function frexp>, 9999, None, None, None), 'fsum': (<built-in function fsum>, 9999, None, None, None), 'gamma': (<built-in function gamma>, 9999, None, None, '\Gamma'), 'gcd': (<built-in function gcd>, 9999, None, None, None), 'hypot': (<built-in function hypot>, 9999, None, None, None), 'isclose': (<built-in function isclose>, 9999, None, None, None), 'isfinite': (<built-in function isfinite>, 9999, None, None, None), 'isinf': (<built-in function isinf>, 9999, None, None, None), 'isnan': (<built-in function isnan>, 9999, None, None, None), 'ldexp': (<built-in function ldexp>, 9999, None, None, None), 'lgamma': (<built-in function lgamma>, 9999, 'log(abs(gamma(%s)))', 'log(abs(gamma(%s)))', '\ln\lvert\Gamma\left({%s}\rvert)\right)'), 'log': (<built-in function log>, 9999, None, None, '\ln'), 'log10': (<built-in function log10>, 9999, None, None, '\log_{10}'), 'log1p': (<built-in function log1p>, 9999, None, None, '\ln\left(1-{%s}\rvert)'), 'log2': (<built-in function log2>, 9999, None, None, '\log_2'), 'modf': (<built-in function modf>, 9999, None, None, None), 'normal_pdf': (<function normal_pdf at 0x7f41ac00f2f0>, 9999, None, None, None), 'pow': (<built-in function pow>, 9999, None, None, None), 'radians': (<built-in function radians>, 9999, None, None, '%s\cdot\frac{2\pi}{360}'), 'sin': (<built-in function sin>, 9999, None, None, None), 'sinh': (<built-in function sinh>, 9999, None, None, None), 'sqrt': (<function sqrt at 0x7f41bfeb0840>, 9999, None, None, '\sqrt{%s}'), 'tan': (<built-in function tan>, 9999, None, None, None), 'tanh': (<built-in function tanh>, 9999, None, None, None), 'trunc': (<built-in function trunc>, 9999, None, None, '\left\lfloor{%s}\right\rfloor')})[source]

Bases: ast.NodeVisitor

Parameters:dialect – int index in _operators of symbols to use
__init__(dialect, operators={<class '_ast.USub'>: (<built-in function neg>, 1150, '-', '-', '-'), <class '_ast.Gt'>: (<built-in function gt>, 600, ' > ', ' > ', ' \\gtr '), <class '_ast.Pow'>: (<function pow>, 1400, '^', '**', '^'), <class '_ast.Num'>: (None, 9000), <class '_ast.Or'>: (<built-in function or_>, 300, ' or ', ' or ', ' \\vee '), <class '_ast.Div'>: (<built-in function truediv>, 1201, '/', '/', '\\frac{%s}{%s}'), <class '_ast.BitXor'>: (<built-in function xor>, 800, ' xor ', ' xor ', ' xor '), <class '_ast.FloorDiv'>: (<built-in function floordiv>, 1201, '//', '//', '\\left\\lfloor\\frac{%s}{%s}\\right\\rfloor'), <class '_ast.Sub'>: (<built-in function sub>, 1101, '-', '-', '-'), <class '_ast.LShift'>: (<built-in function lshift>, 1000, ' << ', ' << ', ' \\ll '), <class '_ast.Not'>: (<built-in function not_>, 500, 'not ', 'not ', '\\neg'), <class '_ast.Lt'>: (<built-in function lt>, 600, ' < ', ' < ', ' \\ltr '), <class '_ast.GtE'>: (<built-in function ge>, 600, ' >= ', ' >= ', ' \\gec '), <class '_ast.Mod'>: (<built-in function mod>, 1200, ' mod ', '%', ' \\bmod '), <class '_ast.Name'>: (None, 9000), <class '_ast.Eq'>: (<built-in function eq>, 600, '=', ' == ', ' = '), <class '_ast.Mult'>: (<built-in function mul>, 1200, '*', '*', ' \\cdot '), <class '_ast.RShift'>: (<built-in function rshift>, 1000, ' >> ', ' >> ', ' \\gg '), <class '_ast.UAdd'>: (<built-in function pos>, 1150, '+', '+', '+'), <class '_ast.LtE'>: (<built-in function le>, 600, ' <= ', ' <= ', ' \\leq '), <class '_ast.Add'>: (<built-in function add>, 1100, '+', '+', '+'), <class '_ast.And'>: (<built-in function and_>, 400, ' and ', ' and ', ' \\wedge '), <class '_ast.Call'>: (None, 9000), <class '_ast.Invert'>: (<built-in function not_>, 1300, '~', '~', '\\sim ')}, functions={'abs': (<built-in function abs>, 9999, None, None, '\\lvert{%s}\\rvert'), 'acos': (<built-in function acos>, 9999, None, None, '\\arccos'), 'acosh': (<built-in function acosh>, 9999, None, None, '\\cosh^{-1}'), 'asin': (<built-in function asin>, 9999, None, None, '\\arcsin'), 'asinh': (<built-in function asinh>, 9999, None, None, '\\sinh^{-1}'), 'atan': (<built-in function atan>, 9999, None, None, '\\arctan'), 'atan2': (<built-in function atan2>, 9999, None, None, None), 'atanh': (<built-in function atanh>, 9999, None, None, '\\tanh^{-1}'), 'ceil': (<built-in function ceil>, 9999, None, None, '\\left\\lceil{%s}\\right\\rceil'), 'copysign': (<built-in function copysign>, 9999, None, None, None), 'cos': (<built-in function cos>, 9999, None, None, None), 'cosh': (<built-in function cosh>, 9999, None, None, None), 'degrees': (<built-in function degrees>, 9999, None, None, '%s\\cdot\\frac{360}{2\\pi}'), 'erf': (<built-in function erf>, 9999, None, None, None), 'erfc': (<built-in function erfc>, 9999, None, None, None), 'exp': (<built-in function exp>, 9999, None, None, 'e^{%s}'), 'expm1': (<built-in function expm1>, 9999, None, None, 'e^{%s}-1'), 'fabs': (<built-in function fabs>, 9999, None, None, '\\lvert{%s}\\rvert'), 'factorial': (<built-in function factorial>, 9999, '%s!', 'fact', '%s!'), 'factorial2': (<function factorial2 at 0x7f41c01322f0>, 9999, '%s!', 'fact', '%s!!'), 'floor': (<built-in function floor>, 9999, None, None, '\\left\\lfloor{%s}\\right\\rfloor'), 'fmod': (<built-in function fmod>, 9999, None, None, None), 'frexp': (<built-in function frexp>, 9999, None, None, None), 'fsum': (<built-in function fsum>, 9999, None, None, None), 'gamma': (<built-in function gamma>, 9999, None, None, '\\Gamma'), 'gcd': (<built-in function gcd>, 9999, None, None, None), 'hypot': (<built-in function hypot>, 9999, None, None, None), 'isclose': (<built-in function isclose>, 9999, None, None, None), 'isfinite': (<built-in function isfinite>, 9999, None, None, None), 'isinf': (<built-in function isinf>, 9999, None, None, None), 'isnan': (<built-in function isnan>, 9999, None, None, None), 'ldexp': (<built-in function ldexp>, 9999, None, None, None), 'lgamma': (<built-in function lgamma>, 9999, 'log(abs(gamma(%s)))', 'log(abs(gamma(%s)))', '\\ln\\lvert\\Gamma\\left({%s}\\rvert)\\right)'), 'log': (<built-in function log>, 9999, None, None, '\\ln'), 'log10': (<built-in function log10>, 9999, None, None, '\\log_{10}'), 'log1p': (<built-in function log1p>, 9999, None, None, '\\ln\\left(1-{%s}\\rvert)'), 'log2': (<built-in function log2>, 9999, None, None, '\\log_2'), 'modf': (<built-in function modf>, 9999, None, None, None), 'normal_pdf': (<function normal_pdf at 0x7f41ac00f2f0>, 9999, None, None, None), 'pow': (<built-in function pow>, 9999, None, None, None), 'radians': (<built-in function radians>, 9999, None, None, '%s\\cdot\\frac{2\\pi}{360}'), 'sin': (<built-in function sin>, 9999, None, None, None), 'sinh': (<built-in function sinh>, 9999, None, None, None), 'sqrt': (<function sqrt at 0x7f41bfeb0840>, 9999, None, None, '\\sqrt{%s}'), 'tan': (<built-in function tan>, 9999, None, None, None), 'tanh': (<built-in function tanh>, 9999, None, None, None), 'trunc': (<built-in function trunc>, 9999, None, None, '\\left\\lfloor{%s}\\right\\rfloor')})[source]
Parameters:dialect – int index in _operators of symbols to use
prec(op)[source]

calculate the precedence of op

visit_Call(n)[source]
visit_Name(n)[source]
visit_NameConstant(node)[source]
visit_UnaryOp(n)[source]
__class__

alias of builtins.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_BinOp(n)[source]
visit_Compare(n)[source]
visit_Num(n)[source]
generic_visit(n)[source]

Called if no explicit visitor function exists for a node.