Is there a solution to sympify() strings including custom methods?
Sympy has the beautiful function sympify.
- It can reduce strings to sympy-functions and reduce all expressions. Nice!
- It allows to add 'sympify'-able functions as class.
This is the official example.
from sympy import Matrix, sympify
class MyList1(object):
def __iter__(self):
yield 1
yield 2
return
def __getitem__(self, i): return list(self)[i]
def _sympy_(self): return Matrix(self)
local_dict= {"MyList1": MyList1}
print(sympify(MyList1())) # Matrix([[1], [2]])
print(sympify('MyList1()')) # MyList1()
print(sympify('MyList1()', locals=local_dict)) # <__main__.MyList1 object at 0x0000000006D0AA20>
The last two lines can not be reduced by sympify- our class is obviously not known when sympifying a string. Putting the class into 'locals' did not work for me.
Is there a solution to reduce strings?
- Need sympy function for log2(x) capable of being used in sympy.solve did not work for me
- SymPy: Safely parsing strings was not solved
- Also: In https://stackoverflow.com/a/58487317/5626139, the class type was id
Functionand notObject. Which one to use?