Change "instances" class variables of Smybols and children of Symbols to Dict instead of List. Start expand.py (not completed)
This commit is contained in:
parent
77c8973eb7
commit
1738fa5d4c
6 changed files with 41 additions and 27 deletions
|
@ -24,6 +24,10 @@ class Tree(ABC):
|
||||||
self.depth_first_order = depth_first_order
|
self.depth_first_order = depth_first_order
|
||||||
self.children = children if children else []
|
self.children = children if children else []
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_leaf(self) -> bool:
|
||||||
|
return not self.children
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f'Tree({self.value}, {self.children})' if self.children else f'Tree({self.value})'
|
return f'Tree({self.value}, {self.children})' if self.children else f'Tree({self.value})'
|
||||||
|
|
||||||
|
|
|
@ -7,18 +7,22 @@ class Operator(Symbols):
|
||||||
"""
|
"""
|
||||||
Represent an operator, like +, *, sin, anything that can be applied to an expression
|
Represent an operator, like +, *, sin, anything that can be applied to an expression
|
||||||
"""
|
"""
|
||||||
instances = []
|
instances = {}
|
||||||
|
|
||||||
def __init__(self, name: str, precedence: int, call: Callable):
|
def __init__(self, name: str, precedence: int, call: Callable, repeated_op: Operator = None):
|
||||||
"""
|
"""
|
||||||
:param name of the operator
|
:param name of the operator
|
||||||
:param precedence: precedence of the operator, higher is better
|
:param precedence: precedence of the operator, higher is better
|
||||||
:param call: function to apply the operator
|
:param call: function to apply the operator
|
||||||
|
:param repeated_op: if you repeat the operator what do you get ?
|
||||||
|
for exemple a+a+a+a -> 4*a, the repeated_op of Add is Mul
|
||||||
"""
|
"""
|
||||||
|
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.precedence = precedence
|
self.precedence = precedence
|
||||||
self.call = call
|
self.call = call
|
||||||
Operator.instances.append(self)
|
self.repeated_op = repeated_op
|
||||||
|
Operator.instances[name] = self
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'{self.name}'
|
return f'{self.name}'
|
||||||
|
@ -29,11 +33,11 @@ class UnaryOperator(Operator):
|
||||||
Represent a unary operator, like sin, cos, - etc...
|
Represent a unary operator, like sin, cos, - etc...
|
||||||
all operators that take only one argument
|
all operators that take only one argument
|
||||||
"""
|
"""
|
||||||
instances = []
|
instances = {}
|
||||||
|
|
||||||
def __init__(self, name: str, precedence: int, call: Callable):
|
def __init__(self, name: str, precedence: int, call: Callable, repeated_op: Operator = None):
|
||||||
UnaryOperator.instances.append(self)
|
UnaryOperator.instances[name] = self
|
||||||
super().__init__(name, precedence, call)
|
super().__init__(name, precedence, call, repeated_op)
|
||||||
|
|
||||||
def apply(self, expr):
|
def apply(self, expr):
|
||||||
return self.call(expr)
|
return self.call(expr)
|
||||||
|
@ -77,11 +81,11 @@ class BinOperator(Operator):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Used to store all the instances of BinOperator, used in the parser
|
# Used to store all the instances of BinOperator, used in the parser
|
||||||
instances = []
|
instances = {}
|
||||||
|
|
||||||
def __init__(self, name: str, precedence: int, properties: BinProperties, call: Callable):
|
def __init__(self, name: str, precedence: int, properties: BinProperties, call: Callable, repeated_op: Operator = None ):
|
||||||
BinOperator.instances.append(self)
|
BinOperator.instances[name] = self
|
||||||
super().__init__(name, precedence, call)
|
super().__init__(name, precedence, call, repeated_op)
|
||||||
self.properties = properties
|
self.properties = properties
|
||||||
|
|
||||||
def apply(self, left, right):
|
def apply(self, left, right):
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Symbols:
|
class Symbols:
|
||||||
"""
|
"""
|
||||||
All maths things (other than number) that will be parsed need to be of "Symbols" class
|
All maths things (other than integers) that will be parsed need to be of "Symbols" class
|
||||||
"""
|
"""
|
||||||
instances = []
|
instances = {}
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
|
assert name not in Symbols.instances, f'Symbol with name {name} already exists'
|
||||||
self.name = name
|
self.name = name
|
||||||
Symbols.instances.append(self)
|
Symbols.instances[name] = self
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
@ -39,10 +39,10 @@ class Var(Symbols):
|
||||||
"""
|
"""
|
||||||
variable, like 'x' in x+2
|
variable, like 'x' in x+2
|
||||||
"""
|
"""
|
||||||
instances = []
|
instances = {}
|
||||||
|
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
super().__init__(name)
|
super().__init__(name)
|
||||||
self.__class__.instances.append(self)
|
self.__class__.instances[name] = self
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,16 +11,11 @@ ParenthesisRight = Symbols(')')
|
||||||
|
|
||||||
Number = Union[int, float, Fraction]
|
Number = Union[int, float, Fraction]
|
||||||
|
|
||||||
name_to_symbol = {sy.name:sy for sy in Symbols.instances}
|
name_to_symbol = Symbols.instances
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def update_symbols_dict():
|
|
||||||
global name_to_symbol
|
|
||||||
name_to_symbol = {sy.name:sy for sy in Symbols.instances}
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
example1 = "a + b * c + d"
|
example1 = "a + b * c + d"
|
||||||
-
|
-
|
||||||
|
@ -37,7 +32,7 @@ def preprocess(expr: str) -> List:
|
||||||
:param expr: string expression
|
:param expr: string expression
|
||||||
:return: list of symbols and numbers
|
:return: list of symbols and numbers
|
||||||
"""
|
"""
|
||||||
update_symbols_dict()
|
|
||||||
return_list = []
|
return_list = []
|
||||||
expr = expr.strip()
|
expr = expr.strip()
|
||||||
expr = expr.replace(' ', '')
|
expr = expr.replace(' ', '')
|
||||||
|
|
14
python_symb/TreeModification/expand.py
Normal file
14
python_symb/TreeModification/expand.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
from python_symb.Expressions.expr import Expr
|
||||||
|
from python_symb.MathTypes.symbols import Var
|
||||||
|
from python_symb.MathTypes.operator_file import Operator, BinOperator, Add, Mul
|
||||||
|
|
||||||
|
def expand(expr: Expr) -> Expr:
|
||||||
|
"""
|
||||||
|
Expand an expression
|
||||||
|
:param expr: expression to expand
|
||||||
|
:return: expanded expression
|
||||||
|
"""
|
||||||
|
|
||||||
|
if expr.is_leaf:
|
||||||
|
return expr
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
from python_symb.Expressions.expr import Expr
|
from python_symb.Expressions.expr import Expr
|
||||||
from python_symb.MathTypes.symbols import Var
|
from python_symb.MathTypes.symbols import Var
|
||||||
from python_symb.Parsing.parse import update_symbols_dict
|
|
||||||
|
|
||||||
|
|
||||||
class Visual:
|
class Visual:
|
||||||
|
@ -72,8 +71,6 @@ class Visual:
|
||||||
if v not in Var.instances:
|
if v not in Var.instances:
|
||||||
Var(v)
|
Var(v)
|
||||||
|
|
||||||
# Update the symbols dict
|
|
||||||
update_symbols_dict()
|
|
||||||
|
|
||||||
|
|
||||||
def show_tree(self):
|
def show_tree(self):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue