Is there a way to make a constant symbol in Sympy? I haven't found one yet. Basically, it would be a lot like pi and other constants, but you could make your own. In other words,
>>> c = ConstantSymbol('c', 1.23)
>>> c + 1
c+1
>>> _.evalf()
2.23
That way, formulas could stay in a nice form until evaluation occurs. Also, subs will still allow changing out constants.
Here's example code that does that:
class ConstantSymbol(s.NumberSymbol):
is_real = True
is_irrational = False
is_algebraic = True
__slots__ = ['name', 'value']
def __new__(cls, name, value):
self = super(ConstantSymbol,cls).__new__(cls)
self.name=name
self.value=s.Float(value)
return self
def __getnewargs__(self):
return (self.name,self.value)
def _latex(self, printer):
return printer.doprint(s.Symbol(self.name))
def _sympystr(self, printer):
return printer.doprint(s.Symbol(self.name))
def _sympyrepr(self, printer):
return printer.doprint(s.Symbol(self.name))
def _mathml(self, printer):
return printer.doprint(s.Symbol(self.name))
def _as_mpf_val(self, prec):
return self.value._as_mpf_val(price)
The problem with the above code is that sympy.lamdify uses _sympystr() instead of evaluating it first.
Is there something like this, or would something like this be useful?