From 073de786f5d06955a31411d0d81870d211a62ff2 Mon Sep 17 00:00:00 2001 From: Didictateur Date: Sun, 15 Feb 2026 00:11:17 +0100 Subject: [PATCH] basic operations --- main.py => example.py | 0 legrandchien.py | 334 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 330 insertions(+), 4 deletions(-) rename main.py => example.py (100%) diff --git a/main.py b/example.py similarity index 100% rename from main.py rename to example.py diff --git a/legrandchien.py b/legrandchien.py index 056b0da..14f94fe 100644 --- a/legrandchien.py +++ b/legrandchien.py @@ -8,7 +8,11 @@ class Operation(Enum): SUB = 4 MULT = 5 DIV = 6 - EXP = 7 + POW = 7 + SIN = 8 + COS = 9 + LN = 10 + EXP = 11 # const class class Const: @@ -27,7 +31,7 @@ class Const: Operation.CONST, None, None, - other.value + other ) if other.type == "Var": return Equation( @@ -39,7 +43,7 @@ class Const: Operation.VAR, None, None, - other.name + other ) if other.type == "Equation": return Equation( @@ -48,12 +52,244 @@ class Const: None, self.value ) + 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 class Var: - def __init__(self, name) -> None: + def __init__(self, name, degree=0) -> None: self.name = name 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 class Equation: @@ -64,6 +300,96 @@ class Equation: self.value = value 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): + 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.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)