Skip to main content

malachite_base/unsigned_polynomial/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::num::basic::unsigneds::PrimitiveUnsigned;
10use crate::strings::typst::ToTypst;
11use crate::unsigned_polynomial::UnsignedPolynomial;
12use crate::unsigned_polynomial::conversion::string::to_string::Language;
13use crate::vars::Var;
14use crate::vars::xyz::XyzVars;
15use core::fmt::{Formatter, Result};
16
17impl<T: PrimitiveUnsigned> ToTypst for UnsignedPolynomial<T> {
18    /// Writes a [`UnsignedPolynomial`] as a Typst math-mode fragment.
19    ///
20    /// The variable is called `x`.
21    /// [`to_typst_string_with`](crate::polynomial::Polynomial::to_typst_string_with) is the way to
22    /// call it something else.
23    ///
24    /// The fragment is the polynomial as it would be written by hand: the terms in order of
25    /// decreasing degree, joined with `+`, each one its coefficient followed by its variable and
26    /// then a superscript. A coefficient of 1 is left off, and so is an exponent of 1; the constant
27    /// term is its coefficient alone, and the zero polynomial, which has no terms, is `0`. Nothing
28    /// stands between a coefficient and its variable, since a number written against a variable can
29    /// only be multiplying it, and a digit ends the run of letters that Typst would otherwise read
30    /// as one name.
31    ///
32    /// A superscript is parenthesized only when the exponent has more than one digit, since a
33    /// superscript of one character needs nothing to hold it together.
34    ///
35    /// # Worst-case complexity
36    /// $T(n) = O(n \log n \log\log n)$
37    ///
38    /// $M(n) = O(n \log n)$
39    ///
40    /// where $T$ is time, $M$ is additional memory, and $n$ is the sum of the bits of the
41    /// coefficients.
42    ///
43    /// # Examples
44    /// ```
45    /// use core::str::FromStr;
46    /// use malachite_base::strings::typst::ToTypst;
47    /// use malachite_base::unsigned_polynomial::UnsignedPolynomial;
48    ///
49    /// assert_eq!(
50    ///     UnsignedPolynomial::<u64>::from_str("x^2+3*x+2")
51    ///         .unwrap()
52    ///         .to_typst_string(),
53    ///     "x^2+3x+2"
54    /// );
55    /// assert_eq!(
56    ///     UnsignedPolynomial::<u64>::from_str("0")
57    ///         .unwrap()
58    ///         .to_typst_string(),
59    ///     "0"
60    /// );
61    /// assert_eq!(
62    ///     UnsignedPolynomial::<u64>::from_str("5")
63    ///         .unwrap()
64    ///         .to_typst_string(),
65    ///     "5"
66    /// );
67    /// assert_eq!(
68    ///     UnsignedPolynomial::<u64>::from_str("x")
69    ///         .unwrap()
70    ///         .to_typst_string(),
71    ///     "x"
72    /// );
73    /// assert_eq!(
74    ///     UnsignedPolynomial::<u64>::from_str("2*x^3")
75    ///         .unwrap()
76    ///         .to_typst_string(),
77    ///     "2x^3"
78    /// );
79    ///
80    /// // An exponent of more than one digit is parenthesized.
81    /// assert_eq!(
82    ///     UnsignedPolynomial::<u64>::from_str("x^12+x^2")
83    ///         .unwrap()
84    ///         .to_typst_string(),
85    ///     "x^(12)+x^2"
86    /// );
87    /// ```
88    ///
89    /// The value column holds each polynomial as [`Display`](core::fmt::Display) writes it.
90    ///
91    /// | value       | fragment     |
92    /// |-------------|--------------|
93    /// | `x^2+3*x+2` | `x^2+3x+2`   |
94    /// | `0`         | `0`          |
95    /// | `5`         | `5`          |
96    /// | `x`         | `x`          |
97    /// | `2*x^3`     | `2x^3`       |
98    /// | `x^12+x^2`  | `x^(12)+x^2` |
99    #[inline]
100    fn fmt_typst(&self, f: &mut Formatter) -> Result {
101        self.write_with_var(Var::new(&XyzVars, 0), Language::Typst, f)
102    }
103}