malachite_nz/gaussian_integer/conversion/imaginary_from.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::gaussian_integer::GaussianInteger;
10use crate::integer::Integer;
11use malachite_base::num::basic::traits::Zero;
12use malachite_base::num::conversion::traits::ImaginaryFrom;
13
14impl<T> ImaginaryFrom<T> for GaussianInteger
15where
16 Integer: From<T>,
17{
18 /// Converts a value of any type that converts to an [`Integer`] — including [`Integer`]
19 /// itself — to a purely imaginary [`GaussianInteger`].
20 ///
21 /// # Worst-case complexity
22 /// Same as the complexity of the corresponding [`Integer`] conversion.
23 ///
24 /// # Examples
25 /// ```
26 /// use malachite_base::num::conversion::traits::ImaginaryFrom;
27 /// use malachite_nz::gaussian_integer::GaussianInteger;
28 /// use malachite_nz::integer::Integer;
29 /// use malachite_nz::natural::Natural;
30 ///
31 /// assert_eq!(GaussianInteger::imaginary_from(123u32).to_string(), "123i");
32 /// assert_eq!(
33 /// GaussianInteger::imaginary_from(-123i64).to_string(),
34 /// "-123i"
35 /// );
36 /// assert_eq!(
37 /// GaussianInteger::imaginary_from(Natural::from(123u32)).to_string(),
38 /// "123i"
39 /// );
40 /// assert_eq!(
41 /// GaussianInteger::imaginary_from(Integer::from(-123)).to_string(),
42 /// "-123i"
43 /// );
44 /// ```
45 fn imaginary_from(x: T) -> Self {
46 Self {
47 real: Integer::ZERO,
48 imaginary: Integer::from(x),
49 }
50 }
51}