malachite-base 0.13.0

A collection of utilities, including new arithmetic traits and iterators that generate all values of a type.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// Copyright © 2026 Mikhail Hogrefe
//
// This file is part of Malachite.
//
// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.

use crate::num::arithmetic::mod_mul::{
    mod_mul_precompute_shoup, mod_mul_shoup, mod_mul_shoup_lazy,
};
use crate::num::arithmetic::traits::{ModIsReduced, ModPowerOf2IsReduced};
use crate::num::basic::integers::PrimitiveInt;
use crate::num::basic::unsigneds::PrimitiveUnsigned;
use crate::num::conversion::traits::ExactFrom;
use crate::polynomial::{ModEvaluate, ModEvaluateGeometric, ModEvaluateMany, ModPowerOf2Evaluate};
use crate::unsigned_polynomial::UnsignedPolynomial;
use crate::unsigned_polynomial::arithmetic::mod_mul::ModData;
use crate::unsigned_polynomial::arithmetic::mod_mul_middle::mod_mul_middle_karatsuba;
use crate::unsigned_polynomial::arithmetic::mod_mul_truncated::mod_mul_truncated_to_out;
use alloc::vec;
use alloc::vec::Vec;
use core::cmp::max;

// Evaluates a polynomial at x modulo 2^pow, after checking that pow fits in `T` and that the
// coefficients and x are reduced.
fn mod_power_of_2_evaluate<T: PrimitiveUnsigned>(p: &UnsignedPolynomial<T>, x: T, pow: u64) -> T {
    assert!(pow <= T::WIDTH);
    assert!(
        p.mod_power_of_2_is_reduced(pow),
        "self must be reduced mod 2^pow, but {p} has a coefficient >= 2^{pow}"
    );
    assert!(
        x.significant_bits() <= pow,
        "x must be reduced mod 2^pow, but {x} >= 2^{pow}"
    );
    let mut value = T::ZERO;
    for &c in p.coefficients.iter().rev() {
        value = value.wrapping_mul(x).wrapping_add(c);
    }
    value.mod_power_of_2(pow)
}

impl<T: PrimitiveUnsigned> ModPowerOf2Evaluate<T> for &UnsignedPolynomial<T> {
    type Output = T;

    /// Evaluates an [`UnsignedPolynomial`] at a value of its coefficient type, modulo $2^k$, taking
    /// the polynomial by reference. The coefficients and the value must already be reduced modulo
    /// $2^k$, and $k$ may be at most the width of the type.
    ///
    /// $$
    /// f(p, x, k) = \sum_{i=0}^{n-1} c_i x^i \bmod 2^k,
    /// $$
    ///
    /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length. The zero polynomial
    /// evaluates to 0 everywhere.
    ///
    /// Reducing modulo $2^k$ commutes with wrapping arithmetic, which works modulo $2^w$ for the
    /// type's width $w \geq k$, so Horner's rule is carried out with wrapping multiplications and
    /// additions and the value is reduced once, at the end.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
    ///
    /// # Panics
    /// Panics if `pow` is greater than `T::WIDTH`, or if any coefficient of `self` or `x` is
    /// greater than or equal to $2^k$.
    ///
    /// # Examples
    /// ```
    /// use core::str::FromStr;
    /// use malachite_base::polynomial::ModPowerOf2Evaluate;
    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
    ///
    /// let p = UnsignedPolynomial::<u8>::from_str("5*x^2+3*x+7").unwrap();
    /// // 5 * 36 + 3 * 6 + 7 = 205, which is 13 mod 16.
    /// assert_eq!((&p).mod_power_of_2_evaluate(6, 4), 13);
    /// assert_eq!((&p).mod_power_of_2_evaluate(0, 4), 7);
    /// // All 8 bits of a u8: 205 itself.
    /// assert_eq!((&p).mod_power_of_2_evaluate(6, 8), 205);
    /// ```
    ///
    /// This is equivalent to `nmod_poly_evaluate_nmod` from `nmod_poly/evaluate_nmod.c`, FLINT
    /// 3.6.0, with the modulus $2^k$, except that the value must be reduced.
    #[inline]
    fn mod_power_of_2_evaluate(self, x: T, pow: u64) -> T {
        mod_power_of_2_evaluate(self, x, pow)
    }
}

impl<T: PrimitiveUnsigned> ModPowerOf2Evaluate<T> for UnsignedPolynomial<T> {
    type Output = T;

    /// Evaluates an [`UnsignedPolynomial`] at a value of its coefficient type, modulo $2^k$, taking
    /// the polynomial by value. The coefficients and the value must already be reduced modulo
    /// $2^k$, and $k$ may be at most the width of the type.
    ///
    /// $$
    /// f(p, x, k) = \sum_{i=0}^{n-1} c_i x^i \bmod 2^k,
    /// $$
    ///
    /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length. The zero polynomial
    /// evaluates to 0 everywhere.
    ///
    /// Reducing modulo $2^k$ commutes with wrapping arithmetic, which works modulo $2^w$ for the
    /// type's width $w \geq k$, so Horner's rule is carried out with wrapping multiplications and
    /// additions and the value is reduced once, at the end.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
    ///
    /// # Panics
    /// Panics if `pow` is greater than `T::WIDTH`, or if any coefficient of `self` or `x` is
    /// greater than or equal to $2^k$.
    ///
    /// # Examples
    /// ```
    /// use core::str::FromStr;
    /// use malachite_base::polynomial::ModPowerOf2Evaluate;
    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
    ///
    /// let p = UnsignedPolynomial::<u8>::from_str("5*x^2+3*x+7").unwrap();
    /// // 5 * 36 + 3 * 6 + 7 = 205, which is 13 mod 16.
    /// assert_eq!(p.clone().mod_power_of_2_evaluate(6, 4), 13);
    /// assert_eq!(p.clone().mod_power_of_2_evaluate(0, 4), 7);
    /// // All 8 bits of a u8: 205 itself.
    /// assert_eq!(p.mod_power_of_2_evaluate(6, 8), 205);
    /// ```
    ///
    /// This is equivalent to `nmod_poly_evaluate_nmod` from `nmod_poly/evaluate_nmod.c`, FLINT
    /// 3.6.0, with the modulus $2^k$, except that the value must be reduced.
    #[inline]
    fn mod_power_of_2_evaluate(self, x: T, pow: u64) -> T {
        mod_power_of_2_evaluate(&self, x, pow)
    }
}

// The shortest polynomial evaluated with Shoup's method, when `T` is wider than 32 bits: Shoup's
// precomputation is a two-by-one division, which is only paid back from this length on. For
// narrower types every polynomial of length 2 or more uses it. Tuned on Apple M-series for `u64`
// and `u128`; FLINT's `FLINT_MULMOD_SHOUP_THRESHOLD` is 10.
const MOD_EVALUATE_SHOUP_THRESHOLD: usize = 3;

// Evaluates a polynomial at `x` modulo `m` with Horner's rule, reducing after every step.
// `coefficients` must be nonempty.
//
// This is equivalent to `_nmod_poly_evaluate_nmod_horner` from `nmod_poly/evaluate_nmod.c`, FLINT
// 3.6.0.
crate_test_fn! {mod_evaluate_horner<T: PrimitiveUnsigned>(coefficients: &[T], x: T, m: T) -> T {
    let data = T::precompute_mod_mul_data(&m);
    let (&last, rest) = coefficients.split_last().unwrap();
    let mut value = last;
    for &c in rest.iter().rev() {
        value.mod_mul_precomputed_assign(x, m, &data);
        value.mod_add_assign(c, m);
    }
    value
}}

// Evaluates a polynomial at `x` modulo `m` with Horner's rule, multiplying by `x` with Shoup's
// method. `coefficients` must be nonempty, `x_precomp` must be `mod_mul_precompute_shoup(x, m)`,
// and the top bit of `m` must be clear.
//
// This is equivalent to `_nmod_poly_evaluate_nmod_precomp` from `nmod_poly/evaluate_nmod.c`, FLINT
// 3.6.0.
crate_test_fn! {mod_evaluate_shoup<T: PrimitiveUnsigned>(
    coefficients: &[T],
    x: T,
    x_precomp: T,
    m: T,
) -> T {
    let (&last, rest) = coefficients.split_last().unwrap();
    let mut value = last;
    for &c in rest.iter().rev() {
        value = mod_mul_shoup(x, value, x_precomp, m);
        value.mod_add_assign(c, m);
    }
    value
}}

// Evaluates a polynomial at `x` modulo `m` like `mod_evaluate_shoup`, but reduces only partially:
// the result is congruent to the polynomial's value and less than $3m - 1$. `coefficients` must be
// nonempty, `x_precomp` must be `mod_mul_precompute_shoup(x, m)`, and `m` must be at most `T::MAX /
// 3`, so that $3m - 1$ values fit.
//
// This is equivalent to `_nmod_poly_evaluate_nmod_precomp_lazy` from `nmod_poly/evaluate_nmod.c`,
// FLINT 3.6.0.
crate_test_fn! {mod_evaluate_shoup_lazy<T: PrimitiveUnsigned>(
    coefficients: &[T],
    x: T,
    x_precomp: T,
    m: T,
) -> T {
    let (&last, rest) = coefficients.split_last().unwrap();
    let mut value = last;
    for &c in rest.iter().rev() {
        // value is x * value mod m, or that plus m
        value = mod_mul_shoup_lazy(x, value, x_precomp, m);
        // value is now less than 3m - 1, since c < m
        value.wrapping_add_assign(c);
    }
    value
}}

// Evaluates the polynomial with the given coefficients at `x` modulo `m`. The coefficients and `x`
// must be less than `m`; this is not checked. It is public, but hidden, because malachite-nz
// evaluates an `IntegerPolynomial` modulo a word with it, after reducing the coefficients.
//
// This is equivalent to `_nmod_poly_evaluate_nmod` from `nmod_poly/evaluate_nmod.c`, FLINT 3.6.0,
// except that rectangular splitting is not used.
#[doc(hidden)]
pub fn mod_evaluate_slice<T: PrimitiveUnsigned>(coefficients: &[T], x: T, m: T) -> T {
    let len = coefficients.len();
    if len == 0 {
        return T::ZERO;
    }
    if len == 1 || x == T::ZERO {
        return coefficients[0];
    }
    // Shoup's method needs the top bit of m clear
    if m.get_highest_bit() || (T::WIDTH > u32::WIDTH && len < MOD_EVALUATE_SHOUP_THRESHOLD) {
        return mod_evaluate_horner(coefficients, x, m);
    }
    let x_precomp = mod_mul_precompute_shoup(x, m);
    // The lazy loop's values are less than 3m - 1, so it is used when those fit: when m <= (2^W +
    // 1) / 3, which is T::MAX / 3 since every width W is even. FLINT calls this bound LAZY_MAX.
    if m <= T::MAX / T::from(3u8) {
        let mut value = mod_evaluate_shoup_lazy(coefficients, x, x_precomp, m);
        // correct the excess
        let two_m = m << 1;
        if value >= two_m {
            value -= two_m;
        } else if value >= m {
            value -= m;
        }
        value
    } else {
        mod_evaluate_shoup(coefficients, x, x_precomp, m)
    }
}

fn mod_evaluate<T: PrimitiveUnsigned>(p: &UnsignedPolynomial<T>, x: T, m: T) -> T {
    assert!(
        p.mod_is_reduced(&m),
        "self must be reduced mod m, but {p} has a coefficient >= {m}"
    );
    assert!(x < m, "x must be reduced mod m, but {x} >= {m}");
    mod_evaluate_slice(&p.coefficients, x, m)
}

impl<T: PrimitiveUnsigned> ModEvaluate<T> for &UnsignedPolynomial<T> {
    type Output = T;

    /// Evaluates an [`UnsignedPolynomial`] at a value of its coefficient type, modulo a value of
    /// that type, taking the polynomial by reference. The coefficients and the value must already
    /// be reduced modulo `m`.
    ///
    /// $$
    /// f(p, x, m) = \sum_{i=0}^{n-1} c_i x^i \bmod m,
    /// $$
    ///
    /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length. The zero polynomial
    /// evaluates to 0 everywhere.
    ///
    /// The value is found with Horner's rule, reducing after every step. When the polynomial is
    /// long enough and the top bit of `m` is clear, every multiplication is by the same `x`, so
    /// Shoup's method is used: $\lfloor x 2^W / m \rfloor$, where $W$ is the width of `T`, is
    /// computed once, and each product is then reduced with a multiplication in place of a
    /// division. When $m \leq (2^W - 1) / 3$ the reductions are also lazy, leaving the value below
    /// $3m - 1$ until the end.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
    ///
    /// # Panics
    /// Panics if `m` is 0, or if any coefficient of `self` or `x` is greater than or equal to `m`.
    ///
    /// # Examples
    /// ```
    /// use core::str::FromStr;
    /// use malachite_base::polynomial::ModEvaluate;
    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
    ///
    /// let p = UnsignedPolynomial::<u8>::from_str("5*x^2+3*x+7").unwrap();
    /// // 5 * 36 + 3 * 6 + 7 = 205, which is 10 mod 13.
    /// assert_eq!((&p).mod_evaluate(6, 13), 10);
    /// assert_eq!((&p).mod_evaluate(0, 13), 7);
    /// // 205 itself, modulo a larger modulus.
    /// assert_eq!((&p).mod_evaluate(6, 211), 205);
    /// ```
    ///
    /// This is equivalent to `nmod_poly_evaluate_nmod` from `nmod_poly/evaluate_nmod.c`, FLINT
    /// 3.6.0, except that the value must be reduced.
    #[inline]
    fn mod_evaluate(self, x: T, m: T) -> T {
        mod_evaluate(self, x, m)
    }
}

impl<T: PrimitiveUnsigned> ModEvaluate<T> for UnsignedPolynomial<T> {
    type Output = T;

    /// Evaluates an [`UnsignedPolynomial`] at a value of its coefficient type, modulo a value of
    /// that type, taking the polynomial by value. The coefficients and the value must already be
    /// reduced modulo `m`.
    ///
    /// $$
    /// f(p, x, m) = \sum_{i=0}^{n-1} c_i x^i \bmod m,
    /// $$
    ///
    /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length. The zero polynomial
    /// evaluates to 0 everywhere.
    ///
    /// The value is found with Horner's rule, reducing after every step. When the polynomial is
    /// long enough and the top bit of `m` is clear, every multiplication is by the same `x`, so
    /// Shoup's method is used: $\lfloor x 2^W / m \rfloor$, where $W$ is the width of `T`, is
    /// computed once, and each product is then reduced with a multiplication in place of a
    /// division. When $m \leq (2^W - 1) / 3$ the reductions are also lazy, leaving the value below
    /// $3m - 1$ until the end.
    ///
    /// # Worst-case complexity
    /// $T(n) = O(n)$
    ///
    /// $M(n) = O(1)$
    ///
    /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
    ///
    /// # Panics
    /// Panics if `m` is 0, or if any coefficient of `self` or `x` is greater than or equal to `m`.
    ///
    /// # Examples
    /// ```
    /// use core::str::FromStr;
    /// use malachite_base::polynomial::ModEvaluate;
    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
    ///
    /// let p = UnsignedPolynomial::<u8>::from_str("5*x^2+3*x+7").unwrap();
    /// // 5 * 36 + 3 * 6 + 7 = 205, which is 10 mod 13.
    /// assert_eq!(p.clone().mod_evaluate(6, 13), 10);
    /// assert_eq!(p.clone().mod_evaluate(0, 13), 7);
    /// // 205 itself, modulo a larger modulus.
    /// assert_eq!(p.mod_evaluate(6, 211), 205);
    /// ```
    ///
    /// This is equivalent to `nmod_poly_evaluate_nmod` from `nmod_poly/evaluate_nmod.c`, FLINT
    /// 3.6.0, except that the value must be reduced.
    #[inline]
    fn mod_evaluate(self, x: T, m: T) -> T {
        mod_evaluate(&self, x, m)
    }
}

// The numbers of points evaluated together by `mod_evaluate_many_in_place`, for types at most 32
// bits wide and for wider types. Horner's rule is a chain of dependent multiplications, so running
// several points through one pass over the coefficients keeps more multiplications in flight. Tuned
// on Apple M-series: for `u64` a block of 8 is 3.4 to 4.8 times as fast as one point at a time at
// length 256, and for `u32` a block of 4 is best.
const MOD_EVALUATE_MANY_NARROW_BLOCK: usize = 4;
const MOD_EVALUATE_MANY_WIDE_BLOCK: usize = 8;

// Replaces each of the `N` points in `xs` with the polynomial's value there modulo `m`, with
// Horner's rule for all of them in one pass over the coefficients. `coefficients` must be nonempty,
// and the points and coefficients reduced.
crate_test_fn! {mod_evaluate_horner_block<T: PrimitiveUnsigned, const N: usize>(
    coefficients: &[T],
    xs: &mut [T; N],
    m: T,
) {
    let data = T::precompute_mod_mul_data(&m);
    let (&last, rest) = coefficients.split_last().unwrap();
    let points = *xs;
    let mut values = [last; N];
    for &c in rest.iter().rev() {
        for (value, &x) in values.iter_mut().zip(points.iter()) {
            value.mod_mul_precomputed_assign(x, m, &data);
            value.mod_add_assign(c, m);
        }
    }
    *xs = values;
}}

// Like `mod_evaluate_horner_block`, multiplying by each point with Shoup's method. The top bit of
// `m` must be clear.
crate_test_fn! {mod_evaluate_shoup_block<T: PrimitiveUnsigned, const N: usize>(
    coefficients: &[T],
    xs: &mut [T; N],
    m: T,
) {
    let points = *xs;
    let precomps = points.map(|x| mod_mul_precompute_shoup(x, m));
    let (&last, rest) = coefficients.split_last().unwrap();
    let mut values = [last; N];
    for &c in rest.iter().rev() {
        for ((value, &x), &x_precomp) in values.iter_mut().zip(points.iter()).zip(precomps.iter()) {
            *value = mod_mul_shoup(x, *value, x_precomp, m);
            value.mod_add_assign(c, m);
        }
    }
    *xs = values;
}}

// Like `mod_evaluate_shoup_block`, with lazy reduction as in `mod_evaluate_shoup_lazy`. `m` must be
// at most `T::MAX / 3`. The values are fully reduced at the end.
crate_test_fn! {mod_evaluate_shoup_lazy_block<T: PrimitiveUnsigned, const N: usize>(
    coefficients: &[T],
    xs: &mut [T; N],
    m: T,
) {
    let points = *xs;
    let precomps = points.map(|x| mod_mul_precompute_shoup(x, m));
    let (&last, rest) = coefficients.split_last().unwrap();
    let mut values = [last; N];
    for &c in rest.iter().rev() {
        for ((value, &x), &x_precomp) in values.iter_mut().zip(points.iter()).zip(precomps.iter()) {
            // value is x * value mod m, or that plus m, and then less than 3m - 1
            *value = mod_mul_shoup_lazy(x, *value, x_precomp, m);
            value.wrapping_add_assign(c);
        }
    }
    let two_m = m << 1;
    for value in &mut values {
        if *value >= two_m {
            *value -= two_m;
        } else if *value >= m {
            *value -= m;
        }
    }
    *xs = values;
}}

// Replaces each point in `xs` with the polynomial's value there modulo `m`. The points and the
// coefficients must be reduced; this is not checked. Blocks of points are evaluated together, with
// the method `mod_evaluate_slice` would choose for one point. Evaluates the points in `xs` in
// blocks of `N`, with the method chosen by `mod_evaluate_many_in_place`, and returns the leftover
// points, fewer than `N` of them.
fn mod_evaluate_many_blocks<'a, T: PrimitiveUnsigned, const N: usize>(
    coefficients: &[T],
    xs: &'a mut [T],
    m: T,
    shoup: bool,
    lazy: bool,
) -> &'a mut [T] {
    let (blocks, remainder) = xs.as_chunks_mut::<N>();
    for block in blocks {
        if !shoup {
            mod_evaluate_horner_block(coefficients, block, m);
        } else if lazy {
            mod_evaluate_shoup_lazy_block(coefficients, block, m);
        } else {
            mod_evaluate_shoup_block(coefficients, block, m);
        }
    }
    remainder
}

// Replaces each point in `xs` with the polynomial's value there modulo `m`. The points and the
// coefficients must be reduced; this is not checked. Blocks of points are evaluated together, with
// the method `mod_evaluate_slice` would choose for one point.
crate_test_fn! {mod_evaluate_many_in_place<T: PrimitiveUnsigned>(
    coefficients: &[T],
    xs: &mut [T],
    m: T,
) {
    let len = coefficients.len();
    match len {
        0 => xs.fill(T::ZERO),
        1 => xs.fill(coefficients[0]),
        _ => {
            // As in mod_evaluate_slice: Shoup's method needs the top bit of m clear, and is used
            // for short polynomials only when T is at most 32 bits wide
            let shoup = !m.get_highest_bit()
                && (T::WIDTH <= u32::WIDTH || len >= MOD_EVALUATE_SHOUP_THRESHOLD);
            let lazy = m <= T::MAX / T::from(3u8);
            // Wider types take blocks of the wide size first; the leftover points go through blocks
            // of the narrow size, and then one at a time
            let xs = if T::WIDTH <= u32::WIDTH {
                xs
            } else {
                mod_evaluate_many_blocks::<T, MOD_EVALUATE_MANY_WIDE_BLOCK>(
                    coefficients,
                    xs,
                    m,
                    shoup,
                    lazy,
                )
            };
            let remainder = mod_evaluate_many_blocks::<T, MOD_EVALUATE_MANY_NARROW_BLOCK>(
                coefficients,
                xs,
                m,
                shoup,
                lazy,
            );
            for x in remainder {
                *x = mod_evaluate_slice(coefficients, *x, m);
            }
        }
    }
}}

impl<T: PrimitiveUnsigned> ModEvaluateMany<T> for &UnsignedPolynomial<T> {
    type Output = T;

    /// Evaluates an [`UnsignedPolynomial`] at each of several values of its coefficient type,
    /// modulo a value of that type. The coefficients and the values must already be reduced modulo
    /// `m`.
    ///
    /// $$
    /// f(p, (x_j)_{j=0}^{k-1}, m) = \left ( \sum_{i=0}^{n-1} c_i x_j^i \bmod m
    /// \right )_{j=0}^{k-1},
    /// $$
    ///
    /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length.
    ///
    /// The result is the same as calling
    /// [`mod_evaluate`](crate::polynomial::ModEvaluate::mod_evaluate) at each value, with the same
    /// choice between Horner's rule and Shoup's method, but the polynomial is checked once, and
    /// several values are evaluated together in each pass over the coefficients. Horner's rule is a
    /// chain of dependent multiplications, so interleaving independent chains keeps the processor's
    /// multipliers busy.
    ///
    /// # Worst-case complexity
    /// $T(n, k) = O(nk)$
    ///
    /// $M(k) = O(k)$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `self.len()`, and $k$ is `xs.len()`.
    ///
    /// # Panics
    /// Panics if `m` is 0, or if any coefficient of `self` or any value in `xs` is greater than or
    /// equal to `m`.
    ///
    /// # Examples
    /// ```
    /// use core::str::FromStr;
    /// use malachite_base::polynomial::ModEvaluateMany;
    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
    ///
    /// let p = UnsignedPolynomial::<u8>::from_str("5*x^2+3*x+7").unwrap();
    /// // 7, 15, 33, 61, 99, and 147, mod 13
    /// assert_eq!(
    ///     (&p).mod_evaluate_many(&[0, 1, 2, 3, 4, 5], 13),
    ///     &[7, 2, 7, 9, 8, 4]
    /// );
    /// ```
    ///
    /// This is equivalent to `nmod_poly_evaluate_nmod_vec_iter` from
    /// `nmod_poly/evaluate_nmod_vec.c`, FLINT 3.6.0, except that the values must be reduced.
    fn mod_evaluate_many(self, xs: &[T], m: T) -> Vec<T> {
        assert!(
            self.mod_is_reduced(&m),
            "self must be reduced mod m, but {self} has a coefficient >= {m}"
        );
        for &x in xs {
            assert!(x < m, "x must be reduced mod m, but {x} >= {m}");
        }
        let mut values = xs.to_vec();
        mod_evaluate_many_in_place(&self.coefficients, &mut values, m);
        values
    }
}

// Whether geometric evaluation of a polynomial of length `n` at `k` points modulo `m` uses
// Bluestein's trick rather than evaluating at each power of q: when the polynomial and the number
// of points are both long enough. Evaluating at each power is fast with Shoup's multiplication, and
// fastest with its lazy form, which needs the top two bits of `m` clear; when the top bit is set,
// Shoup's multiplication is unavailable and Bluestein's trick wins much sooner. The middle product
// accumulates in three words once `m` has more than about W - 8 bits, where W is `T::WIDTH`, which
// slows it. Measured on an Apple M-series machine, 2026-10, for `u64` with moduli of 20, 30, 40,
// 50, 62, 63, and 64 bits; the boundaries for other types are scaled by their width without being
// measured.
fn mod_evaluate_geometric_fast_preferred<T: PrimitiveUnsigned>(n: usize, k: usize, m: T) -> bool {
    let bits = m.significant_bits();
    let width = T::WIDTH;
    let (min_n, min_k) = if bits == width {
        (32, 32)
    } else if bits == width - 1 {
        (256, 256)
    } else if bits + 8 > width {
        (512, 1024)
    } else if bits << 4 > width * 5 {
        (256, 256)
    } else {
        (128, 128)
    };
    n >= min_n && k >= min_k
}

// Evaluates the polynomial with the given coefficients, reduced modulo `m`, at $1, q, q^2, \ldots,
// q^{k-1}$ modulo `m`, generating the powers of q and then evaluating at them all, several points
// at a time.
//
// This is equivalent to `_nmod_poly_evaluate_geometric_nmod_vec_iter` from
// `nmod_poly/evaluate_geometric_nmod_vec.c`, FLINT 3.6.0, with the ratio q in place of FLINT's
// $r^2$.
crate_test_fn! {mod_evaluate_geometric_iter<T: PrimitiveUnsigned>(
    coefficients: &[T],
    q: T,
    k: usize,
    m: T,
) -> Vec<T> {
    let mut values = Vec::with_capacity(k);
    if k != 0 {
        let mut power = T::ONE % m;
        values.push(power);
        if m.get_highest_bit() {
            let data = T::precompute_mod_mul_data(&m);
            for _ in 1..k {
                power.mod_mul_precomputed_assign(q, m, &data);
                values.push(power);
            }
        } else {
            let q_precomp = mod_mul_precompute_shoup(q, m);
            for _ in 1..k {
                power = mod_mul_shoup(q, power, q_precomp, m);
                values.push(power);
            }
        }
    }
    mod_evaluate_many_in_place(coefficients, &mut values, m);
    values
}}

// Evaluates the polynomial with the given coefficients, reduced modulo `m`, at $1, q, q^2, \ldots,
// q^{k-1}$ modulo `m` with Bluestein's trick, where `q_inverse` is the inverse of q modulo `m`.
//
// With $\binom{t}{2} = t(t-1)/2$, every product $ij$ is $\binom{i+j}{2} - \binom{i}{2} -
// \binom{j}{2}$, so
// $$
// \sum_i c_i q^{ij} = q^{-\binom{j}{2}} \sum_i \left ( c_i q^{-\binom{i}{2}} \right )
// q^{\binom{i+j}{2}}.
// $$
// The sum is coefficient $n - 1 + j$ of the product of the reverse of $(c_i q^{-\binom{i}{2}})_i$
// and $(q^{\binom{t}{2}})_t$, so all $k$ values are one middle product, the $k$ coefficients of
// that product from coefficient $n - 1$ on. Leading zero coefficients of the polynomial are
// skipped, shortening $n$.
//
// This is equivalent to `_nmod_poly_evaluate_geometric_nmod_vec_fast` from
// `nmod_poly/evaluate_geometric_nmod_vec.c`, FLINT 3.6.0, with exponents $\binom{t}{2}$ in place of
// FLINT's $t^2/2$, so that no square root of q is needed.
crate_test_fn! {mod_evaluate_geometric_fast<T: PrimitiveUnsigned>(
    coefficients: &[T],
    q: T,
    q_inverse: T,
    k: usize,
    m: T,
) -> Vec<T> {
    if k == 0 {
        return Vec::new();
    }
    let Some(start) = coefficients.iter().position(|&c| c != T::ZERO) else {
        return vec![T::ZERO; k];
    };
    let n = coefficients.len();
    let a_len = n - start;
    let data = T::precompute_mod_mul_data(&m);
    // binomial_powers[t] = q^C(t, 2), for t < n + k - 1.
    let b_len = n + k - 1;
    let mut binomial_powers = Vec::with_capacity(b_len);
    let mut power = T::ONE % m;
    let mut step = T::ONE % m;
    for _ in 0..b_len {
        binomial_powers.push(power);
        power.mod_mul_precomputed_assign(step, m, &data);
        step.mod_mul_precomputed_assign(q, m, &data);
    }
    // inverse_powers[t] = q^-C(t, 2), for t < max(n, k).
    let w_len = max(n, k);
    let mut inverse_powers = Vec::with_capacity(w_len);
    let mut power = T::ONE % m;
    let mut step = T::ONE % m;
    for _ in 0..w_len {
        inverse_powers.push(power);
        power.mod_mul_precomputed_assign(step, m, &data);
        step.mod_mul_precomputed_assign(q_inverse, m, &data);
    }
    // The scaled coefficients, from the first nonzero one on, reversed.
    let scaled: Vec<T> = coefficients[start..]
        .iter()
        .zip(&inverse_powers[start..])
        .rev()
        .map(|(&c, &w)| c.mod_mul_precomputed(w, m, &data))
        .collect();
    let mut product = vec![T::ZERO; k];
    mod_mul_middle_karatsuba(
        &mut product,
        &scaled,
        &binomial_powers[start..start + a_len + k - 1],
        &ModData::new(m, a_len),
    );
    product
        .iter()
        .zip(&inverse_powers)
        .map(|(&z, &w)| z.mod_mul_precomputed(w, m, &data))
        .collect()
}}

// Evaluates like `mod_evaluate_geometric_fast`, but computes the sums with a truncated product of
// length $n + k - 1$ and keeps its last $k$ coefficients, discarding the first $n - 1$, rather than
// with a middle product. It is kept to measure what the middle product saves.
crate_test_fn! {
#[allow(dead_code)]
mod_evaluate_geometric_fast_truncated<T: PrimitiveUnsigned>(
    coefficients: &[T],
    q: T,
    q_inverse: T,
    k: usize,
    m: T,
) -> Vec<T> {
    if k == 0 {
        return Vec::new();
    }
    let Some(start) = coefficients.iter().position(|&c| c != T::ZERO) else {
        return vec![T::ZERO; k];
    };
    let n = coefficients.len();
    let a_len = n - start;
    let data = T::precompute_mod_mul_data(&m);
    // binomial_powers[t] = q^C(t, 2), for t < n + k - 1.
    let b_len = n + k - 1;
    let mut binomial_powers = Vec::with_capacity(b_len);
    let mut power = T::ONE % m;
    let mut step = T::ONE % m;
    for _ in 0..b_len {
        binomial_powers.push(power);
        power.mod_mul_precomputed_assign(step, m, &data);
        step.mod_mul_precomputed_assign(q, m, &data);
    }
    // inverse_powers[t] = q^-C(t, 2), for t < max(n, k).
    let w_len = max(n, k);
    let mut inverse_powers = Vec::with_capacity(w_len);
    let mut power = T::ONE % m;
    let mut step = T::ONE % m;
    for _ in 0..w_len {
        inverse_powers.push(power);
        power.mod_mul_precomputed_assign(step, m, &data);
        step.mod_mul_precomputed_assign(q_inverse, m, &data);
    }
    // The scaled coefficients, from the first nonzero one on, reversed.
    let scaled: Vec<T> = coefficients[start..]
        .iter()
        .zip(&inverse_powers[start..])
        .rev()
        .map(|(&c, &w)| c.mod_mul_precomputed(w, m, &data))
        .collect();
    let mut product = vec![T::ZERO; a_len + k - 1];
    mod_mul_truncated_to_out(
        &mut product,
        &scaled,
        &binomial_powers[start..start + a_len + k - 1],
        m,
    );
    product[a_len - 1..]
        .iter()
        .zip(&inverse_powers)
        .map(|(&z, &w)| z.mod_mul_precomputed(w, m, &data))
        .collect()
}}

impl<T: PrimitiveUnsigned> ModEvaluateGeometric<T> for &UnsignedPolynomial<T> {
    type Output = T;

    /// Evaluates an [`UnsignedPolynomial`] at $1, q, q^2, \ldots, q^{k-1}$, modulo a value of its
    /// coefficient type. The coefficients and `q` must already be reduced modulo `m`.
    ///
    /// $$
    /// f(p, q, k, m) = \left ( \sum_{i=0}^{n-1} c_i q^{ij} \bmod m \right )_{j=0}^{k-1},
    /// $$
    ///
    /// where $c_i$ is the coefficient of $x^i$ in $p$ and $n$ is its length.
    ///
    /// The powers of `q` are computed with Shoup's method when the top bit of `m` is clear, since
    /// every multiplication is by `q`, and are then evaluated as by
    /// [`mod_evaluate_many`](crate::polynomial::ModEvaluateMany::mod_evaluate_many), in place.
    ///
    /// # Worst-case complexity
    /// $T(n, k) = O(nk)$
    ///
    /// $M(k) = O(k)$
    ///
    /// where $T$ is time, $M$ is additional memory, $n$ is `self.len()`, and $k$ is `k`.
    ///
    /// # Panics
    /// Panics if `m` is 0, or if any coefficient of `self` or `q` is greater than or equal to `m`.
    ///
    /// # Examples
    /// ```
    /// use core::str::FromStr;
    /// use malachite_base::polynomial::ModEvaluateGeometric;
    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
    ///
    /// let p = UnsignedPolynomial::<u8>::from_str("5*x^2+3*x+7").unwrap();
    /// // At 1, 2, 4, and 8: 15, 33, 99, and 351, mod 13
    /// assert_eq!((&p).mod_evaluate_geometric(2, 4, 13), &[2, 7, 8, 0]);
    /// ```
    ///
    /// This is equivalent to `nmod_poly_evaluate_geometric_nmod_vec_iter` from
    /// `nmod_poly/evaluate_geometric_nmod_vec.c`, FLINT 3.6.0, with `q` in place of FLINT's $r^2$:
    /// FLINT evaluates at the powers of the square of its argument.
    fn mod_evaluate_geometric(self, q: T, k: u64, m: T) -> Vec<T> {
        assert!(
            self.mod_is_reduced(&m),
            "self must be reduced mod m, but {self} has a coefficient >= {m}"
        );
        assert!(q < m, "q must be reduced mod m, but {q} >= {m}");
        let k = usize::exact_from(k);
        let n = self.coefficients.len();
        if mod_evaluate_geometric_fast_preferred(n, k, m)
            && q != T::ZERO
            && let Some(q_inverse) = q.mod_inverse(m)
        {
            mod_evaluate_geometric_fast(&self.coefficients, q, q_inverse, k, m)
        } else {
            mod_evaluate_geometric_iter(&self.coefficients, q, k, m)
        }
    }
}