malachite_nz/integer_vector/arithmetic/canonicalize_sign.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer_vector::IntegerVector;
10use malachite_base::num::arithmetic::traits::{
11 CanonicalizeSign, CanonicalizeSignAssign, NegAssign,
12};
13use malachite_base::vector::Vector;
14
15impl CanonicalizeSign for IntegerVector {
16 type Output = Self;
17
18 /// Brings an [`IntegerVector`] into canonical sign form, taking the vector by value: negates it
19 /// if its pivot, its first nonzero element, is negative.
20 ///
21 /// A vector and its negation are the same up to multiplication by $\pm 1$, the units of the
22 /// integers, and this picks the one whose pivot is positive to represent both. A vector of
23 /// zeros has no pivot and is left alone.
24 ///
25 /// # Worst-case complexity
26 /// $T(n) = O(n)$
27 ///
28 /// $M(n) = O(1)$
29 ///
30 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
31 ///
32 /// # Examples
33 /// ```
34 /// use core::str::FromStr;
35 /// use malachite_base::num::arithmetic::traits::CanonicalizeSign;
36 /// use malachite_nz::integer_vector::IntegerVector;
37 ///
38 /// let v = IntegerVector::from_str("(0, -2, 3)").unwrap();
39 /// assert_eq!(v.canonicalize_sign().to_string(), "(0, 2, -3)");
40 /// let v = IntegerVector::from_str("(0, 2, -3)").unwrap();
41 /// assert_eq!(v.canonicalize_sign().to_string(), "(0, 2, -3)");
42 /// ```
43 #[inline]
44 fn canonicalize_sign(mut self) -> Self {
45 self.canonicalize_sign_assign();
46 self
47 }
48}
49
50impl CanonicalizeSign for &IntegerVector {
51 type Output = IntegerVector;
52
53 /// Brings an [`IntegerVector`] into canonical sign form, taking the vector by reference:
54 /// negates it if its pivot, its first nonzero element, is negative.
55 ///
56 /// See the documentation for the [`CanonicalizeSign`] implementation on [`IntegerVector`].
57 ///
58 /// # Worst-case complexity
59 /// $T(n) = O(n)$
60 ///
61 /// $M(n) = O(n)$
62 ///
63 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
64 /// elements.
65 ///
66 /// # Examples
67 /// ```
68 /// use core::str::FromStr;
69 /// use malachite_base::num::arithmetic::traits::CanonicalizeSign;
70 /// use malachite_nz::integer_vector::IntegerVector;
71 ///
72 /// let v = IntegerVector::from_str("(0, -2, 3)").unwrap();
73 /// assert_eq!((&v).canonicalize_sign().to_string(), "(0, 2, -3)");
74 /// ```
75 #[inline]
76 fn canonicalize_sign(self) -> IntegerVector {
77 if self.pivot().is_some_and(|x| *x < 0u32) {
78 -self
79 } else {
80 self.clone()
81 }
82 }
83}
84
85impl CanonicalizeSignAssign for IntegerVector {
86 /// Replaces an [`IntegerVector`] with its canonical sign form: negates it if its pivot, its
87 /// first nonzero element, is negative.
88 ///
89 /// See the documentation for the [`CanonicalizeSign`] implementation on [`IntegerVector`].
90 ///
91 /// # Worst-case complexity
92 /// $T(n) = O(n)$
93 ///
94 /// $M(n) = O(1)$
95 ///
96 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.dimension()`.
97 ///
98 /// # Examples
99 /// ```
100 /// use core::str::FromStr;
101 /// use malachite_base::num::arithmetic::traits::CanonicalizeSignAssign;
102 /// use malachite_nz::integer_vector::IntegerVector;
103 ///
104 /// let mut v = IntegerVector::from_str("(0, -2, 3)").unwrap();
105 /// v.canonicalize_sign_assign();
106 /// assert_eq!(v.to_string(), "(0, 2, -3)");
107 /// ```
108 #[inline]
109 fn canonicalize_sign_assign(&mut self) {
110 if self.pivot().is_some_and(|x| *x < 0u32) {
111 self.neg_assign();
112 }
113 }
114}