malachite_nz/integer_polynomial/arithmetic/neg.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_polynomial::IntegerPolynomial;
10use core::ops::Neg;
11use malachite_base::num::arithmetic::traits::NegAssign;
12
13impl Neg for IntegerPolynomial {
14 type Output = Self;
15
16 /// Negates an [`IntegerPolynomial`], taking it by value.
17 ///
18 /// $$
19 /// f(p) = -p.
20 /// $$
21 ///
22 /// # Worst-case complexity
23 /// $T(n) = O(n)$
24 ///
25 /// $M(n) = O(1)$
26 ///
27 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
28 ///
29 /// # Examples
30 /// ```
31 /// use core::str::FromStr;
32 /// use malachite_base::num::basic::traits::Zero;
33 /// use malachite_nz::integer_polynomial::IntegerPolynomial;
34 ///
35 /// let p = IntegerPolynomial::from_str("x^2-3*x+2").unwrap();
36 /// assert_eq!((-p).to_string(), "-x^2+3*x-2");
37 /// assert_eq!(-IntegerPolynomial::ZERO, IntegerPolynomial::ZERO);
38 /// ```
39 ///
40 /// This is equivalent to `fmpz_poly_neg` from `fmpz_poly/neg.c`, FLINT 3.6.0.
41 fn neg(mut self) -> Self {
42 self.neg_assign();
43 self
44 }
45}
46
47impl Neg for &IntegerPolynomial {
48 type Output = IntegerPolynomial;
49
50 /// Negates an [`IntegerPolynomial`], taking it by reference.
51 ///
52 /// $$
53 /// f(p) = -p.
54 /// $$
55 ///
56 /// # Worst-case complexity
57 /// $T(n) = O(n)$
58 ///
59 /// $M(n) = O(n)$
60 ///
61 /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
62 /// coefficients.
63 ///
64 /// # Examples
65 /// ```
66 /// use core::str::FromStr;
67 /// use malachite_base::num::basic::traits::Zero;
68 /// use malachite_nz::integer_polynomial::IntegerPolynomial;
69 ///
70 /// let p = IntegerPolynomial::from_str("x^2-3*x+2").unwrap();
71 /// assert_eq!((-&p).to_string(), "-x^2+3*x-2");
72 /// assert_eq!(-&IntegerPolynomial::ZERO, IntegerPolynomial::ZERO);
73 /// ```
74 ///
75 /// This is equivalent to `fmpz_poly_neg` from `fmpz_poly/neg.c`, FLINT 3.6.0.
76 fn neg(self) -> IntegerPolynomial {
77 IntegerPolynomial {
78 coefficients: self.coefficients.iter().map(|c| -c).collect(),
79 }
80 }
81}
82
83impl NegAssign for IntegerPolynomial {
84 /// Negates an [`IntegerPolynomial`] in place.
85 ///
86 /// $$
87 /// p \gets -p.
88 /// $$
89 ///
90 /// # Worst-case complexity
91 /// $T(n) = O(n)$
92 ///
93 /// $M(n) = O(1)$
94 ///
95 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.len()`.
96 ///
97 /// # Examples
98 /// ```
99 /// use core::str::FromStr;
100 /// use malachite_base::num::arithmetic::traits::NegAssign;
101 /// use malachite_nz::integer_polynomial::IntegerPolynomial;
102 ///
103 /// let mut p = IntegerPolynomial::from_str("x^2-3*x+2").unwrap();
104 /// p.neg_assign();
105 /// assert_eq!(p.to_string(), "-x^2+3*x-2");
106 /// ```
107 fn neg_assign(&mut self) {
108 for c in &mut self.coefficients {
109 c.neg_assign();
110 }
111 }
112}