mathy.core.expressions

AbsExpression

AbsExpression(
    self,
    child: mathy.core.expressions.MathExpression = None,
    child_on_left: bool = True,
)
Evaluates the absolute value of an expression.

AddExpression

AddExpression(self, left=None, right=None)
Add one and two

BinaryExpression

BinaryExpression(self, left=None, right=None)
An expression that operates on two sub-expressions

get_priority

BinaryExpression.get_priority(self) -> int
Return a number representing the order of operations priority of this node. This can be used to check if a node is locked with respect to another node, i.e. the other node must be resolved first during evaluation because of it's priority.

to_math_ml_fragment

BinaryExpression.to_math_ml_fragment(self) -> str
Render this node as a MathML element fragment

ConstantExpression

ConstantExpression(self, value=None)
A Constant value node, where the value is accessible as node.value

DivideExpression

DivideExpression(self, left=None, right=None)
Divide one by two

EqualExpression

EqualExpression(self, left=None, right=None)
Evaluate equality of two expressions

operate

EqualExpression.operate(
    self,
    one: Union[float, int],
    two: Union[float, int],
) -> Union[float, int]
This is where assignment of context variables might make sense. But context is not present in the expression's operate method.

Warning

TODO: Investigate this thoroughly.

FunctionExpression

FunctionExpression(
    self,
    child: mathy.core.expressions.MathExpression = None,
    child_on_left: bool = True,
)
A Specialized UnaryExpression that is used for functions. The function name in text (used by the parser and tokenizer) is derived from the name() method on the class.

MathExpression

MathExpression(
    self,
    id: str = None,
    left: 'MathExpression' = None,
    right: 'MathExpression' = None,
    parent: 'MathExpression' = None,
)
Math tree node with helpers for manipulating expressions.

x y + z =

add_class

MathExpression.add_class(self, classes) -> 'MathExpression'
Associate a class name with an expression. This class name will be attached to nodes when the expression is converted to a capable output format.

See MathExpression.to_math_ml_fragment

all_changed

MathExpression.all_changed(self) -> None
Mark this node and all of its children as changed

clear_classes

MathExpression.clear_classes(self) -> None
Clear all the classes currently set on the nodes in this expression.

clone

MathExpression.clone(self) -> 'MathExpression'
A specialization of the clone method that can track and report a cloned subtree node.

See MathExpression.clone_from_root for more details.

clone_from_root

MathExpression.clone_from_root(
    self,
    node: 'MathExpression' = None,
) -> 'MathExpression'
Clone this node including the entire parent hierarchy that it has. This is useful when you want to clone a subtree and still maintain the overall hierarchy.

Arguments

  • node (MathExpression): The node to clone.

Returns

(MathExpression): The cloned node.

color

Color to use for this node when rendering it as changed with .terminal_text

evaluate

MathExpression.evaluate(
    self,
    context: Dict[str, Union[float, int]] = None,
) -> Union[float, int]
Evaluate the expression, resolving all variables to constant values

find_id

MathExpression.find_id(
    self,
    id: str,
) -> Optional[MathExpression]
Find an expression by its unique ID.

Returns: The found MathExpression or None

find_type

MathExpression.find_type(self, instanceType:Type[~NodeType]) -> List[~NodeType]
Find an expression in this tree by type.

  • instanceType: The type to check for instances of

Returns the found MathExpression objects of the given type.

make_ml_tag

MathExpression.make_ml_tag(
    self,
    tag: str,
    content: str,
    classes: List[str] = [],
) -> str
Make a MathML tag for the given content while respecting the node's given classes.

Arguments

  • tag (str): The ML tag name to create.
  • content (str): The ML content to place inside of the tag. classes (List[str]) An array of classes to attach to this tag.

Returns

(str): A MathML element with the given tag, content, and classes

path_to_root

MathExpression.path_to_root(self) -> str
Generate a namespaced path key to from the current node to the root. This key can be used to identify a node inside of a tree.

raw

raw text representation of the expression.

set_changed

MathExpression.set_changed(self) -> None
Mark this node as having been changed by the application of a Rule

terminal_text

Text output of this node that includes terminal color codes that highlight which nodes have been changed in this tree as a result of a transformation.

to_list

MathExpression.to_list(
    self,
    visit: str = 'preorder',
) -> List[MathExpression]
Convert this node hierarchy into a list.

to_math_ml

MathExpression.to_math_ml(self) -> str
Convert this expression into a MathML container.

to_math_ml_fragment

MathExpression.to_math_ml_fragment(self) -> str
Convert this single node into MathML.

with_color

MathExpression.with_color(self, text:str, style='bright') -> str
Render a string that is colored if something has changed

MultiplyExpression

MultiplyExpression(self, left=None, right=None)
Multiply one and two

NegateExpression

NegateExpression(
    self,
    child: mathy.core.expressions.MathExpression = None,
    child_on_left: bool = True,
)
Negate an expression, e.g. 4 becomes -4

to_math_ml_fragment

NegateExpression.to_math_ml_fragment(self) -> str
Convert this single node into MathML.

PowerExpression

PowerExpression(self, left=None, right=None)
Raise one to the power of two

SgnExpression

SgnExpression(
    self,
    child: mathy.core.expressions.MathExpression = None,
    child_on_left: bool = True,
)

operate

SgnExpression.operate(self, value:Union[float, int]) -> Union[float, int]
Determine the sign of an value.

Returns

(int): -1 if negative, 1 if positive, 0 if 0

SubtractExpression

SubtractExpression(self, left=None, right=None)
Subtract one from two

UnaryExpression

UnaryExpression(
    self,
    child: mathy.core.expressions.MathExpression = None,
    child_on_left: bool = True,
)
An expression that operates on one sub-expression