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
// Copyright © 2023 Marvin Beckmann
//
// This file is part of qFALL-math.
//
// qFALL-math is free software: you can redistribute it and/or modify it under
// the terms of the Mozilla Public License Version 2.0 as published by the
// Mozilla Foundation. See <https://mozilla.org/en-US/MPL/2.0/>.
//! Implementations to evaluate a [`PolyOverZ`].
//! For each reasonable type, an implementation
//! of the [`Evaluate`] trait should be implemented.
use super::PolyOverZ;
use crate::{integer::Z, rational::Q, traits::Evaluate};
use flint_sys::fmpz_poly::{fmpz_poly_evaluate_fmpq, fmpz_poly_evaluate_fmpz};
impl<Integer: Into<Z>> Evaluate<Integer, Z> for PolyOverZ {
/// Evaluates a [`PolyOverZ`] on a given input.
///
/// Parameters:
/// - `value`: the value with which to evaluate the polynomial.
///
/// Returns the evaluation of the polynomial as a [`Z`].
///
/// # Examples
/// ```
/// use qfall_math::traits::*;
/// use qfall_math::integer::Z;
/// use qfall_math::integer::PolyOverZ;
/// use std::str::FromStr;
///
/// let poly = PolyOverZ::from_str("5 0 1 2 -3 1").unwrap();
/// let res: Z = poly.evaluate(3);
/// ```
fn evaluate(&self, value: Integer) -> Z {
let value = value.into();
let mut res = Z::default();
unsafe { fmpz_poly_evaluate_fmpz(&mut res.value, &self.poly, &value.value) };
res
}
}
impl<Rational: Into<Q>> Evaluate<Rational, Q> for PolyOverZ {
/// Evaluates a [`PolyOverZ`] on a given input.
///
/// Parameters:
/// - `value`: the value with which to evaluate the polynomial.
///
/// Returns the evaluation of the polynomial as a [`Q`].
///
/// # Examples
/// ```
/// use qfall_math::traits::*;
/// use qfall_math::rational::Q;
/// use qfall_math::integer::PolyOverZ;
/// use std::str::FromStr;
///
/// let poly = PolyOverZ::from_str("5 0 1 2 -3 1").unwrap();
/// let value = Q::from((3, 2));
/// let res: Q = poly.evaluate(&value);
/// ```
fn evaluate(&self, value: Rational) -> Q {
let value = value.into();
let mut res = Q::default();
unsafe { fmpz_poly_evaluate_fmpq(&mut res.value, &self.poly, &value.value) };
res
}
}
#[cfg(test)]
mod test_evaluate_z {
use crate::integer::{PolyOverZ, Z};
use crate::traits::Evaluate;
use std::str::FromStr;
/// Tests if evaluate works for [`Z`] as input
#[test]
fn eval_z() {
let poly = PolyOverZ::from_str("2 1 2").unwrap();
let res: Z = poly.evaluate(Z::from(3));
assert_eq!(7, res);
}
/// Tests if evaluate with a reference works
#[test]
fn eval_z_ref() {
let poly = PolyOverZ::from_str("2 1 2").unwrap();
let res: Z = poly.evaluate(&Z::from(3));
assert_eq!(7, res);
}
/// Tests if evaluate works with negative values
#[test]
fn eval_z_negative() {
let poly = PolyOverZ::from_str("2 1 2").unwrap();
let res: Z = poly.evaluate(-5);
assert_eq!(-9, res);
}
/// Tests if evaluate works with large integers
#[test]
fn eval_z_large() {
let poly = PolyOverZ::from_str("2 1 2").unwrap();
let res: Z = poly.evaluate(&Z::from_str(&"1".repeat(65)).unwrap());
let mut res_cmp_str = "2".repeat(64);
res_cmp_str.push('3');
assert_eq!(Z::from_str(&res_cmp_str).unwrap(), res);
}
/// Test if evaluate works with max of [`i64`],[`i32`], ...
#[test]
fn eval_max() {
let poly = PolyOverZ::from_str("2 1 2").unwrap();
// signed
let _: Z = poly.evaluate(i64::MAX);
let _: Z = poly.evaluate(i32::MAX);
let _: Z = poly.evaluate(i16::MAX);
let _: Z = poly.evaluate(i8::MAX);
//unsigned
let _: Z = poly.evaluate(u64::MAX);
let _: Z = poly.evaluate(u32::MAX);
let _: Z = poly.evaluate(u16::MAX);
let _: Z = poly.evaluate(u8::MAX);
}
/// Test if evaluate works with min of [`i64`],[`i32`], ...
#[test]
fn eval_min() {
let poly = PolyOverZ::from_str("2 1 2").unwrap();
// signed
let _: Z = poly.evaluate(i64::MIN);
let _: Z = poly.evaluate(i32::MIN);
let _: Z = poly.evaluate(i16::MIN);
let _: Z = poly.evaluate(i8::MIN);
// unsigned
let _: Z = poly.evaluate(u64::MIN);
let _: Z = poly.evaluate(u32::MIN);
let _: Z = poly.evaluate(u16::MIN);
let _: Z = poly.evaluate(u8::MIN);
}
}
#[cfg(test)]
mod test_evaluate_q {
use crate::{integer::PolyOverZ, rational::Q, traits::Evaluate};
use std::str::FromStr;
/// Ensures that positive values return expected evaluation
#[test]
fn evaluate_positive() {
let poly = PolyOverZ::from_str("2 1 3").unwrap();
let value = Q::from((3, 2));
let res_ref = poly.evaluate(&value);
let res = poly.evaluate(value);
assert_eq!(Q::from((11, 2)), res);
assert_eq!(res_ref, res);
}
/// Ensures that negative values return expected evaluation
#[test]
fn evaluate_negative() {
let poly = PolyOverZ::from_str("2 1 3").unwrap();
let value = Q::from((-3, 2));
let res_ref = poly.evaluate(&value);
let res = poly.evaluate(value);
assert_eq!(Q::from((-7, 2)), res);
assert_eq!(res_ref, res);
}
/// Ensures that positive large values return expected evaluation
#[test]
fn evaluate_large_positive() {
let poly = PolyOverZ::from_str(&format!("2 {} 1", (u64::MAX - 1) / 2)).unwrap();
let value = Q::from((u64::MAX - 1) / 2);
let res_ref = poly.evaluate(&value);
let res = poly.evaluate(value);
assert_eq!(Q::from(u64::MAX - 1), res);
assert_eq!(res_ref, res);
}
/// Ensures that negative large values return expected evaluation
#[test]
fn evaluate_large_negative() {
let poly = PolyOverZ::from_str(&format!("2 {} 2", u64::MAX)).unwrap();
let value = Q::from_str(&format!("-{}", (u64::MAX - 1) / 2)).unwrap();
let res_ref = poly.evaluate(&value);
let res = poly.evaluate(value);
assert_eq!(Q::ONE, res);
assert_eq!(res_ref, res);
}
}