Expression Simplifier
SRToolkit.utils.expression_simplifier
simplify
simplify(expr: Union[List[str], Node], symbol_library: SymbolLibrary = SymbolLibrary.default_symbols()) -> Union[List[str], Node]
Simplifies a mathematical expression by
- Making use of sympy's simplification functions
- Simplifying constants, e.g. C*C + C -> C
Examples:
>>> expr = ["C", "+", "C" "*", "C", "+", "X_0", "*", "X_1", "/", "X_0"]
>>> print("".join(simplify(expr)))
C+X_1
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
expr
|
Union[List[str], Node]
|
The expression given as a list of tokens in the infix notation or as an instance of SRToolkit.utils.expression_tree.Node |
required |
symbol_library
|
SymbolLibrary
|
The symbol library to use. Defaults to SymbolLibrary.default_symbols(). |
default_symbols()
|
Raises:
| Type | Description |
|---|---|
Exception
|
If problems occur during simplification or if the expression contains invalid symbols. |
Returns:
| Type | Description |
|---|---|
Union[List[str], Node]
|
The simplified expression |