Last active
October 25, 2021 08:48
-
-
Save maedoc/6949dabc1a3aeb8ddb246e43c4ea7e65 to your computer and use it in GitHub Desktop.
Low entropy symbolic model definitions in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sympy | |
class DfunSymGen(dict): | |
def __init__(self): | |
self.symbols = [] | |
def __setitem__(self, key, val): | |
dict.__setitem__(self, key, val) | |
self.symbols.append(key) | |
def __getitem__(self, key): | |
if key not in self: | |
if key in dir(sympy): | |
obj = getattr(sympy, key) | |
else: | |
obj = sympy.Symbol(key) | |
self.__setitem__(key, obj) | |
return super().__getitem__(key) | |
class DfunMeta(type): | |
@classmethod | |
def __prepare__(metacls, name, bases, **kwds): | |
return DfunSymGen() | |
def __new__(cls, name, bases, classdict): | |
result = type.__new__(cls, name, bases, dict(classdict)) | |
result.symbols = classdict.symbols | |
return result | |
class Montbrio(metaclass=DfunMeta): | |
dr = 1/tau * ( Delta / (pi * tau) + 2 * V * r) | |
dV = 1/tau * ( V**2 - pi**2 * tau**2 * r**2 + eta + J * tau * r + I + cr * Coupling_Term_r + cv * Coupling_Term_V) | |
m = Montbrio() | |
print(m.dr) # -> (Delta/(pi*tau) + 2*V*r)/tau | |
print(m.dV) # | |
print(m.symbols) # -> [..., 'cv', 'Coupling_Term_V', 'dV'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment