Skip to main content

malachite_q/rational_vector/arithmetic/
scalar_div.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::Rational;
10use crate::rational_vector::RationalVector;
11use core::ops::{Div, DivAssign};
12use malachite_base::num::basic::traits::{One, Zero};
13
14impl Div<Rational> for RationalVector {
15    type Output = Self;
16
17    /// Divides every element of a [`RationalVector`] by a [`Rational`], taking the vector by value
18    /// and the scalar by value.
19    ///
20    /// The quotient is taken element by element, so the result has the same dimension as the
21    /// vector.
22    ///
23    /// $$
24    /// f(v, c) = \frac{v}{c} = (v_0/c, v_1/c, \ldots, v_{n-1}/c).
25    /// $$
26    ///
27    /// # Worst-case complexity
28    /// $T(n) = O(n \log n \log\log n)$
29    ///
30    /// $M(n) = O(n \log n)$
31    ///
32    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
33    /// elements and of the scalar.
34    ///
35    /// # Panics
36    /// Panics if `c` is zero.
37    ///
38    /// # Examples
39    /// ```
40    /// use core::str::FromStr;
41    /// use malachite_q::Rational;
42    /// use malachite_q::rational_vector::RationalVector;
43    ///
44    /// let v = RationalVector::from_str("(1/2, -2/3, 3)").unwrap();
45    /// let c = Rational::from_signeds(3, 4);
46    /// assert_eq!((v / c).to_string(), "(2/3, -8/9, 4)");
47    /// ```
48    #[inline]
49    fn div(mut self, c: Rational) -> Self {
50        self /= &c;
51        self
52    }
53}
54
55impl Div<&Rational> for RationalVector {
56    type Output = Self;
57
58    /// Divides every element of a [`RationalVector`] by a [`Rational`], taking the vector by value
59    /// and the scalar by reference.
60    ///
61    /// See the documentation for the [`Div`] implementation that takes the vector and the scalar by
62    /// value for details.
63    ///
64    /// # Worst-case complexity
65    /// $T(n) = O(n \log n \log\log n)$
66    ///
67    /// $M(n) = O(n \log n)$
68    ///
69    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
70    /// elements and of the scalar.
71    ///
72    /// # Panics
73    /// Panics if `c` is zero.
74    ///
75    /// # Examples
76    /// ```
77    /// use core::str::FromStr;
78    /// use malachite_q::Rational;
79    /// use malachite_q::rational_vector::RationalVector;
80    ///
81    /// let v = RationalVector::from_str("(1/2, -2/3, 3)").unwrap();
82    /// let c = Rational::from_signeds(3, 4);
83    /// assert_eq!((v / &c).to_string(), "(2/3, -8/9, 4)");
84    /// ```
85    #[inline]
86    fn div(mut self, c: &Rational) -> Self {
87        self /= c;
88        self
89    }
90}
91
92impl Div<Rational> for &RationalVector {
93    type Output = RationalVector;
94
95    /// Divides every element of a [`RationalVector`] by a [`Rational`], taking the vector by
96    /// reference and the scalar by value.
97    ///
98    /// See the documentation for the [`Div`] implementation that takes the vector and the scalar by
99    /// value for details.
100    ///
101    /// # Worst-case complexity
102    /// $T(n) = O(n \log n \log\log n)$
103    ///
104    /// $M(n) = O(n \log n)$
105    ///
106    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
107    /// elements and of the scalar.
108    ///
109    /// # Panics
110    /// Panics if `c` is zero.
111    ///
112    /// # Examples
113    /// ```
114    /// use core::str::FromStr;
115    /// use malachite_q::Rational;
116    /// use malachite_q::rational_vector::RationalVector;
117    ///
118    /// let v = RationalVector::from_str("(1/2, -2/3, 3)").unwrap();
119    /// let c = Rational::from_signeds(3, 4);
120    /// assert_eq!((&v / c).to_string(), "(2/3, -8/9, 4)");
121    /// ```
122    #[inline]
123    fn div(self, c: Rational) -> RationalVector {
124        self / &c
125    }
126}
127
128impl Div<&Rational> for &RationalVector {
129    type Output = RationalVector;
130
131    /// Divides every element of a [`RationalVector`] by a [`Rational`], taking the vector by
132    /// reference and the scalar by reference.
133    ///
134    /// See the documentation for the [`Div`] implementation that takes the vector and the scalar by
135    /// value for details.
136    ///
137    /// # Worst-case complexity
138    /// $T(n) = O(n \log n \log\log n)$
139    ///
140    /// $M(n) = O(n \log n)$
141    ///
142    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
143    /// elements and of the scalar.
144    ///
145    /// # Panics
146    /// Panics if `c` is zero.
147    ///
148    /// # Examples
149    /// ```
150    /// use core::str::FromStr;
151    /// use malachite_q::Rational;
152    /// use malachite_q::rational_vector::RationalVector;
153    ///
154    /// let v = RationalVector::from_str("(1/2, -2/3, 3)").unwrap();
155    /// let c = Rational::from_signeds(3, 4);
156    /// assert_eq!((&v / &c).to_string(), "(2/3, -8/9, 4)");
157    /// ```
158    fn div(self, c: &Rational) -> RationalVector {
159        RationalVector {
160            elements: match *c {
161                Rational::ZERO => panic!("division by zero"),
162                Rational::ONE => self.elements.clone(),
163                // Each quotient is reduced by the cross-cancellation of `Rational`'s `Div`.
164                _ => self.elements.iter().map(|x| x / c).collect(),
165            },
166        }
167    }
168}
169
170impl DivAssign<Rational> for RationalVector {
171    /// Divides every element of a [`RationalVector`] by a [`Rational`] in place, taking the scalar
172    /// by value.
173    ///
174    /// See the documentation for the [`Div`] implementation that takes the vector and the scalar by
175    /// value for details.
176    ///
177    /// # Worst-case complexity
178    /// $T(n) = O(n \log n \log\log n)$
179    ///
180    /// $M(n) = O(n \log n)$
181    ///
182    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
183    /// elements and of the scalar.
184    ///
185    /// # Panics
186    /// Panics if `c` is zero.
187    ///
188    /// # Examples
189    /// ```
190    /// use core::str::FromStr;
191    /// use malachite_q::Rational;
192    /// use malachite_q::rational_vector::RationalVector;
193    ///
194    /// let mut v = RationalVector::from_str("(1/2, -2/3, 3)").unwrap();
195    /// v /= Rational::from_signeds(3, 4);
196    /// assert_eq!(v.to_string(), "(2/3, -8/9, 4)");
197    /// ```
198    #[inline]
199    fn div_assign(&mut self, c: Rational) {
200        *self /= &c;
201    }
202}
203
204impl DivAssign<&Rational> for RationalVector {
205    /// Divides every element of a [`RationalVector`] by a [`Rational`] in place, taking the scalar
206    /// by reference.
207    ///
208    /// See the documentation for the [`Div`] implementation that takes the vector and the scalar by
209    /// value for details.
210    ///
211    /// # Worst-case complexity
212    /// $T(n) = O(n \log n \log\log n)$
213    ///
214    /// $M(n) = O(n \log n)$
215    ///
216    /// where $T$ is time, $M$ is additional memory, and $n$ is the total number of bits of the
217    /// elements and of the scalar.
218    ///
219    /// # Panics
220    /// Panics if `c` is zero.
221    ///
222    /// # Examples
223    /// ```
224    /// use core::str::FromStr;
225    /// use malachite_q::Rational;
226    /// use malachite_q::rational_vector::RationalVector;
227    ///
228    /// let mut v = RationalVector::from_str("(1/2, -2/3, 3)").unwrap();
229    /// v /= &Rational::from_signeds(3, 4);
230    /// assert_eq!(v.to_string(), "(2/3, -8/9, 4)");
231    /// ```
232    fn div_assign(&mut self, c: &Rational) {
233        match *c {
234            Rational::ZERO => panic!("division by zero"),
235            Rational::ONE => {}
236            _ => {
237                for x in &mut self.elements {
238                    *x /= c;
239                }
240            }
241        }
242    }
243}