On Thursday 20 November 2008 02:21:06 Ondrej Certik wrote:
> On Wed, Nov 19, 2008 at 4:14 PM, Riccardo Gori <[EMAIL PROTECTED]> wrote:
> > On Tuesday 18 November 2008 04:01:11 nnms wrote:
> >> > Yes, and because it's the variable IMHO is the one the user should
> >> > use. Also in the existing subs routine it works in this way, so it
> >> > should work also with that new syntax. i.e: it would be confusing if
> >> > this works
> >> >
> >> > In [4]: f.subs({x:3,t:9})
> >> > Out[4]: 27
> >> >
> >> > and this not:
> >> >
> >> > In [5]: f(x=3,t=9)
> >> > Out[5]: 3⋅θ
> >> >
> >> > And again if this works:
> >> >
> >> > In [6]: f(x=3,theta=9)
> >> > Out[6]: 27
> >> >
> >> > and this not:
> >> >
> >> > In [7]: f.subs({x:3,theta:9})
> >>
> >> OK. I see better what you are saying. The fact that f({x:3, t:2})
> >> behaves differently from f(x=3,t=2) is a little awkward. Note that
> >> there is the same problem with f.subs({x:3, t:2}) and f.subs({'x':
> >> 3,'t':2}). These two do not behave the same. Unfortunately, I can't
> >> think of a fix for this particular issue. The syntax f(x=2,t=3) always
> >> passes the keys as strings. I'm not sure how you would get the
> >> original object t. Same issue with f.subs({'x':3,'t':2}). I can't
> >> think of a way for sympy to retrieve what t is in the original
> >> function. However, I do think the issue is more one of a person
> >> understanding python rather than a problem with the syntax.
> >>
> >> -Lance
> >
> > Hi Lance,
> > here is a proof of concept using eval. I just looked at var function code
> > to see how it obtain the global dict and used the same "hack", but it
> > seems to work:
> >
> >    def __call__(self, *args, **kwargs):
> >        import inspect
> >        frame = inspect.currentframe().f_back.f_globals
> >        nkwargs = {}
> >        try:
> >            for k, v in kwargs.iteritems():
> >                nkwargs[eval(k, frame)] = v
> >        finally:
> >            del frame
> >        return self._subs_dict(nkwargs)
>
> Wow, nice trick.
>
> But I am worried this is too hacky --- I don't like playing with
> frames, maybe with the exception of var().
>
> I think we should rather use just python dictionaries and that's it.
>
> Ondrej

Sorry Ondrej, the problem is the hack or that you're fine with this behaviour?

In [1]: a = Symbol("alpha")

In [2]: f = cos(a)      #simple function

In [3]: f(a=pi)         #Nothing happens using a
Out[3]: cos(α)

In [3]: f([[a,pi]])     #calling with lists it works with a...
Out[3]: -1

In [4]: f([[alpha,pi]])         #and fail with alpha
---------------------------------------------------------------------------
NameError

In [4]: f(alpha=pi)             #It work with alpha
Out[4]: -1

In [5]: f.subs(a,pi)            #It works  with a
Out[5]: -1

In [6]: f.subs(alpha,pi)        #another obvious error
---------------------------------------------------------------------------
NameError

Cheers,
Riccardo

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy-patches" group.
To post to this group, send email to sympy-patches@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sympy-patches?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to