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