I would just create a subclass of Function called gcd, and implement
doit() on it, which calls the normal gcd. Then replace gcd with this
one in the locals dictionary. The advantage of doing it this way over
just using Function('gcd') is that you can then call .doit() on it to
evaluate it. This is the standard way that delayed evaluation classes
in SymPy work, like Sum or Integral. You can also set nargs to 2 to
require it to take two arguments (unless you want the multiargument
gcd).

Aaron Meurer

On Tue, Mar 26, 2013 at 2:00 PM, Duane Nykamp <[email protected]> wrote:
> I'd like to delay evaluation of functions like gcd so that I can have users
> enter an expression like gcd(a,b), where a and b will be replaced by
> numbers.  If I just enter gcd(a,b), it finds the gcd of the polynomials a
> and b, which is one.  Based on earlier feedback I receive here and by trial
> and error, I came up with this method to delay evaluation, and I'm just
> wondering if this is a reasonable way to do it.  I can't say I really
> understand the difference between subs and replace.  I just messed around
> until I found a method that worked.
>
> For example, if a and b are eventually going to be 3 and 9, the following
> seems to work correctly, giving 3.  I'm using parse_expr, as the expressions
> are entered in a web page.
>
> In [170]: parse_expr('gcd(a,b)',local_dict={'gcd':
> Function('gcd')}).subs([(Symbol('a'),3),(Symbol('b'),9)]).replace(Function('gcd'),gcd)
> Out[170]: 3
>
> Is this a reasonable approach?  Or is there a better method?
>
> Thanks,
> Duane
>
> --
> 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?hl=en-US.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
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?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to