-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fmpz_mpoly.gcd does not include content of polynomials #249
Comments
The mpoly types have not been included in a full release yet (I guess you are using a prerelease?) so it should still be possible to change this without breaking compatibility. Currently this just calls the FLINT function In [6]: [n] = ctx.gens()
In [7]: (4*(n**2-1)).gcd(2*(n-1))
Out[7]: n - 1 It would be worth reviewing all poly/mpoly types to see if this is consistent. |
Related #189 |
I'm indeed using a pre-release. Specifically version It looks like the decision to return only the monic GCD was made in FLINT. To me the decision to exclude the content from the GCD is counter-intuitive, but if you wish to maintain compatibility with FLINT, wrapping the |
It seems to be consistent with In [2]: x = flint.fmpq_poly([0, 1])
In [3]: (4*(x**2-1)).gcd(2*(x-1))
Out[3]: x + (-1)
In [4]: x = flint.nmod_poly([0, 1], 11)
In [5]: (4*(x**2-1)).gcd(2*(x-1))
Out[5]: x + 10
In [27]: ctx = flint.fmpz_mod_mpoly_ctx.get('n', 11)
In [28]: [n] = ctx.gens()
In [29]: (-4*(n**2-1)).gcd(-2*(n-1))
Out[29]: n + 10 I guess it is natural for GCD of a polynomial over a field to be monic. |
It would be reasonable top add a In [33]: (4*n**3 + 2*n**2).term_content()
Out[33]: n^2 |
Actually term content is the GCD of the terms: In [42]: ctx = flint.fmpz_mpoly_ctx.get('n')
In [43]: [n] = ctx.gens()
In [44]: (4*n**3 + 2*n**2).term_content()
Out[44]: 2*n^2 Just when the ground domain is a field the gcd of the terms is defined as always monic. |
In general it would be good to have Returning a monic GCD seems to be consistent with SymPy's analogous types over field domains: In [7]: R = QQ[x]
In [8]: xR = R(x)
In [9]: R.gcd(4*(xR**2-1), 2*(xR-1))
Out[9]: x - 1
In [10]: R = ZZ[x]
In [11]: xR = R(x)
In [12]: R.gcd(4*(xR**2-1), 2*(xR-1))
Out[12]: 2*x - 2
In [13]: Poly(4*(x**2-1), x, domain=QQ).gcd(Poly(2*(x-1), x, domain=QQ))
Out[13]: Poly(x - 1, x, domain='QQ')
In [14]: Poly(4*(x**2-1), x, domain=ZZ).gcd(Poly(2*(x-1), x, domain=ZZ))
Out[14]: Poly(2*x - 2, x, domain='ZZ')
In [25]: R = QQ.frac_field(y)[x]
In [26]: R
Out[26]: QQ(y)[x]
In [27]: R.gcd((y**2*xR**2-1), (y*xR-1))
Out[27]: x - 1/y |
I'm not sure that content is what you want here. What is removed from the GCD is the leading coefficient. What you want I think is some GCD of the leading coefficients: In [8]: p1 = 2*(n**2 - 1)
In [9]: p2 = 2*(n - 1)
In [10]: p1.gcd(p2)
Out[10]: n - 1
In [11]: p1.leading_coefficient()
Out[11]: 2
In [12]: p2.leading_coefficient()
Out[12]: 2
In [13]: c1 = p2.leading_coefficient()
In [14]: c2 = p2.leading_coefficient()
In [15]: c1.numer().gcd(c2.numer())
Out[15]: 2 |
Seems like you are correct. This example displays this behavior even better:
Still, one can run
And in this case we cannot simply use Either way - the solution using the |
You can still implement it as you suggested before. This one is provided in FLINT though as In [1]: QQ.gcd(QQ(2,3), QQ(4,9))
Out[1]: 2/9 The doc for I'm not sure though if it would be better to change the definition of In [7]: from flint.types._gr import gr_fmpq_ctx, gr_gr_poly_ctx
In [8]: ctx = gr_gr_poly_ctx.new(gr_fmpq_ctx)
In [9]: x = ctx.gen()
In [10]: (2*x).gcd(4*x)
Out[10]: x |
I've added |
When using
fmpz_mpoly
, the gcd contains the content of the polynomials:However, when using
fmpq_mpoly
, gcd does not contain the content of the polynomials:Digging further, I've noticed that
fmpq
does not have a gcd method, which could explain this root cause.The GCD of two rationals
a/b
,c/d
is simply extended asgcd(a*d, b*c) / (b*d)
.I'm not sure if this falls under the category of a bugfix or a feature request, but is it possible to achieve this behavior?
The text was updated successfully, but these errors were encountered: