On 30 Apr., 18:07, Ronan Lamy <[email protected]> wrote:
> Le samedi 30 avril 2011 à 08:59 -0700, Tom Bachmann a écrit :
>
> > This is roughly what I had in mind:
> > [Everything is fake here, to only thing that is supposed to be
> > illustrated is how assumptions get cleared up when their objects
> > vanish.]
>
> > import weakref
>
> > class Assumption(object):
> > pass
>
> > class AssumePositive(Assumption):
>
> > def __init__(self, x):
> > self.var = weakref.ref(x, self.cb)
>
> > def cb(self, var):
> > if self.callback:
> > self.callback(self)
>
> > def __str__(self):
> > return str(self.var) + ' > 0'
>
> > class Symbol(object):
> > def __init__(self, name):
> > self.name = name
>
> > def __str__(self):
> > return str(self.name)
>
> > assumptions = {}
>
> > def remove_assumption(wr):
> > print 'removing assumption', assumptions[wr]
> > del assumptions[wr]
>
> > N = 0
> > def assume(a):
> > global N
> > print 'adding assumption', a, 'as', N
> > assumptions[a] = N
> > a.callback = remove_assumption
> > N += 1
>
> > def test2():
> > x = Symbol('y')
> > assume(AssumePositive(x))
> > print assumptions
> > print 'leaving test2'
>
> > def test():
> > x = Symbol('x')
> > assume(AssumePositive(x))
> > print assumptions
> > print 'entering test2'
> > test2()
> > print assumptions
> > print 'leaving test'
>
> > test()
>
> I don't see how this is in any way better than storing the assumptions
> on the object. For instance, assume(AssumePositive(x + y)) won't work
> properly. And it's not clear how assumptions can be retrieved.
I was just making a suggestion on how to implement certain
functionality with the assumptions implemented, not arguing why.
[Actually I don't see why one would want to separate out assumptions
in the first place, but I suppose there are good reasons]. As for
things like AssumePositive(x+y) you are right, but this is just
because my example implementation is simplistic. One way around this
would be to hijack all free symbols inside an assumption into week
references, something like
class Assumption(object)
def __init__(expr):
# This object means that EXPR is true.
sym = expr.free_symbols
for x in s:
expr = expr.subs(x, weakref.ref(x))
As for retrieving assumptions, I don't know, before posting my
previous mail I just had a quick look at assumptions/assume.py and it
seemed to me that assumptions are simply stored as a set. I don't know
how the assumptions system works so I cannot say anything more.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy?hl=en.