ConstantSymbol

81 views
Skip to first unread message

Henry Schreiner

unread,
Mar 31, 2016, 11:23:12 AM3/31/16
to sympy
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?

Aaron Meurer

unread,
Mar 31, 2016, 11:46:50 AM3/31/16
to sy...@googlegroups.com
Sure, it sounds useful.

I think the assumptions should mirror the assumptions of self.value
(currently, Floats set is_rational to None), which I believe is
intentional.

For lambdify, I suppose LambdaPrinter ought to define its own print
method. That might break existing code that expects it to use str, so
we should make sure it falls back.

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/c5ac54f3-1012-4bc9-86d4-88a7740ce27b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Björn Dahlgren

unread,
Sep 20, 2016, 8:39:06 AM9/20/16
to sympy
Henry, did you get any further with this?
I have often missed this kind of object, I usually keep track of the values of constants in a dictionary and perform xreplace
before calling e.g. lambdify. But using a ConstantSymbol along the lines which you have described would make the
code much cleaner.

Looking a bit further, I think a natural next step would then be to pre-define symbols for the natural constants in sympy.physics.units, e.g.:
Avogadro_constant = ConstantSymbol('N_A', avogadro_number/mol)

using objects like this with lambdify would then require the user to choose a system of units (e.g. SI, imperial units, etc) at invocation
of `lambdify`. The callback returned by lambdify would then assume nondimensionalised input in the chosen unit system
(which means that the user would likely appreciate having a corresponding nondimensionalising-callback for work with input carrying units).

Best,
Björn

B.L.

unread,
Apr 2, 2020, 12:24:06 PM4/2/20
to sympy
Any updates on this?
Is it now possible to do a Constant Symbol in numpy?

If not, is subclassing NumberSymbol the best option?
I did not find any documentation about how to do it, the sample from Henry is the largest piece of information I found.

Thank you.

Aaron Meurer

unread,
Apr 2, 2020, 1:09:22 PM4/2/20
to sympy
I don't think so. It would be good to open an issue about this. I
think Henry's example is a good start, but we also need to modify the
lambdify printer as I described.

Aaron Meurer
> --
> 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 sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/9c6f4941-2071-4294-b7a2-31c9030f3cd9%40googlegroups.com.

B.L.

unread,
Apr 3, 2020, 3:48:16 AM4/3/20
to sympy
Reply all
Reply to author
Forward
0 new messages