neco-root 0.1.1

verified root brackets, refinement evidence, and local membership analysis
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
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

extern crate alloc;

use core::cmp::Ordering;

use neco_algnum::{AlgnumError, RealAlgebraic};
use neco_bigint::{
    BigInt, BigUint, BigintError, Dyadic, DyadicEnclosure, RawRational, ReducedRational, Sign,
};
use neco_formsum::{FormSumErrorKind, RawFormSum, RawTerm};
use neco_monomial::RawMonomial;
use neco_polynomial::{DerivativeOrder, Enclosure, IntervalFailure, Polynomial, PolynomialFailure};

pub const MAX_CANDIDATE_POINTS: usize = 4_096;
pub const MAX_DERIVATIVE_ORDER: usize = 64;
pub const MAX_ROOT_REFINEMENT_ITERATIONS: usize = 19;
pub const MAX_ROOT_REFINEMENT_PRECISION_BITS: u32 = 256;

#[derive(Debug, Eq, PartialEq)]
pub struct RootBracket {
    polynomial: Polynomial,
    enclosure: Enclosure,
    evidence: RootEvidence,
}

impl RootBracket {
    pub fn sign_change(polynomial: Polynomial, enclosure: Enclosure) -> Result<Self, RootFailure> {
        let lower = sign_at(&polynomial, enclosure.lower())?;
        let upper = sign_at(&polynomial, enclosure.upper())?;
        if !opposite_nonzero(lower, upper) {
            return Err(RootFailure::NoSignChange);
        }
        Ok(Self {
            polynomial,
            enclosure,
            evidence: RootEvidence::SignChange { lower, upper },
        })
    }

    pub fn even_multiplicity_touch(
        polynomial: Polynomial,
        enclosure: Enclosure,
        point: Dyadic,
        max_order: DerivativeOrder,
    ) -> Result<Self, RootFailure> {
        if !enclosure.contains(&point) {
            return Err(RootFailure::PointOutsideEnclosure);
        }
        let multiplicity = classify_multiplicity(&polynomial, &point, max_order)?;
        match multiplicity {
            RootMultiplicity::Multiple(order) if order.get() >= 2 && order.get() % 2 == 0 => {
                Ok(Self {
                    polynomial,
                    enclosure,
                    evidence: RootEvidence::EvenMultiplicityTouch {
                        point,
                        multiplicity,
                    },
                })
            }
            _ => Err(RootFailure::NoEvenMultiplicityTouch),
        }
    }

    pub fn polynomial(&self) -> &Polynomial {
        &self.polynomial
    }

    pub fn enclosure(&self) -> &Enclosure {
        &self.enclosure
    }

    pub fn evidence(&self) -> &RootEvidence {
        &self.evidence
    }

    pub fn refine_bisection(&self, iterations: usize) -> Result<RootIsolation, RootFailure> {
        validate_resource_limit(
            RootResource::RefinementIterations,
            iterations,
            MAX_ROOT_REFINEMENT_ITERATIONS,
        )?;
        match &self.evidence {
            RootEvidence::SignChange { lower, .. } => {
                refine_sign_change(self, iterations, *lower, RootRefinementMethod::Bisection, 0)
            }
            RootEvidence::EvenMultiplicityTouch { point, .. } => {
                exact_root_isolation(self, point, RootRefinementMethod::Bisection)
            }
        }
    }

    pub fn refine_newton_bisection(
        &self,
        iterations: usize,
        precision_bits: u32,
    ) -> Result<RootIsolation, RootFailure> {
        validate_resource_limit(
            RootResource::RefinementIterations,
            iterations,
            MAX_ROOT_REFINEMENT_ITERATIONS,
        )?;
        validate_resource_limit(
            RootResource::RefinementPrecisionBits,
            precision_bits as usize,
            MAX_ROOT_REFINEMENT_PRECISION_BITS as usize,
        )?;
        match &self.evidence {
            RootEvidence::SignChange { lower, .. } => refine_sign_change(
                self,
                iterations,
                *lower,
                RootRefinementMethod::NewtonBisection,
                precision_bits,
            ),
            RootEvidence::EvenMultiplicityTouch { point, .. } => {
                exact_root_isolation(self, point, RootRefinementMethod::NewtonBisection)
            }
        }
    }
}

#[derive(Debug, Eq, PartialEq)]
pub enum RootEvidence {
    SignChange {
        lower: Sign,
        upper: Sign,
    },
    EvenMultiplicityTouch {
        point: Dyadic,
        multiplicity: RootMultiplicity,
    },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RootMultiplicity {
    Simple,
    Multiple(DerivativeOrder),
}

#[derive(Debug, Eq, PartialEq)]
pub struct MembershipJudgmentRequest {
    target_polynomial: Polynomial,
    target_enclosure: Enclosure,
    candidate_points: alloc::vec::Vec<Dyadic>,
    max_order: DerivativeOrder,
    zero_exclusion_precision_bits: u32,
}

impl MembershipJudgmentRequest {
    pub fn new(
        target_polynomial: Polynomial,
        target_enclosure: Enclosure,
        candidate_points: alloc::vec::Vec<Dyadic>,
        max_order: DerivativeOrder,
        zero_exclusion_precision_bits: u32,
    ) -> Result<Self, RootFailure> {
        validate_resource_limit(
            RootResource::CandidatePoints,
            candidate_points.len(),
            MAX_CANDIDATE_POINTS,
        )?;
        validate_resource_limit(
            RootResource::DerivativeOrder,
            max_order.get(),
            MAX_DERIVATIVE_ORDER,
        )?;
        validate_resource_limit(
            RootResource::RegionSignPrecisionBits,
            zero_exclusion_precision_bits as usize,
            MAX_ROOT_REFINEMENT_PRECISION_BITS as usize,
        )?;
        Ok(Self {
            target_polynomial,
            target_enclosure,
            candidate_points,
            max_order,
            zero_exclusion_precision_bits,
        })
    }

    pub fn target_polynomial(&self) -> &Polynomial {
        &self.target_polynomial
    }

    pub fn target_enclosure(&self) -> &Enclosure {
        &self.target_enclosure
    }

    pub fn candidate_points(&self) -> &[Dyadic] {
        &self.candidate_points
    }

    pub fn max_order(&self) -> DerivativeOrder {
        self.max_order
    }

    pub fn zero_exclusion_precision_bits(&self) -> u32 {
        self.zero_exclusion_precision_bits
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RegionSign {
    ConstantPositive,
    ConstantNegative,
    ConstantZero,
    SignVaries,
    ContainsZero,
    Undetermined,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RegionSignJudgment {
    sign: RegionSign,
}

impl RegionSignJudgment {
    fn new(sign: RegionSign) -> Self {
        Self { sign }
    }

    pub fn sign(&self) -> RegionSign {
        self.sign
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MembershipTransitionReport {
    loci: alloc::vec::Vec<MembershipLocus>,
}
impl MembershipTransitionReport {
    fn new(loci: alloc::vec::Vec<MembershipLocus>) -> Self {
        Self { loci }
    }

    pub fn loci(&self) -> &[MembershipLocus] {
        &self.loci
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MembershipLocus {
    point_index: usize,
    kind: MembershipLocusKind,
    multiplicity: RootMultiplicity,
}

impl MembershipLocus {
    fn new(point_index: usize, kind: MembershipLocusKind, multiplicity: RootMultiplicity) -> Self {
        Self {
            point_index,
            kind,
            multiplicity,
        }
    }

    pub fn point_index(&self) -> usize {
        self.point_index
    }

    pub fn kind(&self) -> MembershipLocusKind {
        self.kind
    }

    pub fn multiplicity(&self) -> RootMultiplicity {
        self.multiplicity
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum MembershipLocusKind {
    Transition,
    SingularContact,
}

fn classify_local_multiplicity(
    request: &MembershipJudgmentRequest,
) -> Result<alloc::vec::Vec<(usize, RootMultiplicity)>, RootFailure> {
    for point in request.candidate_points() {
        if !request.target_enclosure().contains(point) {
            return Err(RootFailure::PointOutsideEnclosure);
        }
    }

    let mut roots = alloc::vec::Vec::new();
    for (index, point) in request.candidate_points().iter().enumerate() {
        let algebraic_point = dyadic_to_algebraic(point)?;
        if request
            .target_polynomial()
            .evaluate(&algebraic_point)?
            .is_zero()
        {
            let multiplicity =
                classify_multiplicity(request.target_polynomial(), point, request.max_order())?;
            roots.push((index, multiplicity));
        }
    }
    Ok(roots)
}

pub fn classify_region_sign(
    request: &MembershipJudgmentRequest,
) -> Result<RegionSignJudgment, RootFailure> {
    if request.target_polynomial().degree() == 0 {
        let coefficient = request
            .target_polynomial()
            .coefficients()
            .first()
            .ok_or(PolynomialFailure::EmptyCoefficients)?;
        return match coefficient.sign()? {
            Sign::Positive => Ok(RegionSignJudgment::new(RegionSign::ConstantPositive)),
            Sign::Negative => Ok(RegionSignJudgment::new(RegionSign::ConstantNegative)),
            Sign::Zero => Ok(RegionSignJudgment::new(RegionSign::ConstantZero)),
        };
    }

    let roots = classify_local_multiplicity(request)?;
    if !roots.is_empty() {
        if roots.iter().any(|(point_index, multiplicity)| {
            let point = &request.candidate_points()[*point_index];
            let is_interior = request.target_enclosure().lower() < point
                && point < request.target_enclosure().upper();
            let has_odd_multiplicity = match multiplicity {
                RootMultiplicity::Simple => true,
                RootMultiplicity::Multiple(order) => order.get() % 2 == 1,
            };
            is_interior && has_odd_multiplicity
        }) {
            return Ok(RegionSignJudgment::new(RegionSign::SignVaries));
        }
        return Ok(RegionSignJudgment::new(RegionSign::ContainsZero));
    }

    let range = request.target_polynomial().evaluate_enclosure(
        request.target_enclosure(),
        request.zero_exclusion_precision_bits(),
    )?;
    if range.lower().integer().sign() == Sign::Positive {
        return Ok(RegionSignJudgment::new(RegionSign::ConstantPositive));
    }
    if range.upper().integer().sign() == Sign::Negative {
        return Ok(RegionSignJudgment::new(RegionSign::ConstantNegative));
    }

    let lower_sign = sign_at(
        request.target_polynomial(),
        request.target_enclosure().lower(),
    )?;
    let upper_sign = sign_at(
        request.target_polynomial(),
        request.target_enclosure().upper(),
    )?;

    match (lower_sign, upper_sign) {
        (Sign::Positive, Sign::Negative) | (Sign::Negative, Sign::Positive) => {
            Ok(RegionSignJudgment::new(RegionSign::SignVaries))
        }
        _ => Ok(RegionSignJudgment::new(RegionSign::Undetermined)),
    }
}

pub fn locate_membership_transitions(
    request: &MembershipJudgmentRequest,
) -> Result<MembershipTransitionReport, RootFailure> {
    let roots = classify_local_multiplicity(request)?;
    let mut loci = alloc::vec::Vec::new();

    for (point_index, multiplicity) in roots {
        let kind = match multiplicity {
            RootMultiplicity::Simple => MembershipLocusKind::Transition,
            RootMultiplicity::Multiple(order) => {
                if order.get() % 2 == 0 {
                    MembershipLocusKind::SingularContact
                } else {
                    MembershipLocusKind::Transition
                }
            }
        };
        loci.push(MembershipLocus::new(point_index, kind, multiplicity));
    }

    Ok(MembershipTransitionReport::new(loci))
}

#[derive(Debug, Eq, PartialEq)]
pub struct RootIsolation {
    enclosure: Enclosure,
    witness: ConvergenceWitness,
}

impl RootIsolation {
    fn new(enclosure: Enclosure, witness: ConvergenceWitness) -> Self {
        Self { enclosure, witness }
    }

    pub fn enclosure(&self) -> &Enclosure {
        &self.enclosure
    }

    pub fn witness(&self) -> &ConvergenceWitness {
        &self.witness
    }
}

#[derive(Debug, Eq, PartialEq)]
pub struct ConvergenceWitness {
    method: RootRefinementMethod,
    status: ConvergenceStatus,
    iterations: usize,
    newton_steps: usize,
    newton_fallbacks: usize,
    initial: Enclosure,
    final_enclosure: Enclosure,
}

impl ConvergenceWitness {
    fn new(
        method: RootRefinementMethod,
        status: ConvergenceStatus,
        iterations: usize,
        newton_steps: usize,
        newton_fallbacks: usize,
        initial: Enclosure,
        final_enclosure: Enclosure,
    ) -> Self {
        Self {
            method,
            status,
            iterations,
            newton_steps,
            newton_fallbacks,
            initial,
            final_enclosure,
        }
    }

    pub fn method(&self) -> RootRefinementMethod {
        self.method
    }

    pub fn status(&self) -> ConvergenceStatus {
        self.status
    }

    pub fn iterations(&self) -> usize {
        self.iterations
    }

    pub fn newton_steps(&self) -> usize {
        self.newton_steps
    }

    pub fn newton_fallback_count(&self) -> usize {
        self.newton_fallbacks
    }

    pub fn newton_fallback_reason(&self) -> Option<NewtonFallbackReason> {
        (self.newton_fallbacks > 0).then_some(NewtonFallbackReason::DerivativeEnclosureContainsZero)
    }

    pub fn initial(&self) -> &Enclosure {
        &self.initial
    }

    pub fn final_enclosure(&self) -> &Enclosure {
        &self.final_enclosure
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RootRefinementMethod {
    Bisection,
    NewtonBisection,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum NewtonFallbackReason {
    DerivativeEnclosureContainsZero,
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ConvergenceStatus {
    ExactDyadicRoot,
    Refined,
    IterationLimit,
}

#[derive(Debug, Eq, PartialEq)]
pub enum RootFailure {
    Polynomial(PolynomialFailure),
    Interval(IntervalFailure),
    Algebra(AlgnumError),
    Arithmetic(BigintError),
    FormSum(FormSumErrorKind),
    NoSignChange,
    NoRootAtPoint,
    NoEvenMultiplicityTouch,
    PointOutsideEnclosure,
    MultiplicityUndetermined,
    NewtonDerivativeContainsZero,
    ResourceLimit {
        resource: RootResource,
        requested: usize,
        maximum: usize,
    },
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RootResource {
    CandidatePoints,
    DerivativeOrder,
    RefinementIterations,
    RefinementPrecisionBits,
    RegionSignPrecisionBits,
}

pub fn classify_multiplicity(
    polynomial: &Polynomial,
    point: &Dyadic,
    max_order: DerivativeOrder,
) -> Result<RootMultiplicity, RootFailure> {
    validate_resource_limit(
        RootResource::DerivativeOrder,
        max_order.get(),
        MAX_DERIVATIVE_ORDER,
    )?;
    let algebraic_point = dyadic_to_algebraic(point)?;
    if polynomial.evaluate(&algebraic_point)?.sign()? != Sign::Zero {
        return Err(RootFailure::NoRootAtPoint);
    }
    for order in 1..=max_order.get() {
        let derivative = polynomial.derivative(DerivativeOrder::new(order))?;
        if derivative.evaluate(&algebraic_point)?.sign()? != Sign::Zero {
            return if order == 1 {
                Ok(RootMultiplicity::Simple)
            } else {
                Ok(RootMultiplicity::Multiple(DerivativeOrder::new(order)))
            };
        }
    }
    Err(RootFailure::MultiplicityUndetermined)
}

impl From<PolynomialFailure> for RootFailure {
    fn from(error: PolynomialFailure) -> Self {
        Self::Polynomial(error)
    }
}

impl From<IntervalFailure> for RootFailure {
    fn from(error: IntervalFailure) -> Self {
        Self::Interval(error)
    }
}

impl From<AlgnumError> for RootFailure {
    fn from(error: AlgnumError) -> Self {
        Self::Algebra(error)
    }
}

impl From<BigintError> for RootFailure {
    fn from(error: BigintError) -> Self {
        Self::Arithmetic(error)
    }
}

impl From<FormSumErrorKind> for RootFailure {
    fn from(error: FormSumErrorKind) -> Self {
        Self::FormSum(error)
    }
}

fn refine_sign_change(
    bracket: &RootBracket,
    iterations: usize,
    initial_lower_sign: Sign,
    method: RootRefinementMethod,
    precision_bits: u32,
) -> Result<RootIsolation, RootFailure> {
    let initial = clone_enclosure(bracket.enclosure())?;
    let mut lower = bracket.enclosure().lower().try_clone()?;
    let mut upper = bracket.enclosure().upper().try_clone()?;
    let mut lower_sign = initial_lower_sign;
    let mut status = ConvergenceStatus::IterationLimit;
    let mut used_iterations = 0;
    let mut newton_steps = 0;
    let mut newton_fallbacks = 0;

    for _ in 0..iterations {
        used_iterations += 1;
        let candidate = if method == RootRefinementMethod::NewtonBisection {
            match newton_candidate(&bracket.polynomial, &lower, &upper, precision_bits) {
                Ok(candidate) if lower < candidate && candidate < upper => {
                    newton_steps += 1;
                    candidate
                }
                Ok(_) => lower.midpoint(&upper)?,
                Err(RootFailure::NewtonDerivativeContainsZero) => {
                    newton_fallbacks += 1;
                    lower.midpoint(&upper)?
                }
                Err(error) => return Err(error),
            }
        } else {
            lower.midpoint(&upper)?
        };
        let sign = sign_at(&bracket.polynomial, &candidate)?;
        if sign == Sign::Zero {
            lower = candidate.try_clone()?;
            upper = candidate;
            status = ConvergenceStatus::ExactDyadicRoot;
            break;
        }
        if sign == lower_sign {
            lower = candidate;
            lower_sign = sign;
        } else {
            upper = candidate;
        }
        status = ConvergenceStatus::Refined;
    }

    let final_enclosure = Enclosure::new(lower, upper)?;
    Ok(RootIsolation::new(
        clone_enclosure(&final_enclosure)?,
        ConvergenceWitness::new(
            method,
            status,
            used_iterations,
            newton_steps,
            newton_fallbacks,
            initial,
            final_enclosure,
        ),
    ))
}

fn exact_root_isolation(
    bracket: &RootBracket,
    point: &Dyadic,
    method: RootRefinementMethod,
) -> Result<RootIsolation, RootFailure> {
    let exact = Enclosure::new(point.try_clone()?, point.try_clone()?)?;
    Ok(RootIsolation::new(
        clone_enclosure(&exact)?,
        ConvergenceWitness::new(
            method,
            ConvergenceStatus::ExactDyadicRoot,
            0,
            0,
            0,
            clone_enclosure(&bracket.enclosure)?,
            exact,
        ),
    ))
}

fn newton_candidate(
    polynomial: &Polynomial,
    lower: &Dyadic,
    upper: &Dyadic,
    precision_bits: u32,
) -> Result<Dyadic, RootFailure> {
    let midpoint = lower.midpoint(upper)?;
    let midpoint_value = dyadic_to_algebraic(&midpoint)?;
    let derivative = polynomial.derivative(DerivativeOrder::new(1))?;
    let value_enclosure = polynomial
        .evaluate(&midpoint_value)?
        .enclose(precision_bits)?
        .enclosure()
        .try_clone()?;
    let derivative_enclosure = derivative
        .evaluate(&midpoint_value)?
        .enclose(precision_bits)?
        .enclosure()
        .try_clone()?;
    if contains_zero(&derivative_enclosure) {
        return Err(RootFailure::NewtonDerivativeContainsZero);
    }
    let quotient = enclosure_div(&value_enclosure, &derivative_enclosure, precision_bits)?;
    midpoint.sub(&quotient.midpoint()?).map_err(Into::into)
}

fn sign_at(polynomial: &Polynomial, point: &Dyadic) -> Result<Sign, RootFailure> {
    Ok(polynomial.evaluate(&dyadic_to_algebraic(point)?)?.sign()?)
}

fn dyadic_to_algebraic(value: &Dyadic) -> Result<RealAlgebraic, RootFailure> {
    if value.is_zero() {
        return Ok(RealAlgebraic::from_form_sum(&neco_formsum::FormSum::zero())?);
    }
    let denominator = BigUint::one()?.shl_bits(value.exponent() as usize)?;
    let raw = RawFormSum::new(alloc::vec![RawTerm::new(
        RawRational::new(value.integer().try_clone()?, denominator),
        RawMonomial::positive(alloc::vec![]),
    )]);
    let sum = raw
        .normalize()
        .map_err(|errors| RootFailure::FormSum(errors.into_parts().0))?;
    Ok(RealAlgebraic::from_form_sum(&sum)?)
}

fn clone_enclosure(enclosure: &Enclosure) -> Result<Enclosure, RootFailure> {
    Ok(Enclosure::from_dyadic_enclosure(
        enclosure.as_dyadic_enclosure().try_clone()?,
    ))
}

fn contains_zero(enclosure: &DyadicEnclosure) -> bool {
    enclosure.contains_dyadic(&Dyadic::new(BigInt::zero(), 0))
}

fn enclosure_div(
    numerator: &DyadicEnclosure,
    denominator: &DyadicEnclosure,
    precision_bits: u32,
) -> Result<DyadicEnclosure, RootFailure> {
    let candidates = [
        dyadic_to_rational(numerator.lower())?.div(&dyadic_to_rational(denominator.lower())?)?,
        dyadic_to_rational(numerator.lower())?.div(&dyadic_to_rational(denominator.upper())?)?,
        dyadic_to_rational(numerator.upper())?.div(&dyadic_to_rational(denominator.lower())?)?,
        dyadic_to_rational(numerator.upper())?.div(&dyadic_to_rational(denominator.upper())?)?,
    ];
    let mut lower = candidates[0].try_clone()?;
    let mut upper = candidates[0].try_clone()?;
    for candidate in candidates.iter().skip(1) {
        if candidate.cmp(&lower) == Ordering::Less {
            lower = candidate.try_clone()?;
        }
        if candidate.cmp(&upper) == Ordering::Greater {
            upper = candidate.try_clone()?;
        }
    }
    Ok(DyadicEnclosure::new(
        lower.dyadic_floor(precision_bits)?,
        upper.dyadic_ceil(precision_bits)?,
    )?)
}

fn dyadic_to_rational(value: &Dyadic) -> Result<ReducedRational, BigintError> {
    let denominator = BigUint::one()?.shl_bits(value.exponent() as usize)?;
    RawRational::new(value.integer().try_clone()?, denominator)
        .reduce()
        .map(|reduction| reduction.into_reduced())
}

fn opposite_nonzero(left: Sign, right: Sign) -> bool {
    matches!(
        (left, right),
        (Sign::Negative, Sign::Positive) | (Sign::Positive, Sign::Negative)
    )
}

fn validate_resource_limit(
    resource: RootResource,
    requested: usize,
    maximum: usize,
) -> Result<(), RootFailure> {
    if requested > maximum {
        return Err(RootFailure::ResourceLimit {
            resource,
            requested,
            maximum,
        });
    }
    Ok(())
}