Hey Matthieu,
The problem is you are returning the *original* *modified* object you
passed in, not a new object like you seem to be expecting. Just make a copy
of l before making any changes to it. Since the function is straight
forward, I'd recommend just making it iterative using a while loop.
Best,
Travis
On Saturday, June 29, 2013 2:39:02 PM UTC+2, Matthieu Deneufchâtel wrote:
>
> I might have misunderstood your suggestion, but I don't think that it is
> the solution (I tried it and I get the same problem).
> My problem is that when I try the same expansion several times in a row,
> the list is not cleared after each call and thus the coefficients are false:
> sage: M=FreeMonoid(2,'x,y')
> sage: to_pbw(pbw_element(M.1))
> {word: y: 1}
> sage: to_pbw(pbw_element(M.1))
> {word: y: 2}
> sage: to_pbw(pbw_element(M.1))
> {word: y: 3}
> Thanks anyway!
>
>
> Le samedi 29 juin 2013 12:42:31 UTC+2, Mike Hansen a écrit :
>>
>> I'm guessing this is just due to you using dict() in the function
>> definition. See http://effbot.org/zone/default-values.htm . If you
>> replace it with None do something like
>>
>> if l is None:
>> l = {}
>>
>> at the beginning of the function, then things should be okay.
>>
>> --Mike
>> --Mike
>>
>>
>> On Sat, Jun 29, 2013 at 11:29 AM, Matthieu Deneufchâtel
>> <[email protected]> wrote:
>> > In a recursive function, I use a dictionary (which contains the
>> elements of
>> > the expansion of the argument on another basis). Since each call to the
>> > function needs the dictionary, I don't want the user to be forced to
>> put the
>> > dictionary as an argument (i.e. I want him to call to_pbw(elem) instead
>> of
>> > to_pbw(elem,{})). But I get a little confused between local and global
>> > variables with the code below since the dictionary is not emptied when
>> > needed (my function as the expected behavior only when I call
>> > to_pbw(elem,{})).
>> >
>> > Could anyone help me (provided my question is not unclear)?
>> > Matthieu
>> >
>> > def to_pbw(f,l=dict()):
>> > from sage.combinat.words.finite_word import to_monoid_element
>> > if f!= 0 * f.parent().one():
>> > support = [to_word(i[1]) for i in list(f)]
>> > min=support[0]
>> > if len(support) > 1:
>> > for word in support[1:len(support)-1]:
>> > if min.lex_less(word):
>> > min = word
>> > coeff = list(f)[support.index(min)][0]
>> > if min not in l.keys():
>> > l[min]=coeff
>> > else:
>> > l[min] += coeff
>> > remainder = f - coeff * pbw_element(to_monoid_element(min))
>> > return to_pbw(remainder,l)
>> > return l
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups
>> > "sage-devel" 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/sage-devel.
>> > For more options, visit https://groups.google.com/groups/opt_out.
>> >
>> >
>>
>
--
You received this message because you are subscribed to the Google Groups
"sage-devel" 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/sage-devel.
For more options, visit https://groups.google.com/groups/opt_out.