This patch doesn't treat dummies well, but I guess that can be fixed
later:

In [6]: a_d = Symbol('a',dummy=True)
In [7]: a = Symbol('alpha',dummy=True)
In [8]: f = 2*a*a_d*x
In [9]: f
Out[9]: 2*x*a*alpha
In [10]: f(a=2)
Out[10]: 2*x*a*alpha
In [11]: f(a_d=2)
Out[11]: 2*x*a*alpha
In [12]: f(alpha=2)
Out[12]: 2*x*a*alpha
In [13]: a = Symbol('alpha')
In [14]: f = 2*a*a_d*x
In [15]: f(alpha=2)
Out[15]: 4*x*a
In [16]: f(a=2)
Out[16]: 2*alpha*x*a


On Nov 19, 10:42 pm, Lance Larsen <[EMAIL PROTECTED]> wrote:
> For what it is worth, I fixed the errors in the last patch submitted.
> Looks like a previous patch has already been applied in main
> repository though. I used a different method to export this patch. I
> used the command 'hg qdiff' to get the diff info and stole the patch
> header from my previously submitted patch. Don't know if this causes
> problems. 'hg email' was complaining about my parent repository setup
> and I didn't have time to debug this so I tried a workaround.
>
> -Lance
>
> # HG changeset patch
> # User "Lance Larsen <[EMAIL PROTECTED]>"
> # Date 1226946818 25200
> # Node ID 5699c540c62b31ddf3c96e07fae6fe64cf10eb1e
> # Parent  d1019a5c66d12d524fe64359c1bd8054cab0cfa9
> Substitution syntax extension - implements f.subs(x=1,y=2) and
> shorthand syntax f(x=1,y=2).
>
> diff -r dab6435e04fd sympy/core/basic.py
> --- a/sympy/core/basic.py       Thu Nov 06 23:12:01 2008 +0100
> +++ b/sympy/core/basic.py       Wed Nov 19 14:33:25 2008 -0700
> @@ -899,26 +899,43 @@
>          """
>          return self
>
> -    def subs(self, *args):
> +    def subs(self, *args, **kwargs):
>          """
>          Substitutes an expression.
>
>          Calls either _subs_old_new, _subs_dict or _subs_list
> depending
> -        if you give it two arguments (old, new), a dictionary or a
> list.
> +        if you give it two arguments (old, new), a dictionary, a list
> or
> +        pass in named parameters. When named parameters are passed
> in,
> +        these are converted to a dictionary and _subs_dict is called.
>
>          Examples:
>
>          >>> from sympy import *
>          >>> x,y = symbols('xy')
> -        >>> (1+x*y).subs(x, pi)
> -        1 + pi*y
> +        >>> (1+x*y).subs(x=pi, y=2)
> +        1 + 2*pi
>          >>> (1+x*y).subs({x:pi, y:2})
>          1 + 2*pi
>          >>> (1+x*y).subs([(x,pi), (y,2)])
>          1 + 2*pi
> +        >>> (1+x*y).subs(x, pi)
> +        1 + pi*y
> +
> +        Subs can be called implicitly as well as shown in the
> following
> +        examples:
> +
> +        >>> f = 1+x*y
> +        >>> f(x=pi, y=2)
> +        1 + 2*pi
> +        >>> f({x:pi, y:2})
> +        1 + 2*pi
> +        >>> f([(x,pi), (y,2)])
> +        1 + 2*pi
> +        >>> f(x, pi)
> +        1 + pi*y
>
>          """
> -        if len(args) == 1:
> +        if len(args) == 1 and len(kwargs) == 0:
>              sequence = args[0]
>              if isinstance(sequence, dict):
>                  return self._subs_dict(sequence)
> @@ -926,9 +943,11 @@
>                  return self._subs_list(sequence)
>              else:
>                  raise TypeError("Not an iterable container")
> -        elif len(args) == 2:
> +        elif len(args) == 2 and len(kwargs) == 0:
>              old, new = args
>              return self._subs_old_new(old, new)
> +        elif len(args) == 0 and len(kwargs) > 0:
> +            return self._subs_dict(kwargs)
>          else:

Why do we use Exception instead of ValueError or TypeError?

>              raise Exception("subs accept either 1 or 2 arguments")
>
> @@ -1935,9 +1954,25 @@
>          from sympy.integrals import integrate
>          return integrate(self, *args, **kwargs)
>
> -    #XXX fix the removeme
> -    def __call__(self, *args, **removeme):
> -        return Function(self[0])(*args)
> +    def __call__(self, *args, **kwargs):
> +        """Alias for calling the subs function. Substitution using
> two parameters
> +        (i.e. f(x,3)) and list of pairs (f([(x,3),(y,2)])) is not
> supported
> +        because the notation may be confusing to sympy users.
> +
> +        Examples:
> +
> +        >>> from sympy import *
> +        >>> x,y = symbols('xy')
> +        >>> f = 1+x*y
> +        >>> f(x=pi,y=2)
> +        1 + 2*pi
> +        >>> f({x:pi, y:2})
> +        1 + 2*pi
> +        """
> +        if len(args) > 1 or (len(args) == 1 and isinstance(args[0],
> (list, tuple))):
> +            sequence = args[0]
> +            raise Exception("Unsupported syntax. Pass a dictionary or
> name=value pairs.")
> +        return self.subs(*args, **kwargs)
>
>      def __float__(self):
>          result = self.evalf()
> diff -r dab6435e04fd sympy/core/tests/test_subs.py
> --- a/sympy/core/tests/test_subs.py     Thu Nov 06 23:12:01 2008 +0100
> +++ b/sympy/core/tests/test_subs.py     Wed Nov 19 14:33:25 2008 -0700
> @@ -169,3 +169,14 @@
>      assert (f(x,y)).subs(f,sin) == f(x,y)
>      assert (sin(x)+atan2(x,y)).subs([[atan2,f],[sin,g]]) == f(x,y) + g
> (x)
>      assert (g(f(x+y, x))).subs([[f, l], [g, exp]]) == exp(x + sin(x +
> y))
> +
> +def test_subs_call_syntax():
> +    a, b, x, y, p, q = map(Symbol, "abxypq")
> +    f = 2*a*x+3*b*y
> +    assert f.subs(x=1, y=2) == 2*a + 6*b
> +
> +def test_implicit_subs_call_syntax():
> +    a, b, x, y, p, q = map(Symbol, 'abxypq')
> +    f = 2*a*x + 3*b*y
> +    assert f(x=1, y=2) == 2*a + 6*b
> +    assert f({a*x:p,b*y:q}) == 2*p + 3*q
--~--~---------~--~----~------------~-------~--~----~
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