malachite_nz/gaussian_integer/conversion/string/typst.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::{
10 ComparableGaussianInteger, ComparableGaussianIntegerRef, GaussianInteger,
11};
12use core::fmt::{Display, Formatter, Result};
13use malachite_base::strings::typst::ToTypst;
14
15impl ToTypst for GaussianInteger {
16 /// Writes a [`GaussianInteger`] as a Typst math-mode fragment.
17 ///
18 /// The fragment is what [`Display`] gives, which is already how a Gaussian integer is written
19 /// in mathematics: a value with a zero imaginary part is its real part alone, a purely
20 /// imaginary value is a coefficient directly followed by `i` with coefficients of 1 and -1
21 /// elided, and otherwise the real term comes first and the imaginary term follows with a
22 /// joining sign.
23 ///
24 /// The imaginary unit is written as a plain `i`, which Typst sets in italics, as most
25 /// mathematical writing does. An upright one would need a spelling of its own, and would not
26 /// match what [`Display`] gives.
27 ///
28 /// # Worst-case complexity
29 /// $T(n) = O(n (\log n)^2 \log\log n)$
30 ///
31 /// $M(n) = O(n \log n)$
32 ///
33 /// where $T$ is time, $M$ is additional memory, and $n$ is the maximum number of significant
34 /// bits of the real and imaginary parts.
35 ///
36 /// # Examples
37 /// ```
38 /// use malachite_base::num::conversion::traits::ImaginaryFrom;
39 /// use malachite_base::strings::typst::ToTypst;
40 /// use malachite_nz::gaussian_integer::GaussianInteger;
41 ///
42 /// assert_eq!(GaussianInteger::default().to_typst_string(), "0");
43 /// assert_eq!(GaussianInteger::from(2).to_typst_string(), "2");
44 /// assert_eq!(GaussianInteger::from(-2).to_typst_string(), "-2");
45 /// assert_eq!(GaussianInteger::imaginary_from(1).to_typst_string(), "i");
46 /// assert_eq!(GaussianInteger::imaginary_from(-1).to_typst_string(), "-i");
47 /// assert_eq!(GaussianInteger::imaginary_from(2).to_typst_string(), "2i");
48 /// ```
49 ///
50 /// | value | fragment |
51 /// |--------------------------------------|----------|
52 /// | `GaussianInteger::default()` | `0` |
53 /// | `GaussianInteger::from(-2)` | `-2` |
54 /// | `GaussianInteger::imaginary_from(1)` | `i` |
55 /// | `GaussianInteger::imaginary_from(2)` | `2i` |
56 #[inline]
57 fn fmt_typst(&self, f: &mut Formatter) -> Result {
58 Display::fmt(self, f)
59 }
60}
61
62impl ToTypst for ComparableGaussianInteger {
63 /// Writes a [`ComparableGaussianInteger`] as a Typst math-mode fragment.
64 ///
65 /// The fragment is the wrapped [`GaussianInteger`]'s own: the wrapper exists to give an
66 /// ordering, and does not change what the value is.
67 ///
68 /// # Worst-case complexity
69 /// Same as the time and additional memory complexity of `fmt_typst` for [`GaussianInteger`].
70 ///
71 /// # Examples
72 /// ```
73 /// use malachite_base::strings::typst::ToTypst;
74 /// use malachite_nz::gaussian_integer::{ComparableGaussianInteger, GaussianInteger};
75 ///
76 /// let x = GaussianInteger::from(2);
77 /// assert_eq!(
78 /// ComparableGaussianInteger(x.clone()).to_typst_string(),
79 /// x.to_typst_string()
80 /// );
81 /// ```
82 #[inline]
83 fn fmt_typst(&self, f: &mut Formatter) -> Result {
84 self.0.fmt_typst(f)
85 }
86}
87
88impl ToTypst for ComparableGaussianIntegerRef<'_> {
89 /// Writes a [`ComparableGaussianIntegerRef`] as a Typst math-mode fragment.
90 ///
91 /// The fragment is the wrapped [`GaussianInteger`]'s own: the wrapper exists to give an
92 /// ordering, and does not change what the value is.
93 ///
94 /// # Worst-case complexity
95 /// Same as the time and additional memory complexity of `fmt_typst` for [`GaussianInteger`].
96 ///
97 /// # Examples
98 /// ```
99 /// use malachite_base::strings::typst::ToTypst;
100 /// use malachite_nz::gaussian_integer::{ComparableGaussianIntegerRef, GaussianInteger};
101 ///
102 /// let x = GaussianInteger::from(2);
103 /// assert_eq!(
104 /// ComparableGaussianIntegerRef(&x).to_typst_string(),
105 /// x.to_typst_string()
106 /// );
107 /// ```
108 #[inline]
109 fn fmt_typst(&self, f: &mut Formatter) -> Result {
110 self.0.fmt_typst(f)
111 }
112}