-
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
Add truncate methods to fmpz_mod_poly
#180
Conversation
fmpz_mod_poly
src/flint/types/fmpz_mod_poly.pyx
Outdated
def mul_high(self, other, slong start): | ||
r""" | ||
Returns from coefficients from ``start`` onwards of the multiplication of ``self`` with ``other`` | ||
|
||
# TODO!!! | ||
# | ||
# Is this expected behaviour? Seems weird. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The doc says:
Computes the product of poly1 and poly2 and writes the coefficients from start onwards into the high coefficients of res, the remaining coefficients being arbitrary.
I think that the implementation just decided that it was faster to compute the full product rather than selectively compute the highest coefficients.
I guess we accept and document that or otherwise zero out the lower coefficients:
In [23]: f.mul_high(g, 5)
Out[23]: 13*x^8 + 37*x^7 + 17*x^6 + 138*x^5 + 101*x^4 + 45*x^3 + 19*x^2 + 7*x + 2
In [24]: f.mul_high(g, 5).right_shift(5).left_shift(5)
Out[24]: 13*x^8 + 37*x^7 + 17*x^6 + 138*x^5
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah I think my feeling is if we have to implement f.mul_high(g, 5).right_shift(5).left_shift(5)
on our side, we should just not have the method? Maybe the output is good for C flint, but for us the output seems not very interesting.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it is not worth having. I can imagine wanting the product of the leading terms or the leading coefficients but I'm not sure I when I would particularly want what this function does. It does not look like fmpz_mod_poly_mulhigh
is actually used anywhere in Flint itself.
It can also be implemented in a fairly straight-forward way by calling right_shift
before multiplying.
As far as I can tell all positive characteristic poly types have a mulmod function but in all cases it does literally just compute the multiplication followed by divrem apart from a minor optimisation to not bother with divrem if the degrees are small enough. In python-flint's case that would save a memory allocation over I expect that it is a little faster for very small polynomials by saving the allocation. Probably in the context of python-flint though any saving here really comes from reducing the general Python overhead though: In [5]: ctx = fmpz_mod_poly_ctx(127)
In [6]: f = ctx([1, 2])
In [7]: g = ctx([3, 4])
In [8]: h = ctx([1, 2, 3])
In [9]: %timeit (f*g)%h
3.11 µs ± 154 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [10]: %timeit f.mulmod(g, h)
1.23 µs ± 3.54 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) A more important question is how useful these functions actually are. There are many functions in flint that make a lot of sense for Flint's internal use but are perhaps not so useful to a user of python-flint especially if it they are trivially implemented using the features already provided. For each of these functions we should consider what it means for all other types like if we have mulmod for fmpz_mod_poly are we going to have it for fmpq_poly as well? Ideally we add methods to each type in such a way that there is a consistent set of methods across the different types. Currently |
So my plan was to "finish" methods for this type and then use this as a start for I'm happy to * not* include some of these methods, but you mentioned about the truncation ones in another PR so I threw them all in |
That seems reasonable. For each operation now we need to consider if it makes sense for the other types though. Also we are probably already beginning to see diminishing returns from adding further methods to The |
If you're happy with the methods available for fmpz_mod_poly as presented here in this PR my next contribution will be |
This looks good. Thanks |
Excellent! |
This PR adds:
However, the output from
mul_high()
doesnt match my expectation... I'm not sure it's worth including, unless I'm being stupid.The truncate pow is definitely nice, the mul mod seems to not offer much speed up.