Skip to content

mathy.util

compare_expression_string_values

compare_expression_string_values(
    from_expression: str,
    to_expression: str,
    history: Optional[List[Any]] = None,
)
Compare and evaluate two expressions strings to verify they have the same value

compare_expression_values

compare_expression_values(
    from_expression: mathy.core.expressions.MathExpression,
    to_expression: mathy.core.expressions.MathExpression,
    history: Optional[List[Any]] = None,
)
Compare and evaluate two expressions to verify they have the same value

discount

discount(values:List[float], gamma=0.99) -> List[float]
Discount a list of floating point values.

Arguments

  • r (List[float]): the list of floating point values to discount
  • gamma (float): a value between 0 and 0.99 to use when discounting the inputs

Returns

(List[float]): a list of the same size as the input with discounted values

get_term_ex

get_term_ex(
    node: Optional[mathy.core.expressions.MathExpression],
) -> Optional[mathy.util.TermEx]
Extract the 3 components of a naturally ordered term.

Info

This doesn't care about whether the node is part of a larger term, it only looks at its children.

Example

4 x 7 ^ *

TermEx(coefficient=4, variable="x", exponent=7)

get_terms

get_terms(
    expression: mathy.core.expressions.MathExpression,
) -> List[mathy.core.expressions.MathExpression]
Walk the given expression tree and return a list of nodes representing the distinct terms it contains.

Arguments

  • expression (MathExpression): the expression to find term nodes in

Returns

(List[MathExpression]): a list of term nodes

has_like_terms

has_like_terms(expression:mathy.core.expressions.MathExpression) -> bool
Return True if a given expression has more than one of any type of term.

Examples

  • x + y + z = False
  • x^2 + x = False
  • y + 2x = True
  • x^2 + 4x^3 + 2y = True

is_debug_mode

is_debug_mode() -> bool
Debug mode enables extra logging and assertions, but is slower.

is_preferred_term_form

is_preferred_term_form(expression:mathy.core.expressions.MathExpression) -> bool

Return True if a given term has been simplified so that it only has a max of one coefficient and variable, with the variable on the right and the coefficient on the left side

Examples

  • Complex = 2 * 2x^2
  • Simple = x^2 * 4
  • Preferred = 4x^2

is_simple_term

is_simple_term(node:mathy.core.expressions.MathExpression) -> bool
Return True if a given term has been simplified so it only has at most one of each variable and a constant.

Examples

  • Simple = 2x^2 * 2y
  • Complex = 2x * 2x * 2y
  • Simple = x^2 * 4
  • Complex = 2 * 2x^2

pad_array

pad_array(in_list:List[Any], max_length:int, value:Any=0) -> List[Any]
Pad a list to the given size with the given padding value.

Arguments:

in_list (List[Any]): List of values to pad to the given length max_length (int): The desired length of the array value (Any): a value to insert in order to pad the array to max length

Returns

(List[Any]): An array padded to max_length size

TermEx

TermEx(self, /, *args, **kwargs)
TermEx(coefficient, variable, exponent)

coefficient

An optional integer or float coefficient

exponent

An optional integer or float exponent

variable

An optional variable

terms_are_like

terms_are_like(
    one: Union[mathy.util.TermResult, mathy.core.expressions.MathExpression, typing_extensions.Literal[False]],
    two: Union[mathy.util.TermResult, mathy.core.expressions.MathExpression, typing_extensions.Literal[False]],
) -> bool
Determine if two math expression nodes are like terms.

Arguments

  • one (MathExpression): A math expression that represents a term
  • two (MathExpression): Another math expression that represents a term

Returns

(bool): Whether the terms are like or not.

unlink(
    node: Optional[mathy.core.expressions.MathExpression] = None,
) -> Optional[mathy.core.expressions.MathExpression]
Unlink an expression from it's parent.

  1. Clear expression references in parent
  2. Clear parent in expression

Last update: March 7, 2020