typst_library/math/
matrix.rs

1use smallvec::{SmallVec, smallvec};
2use typst_syntax::Spanned;
3use typst_utils::{Numeric, default_math_class};
4use unicode_math_class::MathClass;
5
6use crate::diag::{At, HintedStrResult, StrResult, bail};
7use crate::foundations::{
8    Array, Content, Dict, Fold, NoneValue, Resolve, Smart, StyleChain, Symbol, Value,
9    array, cast, dict, elem,
10};
11use crate::layout::{Abs, Em, HAlignment, Length, Rel};
12use crate::math::Mathy;
13use crate::visualize::Stroke;
14
15const DEFAULT_ROW_GAP: Em = Em::new(0.2);
16const DEFAULT_COL_GAP: Em = Em::new(0.5);
17
18/// A column vector.
19///
20/// Content in the vector's elements can be aligned with the
21/// [`align`]($math.vec.align) parameter, or the `&` symbol.
22///
23/// This function is for typesetting vector components. To typeset a symbol that
24/// represents a vector, [`arrow`]($math.accent) and [`bold`]($math.bold) are
25/// commonly used.
26///
27/// # Example
28/// ```example
29/// $ vec(a, b, c) dot vec(1, 2, 3)
30///     = a + 2b + 3c $
31/// ```
32#[elem(title = "Vector", Mathy)]
33pub struct VecElem {
34    /// The delimiter to use.
35    ///
36    /// Can be a single character specifying the left delimiter, in which case
37    /// the right delimiter is inferred. Otherwise, can be an array containing a
38    /// left and a right delimiter.
39    ///
40    /// ```example
41    /// #set math.vec(delim: "[")
42    /// $ vec(1, 2) $
43    /// ```
44    #[default(DelimiterPair::PAREN)]
45    pub delim: DelimiterPair,
46
47    /// The horizontal alignment that each element should have.
48    ///
49    /// ```example
50    /// #set math.vec(align: right)
51    /// $ vec(-1, 1, -1) $
52    /// ```
53    #[default(HAlignment::Center)]
54    pub align: HAlignment,
55
56    /// The gap between elements.
57    ///
58    /// ```example
59    /// #set math.vec(gap: 1em)
60    /// $ vec(1, 2) $
61    /// ```
62    #[default(DEFAULT_ROW_GAP.into())]
63    pub gap: Rel<Length>,
64
65    /// The elements of the vector.
66    #[variadic]
67    pub children: Vec<Content>,
68}
69
70/// A matrix.
71///
72/// The elements of a row should be separated by commas, while the rows
73/// themselves should be separated by semicolons. The semicolon syntax merges
74/// preceding arguments separated by commas into an array. You can also use this
75/// special syntax of math function calls to define custom functions that take
76/// 2D data.
77///
78/// Content in cells can be aligned with the [`align`]($math.mat.align)
79/// parameter, or content in cells that are in the same row can be aligned with
80/// the `&` symbol.
81///
82/// # Example
83/// ```example
84/// $ mat(
85///   1, 2, ..., 10;
86///   2, 2, ..., 10;
87///   dots.v, dots.v, dots.down, dots.v;
88///   10, 10, ..., 10;
89/// ) $
90/// ```
91#[elem(title = "Matrix", Mathy)]
92pub struct MatElem {
93    /// The delimiter to use.
94    ///
95    /// Can be a single character specifying the left delimiter, in which case
96    /// the right delimiter is inferred. Otherwise, can be an array containing a
97    /// left and a right delimiter.
98    ///
99    /// ```example
100    /// #set math.mat(delim: "[")
101    /// $ mat(1, 2; 3, 4) $
102    /// ```
103    #[default(DelimiterPair::PAREN)]
104    pub delim: DelimiterPair,
105
106    /// The horizontal alignment that each cell should have.
107    ///
108    /// ```example
109    /// #set math.mat(align: right)
110    /// $ mat(-1, 1, 1; 1, -1, 1; 1, 1, -1) $
111    /// ```
112    #[default(HAlignment::Center)]
113    pub align: HAlignment,
114
115    /// Draws augmentation lines in a matrix.
116    ///
117    /// - `{none}`: No lines are drawn.
118    /// - A single number: A vertical augmentation line is drawn
119    ///   after the specified column number. Negative numbers start from the end.
120    /// - A dictionary: With a dictionary, multiple augmentation lines can be
121    ///   drawn both horizontally and vertically. Additionally, the style of the
122    ///   lines can be set. The dictionary can contain the following keys:
123    ///   - `hline`: The offsets at which horizontal lines should be drawn.
124    ///     For example, an offset of `2` would result in a horizontal line
125    ///     being drawn after the second row of the matrix. Accepts either an
126    ///     integer for a single line, or an array of integers
127    ///     for multiple lines. Like for a single number, negative numbers start from the end.
128    ///   - `vline`: The offsets at which vertical lines should be drawn.
129    ///     For example, an offset of `2` would result in a vertical line being
130    ///     drawn after the second column of the matrix. Accepts either an
131    ///     integer for a single line, or an array of integers
132    ///     for multiple lines. Like for a single number, negative numbers start from the end.
133    ///   - `stroke`: How to [stroke]($stroke) the line. If set to `{auto}`,
134    ///     takes on a thickness of 0.05 em and square line caps.
135    ///
136    /// ```example:"Basic usage"
137    /// $ mat(1, 0, 1; 0, 1, 2; augment: #2) $
138    /// // Equivalent to:
139    /// $ mat(1, 0, 1; 0, 1, 2; augment: #(-1)) $
140    /// ```
141    ///
142    /// ```example:"Customizing the augmentation line"
143    /// $ mat(0, 0, 0; 1, 1, 1; augment: #(hline: 1, stroke: 2pt + green)) $
144    /// ```
145    #[fold]
146    pub augment: Option<Augment>,
147
148    /// The gap between rows and columns.
149    ///
150    /// This is a shorthand to set `row-gap` and `column-gap` to the same value.
151    ///
152    /// ```example
153    /// #set math.mat(gap: 1em)
154    /// $ mat(1, 2; 3, 4) $
155    /// ```
156    #[external]
157    pub gap: Rel<Length>,
158
159    /// The gap between rows.
160    ///
161    /// ```example
162    /// #set math.mat(row-gap: 1em)
163    /// $ mat(1, 2; 3, 4) $
164    /// ```
165    #[parse(
166        let gap = args.named("gap")?;
167        args.named("row-gap")?.or(gap)
168    )]
169    #[default(DEFAULT_ROW_GAP.into())]
170    pub row_gap: Rel<Length>,
171
172    /// The gap between columns.
173    ///
174    /// ```example
175    /// #set math.mat(column-gap: 1em)
176    /// $ mat(1, 2; 3, 4) $
177    /// ```
178    #[parse(args.named("column-gap")?.or(gap))]
179    #[default(DEFAULT_COL_GAP.into())]
180    pub column_gap: Rel<Length>,
181
182    /// An array of arrays with the rows of the matrix.
183    ///
184    /// ```example
185    /// #let data = ((1, 2, 3), (4, 5, 6))
186    /// #let matrix = math.mat(..data)
187    /// $ v := matrix $
188    /// ```
189    #[variadic]
190    #[parse(
191        let mut rows = vec![];
192        let mut width = 0;
193
194        let values = args.all::<Spanned<Value>>()?;
195        if values.iter().any(|spanned| matches!(spanned.v, Value::Array(_))) {
196            for Spanned { v, span } in values {
197                let array = v.cast::<Array>().at(span)?;
198                let row: Vec<_> = array.into_iter().map(Value::display).collect();
199                width = width.max(row.len());
200                rows.push(row);
201            }
202        } else {
203            rows = vec![values.into_iter().map(|spanned| spanned.v.display()).collect()];
204        }
205
206        for row in &mut rows {
207            if row.len() < width {
208                row.resize(width, Content::empty());
209            }
210        }
211
212        rows
213    )]
214    pub rows: Vec<Vec<Content>>,
215}
216
217/// A case distinction.
218///
219/// Content across different branches can be aligned with the `&` symbol.
220///
221/// # Example
222/// ```example
223/// $ f(x, y) := cases(
224///   1 "if" (x dot y)/2 <= 0,
225///   2 "if" x "is even",
226///   3 "if" x in NN,
227///   4 "else",
228/// ) $
229/// ```
230#[elem(Mathy)]
231pub struct CasesElem {
232    /// The delimiter to use.
233    ///
234    /// Can be a single character specifying the left delimiter, in which case
235    /// the right delimiter is inferred. Otherwise, can be an array containing a
236    /// left and a right delimiter.
237    ///
238    /// ```example
239    /// #set math.cases(delim: "[")
240    /// $ x = cases(1, 2) $
241    /// ```
242    #[default(DelimiterPair::BRACE)]
243    pub delim: DelimiterPair,
244
245    /// Whether the direction of cases should be reversed.
246    ///
247    /// ```example
248    /// #set math.cases(reverse: true)
249    /// $ cases(1, 2) = x $
250    /// ```
251    #[default(false)]
252    pub reverse: bool,
253
254    /// The gap between branches.
255    ///
256    /// ```example
257    /// #set math.cases(gap: 1em)
258    /// $ x = cases(1, 2) $
259    /// ```
260    #[default(DEFAULT_ROW_GAP.into())]
261    pub gap: Rel<Length>,
262
263    /// The branches of the case distinction.
264    #[variadic]
265    pub children: Vec<Content>,
266}
267
268/// A delimiter is a single character that is used to delimit a matrix, vector
269/// or cases. The character has to be a Unicode codepoint tagged as a math
270/// "opening", "closing" or "fence".
271///
272/// Typically, the delimiter is stretched to fit the height of whatever it
273/// delimits.
274#[derive(Debug, Default, Copy, Clone, Eq, PartialEq, Hash)]
275pub struct Delimiter(Option<char>);
276
277cast! {
278    Delimiter,
279    self => self.0.into_value(),
280    _: NoneValue => Self::none(),
281    v: Symbol => Self::char(v.get().parse::<char>().map_err(|_| "expected a single-codepoint symbol")?)?,
282    v: char => Self::char(v)?,
283}
284
285impl Delimiter {
286    pub fn none() -> Self {
287        Self(None)
288    }
289
290    pub fn char(c: char) -> StrResult<Self> {
291        if !matches!(
292            default_math_class(c),
293            Some(MathClass::Opening | MathClass::Closing | MathClass::Fence),
294        ) {
295            bail!("invalid delimiter: \"{}\"", c)
296        }
297        Ok(Self(Some(c)))
298    }
299
300    pub fn get(self) -> Option<char> {
301        self.0
302    }
303
304    pub fn find_matching(self) -> Self {
305        match self.0 {
306            None => Self::none(),
307            Some('[') => Self(Some(']')),
308            Some(']') => Self(Some('[')),
309            Some('{') => Self(Some('}')),
310            Some('}') => Self(Some('{')),
311            Some(c) => match default_math_class(c) {
312                Some(MathClass::Opening) => Self(char::from_u32(c as u32 + 1)),
313                Some(MathClass::Closing) => Self(char::from_u32(c as u32 - 1)),
314                _ => Self(Some(c)),
315            },
316        }
317    }
318}
319
320/// A pair of delimiters (one closing, one opening) used for matrices, vectors
321/// and cases.
322#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
323pub struct DelimiterPair {
324    open: Delimiter,
325    close: Delimiter,
326}
327
328cast! {
329    DelimiterPair,
330
331    self => array![self.open, self.close].into_value(),
332
333    v: Array => match v.as_slice() {
334        [open, close] => Self {
335            open: open.clone().cast()?,
336            close: close.clone().cast()?,
337        },
338        _ => bail!("expected 2 delimiters, found {}", v.len())
339    },
340    v: Delimiter => Self { open: v, close: v.find_matching() }
341}
342
343impl DelimiterPair {
344    const PAREN: Self = Self {
345        open: Delimiter(Some('(')),
346        close: Delimiter(Some(')')),
347    };
348    const BRACE: Self = Self {
349        open: Delimiter(Some('{')),
350        close: Delimiter(Some('}')),
351    };
352
353    /// The delimiter's opening character.
354    pub fn open(self) -> Option<char> {
355        self.open.get()
356    }
357
358    /// The delimiter's closing character.
359    pub fn close(self) -> Option<char> {
360        self.close.get()
361    }
362}
363
364/// Parameters specifying how augmentation lines
365/// should be drawn on a matrix.
366#[derive(Debug, Default, Clone, PartialEq, Hash)]
367pub struct Augment<T: Numeric = Length> {
368    pub hline: AugmentOffsets,
369    pub vline: AugmentOffsets,
370    pub stroke: Smart<Stroke<T>>,
371}
372
373impl<T: Numeric + Fold> Fold for Augment<T> {
374    fn fold(self, outer: Self) -> Self {
375        Self {
376            stroke: match (self.stroke, outer.stroke) {
377                (Smart::Custom(inner), Smart::Custom(outer)) => {
378                    Smart::Custom(inner.fold(outer))
379                }
380                // Usually, folding an inner `auto` with an `outer` prefers
381                // the explicit `auto`. However, here `auto` means unspecified
382                // and thus we want `outer`.
383                (inner, outer) => inner.or(outer),
384            },
385            ..self
386        }
387    }
388}
389
390impl Resolve for Augment {
391    type Output = Augment<Abs>;
392
393    fn resolve(self, styles: StyleChain) -> Self::Output {
394        Augment {
395            hline: self.hline,
396            vline: self.vline,
397            stroke: self.stroke.resolve(styles),
398        }
399    }
400}
401
402cast! {
403    Augment,
404    self => {
405        // if the stroke is auto and there is only one vertical line,
406        if self.stroke.is_auto() && self.hline.0.is_empty() && self.vline.0.len() == 1 {
407            return self.vline.0[0].into_value();
408        }
409
410        dict! {
411            "hline" => self.hline,
412            "vline" => self.vline,
413            "stroke" => self.stroke,
414        }.into_value()
415    },
416    v: isize => Augment {
417        hline: AugmentOffsets::default(),
418        vline: AugmentOffsets(smallvec![v]),
419        stroke: Smart::Auto,
420    },
421    mut dict: Dict => {
422        let mut take = |key| dict.take(key).ok().map(AugmentOffsets::from_value).transpose();
423        let hline = take("hline")?.unwrap_or_default();
424        let vline = take("vline")?.unwrap_or_default();
425        let stroke = dict.take("stroke")
426            .ok()
427            .map(Stroke::from_value)
428            .transpose()?
429            .map(Smart::Custom)
430            .unwrap_or(Smart::Auto);
431        Augment { hline, vline, stroke }
432    },
433}
434
435cast! {
436    Augment<Abs>,
437    self => self.into_value(),
438}
439
440/// The offsets at which augmentation lines should be drawn on a matrix.
441#[derive(Debug, Default, Clone, Eq, PartialEq, Hash)]
442pub struct AugmentOffsets(pub SmallVec<[isize; 1]>);
443
444cast! {
445    AugmentOffsets,
446    self => self.0.into_value(),
447    v: isize => Self(smallvec![v]),
448    v: Array => Self(v.into_iter().map(Value::cast).collect::<HintedStrResult<_>>()?),
449}