69 lines
1.5 KiB
Python
69 lines
1.5 KiB
Python
from enum import Enum
|
|
import numpy as np
|
|
|
|
class Operation(Enum):
|
|
VAR = 1
|
|
CONST = 2
|
|
ADD = 3
|
|
SUB = 4
|
|
MULT = 5
|
|
DIV = 6
|
|
EXP = 7
|
|
|
|
# const class
|
|
class Const:
|
|
def __init__(self, value) -> None:
|
|
self.value = value
|
|
self.type = "Const"
|
|
|
|
def __add__(self, other):
|
|
if other.type == "Const":
|
|
return Equation(
|
|
Operation.CONST,
|
|
None,
|
|
None,
|
|
self.value
|
|
) + Equation(
|
|
Operation.CONST,
|
|
None,
|
|
None,
|
|
other.value
|
|
)
|
|
if other.type == "Var":
|
|
return Equation(
|
|
Operation.CONST,
|
|
None,
|
|
None,
|
|
self.value
|
|
) + Equation(
|
|
Operation.VAR,
|
|
None,
|
|
None,
|
|
other.name
|
|
)
|
|
if other.type == "Equation":
|
|
return Equation(
|
|
Operation.CONST,
|
|
None,
|
|
None,
|
|
self.value
|
|
) + other
|
|
|
|
# var class
|
|
class Var:
|
|
def __init__(self, name) -> None:
|
|
self.name = name
|
|
self.type = "Var"
|
|
|
|
# equation class
|
|
class Equation:
|
|
def __init__(self, op, left, right, value):
|
|
self.op = op
|
|
self.left = left
|
|
self.right = right
|
|
self.value = value
|
|
self.type = "Equation"
|
|
|
|
def __add__(self, other):
|
|
if other.type == "Equation":
|
|
return Equation(Operation.ADD, self, other, None)
|