Modules

Equation Module

This is the only module you should need to import

It maps the Expression Class from Equation.core.Expression

Its recomended you use the code:

from Equation import Expression
...

to import the Expression Class, as this is the only Class/Function you should use.

class Equation.Expression(expression, argorder=[], *args, **kwargs)

Expression or Equation Object

This is a object that respresents an equation string in a manner that allows for it to be evaluated

Arithmetic Operators:

Expression objects support combining with the standard arithmetic operators to create new Expression objects, they may also be combined with numerical constant, and strings containg a valid Expression.

>>> from Equation import Expression
>>> fn = Expression("x")
>>> fn += 2
>>> fn
(x + 2)
>>> fn **=3
>>> fn
((x + 2) ^ 3)
>>> fn -= "z"
>>> fn
(((x + 2) ^ 3) - z)
>>> fn(1,2)
25
Parameters:
  • expression (str) – String resprenstation of an equation
  • argorder (list of str) – List of variable names, indicating the position of variable for mapping from positional arguments
fn(*args, **kwargs)

Evaluates the Expression object using the variables passed, where preset arguments will act as the defualt value for said arguments.

Parameters:
  • *args – Positional variables, order as defined by argorder, then position in equation
  • **kwargs – List of variables to be used by the equation for evaluation
Returns:

Result of Expression Evaluation

Raises TypeError:
 

For Invalid number of arguments, or arguments given multiple values, as with normal python functions

fn[var]

Get the preset variable var, from the Expression Object

fn[var] = value

Set the preset variable var to the value value

del fn[var]

Removes the preset variable var from the Expression Object

var in fn
Returns:True if fn has a variable var
var not in fn
Returns:True if fn dosen’t have a variable var
for var in fn

Iterates over all variables var in fn

str(fn)

Generates a Printable version of the Expression

Returns:Latex String respresation of the Expression, suitable for rendering the equation
repr(fn)

Generates a String that correctrly respresents the equation

Returns:Convert the Expression to a String that passed to the constructor, will constuct an identical equation object (in terms of sequence of tokens, and token type/value)
show()

Show RPN tokens

This will print out the internal token list (RPN) of the expression one token perline.

Equation.similar Module

Provides support for similar type comparsion, and allows the tolerance to be changed

Comparsions work by detriming if the following is true

\[1-\frac{\min(A,B)}{\max(A,B)}\leq tolerance\]

If it is true the \(A\) and \(B\) are considered to be equal

This module only needs to be imported if you need to change the tolerance for similarlity tests, otherwise you just need to use the similarlity operators in your expression

>>> from Equation import Expression
>>> fn = Expression("x ~ y")
>>> fn(1,1.1)
False
>>> fn(1,1.00001)
True
>>> fn(1,1.001)
False
>>> from Equation.similar import set_tol
>>> set_tol(1e-2)
>>> fn(1,1.1)
False
>>> fn(1,1.00001)
True
>>> fn(1,1.001)
True

By default the tolerance is \(10^{-5}\) Hence 1.001 isn’t condisered similar to 1, but by changing the tolerance to \(10^{-2}\), 1.001 is condisered similar to 1

Equation.similar.set_tol(value=1e-05)

Set Error Tolerance

Set the tolerance for detriming if two numbers are simliar, i.e \(\left|\frac{a}{b}\right| = 1 \pm tolerance\)

Parameters:value (float) – The Value to set the tolerance to show be very small as it respresents the percentage of acceptable error in detriming if two values are the same.

Next page

→ Examples