Hi,

On 13 October 2011 11:15, smichr <smi...@gmail.com> wrote:

> Does anyone have a suggestion of how to best retrieve the Integer gcd
> and factors from an Add instead of getting the Rational?
>
>    >>> terms_gcd(3*x+9*x**2/2) # I want 3*x*(3*x/2 + 1)
>    3*x*(3*x + 2)/2
>    >>> primitive(3*(3*x/2 + 1))
>    (3/2, 3*x + 2)
>    >>> gcd(3*x,9*x**2/2)
>    x
>

The tool you should use is primitive() (or content(), but this is purely
polynomial manipulation function). You have to change definition of rational
GCD:

In [1]: def qgcd(self, a, b):
   ...:     try:
   ...:         ring = self.get_ring()
   ...:     except DomainError:
   ...:         return self.one
   ...:     else:
   ...:         g = ring.gcd(self.numer(a), self.numer(b))
   ...:         return self.convert(g, ring)
   ...:

In [2]: import types

In [3]: QQ.gcd = types.MethodType(qgcd, QQ, type(QQ))

In [4]: primitive(3*x/2 + 6)
Out[4]:
⎛   x    ⎞
⎜3, ─ + 2⎟
⎝   2    ⎠

For the case of QQ, qgcd() could be simplified, but I made this function
generic, because I could be implemented in Field as an alternate GCD
algorithm. I was thinking about changing GCD algorithm easier, e.g.
primitive(3*x/2 + 6, QQ__gcd=qgcd) or even primitive(3*x/2 + 6,
QQ__gcd="integer").


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

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

Reply via email to