From e042a9f9d579dcafd9282b1fb970a05998370671 Mon Sep 17 00:00:00 2001 From: Didictateur Date: Tue, 17 Feb 2026 14:02:41 +0100 Subject: [PATCH] simplify var creation --- double_pendule.py | 4 ++-- double_pendule_a_ressort.py | 8 ++++---- legrandchien.py | 15 +++++++++++++-- pendule_a_ressort.py | 4 ++-- pendule_simple.py | 3 ++- 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/double_pendule.py b/double_pendule.py index 9cc408f..ca6deb4 100644 --- a/double_pendule.py +++ b/double_pendule.py @@ -11,8 +11,8 @@ m2 = lgc.Const(1) r1 = lgc.Const(1) r2 = lgc.Const(1) -theta1 = lgc.Var("theta1") -theta2 = lgc.Var("theta2") +theta1 = lgc.Var() +theta2 = lgc.Var() x1 = r1 * lgc.cos(theta1) y1 = r1 * lgc.sin(theta1) diff --git a/double_pendule_a_ressort.py b/double_pendule_a_ressort.py index 432540a..0144980 100644 --- a/double_pendule_a_ressort.py +++ b/double_pendule_a_ressort.py @@ -11,10 +11,10 @@ r20 = lgc.Const(1) k1 = lgc.Const(50) k2 = lgc.Const(50) -theta1 = lgc.Var("theta1") -theta2 = lgc.Var("theta2") -r1 = lgc.Var("r1") -r2 = lgc.Var("r2") +theta1 = lgc.Var() +theta2 = lgc.Var() +r1 = lgc.Var() +r2 = lgc.Var() x1 = r1 * lgc.cos(theta1) y1 = r1 * lgc.sin(theta1) diff --git a/legrandchien.py b/legrandchien.py index 6bcf24e..b9234ae 100644 --- a/legrandchien.py +++ b/legrandchien.py @@ -1,6 +1,8 @@ from enum import Enum import numpy as np from tqdm import tqdm +import inspect +import dis class Operation(Enum): VAR = 1 @@ -178,10 +180,19 @@ class Const: # var class class Var: - def __init__(self, name, degree=0) -> None: - self.name = name + def __init__(self, name=None, degree=0) -> None: self.type = "Var" self.degree = degree + if name == None: + frame = inspect.currentframe().f_back + instructions = list(dis.get_instructions(frame.f_code)) + lasti = frame.f_lasti + for i, instr in enumerate(instructions): + if instr.offset > lasti: + if instr.opname in ("STORE_NAME", "STORE_FAST"): + name = instr.argval + break + self.name = name def copy(self): return Var(self.name, self.degree) diff --git a/pendule_a_ressort.py b/pendule_a_ressort.py index d23dc50..421660e 100644 --- a/pendule_a_ressort.py +++ b/pendule_a_ressort.py @@ -8,8 +8,8 @@ m = lgc.Const(1) k = lgc.Const(10) r0 = lgc.Const(1) -theta = lgc.Var("theta") -r = lgc.Var("r") +theta = lgc.Var() +r = lgc.Var() x = r * lgc.cos(theta) y = r * lgc.sin(theta) diff --git a/pendule_simple.py b/pendule_simple.py index 6be3979..6151969 100644 --- a/pendule_simple.py +++ b/pendule_simple.py @@ -7,7 +7,8 @@ g = lgc.Const(-10) m = lgc.Const(1) r = lgc.Const(1) -theta = lgc.Var("theta") +theta = lgc.Var() +print(theta.name) x = r * lgc.cos(theta) y = r * lgc.sin(theta)