algebraeon_nzq/integer/
mod.rs1use crate::natural::*;
2use crate::traits::*;
3use algebraeon_sets::structure::*;
4use malachite_base::num::basic::traits::{One, Two, Zero};
5use std::{
6 ops::{Add, AddAssign, Div, Mul, MulAssign, Neg, Rem, Sub, SubAssign},
7 str::FromStr,
8};
9
10#[derive(Default, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11pub struct Integer(malachite_nz::integer::Integer);
12
13impl Integer {
14 pub(crate) fn from_malachite(value: malachite_nz::integer::Integer) -> Self {
15 Self(value)
16 }
17 pub(crate) fn to_malachite(self) -> malachite_nz::integer::Integer {
18 self.0
19 }
20 pub(crate) fn to_malachite_ref(&self) -> &malachite_nz::integer::Integer {
21 &self.0
22 }
23}
24
25impl std::fmt::Display for Integer {
26 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27 self.0.fmt(f)
28 }
29}
30
31impl From<u8> for Integer {
32 fn from(value: u8) -> Self {
33 Self(malachite_nz::integer::Integer::from(value))
34 }
35}
36impl From<u16> for Integer {
37 fn from(value: u16) -> Self {
38 Self(malachite_nz::integer::Integer::from(value))
39 }
40}
41impl From<u32> for Integer {
42 fn from(value: u32) -> Self {
43 Self(malachite_nz::integer::Integer::from(value))
44 }
45}
46impl From<u64> for Integer {
47 fn from(value: u64) -> Self {
48 Self(malachite_nz::integer::Integer::from(value))
49 }
50}
51impl From<u128> for Integer {
52 fn from(value: u128) -> Self {
53 Self(malachite_nz::integer::Integer::from(value))
54 }
55}
56impl From<usize> for Integer {
57 fn from(value: usize) -> Self {
58 Self(malachite_nz::integer::Integer::from(value))
59 }
60}
61impl From<i8> for Integer {
62 fn from(value: i8) -> Self {
63 Self(malachite_nz::integer::Integer::from(value))
64 }
65}
66impl From<i16> for Integer {
67 fn from(value: i16) -> Self {
68 Self(malachite_nz::integer::Integer::from(value))
69 }
70}
71impl From<i32> for Integer {
72 fn from(value: i32) -> Self {
73 Self(malachite_nz::integer::Integer::from(value))
74 }
75}
76impl From<i64> for Integer {
77 fn from(value: i64) -> Self {
78 Self(malachite_nz::integer::Integer::from(value))
79 }
80}
81impl From<i128> for Integer {
82 fn from(value: i128) -> Self {
83 Self(malachite_nz::integer::Integer::from(value))
84 }
85}
86impl From<isize> for Integer {
87 fn from(value: isize) -> Self {
88 Self(malachite_nz::integer::Integer::from(value))
89 }
90}
91impl From<Natural> for Integer {
92 fn from(value: Natural) -> Self {
93 Self(malachite_nz::integer::Integer::from(value.to_malachite()))
94 }
95}
96impl From<&Natural> for Integer {
97 fn from(value: &Natural) -> Self {
98 Self(malachite_nz::integer::Integer::from(
99 value.to_malachite_ref(),
100 ))
101 }
102}
103
104impl Into<f64> for Integer {
105 fn into(self) -> f64 {
106 if self < Integer::ZERO {
107 -<Self as Into<f64>>::into(-self)
108 } else {
109 let limbs = self.to_malachite().into_twos_complement_limbs_asc();
110 let mut flt = 0.0;
111 for (i, k) in limbs.into_iter().enumerate() {
112 flt += (k as f64) * (2.0 as f64).powf(i as f64 * 64.0);
113 }
114 flt
115 }
116 }
117}
118impl Into<f64> for &Integer {
119 fn into(self) -> f64 {
120 self.clone().into()
121 }
122}
123
124impl FromStr for Integer {
125 type Err = ();
126
127 fn from_str(s: &str) -> Result<Self, Self::Err> {
128 Ok(Self(malachite_nz::integer::Integer::from_str(s)?))
129 }
130}
131
132impl Integer {
133 pub const ZERO: Self = Self(malachite_nz::integer::Integer::ZERO);
134 pub const ONE: Self = Self(malachite_nz::integer::Integer::ONE);
135 pub const TWO: Self = Self(malachite_nz::integer::Integer::TWO);
136}
137
138impl PartialEq<Natural> for Integer {
139 fn eq(&self, other: &Natural) -> bool {
140 self.0.eq(other.to_malachite_ref())
141 }
142}
143impl PartialEq<&Natural> for Integer {
144 fn eq(&self, other: &&Natural) -> bool {
145 self.eq(*other)
146 }
147}
148impl PartialOrd<Natural> for Integer {
149 fn partial_cmp(&self, other: &Natural) -> Option<std::cmp::Ordering> {
150 self.0.partial_cmp(other.to_malachite_ref())
151 }
152}
153impl PartialOrd<&Natural> for Integer {
154 fn partial_cmp(&self, other: &&Natural) -> Option<std::cmp::Ordering> {
155 self.partial_cmp(*other)
156 }
157}
158
159impl AddAssign<Integer> for Integer {
160 fn add_assign(&mut self, rhs: Integer) {
161 self.0.add_assign(rhs.0)
162 }
163}
164impl AddAssign<&Integer> for Integer {
165 fn add_assign(&mut self, rhs: &Integer) {
166 self.0.add_assign(&rhs.0)
167 }
168}
169
170impl SubAssign<Integer> for Integer {
171 fn sub_assign(&mut self, rhs: Integer) {
172 self.0.sub_assign(rhs.0)
173 }
174}
175impl SubAssign<&Integer> for Integer {
176 fn sub_assign(&mut self, rhs: &Integer) {
177 self.0.sub_assign(&rhs.0)
178 }
179}
180
181impl MulAssign<Integer> for Integer {
182 fn mul_assign(&mut self, rhs: Integer) {
183 self.0.mul_assign(rhs.0)
184 }
185}
186impl MulAssign<&Integer> for Integer {
187 fn mul_assign(&mut self, rhs: &Integer) {
188 self.0.mul_assign(&rhs.0)
189 }
190}
191
192impl Neg for Integer {
193 type Output = Integer;
194
195 fn neg(self) -> Self::Output {
196 Integer(self.0.neg())
197 }
198}
199impl Neg for &Integer {
200 type Output = Integer;
201
202 fn neg(self) -> Self::Output {
203 Integer((&self.0).neg())
204 }
205}
206
207impl Add<Integer> for Integer {
208 type Output = Integer;
209
210 fn add(self, rhs: Integer) -> Self::Output {
211 Integer(self.0.add(rhs.0))
212 }
213}
214impl Add<&Integer> for Integer {
215 type Output = Integer;
216
217 fn add(self, rhs: &Integer) -> Self::Output {
218 Integer(self.0.add(&rhs.0))
219 }
220}
221impl Add<Integer> for &Integer {
222 type Output = Integer;
223
224 fn add(self, rhs: Integer) -> Self::Output {
225 Integer((&self.0).add(rhs.0))
226 }
227}
228impl Add<&Integer> for &Integer {
229 type Output = Integer;
230
231 fn add(self, rhs: &Integer) -> Self::Output {
232 Integer((&self.0).add(&rhs.0))
233 }
234}
235
236impl Sub<Integer> for Integer {
237 type Output = Integer;
238
239 fn sub(self, rhs: Integer) -> Self::Output {
240 Integer(self.0.sub(rhs.0))
241 }
242}
243impl Sub<&Integer> for Integer {
244 type Output = Integer;
245
246 fn sub(self, rhs: &Integer) -> Self::Output {
247 Integer(self.0.sub(&rhs.0))
248 }
249}
250impl Sub<Integer> for &Integer {
251 type Output = Integer;
252
253 fn sub(self, rhs: Integer) -> Self::Output {
254 Integer((&self.0).sub(rhs.0))
255 }
256}
257impl Sub<&Integer> for &Integer {
258 type Output = Integer;
259
260 fn sub(self, rhs: &Integer) -> Self::Output {
261 Integer((&self.0).sub(&rhs.0))
262 }
263}
264
265impl Mul<Integer> for Integer {
266 type Output = Integer;
267
268 fn mul(self, rhs: Integer) -> Self::Output {
269 Integer(self.0.mul(rhs.0))
270 }
271}
272impl Mul<&Integer> for Integer {
273 type Output = Integer;
274
275 fn mul(self, rhs: &Integer) -> Self::Output {
276 Integer(self.0.mul(&rhs.0))
277 }
278}
279impl Mul<Integer> for &Integer {
280 type Output = Integer;
281
282 fn mul(self, rhs: Integer) -> Self::Output {
283 Integer((&self.0).mul(rhs.0))
284 }
285}
286impl Mul<&Integer> for &Integer {
287 type Output = Integer;
288
289 fn mul(self, rhs: &Integer) -> Self::Output {
290 Integer((&self.0).mul(&rhs.0))
291 }
292}
293
294impl Rem<Integer> for Integer {
295 type Output = Integer;
296
297 fn rem(self, rhs: Integer) -> Self::Output {
298 use malachite_base::num::arithmetic::traits::Mod;
299 Integer(self.0.mod_op(rhs.0))
300 }
301}
302impl Rem<&Integer> for Integer {
303 type Output = Integer;
304
305 fn rem(self, rhs: &Integer) -> Self::Output {
306 use malachite_base::num::arithmetic::traits::Mod;
307 Integer(self.0.mod_op(&rhs.0))
308 }
309}
310impl Rem<Integer> for &Integer {
311 type Output = Integer;
312
313 fn rem(self, rhs: Integer) -> Self::Output {
314 use malachite_base::num::arithmetic::traits::Mod;
315 Integer((&self.0).mod_op(rhs.0))
316 }
317}
318impl Rem<&Integer> for &Integer {
319 type Output = Integer;
320
321 fn rem(self, rhs: &Integer) -> Self::Output {
322 use malachite_base::num::arithmetic::traits::Mod;
323 Integer((&self.0).mod_op(&rhs.0))
324 }
325}
326
327impl Rem<Natural> for Integer {
328 type Output = Natural;
329
330 fn rem(self, rhs: Natural) -> Self::Output {
331 self.rem(Integer::from(rhs)).abs()
332 }
333}
334impl Rem<&Natural> for Integer {
335 type Output = Natural;
336
337 fn rem(self, rhs: &Natural) -> Self::Output {
338 self.rem(Integer::from(rhs)).abs()
339 }
340}
341impl Rem<Natural> for &Integer {
342 type Output = Natural;
343
344 fn rem(self, rhs: Natural) -> Self::Output {
345 self.rem(Integer::from(rhs)).abs()
346 }
347}
348impl Rem<&Natural> for &Integer {
349 type Output = Natural;
350
351 fn rem(self, rhs: &Natural) -> Self::Output {
352 self.rem(Integer::from(rhs)).abs()
353 }
354}
355
356impl Div<Integer> for Integer {
357 type Output = Integer;
358
359 fn div(self, rhs: Integer) -> Self::Output {
360 Integer(self.0.div(rhs.0))
361 }
362}
363impl Div<&Integer> for Integer {
364 type Output = Integer;
365
366 fn div(self, rhs: &Integer) -> Self::Output {
367 Integer(self.0.div(&rhs.0))
368 }
369}
370impl Div<Integer> for &Integer {
371 type Output = Integer;
372
373 fn div(self, rhs: Integer) -> Self::Output {
374 Integer((&self.0).div(rhs.0))
375 }
376}
377impl Div<&Integer> for &Integer {
378 type Output = Integer;
379
380 fn div(self, rhs: &Integer) -> Self::Output {
381 Integer((&self.0).div(&rhs.0))
382 }
383}
384
385impl DivMod<Integer> for Integer {
386 type DivOutput = Integer;
387
388 type ModOutput = Integer;
389
390 fn div_mod(self, other: Integer) -> (Self::DivOutput, Self::ModOutput) {
391 use malachite_base::num::arithmetic::traits::DivMod;
392 let (q, r) = self.0.div_mod(other.0);
393 (Integer(q), Integer(r))
394 }
395}
396impl DivMod<&Integer> for Integer {
397 type DivOutput = Integer;
398
399 type ModOutput = Integer;
400
401 fn div_mod(self, other: &Integer) -> (Self::DivOutput, Self::ModOutput) {
402 use malachite_base::num::arithmetic::traits::DivMod;
403 let (q, r) = self.0.div_mod(&other.0);
404 (Integer(q), Integer(r))
405 }
406}
407impl DivMod<Integer> for &Integer {
408 type DivOutput = Integer;
409
410 type ModOutput = Integer;
411
412 fn div_mod(self, other: Integer) -> (Self::DivOutput, Self::ModOutput) {
413 use malachite_base::num::arithmetic::traits::DivMod;
414 let (q, r) = (&self.0).div_mod(other.0);
415 (Integer(q), Integer(r))
416 }
417}
418impl DivMod<&Integer> for &Integer {
419 type DivOutput = Integer;
420
421 type ModOutput = Integer;
422
423 fn div_mod(self, other: &Integer) -> (Self::DivOutput, Self::ModOutput) {
424 use malachite_base::num::arithmetic::traits::DivMod;
425 let (q, r) = (&self.0).div_mod(&other.0);
426 (Integer(q), Integer(r))
427 }
428}
429
430impl Abs for Integer {
431 type Output = Natural;
432 fn abs(self) -> Self::Output {
433 use malachite_base::num::arithmetic::traits::UnsignedAbs;
434 Natural::from_malachite(self.0.unsigned_abs())
435 }
436}
437
438impl Abs for &Integer {
439 type Output = Natural;
440 fn abs(self) -> Self::Output {
441 use malachite_base::num::arithmetic::traits::UnsignedAbs;
442 Natural::from_malachite((&self.0).unsigned_abs())
443 }
444}
445
446impl MetaType for Integer {
447 type Structure = CannonicalStructure<Integer>;
448
449 fn structure() -> std::rc::Rc<Self::Structure> {
450 CannonicalStructure::new().into()
451 }
452}