Hi,

On 26 March 2013 21:00, 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?
>

You can use this:

In [4]: class gcd(Function):
   ...:     @classmethod
   ...:     def eval(cls, x, y):
   ...:         if x.is_Number and y.is_Number:
   ...:             return x.gcd(y)
   ...:         else:
   ...:             return None
   ...:

In [5]:

In [5]: var('a,b')
Out[5]: (a, b)

In [6]: gcd(a, b)
Out[6]: gcd(a, b)

In [7]: _.subs(a, 12)
Out[7]: gcd(12, b)

In [8]: _.subs(b, 6)
Out[8]: 6

> 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.
>
>

Mateusz

-- 
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