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
calling invert on a symbol returns 0 #24394
Comments
It looks like this tries to compute the inverse of diff --git a/sympy/polys/polytools.py b/sympy/polys/polytools.py
index be1b71b..95ad56b 100644
--- a/sympy/polys/polytools.py
+++ b/sympy/polys/polytools.py
@@ -40,6 +40,7 @@
PolificationFailed,
ComputationFailed,
GeneratorsError,
+ NotInvertible,
)
from sympy.polys.polyutils import (
basic_from_dict,
@@ -2590,6 +2591,9 @@ def invert(f, g, auto=True):
"""
dom, per, F, G = f._unify(g)
+ if G.degree() < 1:
+ raise NotInvertible('constant modulus in polynomial ring')
+
if auto and dom.is_Ring:
F, G = F.to_field(), G.to_field()
|
Maybe it's deeper than that and the problem is with
The docstring says: sympy/sympy/polys/euclidtools.py Lines 50 to 54 in 4801871
Here we have h=1 which is the correct gcd of f and g. However we have s=0 and so it is not the case that |
I don't think that |
I suppose if |
Possibly it's better for
Probably it should be clearer in the docs that this is the expectation and perhaps |
I wonder if the OP was actually intending to find a symbolic expression for the inverse of x mod p=7.
You can also do >>> pow(x, -1, 7)
Mod(1/x, 7)
>>> pow(x, -1, 7).subs(x, 2)
1/2
>>> mod_inverse(2, 7)
4 |
The result for |
Why is that? Is there a special definition used for rational numbers in Mod? Nothing is documented in the Mod docstring. I don't see the benefit of not treating inversions in Mod as modular inverses. And even if this is the case, then returning this from |
plot(Mod(x, 7)) This is a symbolic version of what In [133]: 1/2 % 7
Out[133]: 0.5 If the argument happens to be given as
Yes, that is wrong. Mod should correctly correspond to In [134]: print(x % 1)
Mod(x, 1) |
I see. So there are three notions of mod: polynomial modulo a polynomial, integer modulo a prime, and real modulo a real. Mod currently does the third of these, and the polys support the first one, but we can only do the second indirectly through Mod when the definitions align, or with some functions like mod_inverse. (there's actually even more notions than that, but those are the relevant ones here) Really, we need a much better way to represent elements of a cyclic finite field (or ring). There's GF in the polys, but it's hidden and clunky to deal with, especially if you do want to deal with symbolic elements. This is something that comes up a lot in many places like StackOverflow questions. Note that an element of GF(p)[x] is not the same as a symbolic element x of GF(p). For instance, This is all mostly off topic for this issue, though. If this existed I would have suggested mentioning it in the error message for invert(0), but since it doesn't, I think we can at best just mention |
It would also be good to have a document in the docs clarifying the difference between polynomial algorithms and integer algorithms. There's a lot of functions that one might expect to operate on
There's also further subtleties depending on whether the base ring of the polynomial operation is A document explaining the mathematical distinctions here with a table of the different functions for each would be quite useful. |
Here is an example :
The text was updated successfully, but these errors were encountered: