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
On Thursday, 31 March 2016 17:46:50 UTC+2, Aaron Meurer wrote:
>
> 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
>
>
> On Thu, Mar 31, 2016 at 11:13 AM, Henry Schreiner
> <[email protected] <javascript:>> wrote:
> > 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?
> >
> > --
> > 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] <javascript:>.
> > To post to this group, send email to [email protected]
> <javascript:>.
> > 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.
>
--
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 post to this group, send email to [email protected].
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/4e5ac282-845e-4458-968e-f72d64a5379f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.