> Put another way, what would be your preferred computer language if you were SymPy starting again? There would obviously need to be an interface to Python, but would you write the rest in C++?
I've studied "Typescript" briefly, because lots of symbolic math are practical due to frontend interfaces, and hope to see it could be better directly implemented on frontend. But I'd give a warning that Javascript/Typescript is very poor for symbolic computation just because of its shallow design of equality (===). I'm not sure about if there are weird struggles like that in SymEngine developers, but there could be possibly is. On the other hand, I think that functional languages like OCamL can be better for symbolic math, like seeing how easy it is to implement basic stuff of symbolic math and functional languages often have more mathematical rigor of its operational semantics so things that looks correct in math and logic are more native for them, and easy to make it work. https://stackoverflow.com/questions/52737089/ocaml-function-to-perform-differentiation I think that the only reason people are gathering around this "old and messy" sympy than those functional languages is that a lot of people studying physical science, or AI stuff are coming to python to use mature libraries. And I believe that's the one and the only reason, and nonetheless other mainstream libraries in Python are messy with objects too, I would ideally want to see symbolic computation implemented in compositions of terms, patterns, combinators, polynomials, logic, grammar sort of thing On Thursday, December 22, 2022 at 1:37:07 PM UTC+2 Oscar wrote: > On Thu, 22 Dec 2022 at 04:52, S.Y. Lee <[email protected]> wrote: > > > > Hashcons looks interesting, but as simple as making every object as > singletons > > I would have a question how this would be implemented effectively on top > of python, for example, how to keep the hash table not grow infinitely with > dead references. > > You would use weakref: > https://docs.python.org/3/library/weakref.html#weakref.WeakValueDictionary > > I will write some blog posts about this in the new year but here's a > simple demo: > > import weakref > > _all_expressions = weakref.WeakValueDictionary() > > class Expr: > def __new__(cls, *args): > expr = _all_expressions.get(args, None) > if expr is not None: > return expr > expr = super().__new__(cls) > _all_expressions[args] = expr > return expr > def __init__(self, *args): > self.args = tuple(args) > def __repr__(self): > return f'{self.args[0]}{self.args[1:]}' > > class Head: > def __init__(self, name): > self.name = name > def __repr__(self): > return self.name > def __call__(*args): > return Expr(*args) > > Add = Head('Add') > Mul = Head('Mul') > > print('empty:', dict(_all_expressions)) > # empty: {} > > expr = Add(1, Mul(2, 3)) > > print('not empty:', dict(_all_expressions)) > # not empty: {(Mul, 2, 3): Mul(2, 3), (Add, 1, Mul(2, 3)): Add(1, Mul(2, > 3))} > > del expr # clears the weakref dict > > print('empty again:', dict(_all_expressions)) > # empty again: {} > > -- > Oscar > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/646a7e72-9516-4c72-95eb-19b95856d9d3n%40googlegroups.com.
