Hello,
I'm planning to use sympy for a project I'm working on and I have a few
questions...
Basically I want to be able to associate a value with a symbol and then be
able to evaluate any function using that symbol for the value it's been
given. This would allow me to define properties in my classes as symbolic
functions but also be able to get the numeric value for the function at the
value specified for each symbol.
Question 1: Is there already a built in way to do this without having to
call .subs() and .evalf() all over the place? (I'm pretty sure the answer
is "no" but I could have overlooked something....)
Question 2: If not does this seem generally useful? Like something worth
incorporating into sympy?
Question 3a: If it DOES seem like something worth incorporating: how can I
help? (I'm somewhat new to programming and don't have any formal
training...should I do a pull request and just do what I think is right?)
Question 3b: If it DOES NOT seem like something worth incorporating: I
implemented this functionality by creating my own Symbol class and Monkey
patching the Expr class (copied the code below). Am I missing something
important here? I don't think this will break sympy... Is there a better
way to approach this? I'm a beginner so any feedback would be appreciated!
CODE:
class dSymbol(Symbol):
"""
Add a default value to Symbols
"""
def __new__(cls, name, value, **assumptions):
return super().__new__(cls, name, **assumptions)
def __init__(self, name, value, **assumptions):
self.default = value
"""
Monkey patch the Expr class (inherited by Mul, Add,... all the function
classes in sympy i'm interested in using)
Add methods:
symvalue
value
"""
def symvalue(self):
"""
returns the expression evaluated at the default value for each dSymbol
the result is still symbolic in that pi will be displayed as 'pi' not
'3.14159'...
"""
s = self.atoms(dSymbol)
for i in s:
self = self.subs(i, i.default)
return self
def value(self, *args, **kwargs):
"""
returns the expression evaluated at the default value for each dSymbol
the result will be numeric (assuming it doesn't contain any 'Symbol')
"""
return self.symvalue().evalf(*args, **kwargs)
Expr.symvalue = symvalue
Expr.value = value
--
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 http://groups.google.com/group/sympy.
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/b12b7e28-aff2-4168-a3a2-c9f5ee67e1ad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.