basic operations
This commit is contained in:
parent
5d891505eb
commit
073de786f5
2 changed files with 330 additions and 4 deletions
334
legrandchien.py
334
legrandchien.py
|
|
@ -8,7 +8,11 @@ class Operation(Enum):
|
||||||
SUB = 4
|
SUB = 4
|
||||||
MULT = 5
|
MULT = 5
|
||||||
DIV = 6
|
DIV = 6
|
||||||
EXP = 7
|
POW = 7
|
||||||
|
SIN = 8
|
||||||
|
COS = 9
|
||||||
|
LN = 10
|
||||||
|
EXP = 11
|
||||||
|
|
||||||
# const class
|
# const class
|
||||||
class Const:
|
class Const:
|
||||||
|
|
@ -27,7 +31,7 @@ class Const:
|
||||||
Operation.CONST,
|
Operation.CONST,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
other.value
|
other
|
||||||
)
|
)
|
||||||
if other.type == "Var":
|
if other.type == "Var":
|
||||||
return Equation(
|
return Equation(
|
||||||
|
|
@ -39,7 +43,7 @@ class Const:
|
||||||
Operation.VAR,
|
Operation.VAR,
|
||||||
None,
|
None,
|
||||||
None,
|
None,
|
||||||
other.name
|
other
|
||||||
)
|
)
|
||||||
if other.type == "Equation":
|
if other.type == "Equation":
|
||||||
return Equation(
|
return Equation(
|
||||||
|
|
@ -49,11 +53,243 @@ class Const:
|
||||||
self.value
|
self.value
|
||||||
) + other
|
) + other
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) - Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) - Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) - other
|
||||||
|
|
||||||
|
def __mul__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) * Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) * Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) * other
|
||||||
|
|
||||||
|
def __truediv__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) / Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) / Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.value
|
||||||
|
) / other
|
||||||
|
|
||||||
# var class
|
# var class
|
||||||
class Var:
|
class Var:
|
||||||
def __init__(self, name) -> None:
|
def __init__(self, name, degree=0) -> None:
|
||||||
self.name = name
|
self.name = name
|
||||||
self.type = "Var"
|
self.type = "Var"
|
||||||
|
self.degree = degree
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) + Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) + Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) + other
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) - Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) - Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) - other
|
||||||
|
|
||||||
|
def __mul__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) * Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) * Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) * other
|
||||||
|
|
||||||
|
def __truediv__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) / Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) / Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
self.name
|
||||||
|
) / other
|
||||||
|
|
||||||
# equation class
|
# equation class
|
||||||
class Equation:
|
class Equation:
|
||||||
|
|
@ -64,6 +300,96 @@ class Equation:
|
||||||
self.value = value
|
self.value = value
|
||||||
self.type = "Equation"
|
self.type = "Equation"
|
||||||
|
|
||||||
|
def copy(self):
|
||||||
|
if self.op == Operation.CONST:
|
||||||
|
return Const(self.value.value)
|
||||||
|
if self.op == Operation.VAR:
|
||||||
|
return Var(self.value.name, self.value.degree)
|
||||||
|
return Operation(self.op, self.left.copy(), self.right.copy(), None)
|
||||||
|
|
||||||
|
def diff(self):
|
||||||
|
if self.op == Operation.CONST:
|
||||||
|
return Const(0)
|
||||||
|
if self.op == Operation.VAR:
|
||||||
|
return Var(self.value.name, self.value.degree+1)
|
||||||
|
if self.op == Operation.ADD:
|
||||||
|
return self.left.diff() + self.right.diff()
|
||||||
|
if self.op == Operation.SUB:
|
||||||
|
return self.left.diff() - self.right.diff()
|
||||||
|
if self.op == Operation.MULT:
|
||||||
|
return self.left.diff()*self.right.copy() + self.left.copy()*self.right.diff()
|
||||||
|
if self.op == Operation.DIV:
|
||||||
|
return (self.left.diff()*self.right.copy() - self.left.copy()*self.right.diff()) \
|
||||||
|
/ (self.right.copy()*self.right.copy())
|
||||||
|
|
||||||
def __add__(self, other):
|
def __add__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return self + Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return self + Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
if other.type == "Equation":
|
if other.type == "Equation":
|
||||||
return Equation(Operation.ADD, self, other, None)
|
return Equation(Operation.ADD, self, other, None)
|
||||||
|
|
||||||
|
def __sub__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return self - Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return self - Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(Operation.SUB, self, other, None)
|
||||||
|
|
||||||
|
def __mul__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return self * Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return self * Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(Operation.MULT, self, other, None)
|
||||||
|
|
||||||
|
def __truediv__(self, other):
|
||||||
|
if other.type == "Const":
|
||||||
|
return self / Equation(
|
||||||
|
Operation.CONST,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Var":
|
||||||
|
return self / Equation(
|
||||||
|
Operation.VAR,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
other
|
||||||
|
)
|
||||||
|
if other.type == "Equation":
|
||||||
|
return Equation(Operation.DIV, self, other, None)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue