qfall_math/rational/poly_over_q/
set.rs1use super::PolyOverQ;
12use crate::{rational::Q, traits::SetCoefficient};
13use flint_sys::fmpq_poly::fmpq_poly_set_coeff_fmpq;
14
15impl<Rational: Into<Q>> SetCoefficient<Rational> for PolyOverQ {
16 unsafe fn set_coeff_unchecked(&mut self, index: i64, value: Rational) {
48 let value = value.into();
49
50 unsafe {
51 fmpq_poly_set_coeff_fmpq(&mut self.poly, index, &value.value);
52 }
53 }
54}
55
56#[cfg(test)]
57mod test_set_coeff_z {
58 use crate::{integer::Z, rational::PolyOverQ, traits::SetCoefficient};
59 use std::str::FromStr;
60
61 #[test]
63 fn set_min_negative_coeff() {
64 let mut poly = PolyOverQ::from_str("2 -1 3/17").unwrap();
65
66 assert!(poly.set_coeff(i64::MIN, 2).is_err());
67 assert!(poly.set_coeff(i32::MIN, 2).is_err());
68 assert!(poly.set_coeff(i16::MIN, 2).is_err());
69 assert!(poly.set_coeff(i8::MIN, 2).is_err());
70 }
71
72 #[test]
74 fn set_coeff_large() {
75 let mut poly = PolyOverQ::from_str("2 -1 3/17").unwrap();
76
77 assert!(poly.set_coeff(2, i32::MAX).is_ok());
78 assert!(poly.set_coeff(2, i64::MAX).is_ok());
79 }
80
81 #[test]
83 fn set_index_large() {
84 let mut poly = PolyOverQ::from_str("2 -1 3/17").unwrap();
85
86 assert!(poly.set_coeff(u8::MAX, 2).is_ok());
87 assert!(poly.set_coeff(u16::MAX, 2).is_ok());
88 }
89
90 #[test]
92 fn set_coeff_working() {
93 let mut poly = PolyOverQ::from_str("4 0 -1 2 3/17").unwrap();
94 let value = 10000;
95
96 poly.set_coeff(0, value).unwrap();
97 poly.set_coeff(5, value).unwrap();
98 assert_eq!(
99 PolyOverQ::from_str("6 10000 -1 2 3/17 0 10000").unwrap(),
100 poly
101 );
102 }
103
104 #[test]
106 fn set_coeff_rest_zero() {
107 let mut poly = PolyOverQ::default();
108
109 poly.set_coeff(4, -10).unwrap();
110 assert_eq!(PolyOverQ::from_str("5 0 0 0 0 -10").unwrap(), poly);
111 }
112
113 #[test]
115 fn set_coeff_z() {
116 let mut poly = PolyOverQ::default();
117
118 poly.set_coeff(4, Z::from(123)).unwrap();
119 poly.set_coeff(5, &Z::from(321)).unwrap();
120 assert_eq!(PolyOverQ::from_str("6 0 0 0 0 123 321").unwrap(), poly);
121 }
122
123 #[test]
125 fn large_coeff_z() {
126 let mut poly = PolyOverQ::default();
127
128 poly.set_coeff(4, u64::MAX).unwrap();
129 assert_eq!(
130 PolyOverQ::from_str(&format!("5 0 0 0 0 {}", u64::MAX)).unwrap(),
131 poly
132 );
133 }
134}
135
136#[cfg(test)]
137mod test_set_coeff_q {
138 use crate::{
139 rational::{PolyOverQ, Q},
140 traits::{GetCoefficient, SetCoefficient},
141 };
142 use std::str::FromStr;
143
144 #[test]
146 fn large_coeff() {
147 let mut poly = PolyOverQ::from_str("1 1").unwrap();
148 let q = Q::from((u64::MAX - 1, u64::MAX));
149
150 poly.set_coeff(2, &q).unwrap();
151
152 assert_eq!(q, poly.get_coeff(2).unwrap());
153 assert_eq!(
154 PolyOverQ::from_str(&format!("3 1 0 {}/{}", u64::MAX - 1, u64::MAX)).unwrap(),
155 poly
156 )
157 }
158}