Created Exp in operator_file.py to represent Power
Implemented Expand and regroup
This commit is contained in:
parent
fa1f19f5cc
commit
8ee24d763b
8 changed files with 335 additions and 58 deletions
|
@ -2,12 +2,11 @@ from __future__ import annotations
|
|||
|
||||
import python_symb.MathTypes.symbols
|
||||
from python_symb.Expressions.tree import Tree
|
||||
from python_symb.MathTypes.operator_file import Add, Mul, Min
|
||||
|
||||
from python_symb.MathTypes.operator_file import UnaryOperator, BinOperator, Add, Mul, Min
|
||||
from python_symb.MathTypes.operator_file import UnaryOperator, BinOperator, Add, Mul, Exp
|
||||
|
||||
from typing import List
|
||||
from python_symb.MathTypes.symbols import Var
|
||||
from python_symb.MathTypes.symbols import Var, var
|
||||
|
||||
from python_symb.Parsing.parse import infix_str_to_postfix
|
||||
|
||||
|
@ -24,9 +23,20 @@ class Expr(Tree):
|
|||
Expr(Mul, [Expr(5), Expr(Expr(Add, [Expr(2), Expr(3)]))])
|
||||
|
||||
"""
|
||||
__match_args__ = ('value', 'children')
|
||||
|
||||
def __init__(self, value, children=None):
|
||||
from python_symb.MathTypes.operator_file import BinOperator, UnaryOperator
|
||||
super().__init__(value, children if children else [])
|
||||
assert all([isinstance(child, Expr) for child in self.children]), f'Invalid children: {self.children} all child should be Expr'
|
||||
|
||||
match value:
|
||||
case BinOperator() as op:
|
||||
assert len(self.children) == 2, f'Invalid number of children for BinOperator{op}: {len(self.children)}'
|
||||
|
||||
case UnaryOperator() as op:
|
||||
assert len(self.children) == 1, f'Invalid number of children for UnaryOperator{op}: {len(self.children)}'
|
||||
|
||||
|
||||
@staticmethod
|
||||
def from_postfix_list(postfix: List):
|
||||
|
@ -60,6 +70,31 @@ class Expr(Tree):
|
|||
expr_rev_polish = infix_str_to_postfix(expr_str)
|
||||
return Expr.from_postfix_list(expr_rev_polish)
|
||||
|
||||
|
||||
def to_infix_str(self, parent_precedence=-100, implicit_mul=True) -> str:
|
||||
"""
|
||||
Return the infix string of the expression
|
||||
"""
|
||||
match self:
|
||||
case Expr(value) if self.is_leaf:
|
||||
return str(value)
|
||||
|
||||
case Expr(UnaryOperator() as op, [child]):
|
||||
return f"{op.name}({child.to_infix_str(parent_precedence=op.precedence)})"
|
||||
|
||||
case Expr(BinOperator() as op, [left, right]):
|
||||
op_name = op.name if not(implicit_mul and op == Mul) else ''
|
||||
if op.precedence < parent_precedence:
|
||||
print("hehehe")
|
||||
print(self, op.precedence, parent_precedence)
|
||||
return f"({left.to_infix_str(op.precedence)}{op_name}{right.to_infix_str(op.precedence)})"
|
||||
else:
|
||||
return f"{left.to_infix_str(op.precedence)}{op_name}{right.to_infix_str(op.precedence)}"
|
||||
|
||||
|
||||
|
||||
|
||||
@staticmethod
|
||||
def bin_op_constructor(self, other, op):
|
||||
"""
|
||||
Construct a binary operation
|
||||
|
@ -73,32 +108,99 @@ class Expr(Tree):
|
|||
return ValueError(f'Invalid type for operation: {other} : {type(other)}')
|
||||
|
||||
def __add__(self, other):
|
||||
return self.bin_op_constructor(other, Add)
|
||||
other_expr = other if isinstance(other, Expr) else Expr(other)
|
||||
return Expr.bin_op_constructor(self, other_expr, Add)
|
||||
|
||||
def __radd__(self, other):
|
||||
other_expr = other if isinstance(other, Expr) else Expr(other)
|
||||
return Expr.bin_op_constructor(other_expr, self, Add)
|
||||
|
||||
def __mul__(self, other):
|
||||
return self.bin_op_constructor(other, Mul)
|
||||
other_expr = other if isinstance(other, Expr) else Expr(other)
|
||||
return Expr.bin_op_constructor(self, other_expr, Mul)
|
||||
|
||||
def __rmul__(self, other):
|
||||
other_expr = other if isinstance(other, Expr) else Expr(other)
|
||||
return Expr.bin_op_constructor(other_expr, self, Mul)
|
||||
|
||||
def __sub__(self, other):
|
||||
return self.bin_op_constructor(other, Min)
|
||||
return Expr.bin_op_constructor(self, other, Min)
|
||||
|
||||
def __hash__(self):
|
||||
"""
|
||||
Two equivalent expressions (without more modification like factorisation, or expanding) should have the same hash
|
||||
see test_eq
|
||||
"""
|
||||
match self:
|
||||
|
||||
case Expr(value) if self.is_leaf:
|
||||
return hash(value)
|
||||
|
||||
case Expr(UnaryOperator() as op, [child]):
|
||||
return hash(op.name + str(hash(child)))
|
||||
|
||||
case Expr(BinOperator() as op, [left, right]):
|
||||
if op.properties.commutative and op.properties.associative:
|
||||
return hash(op.name) + hash(left) + hash(right)
|
||||
else:
|
||||
return hash(op.name) + hash(str(hash(left)) + str(hash(right)))
|
||||
|
||||
case _:
|
||||
print(f'Invalid type: {type(self)}')
|
||||
|
||||
def bad_eq(self, other):
|
||||
return self.__hash__() == other.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
"""temporary"""
|
||||
return self.bad_eq(other)
|
||||
|
||||
|
||||
def test1():
|
||||
x, y = Var('x'), Var('y')
|
||||
expr1 = x + y
|
||||
expr2 = 5+x
|
||||
print(expr1 + expr2)
|
||||
def test():
|
||||
x, y = var('x'), var('y')
|
||||
a, b = var('a'), var('b')
|
||||
def test1():
|
||||
expr1 = x + y
|
||||
expr2 = 5+x
|
||||
print(expr1 + expr2)
|
||||
|
||||
|
||||
def test2():
|
||||
from python_symb.MathTypes.operator_file import Sin
|
||||
a, b = Var('a'), Var('b')
|
||||
expr = Sin(a+b)
|
||||
print(expr)
|
||||
def test2():
|
||||
from python_symb.MathTypes.operator_file import Sin
|
||||
expr = Sin(x+y)
|
||||
print(expr)
|
||||
|
||||
def test_eq():
|
||||
expr = x + y + 3
|
||||
expr2 = 3 + x + y
|
||||
print("----")
|
||||
print(expr)
|
||||
print(expr2)
|
||||
print(expr == expr2)
|
||||
|
||||
def test_return_to_string():
|
||||
expr = x+y
|
||||
new_expr = 5*expr
|
||||
print(new_expr)
|
||||
print(f"new_expr: {new_expr.to_infix_str()}")
|
||||
|
||||
expr2 = x*x*x + y*y*y
|
||||
print(expr2)
|
||||
print(f"expr2: {expr2.to_infix_str()}")
|
||||
|
||||
|
||||
print("test1")
|
||||
test1()
|
||||
print("test2")
|
||||
test2()
|
||||
print("test_eq")
|
||||
test_eq()
|
||||
print("test_return_to_string")
|
||||
test_return_to_string()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
test1()
|
||||
test2()
|
||||
test()
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue