% ^^A  ================================== 元注释 ==================================
% \iffalse meta-comment
%
% 文件：lt3matrix.dtx
%
% 版权所有 (C) 2026 Ms_yam
%
% 本文件可以依据 LaTeX 项目公共许可证（LPPL）1.3c
% 或（由你选择）任何更高版本的条款进行分发和/或修改。
% 该许可证的最新版本位于
%
%    http://www.latex-project.org/lppl.txt
%
% 本文件的维护状态为“维护中”。
%
% 当前维护者：Ms_yam
% 
% 错误报告及维护联系：ms_yam@163.com
%
% \fi
%
% ^^A ================================== 安装信息 ==================================
% \iffalse 
%<*install>
\begingroup

\input docstrip.tex
\keepsilent
\askforoverwritefalse

\preamble
\endpreamble

\postamble
\endpostamble

\generate{
    \file{\jobname.ins} {\from{\jobname.dtx}{install}}
    \file{\jobname.sty} {\from{\jobname.dtx}{package}}   
    \let\MetaPrefix\relax
    \def\MetaPrefix{} 
    \nopreamble\nopostamble
    \file{README-CN.md} {\from{\jobname.dtx}{readme.cn}}
    \file{README.md}    {\from{\jobname.dtx}{readme}}
  }
  
\ifx\documentclass\@undefined
  \endbatchfile
\fi

\endgroup
%</install>
% \fi
%
% ^^A ================================== 文档驱动 ==================================
% \iffalse
%<*driver>
\documentclass[full]{l3doc}
\usepackage{lt3matrix}

% ^^A 添加中文支持及设置超级链接
\usepackage[UTF8,heading]{ctex} 
\hypersetup
  { 
    colorlinks,
    linkcolor=blue,
    hyperindex,
    pdfstartview=FitH,
    plainpages=false,
    backref
  }

% ^^A 引用待办包
\usepackage{todo}

% ^^A 汉化 l3doc 的部分定义
\ExplSyntaxOn
\tl_new:N   \g_tex_hackers_note_tl
\cs_new:Npn \changetocn 
  { \tl_gset:Nn \g_tex_hackers_note_tl { \TeX{}~黑客笔记：  } }
\cs_new:Npn \changetoen 
  { \tl_gset:Nn \g_tex_hackers_note_tl { \TeX{}~hackers~note: } }
\changetocn 
\DeclareDocumentEnvironment { texnote } { }
  {
    \endgraf
    \vspace{0.5em}% 3mm => 0.5em
    \small\textbf{\g_tex_hackers_note_tl}% \TeX~hackers~note:
  }
  { \vspace{0.5em} } % 3mm => 0.5em
\ExplSyntaxOff

% ^^A 不建索引的命令
\DoNotIndex{ \group_begin:, \group_end: }

\begin{document}
  \DocInput{\jobname.dtx}
\end{document}
%</driver>
% \fi
%
% ^^A ================================ RADME ME ================================
% \iffalse 
%<*readme.cn> ^^A README
# 说明

一个轻量级 LaTeX 矩阵宏包，主要用于 TeX 文档中的矩阵演示、数值示例和几何变换。

## 简介

`lt3matrix` 提供一种轻量矩阵类型。使用者可以创建矩阵、设置矩阵元素、
执行矩阵运算、遍历元素，并将矩阵排版到数学公式中。

矩阵具有确定的行数和列数。通常矩阵创建后维度不应修改，宏包不提供直接修改维度的接口；
但当矩阵作为运算结果时，维度会根据运算结果自动更新。矩阵元素可通过赋值命令修改。
矩阵的创建和修改均为全局操作：在 TeX 分组内执行修改命令，分组结束后矩阵内容不会自动恢复。

空矩阵也具有明确的行数和列数，但不包含任何元素。行数和列数不能为负数。

在几何变换中，用户可通过叠加基础变换构建完整变换。
内部实现使用齐次变换矩阵，但用户无需任何相关知识。

应用变换时，通过变换矩阵动态定义变换函数，再用该函数快速计算变换后的坐标。
每次执行变换只执行对应四则运算（由 `fp` 模块执行），无需重复从矩阵中获取数据，
从而大幅提高运算速度。

## AI 使用声明

本项目在开发和文档编写过程中使用了 AI 辅助。

- **代码**：代码由人工编写和修改。AI 用于辅助发现潜在问题并提供分析建议。
所有代码修改和最终决策均由作者完成。
- **文档**：AI 用于协助起草文档。中文版由作者审核和修订，英文版则由 AI 根据中文版翻译。

如中英文文档存在差异，以中文版为准。

## 许可

版权所有 (C) 2026 Ms_yam

本文件可以依据 LaTeX 项目公共许可证（LPPL）1.3c
或（由你选择）任何更高版本的条款进行分发和/或修改。

该许可证的最新版本位于：

http://www.latex-project.org/lppl.txt

本文件的维护状态为“维护中”。

当前维护者：Ms_yam

错误报告及维护联系：ms_yam@163.com

## 编译

本宏包设计为单文件包，可以直接使用以下命令编译：

```sh
# 提取宏包文件
xetex matrix.dtx

# 编译文档
xelatex matrix.dtx
```
%</readme.cn>
%<*readme>
# lt3matrix

A lightweight matrix package for LaTeX, intended mainly for matrix 
demonstrations, numerical examples, and geometric transformations 
in TeX documents.

## Overview

`lt3matrix` provides a lightweight matrix type. Users can create 
matrices, set matrix entries, perform matrix operations, iterate 
over entries, and typeset matrices in mathematical formulas.

Matrices have a fixed number of rows and columns. Normally the 
dimensions should not be changed after creation, and the package
does not provide an interface for directly modifying them; 
however, when a matrix is the result of an operation, its 
dimensions are updated automatically according to the result. 
Matrix entries can be modified via assignment commands. Both 
creation and modification are global operations: if a modification
command is executed inside a TeX group, the matrix contents will
 not be restored automatically after the group ends.

An empty matrix also has a definite number of rows and columns, but
contains no entries. The number of rows and columns cannot be negative.

For geometric transformations, users may build a complete 
transformation by composing basic transformations. Although the 
internal implementation uses homogeneous transformation matrices, 
users need no knowledge of them whatsoever.

When applying a transformation, a transformation function is defined 
dynamically from the transformation matrix, and this function is 
then used to compute transformed coordinates quickly. Each 
transformation execution only performs the corresponding arithmetic 
operations (carried out by the `fp` module), without repeatedly 
retrieving data from the matrix, thereby greatly improving 
computational speed.

## AI Usage Statement

AI tools were used as an auxiliary aid in the development
 and documentation of this project.

- **Code**: The code was written and modified manually. 
AI was used to help identify potential issues and provide suggestions for 
investigation. All code changes and final decisions were made by the author.
- **Documentation**: AI was used to assist in drafting the documentation. 
The Chinese version was reviewed and edited by the author, 
while the English version was translated by AI based on the Chinese version.

In the event of any discrepancies between the Chinese and 
English documentation, the Chinese version shall prevail.

## License

Copyright (C) 2026 Ms_yam

This file may be distributed and/or modified under the terms of the
LaTeX Project Public License (LPPL), version 1.3c or (at your option)
any later version.

The latest version of this license is available at:

http://www.latex-project.org/lppl.txt

This work is maintained.

Current Maintainer: Ms_yam

Contact for bug reports and maintenance: ms_yam@163.com

## Compilation

This package is designed as a single-file package. You can compile it
directly using the following commands:

```sh
# Extract the package files
xetex matrix.dtx

# Compile the documentation
xelatex matrix.dtx
```
%</readme>
% \fi
%
% ^^A ================================== 文档内容 ==================================
%
% \title{基于 \pkg{l3fparray} 的轻量矩阵\\Lightweight Matrix Based on \pkg{l3fparray}}^^A
%
% \author{Ms\_yam\thanks{E-mail: \href{mailto:Ms_yam@163.com}{Ms\_yam@163.com}}}
% 
% \date{2026-11-23}
%
% \maketitle
%
% ^^A ---------------------------------- 文档部分 ----------------------------------
%
% \begin{documentation}
% 
% \changes{v0.2}{2026-11-23}{补充中文用户文档及几何变换接口说明}
%
%
% \section[用户文档]{用户文档~\hyperref[sec:user-guide:en]{\scriptsize[English]}}\label{sec:user-guide:cn}
%
% \subsection{宏包简介}
%
% \pkg{lt3matrix} 宏包提供一种轻量矩阵类型，主要用于 \TeX 文档中的矩阵演示、
% 数值示例和几何变换。使用者可以创建矩阵、设置矩阵元素、执行矩阵运算、
% 遍历元素，并将矩阵排版到数学公式中。
%
% 本宏包中的矩阵具有确定的行数和列数。通常，矩阵创建后其维度不应修改，
% 且本宏包不提供直接修改矩阵维度的接口；但当矩阵作为运算结果时，
% 其维度会根据运算结果自动更新。矩阵元素则可以通过赋值命令修改。
% 矩阵的创建和修改均为全局操作：在 \TeX 分组内执行修改命令，
% 分组结束后矩阵内容不会自动恢复。
%
% 空矩阵也具有明确的行数和列数，但不包含任何元素。
% 行数和列数不能为负数。
%
% 在几何变换中，允许用户通过叠加基础变换的形式来构建完整的变换。
% 虽然内部实现使用的是齐次变换矩阵，但用户从头到尾不需要任何相关知识。
%
% 应用变换时，通过变换矩阵动态定义变换函数，然后使用变换函数快速计算变换后的坐标。
% 每次执行变换，都只是执行对应的四则运算（由 \pkg{fp} 模块执行），
% 而不需要重复从矩阵中获取数据，从而大幅提高运算速度。
% 
% \subsection{快速开始}
%
% 下面的例子创建一个 $2\times3$ 矩阵，然后设置元素，并在数学模式中排版：
% \begin{verbatim}
% \ExplSyntaxOn
% \matrix_new:Nnn \l_demo_matrix { 2 } { 3 }
% \matrix_gset:Nn \l_demo_matrix { 1, 2, 3, 4, 5, 6 }
% \[
%   \matrix_use:N \l_demo_matrix
% \]
% \ExplSyntaxOff
% \end{verbatim}
% 其效果为：
% \ExplSyntaxOn
% \matrix_new:Nnn \l_demo_matrix { 2 } { 3 }
% \matrix_gset:Nn \l_demo_matrix { 1, 2, 3, 4, 5, 6 }
% \[
%   \matrix_use:N \l_demo_matrix
% \]
% \ExplSyntaxOff
%
%
% \subsection{命令与参数约定}
%
% 本宏包主要作为 \pkg{expl3} 的基础扩展宏包，其主要接口遵循
% \pkg{expl3} 的命名规范，使用时应处于 \cs{ExplSyntaxOn} 状态。
% 此外，为方便不熟悉 \pkg{expl3} 的 \LaTeX 用户使用，
% 本宏包也为几何变换等常用操作提供了相应的 \LaTeX2e 风格命令。
%
% \pkg{expl3} 风格命令名称中，冒号后的字母表示参数的形式：
% |N| 接收单个控制序列，|n| 接收按原样传入的一般参数
% （通常写在一对花括号中），|c| 则根据给出的字符名称构造控制序列。
% |e| 表示先对相应参数进行扩展，再将结果传入命令。
% 因此，|N| 形式直接接收矩阵变量，例如 \cs{matrix_new:Nnn}；
% |c| 形式接收矩阵变量的名称（不带反斜杠），例如
% \cs{matrix_new:cnn} |{l_demo_matrix}| |{2}| |{2}|。
% 同一组命令的不同变体执行相同操作，区别仅在于相应参数的传递形式。
%
% 在本文档中，\meta{matrix} 表示矩阵变量；\meta{destination} 表示接收结果的目标矩阵；
% \meta{source} 表示作为输入的源矩阵。除创建命令外，作为参数传入的矩阵
% 都应当已经创建。\meta{comma list} 表示逗号分隔的元素列表。
%
% 以 |g| 开头的矩阵修改命令采用全局赋值。矩阵的更改不会因普通 \TeX{} 分组结束而恢复。
%
% \subsection{基础命令}
%
% \subsubsection{创建矩阵}
%
% \begin{function}[updated=2026-11-18]
%   {\matrix_new:N, \matrix_new:Nnn, \matrix_new:cnn}
%   \begin{syntax}
%     \cs{matrix_new:N}   \meta{matrix}
%     \cs{matrix_new:Nnn} \meta{matrix} \Arg{rows} \Arg{columns}
%   \end{syntax}
%   创建一个普通矩阵，所有元素均初始化为零。
%
%   不带维度参数时，创建一个 $0 \times 0$ 的空矩阵；
%   指定 \meta{rows} 和 \meta{columns} 时，
%   分别创建具有相应行数和列数的矩阵。
%   两个维度参数均按整数表达式求值，且必须为非负数。
%   维度允许为零。
%
%   $0 \times 0$ 的空矩阵通常用于存储矩阵运算的结果。
%   如果矩阵变量已经定义，则产生错误。
% \end{function}
%
% \begin{function}{\matrix_const:Nnnn, \matrix_const:cnnn}
%   \begin{syntax}
%     \cs{matrix_const:Nnnn} \meta{matrix} \Arg{rows} \Arg{columns} \Arg{comma list}
%   \end{syntax}
%   创建一个常量矩阵，并使用逗号分隔列表初始化其元素。
%   元素按照行优先顺序填入；列表中未指定的元素初始化为零。
%   常量矩阵不能通过本宏包提供的矩阵修改命令进行修改。
%
%   \meta{matrix}、\meta{rows} 和 \meta{columns} 的含义与
%   \cs{matrix_new:Nnn} 相同。\meta{comma list} 是矩阵元素列表，
%   每一项均按浮点数表达式求值。
%   列表项数不得超过矩阵的元素总数；如果超过，则产生错误，
%   且矩阵不会被创建。
% \end{function}
% 
%
% \subsubsection{设置和修改矩阵}
%
% 以下命令用于修改已创建矩阵的内容。目标矩阵必须是普通矩阵；
% 如果目标矩阵是常量矩阵，则会产生错误。所有修改操作均为全局操作。
%
% \begin{function}{\matrix_gzero:N, \matrix_gzero:c}
%   \begin{syntax}
%     \cs{matrix_gzero:N} \meta{matrix}
%   \end{syntax}
%   将 \meta{matrix} 的所有元素设为零，同时保持矩阵的维度（行数和列数）不变。
% \end{function}
%
% \begin{function}[updated=2026-11-19]{\matrix_gset:Nn, \matrix_gset:cn, \matrix_gset:Ne, \matrix_gset:ce}
%   \begin{syntax}
%     \cs{matrix_gset:Nn} \meta{matrix} \Arg{comma list}
%   \end{syntax}
%   使用逗号分隔列表重新设置 \meta{matrix} 的所有元素，
%   并按照行优先顺序写入。列表中的每一项均按浮点数表达式求值。
%   列表项数不得超过矩阵的元素总数；如果列表项数不足，
%   则剩余元素设为零。如果列表项数过多，则产生错误，
%   且本次设置不会执行。矩阵的的维度（行数和列数）保持不变。
% \end{function}
%
% \begin{function}
%   {
%     \matrix_gset_eq:NN,
%     \matrix_gset_eq:cN,
%     \matrix_gset_eq:Nc
%   }
%   \begin{syntax}
%     \cs{matrix_gset_eq:NN} \meta{destination} \meta{source}
%   \end{syntax}
%   将 \meta{source} 的维度和元素复制到 \meta{destination}。
%   目标矩阵的维度会相应更新。复制完成后，两个矩阵的数据相互独立；
%   后续修改其中一个矩阵不会影响另一个矩阵。
%
%   \meta{destination} 必须是已创建的普通矩阵；
%   \meta{source} 必须是已创建的矩阵，可以是普通矩阵或常量矩阵。
%   源矩阵不会被修改。
% \end{function}
% 
% 
% \subsubsection{显示和检查矩阵}
%
% \begin{function}{\matrix_use:N, \matrix_use:c}
%   \begin{syntax}
%     \cs{matrix_use:N} \meta{matrix}
%   \end{syntax}
%   在数学模式中排版 \meta{matrix}。非空矩阵以圆括号矩阵的形式显示；
%   空矩阵则显示其维度信息，例如 $2\times0$。
%   本命令不会自动进入数学模式，因此必须在数学模式中调用。
% \end{function}
%
% \begin{function}{\matrix_log:N, \matrix_log:c}
%   \begin{syntax}
%     \cs{matrix_log:N} \meta{matrix}
%   \end{syntax}
%   将 \meta{matrix} 的维度、存储信息和元素内容写入编译日志，
%   用于检查矩阵状态和调试。本命令不会在页面正文中排版矩阵。
% \end{function}
%
% 
% \subsubsection{预定义矩阵}
%
% \begin{variable}{\c_empty_matrix}
%   $0\times0$ 空矩阵。
% \end{variable}
% \begin{variable}{\c_identity_two_matrix, \c_identity_three_matrix, \c_identity_four_matrix}
%   分别为 $2\times2$、$3\times3$ 和 $4\times4$ 单位矩阵。
%
%   \ExplSyntaxOn
%     $\matrix_use:N \c_identity_two_matrix$、$\matrix_use:N \c_identity_three_matrix$、
%     $\matrix_use:N \c_identity_four_matrix$
%   \ExplSyntaxOff
% \end{variable}
%
% \begin{variable}{\g_tmpa_matrix, \g_tmpb_matrix}
%   用于全局赋值的临时矩阵变量。本宏包的内部代码不会使用这些变量，
%   因此可以安全地与本宏包提供的命令一起使用。
%   但是，它们可能会被其他代码覆盖，因此只能用于短期存储。
% \end{variable}
%
% 
% \subsection{条件、映射与运算}
% 
% 
% \subsubsection{矩阵条件句}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_empty:N}
%   \begin{syntax}
%     \cs{matrix_if_empty:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   判断 \meta{matrix} 是否为空矩阵。
%   如果矩阵不包含任何元素，则条件成立，与矩阵的行数和列数无关。
%   因此，$0\times0$、$2\times0$ 和 $0\times3$ 矩阵均为空矩阵。
% \end{function}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_square:N}
%   \begin{syntax}
%     \cs{matrix_if_square:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   判断 \meta{matrix} 是否为方阵，即行数和列数是否相等。
%   空矩阵也参与维度判断，例如，$0\times0$ 矩阵是方阵，
%   而 $2\times0$ 矩阵不是方阵。
% \end{function}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_same_dim:NN}
%   \begin{syntax}
%     \cs{matrix_if_same_dim:NNTF} \meta{matrix1} \meta{matrix2} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   判断两个矩阵的维度是否相同，即行数和列数是否分别相等。
%   矩阵元素的内容不影响判断结果。
% \end{function}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_same_dim:Nnn}
%   \begin{syntax}
%     \cs{matrix_if_same_dim:NnnTF} \meta{matrix} \Arg{rows} \Arg{columns}
%     \Arg{true code} \Arg{false code}
%   \end{syntax}
%   判断 \meta{matrix} 的维度是否与指定维度相同。
%   \meta{rows} 和 \meta{columns} 分别指定行数和列数，
%   并按整数表达式求值。
% \end{function}
%
% \begin{function}[TF, added=2026-11-19]{\matrix_if_zero:N}
%   \begin{syntax}
%     \cs{matrix_if_zero:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   判断 \meta{matrix} 是否为零矩阵，即矩阵中的所有元素是否均为零。
%   浮点数比较采用 $10^{-12}$ 的容差。空矩阵不视为零矩阵，
%   因此该条件对空矩阵不成立。
%   \begin{texnote}
%     本命令会使用内部临时变量。如果在映射中调用此判定时出现异常，
%     请尝试使用 \TeX{} 分组隔离临时变量。
%   \end{texnote}
% \end{function}
%
% \begin{function}[TF, added=2026-11-19]{\matrix_if_identity:N}
%   \begin{syntax}
%     \cs{matrix_if_identity:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   判断 \meta{matrix} 是否为单位矩阵。单位矩阵必须是方阵，
%   主对角线上的元素均为 $1$，其余元素均为 $0$；浮点数比较采用
%   $10^{-12}$ 的容差。空矩阵因为不包含元素 $1$，因此按非单位矩阵处理。
%   \begin{texnote}
%     本命令会使用内部临时变量。如果在映射中调用此判定时出现异常，
%     请尝试使用 \TeX{} 分组隔离临时变量。
%   \end{texnote}
% \end{function}
%
%
% \subsubsection{逐元素运算}
%
% 本节介绍对矩阵中的每个元素分别执行相同运算的命令。
% 根据结果的存储方式，命令分为两种形式：一种直接将结果写回原矩阵，
% 另一种将结果写入另一个目标矩阵。用于存储运算结果的矩阵
% 都必须是已创建的普通矩阵。
%
% 直接写回原矩阵的形式保持矩阵的维度不变；写入目标矩阵的形式
% 则会将目标矩阵的维度更新为与源矩阵相同。
% 当目标矩阵与源矩阵是同一个矩阵时，运算结果仍会写回该矩阵，
% 但相比直接写回原矩阵的形式，需要额外的临时存储分配和回收操作。
% 因此，这种情况下建议使用直接写回原矩阵的形式。
%
% \begin{function}{\matrix_gnegate:N, \matrix_gabs:N, \matrix_gsign:N}
%   \begin{syntax}
%     \cs{matrix_gnegate:N} \meta{matrix}
%     \cs{matrix_gabs:N} \meta{matrix}
%     \cs{matrix_gsign:N} \meta{matrix}
%   \end{syntax}
%   分别将 \meta{matrix} 的每个元素替换为其相反数、绝对值和符号值，
%   并将运算结果写回其自身。
% \end{function}
%
% \begin{function}{\matrix_ground:Nn}
%   \begin{syntax}
%     \cs{matrix_ground:Nn} \meta{matrix} \Arg{digits}
%   \end{syntax}
%   对 \meta{matrix} 的每个元素执行舍入，并将结果写回其自身。
%   \meta{digits} 是传递给 \pkg{l3fp} 的 \texttt{round} 运算的精度参数。
% \end{function}
%
% \begin{function}{\matrix_gnegate:NN, \matrix_gabs:NN, \matrix_gsign:NN}
%   \begin{syntax}
%     \cs{matrix_gnegate:NN} \meta{destination} \meta{source}
%     \cs{matrix_gabs:NN} \meta{destination} \meta{source}
%     \cs{matrix_gsign:NN} \meta{destination} \meta{source}
%   \end{syntax}
%   对 \meta{source} 的每个元素执行相应运算，并将结果写入
%   \meta{destination}。目标矩阵的维度更新为与源矩阵相同。
% \end{function}
%
% \begin{function}{\matrix_ground:NNn}
%   \begin{syntax}
%     \cs{matrix_ground:NNn} \meta{destination} \meta{source} \Arg{digits}
%   \end{syntax}
%   对 \meta{source} 的每个元素执行舍入，并将结果写入 \meta{destination}。
%   \meta{digits} 的含义与 \cs{matrix_ground:Nn} 中相同。
% \end{function}
%
% \begin{function}
%   {
%     \matrix_gadd:Nn, \matrix_gsub:Nn,
%     \matrix_gmul:Nn, \matrix_gdiv:Nn,
%     \matrix_gpower:Nn
%   }
%   \begin{syntax}
%     \cs{matrix_gadd:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gsub:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gmul:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gdiv:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gpower:Nn} \meta{matrix} \Arg{scalar}
%   \end{syntax}
%   将矩阵 \meta{matrix} 的每个元素分别与 \meta{scalar} 执行加、减、乘、除或乘方运算，
%   并将结果写回其自身。对于矩阵 $A$ 和标量 $c$，对应的逐元素运算依次为
%   $A+c$、$A-c$、$A\times c$、$A/c$ 和 $A^c$。
%   \meta{scalar} 按 \pkg{l3fp} 浮点表达式求值。
% \end{function}
%
% \begin{function}
%   {
%     \matrix_gadd:NNn, \matrix_gsub:NNn,
%     \matrix_gmul:NNn, \matrix_gdiv:NNn,
%     \matrix_gpower:NNn
%   }
%   \begin{syntax}
%     \cs{matrix_gadd:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gsub:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gmul:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gdiv:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gpower:NNn} \meta{destination} \meta{source} \Arg{scalar}
%   \end{syntax}
%   将 \meta{source} 的每个元素分别与 \meta{scalar} 执行加、减、乘、除或乘方运算，
%   并将结果写入 \meta{destination}。对于源矩阵 $A$ 和标量 $c$，
%   对应的逐元素运算依次为 $A+c$、$A-c$、$A\times c$、$A/c$ 和 $A^c$。
%   \meta{scalar} 按 \pkg{l3fp} 浮点表达式求值。
% \end{function}
%
%
% \subsubsection{矩阵间的运算}
%
% 本节介绍两个矩阵之间的逐元素加法、逐元素减法和矩阵乘法。
% 用于存储结果的目标矩阵必须是已创建的普通矩阵；作为输入的操作数
% 必须是已创建的矩阵，可以是普通矩阵或常量矩阵。
%
% \begin{function}{\matrix_gadd:NNN, \matrix_gsub:NNN}
%   \begin{syntax}
%     \cs{matrix_gadd:NNN} \meta{destination} \meta{matrix A} \meta{matrix B}
%     \cs{matrix_gsub:NNN} \meta{destination} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   分别计算两个矩阵对应元素的和或差，并将结果写入 \meta{destination}。
%   两个操作数的行数和列数必须分别相等，否则产生维度不匹配错误。
%   目标矩阵的维度更新为与操作数相同。
% \end{function}
%
% \begin{function}{\matrix_gadd:NN, \matrix_gsub:NN}
%   \begin{syntax}
%     \cs{matrix_gadd:NN} \meta{matrix A} \meta{matrix B}
%     \cs{matrix_gsub:NN} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   分别执行原地运算 $A\leftarrow A+B$ 和 $A\leftarrow A-B$。
%   两个矩阵的行数和列数必须分别相等，否则产生维度不匹配错误。
%   运算结果写回第一个矩阵，其维度保持不变；第二个矩阵仅作为输入，
%   不会被修改。
% \end{function}
%
% \begin{function}{\matrix_gmul:NNN}
%   \begin{syntax}
%     \cs{matrix_gmul:NNN} \meta{destination} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   计算两个矩阵的乘积 $AB$。\meta{matrix A} 的列数必须等于
%   \meta{matrix B} 的行数，否则产生维度不匹配错误。
%   将结果写入 \meta{destination}，其维度更新为
%   \meta{matrix A} 的行数乘以 \meta{matrix B} 的列数。
% \end{function}
%
% \begin{function}{\matrix_gmul:NN}
%   \begin{syntax}
%     \cs{matrix_gmul:NN} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   计算两个矩阵的乘积 $AB$，并将结果写回第一个矩阵，即执行
%   $A\leftarrow AB$。乘积的维度由操作数决定，因此第一个矩阵的
%   行数和列数可能发生变化。第二个矩阵仅作为输入，不会被修改。
% \end{function}
% 
% 
% \subsubsection{基于矩阵的映射}
%
% 映射按行优先顺序访问矩阵中的元素，并在每次访问时将当前元素值
% 传递给回调。映射机制只负责读取和传递元素值，不会自动将回调结果
% 写回矩阵。如果矩阵为空，则不会调用回调。
% 映射可以嵌套使用；内层映射结束后，会恢复外层映射的处理状态。
%
% \begin{function}{\matrix_map_function:NN}
%   \begin{syntax}
%     \cs{matrix_map_function:NN} \meta{matrix} \meta{function}
%   \end{syntax}
%   对 \meta{matrix} 的每个元素调用一次 \meta{function}。
%   \meta{function} 必须接收一个参数，该参数为当前元素值。
% \end{function}
%
% \begin{function}{\matrix_map_inline:Nn}
%   \begin{syntax}
%     \cs{matrix_map_inline:Nn} \meta{matrix} \Arg{inline code}
%   \end{syntax}
%   对 \meta{matrix} 的每个元素执行一次 \meta{inline code}。
%   在内联代码中，|##1| 表示当前元素值。%
%   例如，下面的代码依次将矩阵元素写入终端：
%   \begin{verbatim}
%   \matrix_map_inline:Nn \l_demo_matrix
%     { \iow_term:n { ##1 } }
%   \end{verbatim}
% \end{function}
%
% \begin{function}{\matrix_map_break:, \matrix_map_break:n}
%   \begin{syntax}
%     \cs{matrix_map_break:}
%     \cs{matrix_map_break:n} \Arg{code}
%   \end{syntax}
%   在映射回调中调用 \cs{matrix_map_break:} 可以提前结束当前映射。
%   \cs{matrix_map_break:n} 除了结束当前映射，还会在退出映射时执行
%   \meta{code}。
% \end{function}
%
% 
% \subsection{几何变换}
%
% 本宏包提供基于齐次坐标矩阵的二维和三维几何变换。二维变换使用
% $3\times3$ 矩阵，三维变换使用 $4\times4$ 矩阵。
%
% 变换的构建与应用是两个独立阶段：
% 构建阶段负责组合和保存变换矩阵，应用阶段则通过变换函数计算变换后的坐标。
% 两个阶段不会自动同步，即变换矩阵不是立即生效的，而是需要显式调用刷新命令。
% 
% 变换矩阵采用列向量约定，变换矩阵默认初始化单位变换矩阵（即无变换）。
% 每次添加变换时，当前矩阵在右侧乘以新的变换矩阵。
% 因此，若依次添加 $T_1,T_2,\ldots,T_n$，最终矩阵为 $M=T_1T_2\cdots T_n$；
% 位移量、缩放系数、错切系数和角度均按 \pkg{l3fp} 浮点表达式求值。
% 变换矩阵的更新均采用全局状态，不会因普通 \TeX{} 分组结束而恢复。
% 
% \subsubsection{构建变换}
%
% 以下命令用于初始化变换矩阵以及添加各种几何变换。
% 所有变换均基于当前变换矩阵进行组合。
% 注意，构建二维变换时，不能使用三维版函数；反之亦然。
%
% \begin{function}[added=2026-11-21]{\transform_init:n}
%   \begin{syntax}
%     \cs{transform_init:n} \Arg{dimension}
%   \end{syntax}
%   初始化当前变换矩阵。
%   \meta{dimension} 指定变换空间的维数，只能为 $2$ 或 $3$。
%   初始化后，当前变换矩阵为相应维数的单位矩阵，
%   表示尚未添加任何几何变换。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_save:n, \transform_restore:n}
%   \begin{syntax}
%     \cs{transform_save:n}    \Arg{name}
%     \cs{transform_restore:n} \Arg{name}
%   \end{syntax}
%   保存或恢复当前变换矩阵。
%
%   \cs{transform_save:n} 将当前变换矩阵保存到指定名称对应的记录中。
%   如果该名称已经存在，则使用当前矩阵更新原有记录。
%
%   \cs{transform_restore:n} 从指定名称的记录中恢复变换矩阵。
%   如果对应的记录不存在，则不执行任何操作。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_shift:nn, \transform_shift:nnn}
%   \begin{syntax}
%     \cs{transform_shift:nn}  \Arg{dx} \Arg{dy}
%     \cs{transform_shift:nnn} \Arg{dx} \Arg{dy} \Arg{dz}
%   \end{syntax}
%   添加平移变换，分别用于二维和三维空间。偏移量应为纯数字，不能带单位。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_scale:nn, \transform_scale:nnn, 
%     \transform_scale:nnnn, \transform_scale:nnnnnn}
%   \begin{syntax}
%     \cs{transform_scale:nn}  \Arg{sx} \Arg{sy}
%     \cs{transform_scale:nnn} \Arg{sx} \Arg{sy}  \Arg{sz}
%     \cs{transform_scale:nnnn}   \Arg{x} \Arg{y} \Arg{sx} \Arg{sy}
%     \cs{transform_scale:nnnnnn} \Arg{x} \Arg{y} \Arg{z}  \Arg{sx} \Arg{sy} \Arg{sz}
%   \end{syntax}
%   添加基于原点或指定点的缩放变换，分别用于二维和三维空间。如需镜像，则将相关的缩放系数设为 $-1$。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_shear:nn, \transform_shear:nnnn, 
%     \transform_shear:nnnnnn, \transform_shear:nnnnnnnnn}
%   \begin{syntax}
%     \cs{transform_shear:nn} \Arg{xy coefficient} \Arg{yx coefficient}
%     \cs{transform_shear:nnnn} \Arg{x} \Arg{y} \Arg{xy coefficient} \Arg{yx coefficient}
%     \cs{transform_shear:nnnnnn}
%     \  \Arg{xy coefficient} \Arg{xz coefficient}
%     \  \Arg{yx coefficient} \Arg{yz coefficient}
%     \  \Arg{zx coefficient} \Arg{zy coefficient}
%     \cs{transform_shear:nnnnnnnnn} \Arg{x} \Arg{y} \Arg{z}
%     \  \Arg{xy coefficient} \Arg{xz coefficient}
%     \  \Arg{yx coefficient} \Arg{yz coefficient}
%     \  \Arg{zx coefficient} \Arg{zy coefficient}%    
%   \end{syntax}
%   添加基于原点或指定点的错切变换，分别用于二维和三维空间。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_rotate:n, \transform_rotated:n, 
%     \transform_rotate:nnn, \transform_rotated:nnn}
%   \begin{syntax}
%     \cs{transform_rotate:n}  \Arg{angle in radians}
%     \cs{transform_rotated:n} \Arg{angle in degrees}
%     \cs{transform_rotate:nnn}  \Arg{x} \Arg{y} \Arg{angle in radians}
%     \cs{transform_rotated:nnn} \Arg{x} \Arg{y} \Arg{angle in degrees}
%   \end{syntax}
%   添加二维旋转变换，以坐标原点或指定点为旋转中心。正角度为逆时针方向。
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \transform_rotate_x:n, \transform_rotate_y:n, \transform_rotate_z:n,
%     \transform_rotated_x:n, \transform_rotated_y:n, \transform_rotated_z:n,
%     \transform_rotate_x:nnnn, \transform_rotate_y:nnnn, \transform_rotate_z:nnnn,
%     \transform_rotated_x:nnnn, \transform_rotated_y:nnnn, \transform_rotated_z:nnnn
%   }
%   \begin{syntax}
%     \cs{transform_rotate_x:n}  \Arg{angle in radians}
%     \cs{transform_rotate_y:n}  \Arg{angle in radians}
%     \cs{transform_rotate_z:n}  \Arg{angle in radians}
%     \cs{transform_rotated_x:n} \Arg{angle in degrees}
%     \cs{transform_rotated_y:n} \Arg{angle in degrees}
%     \cs{transform_rotated_z:n} \Arg{angle in degrees}
%     \cs{transform_rotate_x:nnnn}  \Arg{x} \Arg{y} \Arg{z} \Arg{angle in radians}
%     \cs{transform_rotate_y:nnnn}  \Arg{x} \Arg{y} \Arg{z} \Arg{angle in radians}
%     \cs{transform_rotate_z:nnnn}  \Arg{x} \Arg{y} \Arg{z} \Arg{angle in radians}
%     \cs{transform_rotated_x:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in degrees}
%     \cs{transform_rotated_y:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in degrees}
%     \cs{transform_rotated_z:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in degrees}
%   \end{syntax}
%   添加三维旋转变换，分别绕 $x$、$y$ 和 $z$ 轴旋转，
%   旋转中心为坐标原点或指定点。正角度的旋转方向遵循右手定则。 
% \end{function}
%
% \subsubsection{刷新变换}
%
% 构建变换时，添加变换的命令只更新当前变换矩阵，
% 不会自动更新用于计算坐标的变换函数。
% 因此，在构建完成后，需要显式刷新变换函数。
% 
% \textbf{刷新采用局部函数定义。}如果在 \TeX{} 分组内执行刷新，
% 分组结束后变换函数会恢复为分组开始前的定义。
%
% \begin{function}[added=2026-11-21]{\transform_refresh:, \transform_refresh:N}
%   \begin{syntax}
%     \cs{transform_refresh:}
%     \cs{transform_refresh:N} \meta{matrix}
%   \end{syntax}
%   根据指定的变换矩阵刷新对应的变换函数。
%   指定矩阵必须为合法的齐次变换矩阵：
%   二维变换使用 $3\times3$ 矩阵，三维变换使用 $4\times4$ 矩阵，
%   且矩阵右下角的元素必须为 $1$。 
% \end{function}
%
% \subsubsection{应用变换}
% 
% 与变换矩阵不一样，本模块始终保存两套变换函数：分别用于二维变换与三维变换。
% 即，使用都可以同时进行二维与三维变换。
%
% \begin{function}[EXP, added=2026-11-21]{\transform_apply:nnn, \transform_apply:nnnn}
%   \begin{syntax}
%     \cs{transform_apply:nnn}  \Arg{x} \Arg{y} \Arg{suffix}
%     \cs{transform_apply:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{suffix}
%   \end{syntax}
%   对给定坐标应用对应的变换，并输出对应结果。坐标应为不带单位的纯数字。
%   各结果分量依次输出并以逗号分隔，且不含齐次分量。
%   \meta{suffix} 会追加到每个变换后的坐标分量之后，如用于添加单位。
%   如果不需要追加内容，可以将其留空。
% \end{function}
% 
% \begin{function}[EXP, added=2026-11-21]{\transform_apply:nn}
%   \begin{syntax}
%     \cs{transform_apply:nn} \Arg{coordinates} \Arg{suffix}
%   \end{syntax}
%   对给定坐标应用对应的变换，并输出对应结果。
%   \meta{coordinates} 是由逗号分隔的坐标列表，
%   包含两个或三个坐标分量，分别用于二维和三维变换。
%   坐标应为不带单位的纯数字。
%   各结果分量依次输出并以逗号分隔，且不含齐次分量。
%   \meta{suffix} 会追加到每个变换后的坐标分量之后，如用于添加单位。
%   如果不需要追加内容，可以将其留空。
% \end{function}
% 
% 
% \begin{function}[added=2026-11-23]{\transform_apply_inline:nnn, \transform_apply_inline:nnnn}
%   \begin{syntax}
%     \cs{transform_apply_inline:nnn}  \Arg{x} \Arg{y} \Arg{code}
%     \cs{transform_apply_inline:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{code}
%   \end{syntax}
%   对给定坐标应用当前变换，并把变换后的坐标分量传给 \meta{code} 执行。
%   在 \meta{code} 中，可用 |#1|、|#2| 以及（仅三维版）|#3| 引用变换后的坐标分量。
%   坐标应为不带单位的纯数字。
%   本函数不能递归调用（即不得在其自身的 \meta{code} 参数中再次调用本函数）。
% \end{function}
% 
% 
% \subsection{\LaTeX2e 命令}
%
% 本小节介绍采用 \LaTeX2e 风格的接口命令。
%
% \subsubsection{初始化与保存恢复}
%
% \begin{function}[added=2026-11-21]{\TransformInit}
%   \begin{syntax}
%     \cs{TransformInit} \oarg{dimension}
%   \end{syntax}
%   初始化当前变换矩阵为单位矩阵。\meta{dimension} 指定变换维度，
%   可取 |2| 或 |3|，省略时默认为 |2|。
%   二维变换使用 $3\times3$ 单位矩阵，三维变换使用 $4\times4$ 单位矩阵。
%   每次开始一组新的变换时，应先调用本命令初始化变换状态。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformSave, \TransformRestore}
%   \begin{syntax}
%     \cs{TransformSave} \Arg{name}
%     \cs{TransformRestore} \Arg{name}
%   \end{syntax}
%   \cs{TransformSave} 将当前变换矩阵以 \meta{name} 为名称保存；
%   \cs{TransformRestore} 则恢复此前以相同名称保存的变换矩阵。
%   名称用于区分不同的保存记录。若指定名称尚无保存记录，
%   \cs{TransformRestore} 不执行恢复操作。
% \end{function}
%
% 
% \subsubsection{平移、缩放与错切}
%
% \begin{function}[added=2026-11-21]{\TransformShift}
%   \begin{syntax}
%     \cs{TransformShift} \Arg{dx, dy}
%     \cs{TransformShift} \Arg{dx, dy, dz}
%   \end{syntax}
%   添加平移变换。二维形式使用水平和垂直方向的位移量 |dx|、|dy|；
%   三维形式还包含深度方向的位移量 |dz|。
%   参数数量必须与当前变换维度相对应。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformScale}
%   \begin{syntax}
%     \cs{TransformScale} \oarg{center} \Arg{scale}
%     \cs{TransformScale} \oarg{center} \Arg{sx, sy}
%     \cs{TransformScale} \oarg{center} \Arg{sx, sy, sz}
%   \end{syntax}
%   添加缩放变换。一个缩放系数表示各方向使用相同的缩放比例；
%   两个系数分别指定二维坐标轴的缩放比例，三个系数分别指定
%   三维坐标轴的缩放比例。
%   可选参数 \meta{center} 用于指定缩放中心，二维形式为 |x,y|，
%   三维形式为 |x,y,z|；省略时以原点为中心。
%   参数数量应与当前变换维度相匹配。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformShear}
%   \begin{syntax}
%     \cs{TransformShear} \oarg{center} \Arg{a, b}
%     \cs{TransformShear} \oarg{center} \Arg{a, b, c, d, e, f}
%   \end{syntax}
%   添加错切变换。二维形式使用两个系数，对应变换矩阵
%   $\left[\begin{smallmatrix}1&a&0\\b&1&0\\0&0&1\end{smallmatrix}\right]$；
%   三维形式使用六个系数，对应变换矩阵
%   $\left[\begin{smallmatrix}1&a&b&0\\c&1&d&0\\e&f&1&0\\0&0&0&1\end{smallmatrix}\right]$。
%   可选参数 \meta{center} 用于指定错切中心，格式与
%   \cs{TransformScale} 中相同；省略时以原点为中心。
%   参数数量应与当前变换维度相匹配。
% \end{function}
%
% 
% \subsubsection{旋转变换}
%
% \begin{function}[added=2026-11-21]{\TransformRotate, \TransformRotated}
%   \begin{syntax}
%     \cs{TransformRotate}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotated} \oarg{center} \Arg{angle in degrees}
%   \end{syntax}
%   添加二维平面旋转，要求当前变换为二维变换。
%   可选参数 \meta{center} 指定旋转中心，格式为 |x, y|；
%   省略时绕原点旋转。
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \TransformRotateX, \TransformRotateY, \TransformRotateZ,
%     \TransformRotatedX, \TransformRotatedY, \TransformRotatedZ
%   }
%   \begin{syntax}
%     \cs{TransformRotateX}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotateY}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotateZ}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotatedX} \oarg{center} \Arg{angle in degrees}
%     \cs{TransformRotatedY} \oarg{center} \Arg{angle in degrees}
%     \cs{TransformRotatedZ} \oarg{center} \Arg{angle in degrees}
%   \end{syntax}
%   添加三维旋转，分别绕 $x$、$y$、$z$ 轴旋转，要求当前变换为三维变换。
%   可选参数 \meta{center} 指定旋转中心，格式为 |x, y, z|；
%   省略时绕原点旋转。
% \end{function}
%
% 
% \subsubsection{刷新与应用变换}
%
% 变换命令只更新当前变换矩阵。要将矩阵系数转换为可调用的坐标变换函数，
% 需要执行刷新操作；此后再调用应用命令计算坐标。
%
% \begin{function}[added=2026-11-21]{\TransformRefresh}
%   \begin{syntax}
%     \cs{TransformRefresh}
%     \cs{TransformRefresh} \oarg{matrix}
%   \end{syntax}
%   根据变换矩阵刷新内部坐标变换函数。省略可选参数时，使用当前变换矩阵；
%   指定 \meta{matrix} 时，则从该矩阵提取变换函数。
%   二维变换矩阵必须为 $3\times3$，三维变换矩阵必须为 $4\times4$；
%   两种形式的右下角元素均须为 $1$，否则报告无效变换矩阵错误。
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformApply}
%   \begin{syntax}
%     \cs{TransformApply} \oarg{suffix} \Arg{coordinates}
%   \end{syntax}
%   对指定坐标应用对应的变换，并输出对应结果。
%   \meta{coordinates} 是由逗号分隔的坐标列表，
%   包含两个或三个坐标分量，分别用于二维和三维变换。
%   坐标分量应为纯数字，不能带单位。
%   各结果分量依次输出并以逗号分隔，且不含齐次分量。
%   \meta{suffix} 会追加到每个变换后的坐标分量之后，如用于添加单位。
%   如果不需要追加内容，可以将其留空。
% \end{function}
%
% 
% \subsection{错误条件}
%
% 当前实现会在以下情况下报告错误：
% \begin{itemize}
%   \item 创建矩阵时，行数或列数为负数；
%   \item 创建矩阵时，矩阵变量已经定义；
%   \item 将常量矩阵用作修改操作的目标矩阵；
%   \item 创建或设置矩阵时，元素列表中的项数超过矩阵的元素总数；
%   \item 执行矩阵间的逐元素二元运算时，两个操作数的维度不一致；
%   \item 执行矩阵乘法时，左操作数的列数与右操作数的行数不一致；
%   \item 几何变换命令的参数数量与二维或三维形式不匹配；
%   \item 刷新变换时，输入矩阵不是符合要求的 $3\times3$ 或 $4\times4$ 变换矩阵，
%         或其右下角元素不为 $1$。
% \end{itemize}
%
% \cleardoublepage{}
% 
% ^^A 清理中文版中的示例代码定义的变量
% \ExplSyntaxOn 
% \cs_undefine:N \l_demo_matrix
% \ExplSyntaxOff
% 
% 
% \section[User Guide]{User Guide~\hyperref[sec:user-guide:cn]{\scriptsize[中文]}}\label{sec:user-guide:en}
% 
% \changetoen ^^A 切换为英文标记
% 
% \subsection{Package Overview}
%
% The \pkg{lt3matrix} package provides a lightweight matrix type, intended
% mainly for matrix demonstrations, numerical examples, and geometric
% transformations in \TeX\ documents. Users can create matrices, set matrix
% entries, perform matrix operations, iterate over entries, and typeset
% matrices in mathematical formulas.
%
% Matrices in this package have a fixed number of rows and columns. Normally,
% the dimensions of a matrix should not be changed after it is created, and
% the package does not provide an interface for directly modifying matrix
% dimensions; however, when a matrix is the result of an operation, its
% dimensions are updated automatically according to the result. Matrix entries
% can be modified via assignment commands. Both the creation and modification
% of matrices are global operations: if a modification command is executed
% inside a \TeX\ group, the matrix contents will not be restored automatically
% after the group ends.
%
% An empty matrix also has a definite number of rows and columns, but contains
% no entries. The number of rows and columns cannot be negative.
%
% For geometric transformations, users may build a complete transformation by
% composing basic transformations. Although the internal implementation uses
% homogeneous transformation matrices, users need no knowledge of them
% whatsoever.
%
% When applying a transformation, a transformation function is defined
% dynamically from the transformation matrix, and this function is then used to
% compute transformed coordinates quickly. Each transformation execution only
% performs the corresponding arithmetic operations (carried out by the
% \pkg{fp} module), without repeatedly retrieving data from the matrix, thereby
% greatly improving computational speed.
%
%
% \subsection{Quick Start}
%
% The following example creates a $2\times3$ matrix, assigns values
% to its elements, and typesets the matrix in mathematical mode:
% \begin{verbatim}
% \ExplSyntaxOn
% \matrix_new:Nnn \l_demo_matrix { 2 } { 3 }
% \matrix_gset:Nn \l_demo_matrix { 1, 2, 3, 4, 5, 6 }
% \[
%   \matrix_use:N \l_demo_matrix
% \]
% \ExplSyntaxOff
% \end{verbatim}
% The output is:
% \ExplSyntaxOn
% \matrix_new:Nnn \l_demo_matrix { 2 } { 3 }
% \matrix_gset:Nn \l_demo_matrix { 1, 2, 3, 4, 5, 6 }
% \[
%   \matrix_use:N \l_demo_matrix
% \]
% \ExplSyntaxOff
%
%
% \subsection{Command and Argument Conventions}
%
% This package is primarily intended as a basic extension to
% \pkg{expl3}. Its main interfaces follow the \pkg{expl3} naming
% conventions and should be used with \cs{ExplSyntaxOn} enabled.
% In addition, the package provides corresponding \LaTeX2e-style
% commands for commonly used operations, such as geometric
% transformations, making them accessible to \LaTeX{} users
% who are not familiar with \pkg{expl3}.
%
% In \pkg{expl3}-style command names, the letters following the
% colon indicate the forms of the arguments:
% |N| accepts a single control sequence, |n| accepts a general
% argument passed as is (usually enclosed in braces), |c| constructs
% a control sequence from a character-based name, and |e| expands
% the corresponding argument before passing the result to the command.
% Thus, the |N| form takes a matrix variable directly, as in
% \cs{matrix_new:Nnn}, whereas the |c| form takes the name of a matrix
% variable (without the leading backslash), as in
% \cs{matrix_new:cnn} |{l_demo_matrix}| |{2}| |{2}|.
% Different variants of the same command perform the same operation;
% they differ only in how the corresponding arguments are passed.
%
% In this document, \meta{matrix} denotes a matrix variable;
% \meta{destination} denotes the target matrix that receives a result;
% and \meta{source} denotes an input matrix.
% Except for creation commands, any matrix passed as an argument
% must have been created beforehand.
% \meta{comma list} denotes a comma-separated list of elements.
%
% Matrix modification commands beginning with |g| use global
% assignments. Changes to a matrix are not reverted when an ordinary
% \TeX{} group ends.
%
%
% \subsection{Basic Commands}
%
% \subsubsection{Creating Matrices}
%
% \begin{function}[updated=2026-11-18]
%   {\matrix_new:N, \matrix_new:Nnn, \matrix_new:cnn}
%   \begin{syntax}
%     \cs{matrix_new:N}   \meta{matrix}
%     \cs{matrix_new:Nnn} \meta{matrix} \Arg{rows} \Arg{columns}
%   \end{syntax}
%   Creates a regular matrix with all elements initialized to zero.
%
%   If no dimensions are specified, an empty $0\times0$ matrix
%   is created. Otherwise, \meta{rows} and \meta{columns}
%   specify the number of rows and columns, respectively.
%   Both dimension arguments are evaluated as integer expressions
%   and must be non-negative. Zero dimensions are allowed.
%
%   An empty $0\times0$ matrix is typically used to store
%   the result of a matrix operation.
%   An error is raised if the matrix variable is already defined.
% \end{function}
%
% \begin{function}{\matrix_const:Nnnn, \matrix_const:cnnn}
%   \begin{syntax}
%     \cs{matrix_const:Nnnn} \meta{matrix} \Arg{rows} \Arg{columns} \Arg{comma list}
%   \end{syntax}
%   Creates a constant matrix and initializes its elements from
%   a comma-separated list. Elements are filled in row-major order;
%   any elements not specified in the list are initialized to zero.
%   A constant matrix cannot be modified using the matrix modification
%   commands provided by this package.
%
%   \meta{matrix}, \meta{rows}, and \meta{columns} have the same
%   meanings as for \cs{matrix_new:Nnn}. \meta{comma list} is the list
%   of matrix elements, with each item evaluated as a floating-point
%   expression.
%   The number of items must not exceed the total number of elements
%   in the matrix. If it does, an error is raised and the matrix
%   is not created.
% \end{function}
%
%
% \subsubsection{Setting and Modifying Matrices}
%
% The following commands modify the contents of an existing matrix.
% The destination matrix must be a regular matrix; an error is raised
% if it is a constant matrix. All modifications are global.
%
% \begin{function}{\matrix_gzero:N, \matrix_gzero:c}
%   \begin{syntax}
%     \cs{matrix_gzero:N} \meta{matrix}
%   \end{syntax}
%   Sets all elements of \meta{matrix} to zero, while preserving
%   its dimensions (number of rows and columns).
% \end{function}
%
% \begin{function}[updated=2026-11-19]{\matrix_gset:Nn, \matrix_gset:cn, \matrix_gset:Ne, \matrix_gset:ce}
%   \begin{syntax}
%     \cs{matrix_gset:Nn} \meta{matrix} \Arg{comma list}
%   \end{syntax}
%   Sets all elements of \meta{matrix} using a comma-separated list,
%   with the elements filled in row-major order. Each item in the list
%   is evaluated as a floating-point expression.
%   The number of items must not exceed the total number of elements
%   in the matrix. If fewer items are provided, the remaining elements
%   are set to zero. If too many items are provided, an error is raised
%   and the assignment is not performed. The dimensions of the matrix
%   remain unchanged.
% \end{function}
%
% \begin{function}
%   {
%     \matrix_gset_eq:NN,
%     \matrix_gset_eq:cN,
%     \matrix_gset_eq:Nc
%   }
%   \begin{syntax}
%     \cs{matrix_gset_eq:NN} \meta{destination} \meta{source}
%   \end{syntax}
%   Copies the dimensions and elements of \meta{source} to
%   \meta{destination}. The dimensions of the destination matrix
%   are updated accordingly. After copying, the two matrices have
%   independent data; subsequent modifications to either matrix
%   do not affect the other.
%
%   \meta{destination} must be an existing regular matrix;
%   \meta{source} must be an existing matrix and may be either
%   a regular matrix or a constant matrix.
%   The source matrix is not modified.
% \end{function}
%
%
% \subsubsection{Displaying and Inspecting Matrices}
%
% \begin{function}{\matrix_use:N, \matrix_use:c}
%   \begin{syntax}
%     \cs{matrix_use:N} \meta{matrix}
%   \end{syntax}
%   Typesets \meta{matrix} in mathematical mode. Non-empty matrices
%   are displayed as parenthesized matrices, while empty matrices
%   display their dimensions, such as $2\times0$.
%   This command does not enter mathematical mode automatically
%   and must therefore be used in mathematical mode.
% \end{function}
%
% \begin{function}{\matrix_log:N, \matrix_log:c}
%   \begin{syntax}
%     \cs{matrix_log:N} \meta{matrix}
%   \end{syntax}
%   Writes the dimensions, storage information, and element contents
%   of \meta{matrix} to the compilation log for inspection and
%   debugging. This command does not typeset the matrix in the
%   document body.
% \end{function}
%
%
% \subsubsection{Predefined Matrices}
%
% \begin{variable}{\c_empty_matrix}
%   The empty $0\times0$ matrix.
% \end{variable}
%
% \begin{variable}{\c_identity_two_matrix, \c_identity_three_matrix, \c_identity_four_matrix}
%   The $2\times2$, $3\times3$, and $4\times4$ identity matrices,
%   respectively.
%
%   \ExplSyntaxOn
%     $\matrix_use:N \c_identity_two_matrix$,
%     $\matrix_use:N \c_identity_three_matrix$,
%     $\matrix_use:N \c_identity_four_matrix$
%   \ExplSyntaxOff
% \end{variable}
%
% \begin{variable}{\g_tmpa_matrix, \g_tmpb_matrix}
%   Scratch matrix variables for global assignments. These variables
%   are not used by the package's internal code and can therefore
%   be safely used with commands provided by this package.
%   However, they may be overwritten by other code and should only
%   be used for short-term storage.
% \end{variable}
%
%
% \subsection{Conditionals, Mapping, and Operations}
%
%
% \subsubsection{Matrix Conditionals}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_empty:N}
%   \begin{syntax}
%     \cs{matrix_if_empty:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether \meta{matrix} is empty.
%   The condition is true if the matrix contains no elements,
%   regardless of its number of rows and columns.
%   Thus, matrices of dimensions $0\times0$, $2\times0$, and $0\times3$
%   are all considered empty.
% \end{function}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_square:N}
%   \begin{syntax}
%     \cs{matrix_if_square:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether \meta{matrix} is square, that is, whether its number
%   of rows equals its number of columns.
%   Empty matrices are also checked according to their dimensions.
%   For example, a $0\times0$ matrix is square, whereas a $2\times0$
%   matrix is not.
% \end{function}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_same_dim:NN}
%   \begin{syntax}
%     \cs{matrix_if_same_dim:NNTF} \meta{matrix1} \meta{matrix2}
%       \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether the two matrices have the same dimensions,
%   that is, whether their respective numbers of rows and columns
%   are equal. The matrix elements do not affect the result.
% \end{function}
%
% \begin{function}[pTF, added=2026-11-19]{\matrix_if_same_dim:Nnn}
%   \begin{syntax}
%     \cs{matrix_if_same_dim:NnnTF} \meta{matrix} \Arg{rows} \Arg{columns}
%       \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether \meta{matrix} has the specified dimensions.
%   \meta{rows} and \meta{columns} specify the number of rows
%   and columns, respectively, and are evaluated as integer expressions.
% \end{function}
%
% \begin{function}[TF, added=2026-11-19]{\matrix_if_zero:N}
%   \begin{syntax}
%     \cs{matrix_if_zero:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether \meta{matrix} is a zero matrix, that is, whether
%   all its elements are zero.
%   Floating-point comparisons use a tolerance of $10^{-12}$.
%   An empty matrix is not considered a zero matrix, so this condition
%   is false for an empty matrix.
%   \begin{texnote}
%     This command uses internal scratch variables. If unexpected
%     behavior occurs when calling this test within a mapping,
%     try enclosing the call in a \TeX{} group to isolate the
%     scratch variables.
%   \end{texnote}
% \end{function}
%
% \begin{function}[TF, added=2026-11-19]{\matrix_if_identity:N}
%   \begin{syntax}
%     \cs{matrix_if_identity:NTF} \meta{matrix} \Arg{true code} \Arg{false code}
%   \end{syntax}
%   Tests whether \meta{matrix} is an identity matrix.
%   An identity matrix must be square, with all elements on the
%   main diagonal equal to $1$ and all other elements equal to $0$.
%   Floating-point comparisons use a tolerance of $10^{-12}$.
%   An empty matrix is not considered an identity matrix, since it
%   contains no element equal to $1$.
%   \begin{texnote}
%     This command uses internal scratch variables. If unexpected
%     behavior occurs when calling this test within a mapping,
%     try enclosing the call in a \TeX{} group to isolate the
%     scratch variables.
%   \end{texnote}
% \end{function}
%
%
% \subsubsection{Element-wise Operations}
%
% This section describes commands that perform the same operation
% on each element of a matrix. Depending on how the results are stored,
% the commands are provided in two forms: one writes the results back
% to the original matrix, while the other writes them to a separate
% destination matrix. The matrix used to store the results must be
% an existing regular matrix.
%
% The form that writes the results back to the original matrix
% preserves its dimensions, whereas the form that writes the results
% to a destination matrix updates the destination dimensions to match
% those of the source matrix.
% If the destination and source are the same matrix, the results are
% still written back to that matrix. However, compared with the form
% that writes directly to the original matrix, this requires additional
% temporary storage allocation and deallocation.
% Therefore, the in-place form is recommended in this case.
%
% \begin{function}{\matrix_gnegate:N, \matrix_gabs:N, \matrix_gsign:N}
%   \begin{syntax}
%     \cs{matrix_gnegate:N} \meta{matrix}
%     \cs{matrix_gabs:N}    \meta{matrix}
%     \cs{matrix_gsign:N}   \meta{matrix}
%   \end{syntax}
%   Replaces each element of \meta{matrix} with its negation,
%   absolute value, or sign, respectively, and writes the results
%   back to the same matrix.
% \end{function}
%
% \begin{function}{\matrix_ground:Nn}
%   \begin{syntax}
%     \cs{matrix_ground:Nn} \meta{matrix} \Arg{digits}
%   \end{syntax}
%   Rounds each element of \meta{matrix} and writes the results
%   back to the same matrix.
%   \meta{digits} is the precision argument passed to the
%   \texttt{round} operation of \pkg{l3fp}.
% \end{function}
%
% \begin{function}{\matrix_gnegate:NN, \matrix_gabs:NN, \matrix_gsign:NN}
%   \begin{syntax}
%     \cs{matrix_gnegate:NN} \meta{destination} \meta{source}
%     \cs{matrix_gabs:NN}    \meta{destination} \meta{source}
%     \cs{matrix_gsign:NN}   \meta{destination} \meta{source}
%   \end{syntax}
%   Applies the corresponding operation to each element of
%   \meta{source} and writes the results to \meta{destination}.
%   The dimensions of the destination matrix are updated to match
%   those of the source matrix.
% \end{function}
%
% \begin{function}{\matrix_ground:NNn}
%   \begin{syntax}
%     \cs{matrix_ground:NNn} \meta{destination} \meta{source} \Arg{digits}
%   \end{syntax}
%   Rounds each element of \meta{source} and writes the results
%   to \meta{destination}. The meaning of \meta{digits} is the same
%   as for \cs{matrix_ground:Nn}.
% \end{function}
%
% \begin{function}
%   {
%     \matrix_gadd:Nn, \matrix_gsub:Nn,
%     \matrix_gmul:Nn, \matrix_gdiv:Nn,
%     \matrix_gpower:Nn
%   }
%   \begin{syntax}
%     \cs{matrix_gadd:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gsub:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gmul:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gdiv:Nn} \meta{matrix} \Arg{scalar}
%     \cs{matrix_gpower:Nn} \meta{matrix} \Arg{scalar}
%   \end{syntax}
%   Performs addition, subtraction, multiplication, division, or
%   exponentiation, respectively, between each element of
%   \meta{matrix} and \meta{scalar}, and writes the results back
%   to the same matrix.
%   For a matrix $A$ and a scalar $c$, the corresponding element-wise
%   operations are $A+c$, $A-c$, $A\times c$, $A/c$, and $A^c$.
%   \meta{scalar} is evaluated as a floating-point expression
%   by \pkg{l3fp}.
% \end{function}
%
% \begin{function}
%   {
%     \matrix_gadd:NNn, \matrix_gsub:NNn,
%     \matrix_gmul:NNn, \matrix_gdiv:NNn,
%     \matrix_gpower:NNn
%   }
%   \begin{syntax}
%     \cs{matrix_gadd:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gsub:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gmul:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gdiv:NNn} \meta{destination} \meta{source} \Arg{scalar}
%     \cs{matrix_gpower:NNn} \meta{destination} \meta{source} \Arg{scalar}
%   \end{syntax}
%   Performs addition, subtraction, multiplication, division, or
%   exponentiation, respectively, between each element of
%   \meta{source} and \meta{scalar}, and writes the results to
%   \meta{destination}.
%   For a source matrix $A$ and a scalar $c$, the corresponding
%   element-wise operations are $A+c$, $A-c$, $A\times c$,
%   $A/c$, and $A^c$.
%   \meta{scalar} is evaluated as a floating-point expression
%   by \pkg{l3fp}.
% \end{function}
%
%
% \subsubsection{Operations Between Matrices}
%
% This section describes element-wise addition, element-wise
% subtraction, and matrix multiplication between two matrices.
% The destination matrix used to store the result must be an existing
% regular matrix; the input operands must be existing matrices and
% may be either regular or constant matrices.
%
% \begin{function}{\matrix_gadd:NNN, \matrix_gsub:NNN}
%   \begin{syntax}
%     \cs{matrix_gadd:NNN} \meta{destination} \meta{matrix A} \meta{matrix B}
%     \cs{matrix_gsub:NNN} \meta{destination} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   Computes the element-wise sum or difference of two matrices,
%   respectively, and writes the result to \meta{destination}.
%   The two operands must have the same number of rows and the same
%   number of columns; otherwise, a dimension mismatch error is raised.
%   The dimensions of the destination matrix are updated to match
%   those of the operands.
% \end{function}
%
% \begin{function}{\matrix_gadd:NN, \matrix_gsub:NN}
%   \begin{syntax}
%     \cs{matrix_gadd:NN} \meta{matrix A} \meta{matrix B}
%     \cs{matrix_gsub:NN} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   Performs the in-place operation $A\leftarrow A+B$ or
%   $A\leftarrow A-B$, respectively.
%   The two matrices must have the same number of rows and the same
%   number of columns; otherwise, a dimension mismatch error is raised.
%   The result is written back to the first matrix, whose dimensions
%   remain unchanged. The second matrix is used only as input
%   and is not modified.
% \end{function}
%
% \begin{function}{\matrix_gmul:NNN}
%   \begin{syntax}
%     \cs{matrix_gmul:NNN} \meta{destination} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   Computes the matrix product $AB$.
%   The number of columns in \meta{matrix A} must equal the number
%   of rows in \meta{matrix B}; otherwise, a dimension mismatch
%   error is raised.
%   The result is written to \meta{destination}, whose dimensions
%   are updated to the number of rows in \meta{matrix A} by
%   the number of columns in \meta{matrix B}.
% \end{function}
%
% \begin{function}{\matrix_gmul:NN}
%   \begin{syntax}
%     \cs{matrix_gmul:NN} \meta{matrix A} \meta{matrix B}
%   \end{syntax}
%   Computes the matrix product $AB$ and writes the result back
%   to the first matrix, performing the operation $A\leftarrow AB$.
%   The dimensions of the first matrix may therefore change
%   according to the product. The second matrix is used only as input
%   and is not modified.
% \end{function}
%
%
% \subsubsection{Mapping over Matrices}
%
% Mapping visits the elements of a matrix in row-major order and
% passes the value of the current element to the callback on each
% iteration. The mapping mechanism only reads and passes element
% values; it does not automatically write callback results back
% to the matrix. If the matrix is empty, the callback is not invoked.
% Mapping can be nested; when an inner mapping ends, the processing
% state of the outer mapping is restored.
%
% \begin{function}{\matrix_map_function:NN}
%   \begin{syntax}
%     \cs{matrix_map_function:NN} \meta{matrix} \meta{function}
%   \end{syntax}
%   Calls \meta{function} once for each element of \meta{matrix}.
%   \meta{function} must accept one argument, which is the value
%   of the current element.
% \end{function}
%
% \begin{function}{\matrix_map_inline:Nn}
%   \begin{syntax}
%     \cs{matrix_map_inline:Nn} \meta{matrix} \Arg{inline code}
%   \end{syntax}
%   Executes \meta{inline code} once for each element of \meta{matrix}.
%   Within the inline code, |##1| represents the value of the current
%   element.
%   For example, the following code writes the matrix elements
%   to the terminal in sequence:
%   \begin{verbatim}
%   \matrix_map_inline:Nn \l_demo_matrix
%     { \iow_term:n { ##1 } }
%   \end{verbatim}
% \end{function}
%
% \begin{function}{\matrix_map_break:, \matrix_map_break:n}
%   \begin{syntax}
%     \cs{matrix_map_break:}
%     \cs{matrix_map_break:n} \Arg{code}
%   \end{syntax}
%   Calling \cs{matrix_map_break:} within a mapping callback
%   terminates the current mapping early.
%   In addition to terminating the current mapping,
%   \cs{matrix_map_break:n} executes \meta{code} when the mapping exits.
% \end{function}
%
%
% \subsection{Geometric Transformations}
%
% This package provides two-dimensional and three-dimensional
% geometric transformations based on homogeneous transformation
% matrices. Two-dimensional transformations use $3\times3$ matrices,
% while three-dimensional transformations use $4\times4$ matrices.
%
% The construction and application of transformations are two
% independent stages. The construction stage builds and combines
% transformation matrices, while the application stage uses
% transformation functions to calculate transformed coordinates.
% These two stages are not synchronized automatically:
% changes to the transformation matrix do not take effect until
% the transformation functions are explicitly refreshed.
%
% Transformation matrices use the column-vector convention.
% The transformation matrix is initialized to the identity matrix,
% representing no transformation.
% Each newly added transformation matrix is multiplied on the left
% of the current matrix. Thus, if transformations
% $T_1,T_2,\ldots,T_n$ are added in that order, the resulting matrix is
% \[
%   M=T_nT_{n-1}\cdots T_2T_1.
% \]
% When this composite matrix is applied to a coordinate column vector,
% the transformations are performed in the order in which they were added.
% Translation offsets, scaling factors, shear coefficients, and angles
% are all evaluated as floating-point expressions by \pkg{l3fp}.
% Updates to transformation matrices are global and are not reverted
% when an ordinary \TeX{} group ends.
%
%
% \subsubsection{Building Transformations}
%
% The following commands initialize the transformation matrix
% and add various geometric transformations.
% Each transformation is composed with the current transformation matrix.
% The two-dimensional and three-dimensional versions of a command
% must be used with the corresponding transformation dimension.
%
% \begin{function}[added=2026-11-21]{\transform_init:n}
%   \begin{syntax}
%     \cs{transform_init:n} \Arg{dimension}
%   \end{syntax}
%   Initializes the current transformation matrix.
%   \meta{dimension} specifies the dimension of the transformation
%   space and must be either $2$ or $3$.
%   After initialization, the current transformation matrix is set
%   to the identity matrix of the corresponding dimension,
%   indicating that no geometric transformation has been added.
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_save:n, \transform_restore:n}
%   \begin{syntax}
%     \cs{transform_save:n}    \Arg{name}
%     \cs{transform_restore:n} \Arg{name}
%   \end{syntax}
%   Saves or restores the current transformation matrix.
%
%   \cs{transform_save:n} saves the current transformation matrix
%   under the specified name. If a record with that name already exists,
%   it is updated with the current matrix.
%
%   \cs{transform_restore:n} restores the transformation matrix
%   previously saved under the specified name.
%   If no record exists for that name, no restoration is performed.
% \end{function}
%
% \begin{function}[added=2026-11-21]{\transform_shift:nn, \transform_shift:nnn}
%   \begin{syntax}
%     \cs{transform_shift:nn}  \Arg{dx} \Arg{dy}
%     \cs{transform_shift:nnn} \Arg{dx} \Arg{dy} \Arg{dz}
%   \end{syntax}
%   Adds a translation, for two-dimensional and three-dimensional
%   spaces, respectively. The offsets must be numerical values
%   without units.
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \transform_scale:nn, \transform_scale:nnn,
%     \transform_scale:nnnn, \transform_scale:nnnnnn
%   }
%   \begin{syntax}
%     \cs{transform_scale:nn} \Arg{sx} \Arg{sy}
%     \cs{transform_scale:nnn} \Arg{sx} \Arg{sy} \Arg{sz}
%     \cs{transform_scale:nnnn} \Arg{x} \Arg{y} \Arg{sx} \Arg{sy}
%     \cs{transform_scale:nnnnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{sx} \Arg{sy} \Arg{sz}
%   \end{syntax}
%   Adds a scaling transformation about the origin or a specified point,
%   for two-dimensional and three-dimensional spaces, respectively.
%   To perform a reflection, set the corresponding scaling factor to $-1$.
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \transform_shear:nn, \transform_shear:nnnn,
%     \transform_shear:nnnnnn, \transform_shear:nnnnnnnnn
%   }
%   \begin{syntax}
%     \cs{transform_shear:nn} \Arg{xy coefficient} \Arg{yx coefficient}
%     \cs{transform_shear:nnnn} \Arg{x} \Arg{y} \Arg{xy coefficient} \Arg{yx coefficient}
%     \cs{transform_shear:nnnnnn}
%     \  \Arg{xy coefficient} \Arg{xz coefficient}
%     \  \Arg{yx coefficient} \Arg{yz coefficient}
%     \  \Arg{zx coefficient} \Arg{zy coefficient}
%     \cs{transform_shear:nnnnnnnnn} \Arg{x} \Arg{y} \Arg{z}
%     \  \Arg{xy coefficient} \Arg{xz coefficient}
%     \  \Arg{yx coefficient} \Arg{yz coefficient}
%     \  \Arg{zx coefficient} \Arg{zy coefficient}
%   \end{syntax}
%   Adds a shear transformation about the origin or a specified point,
%   for two-dimensional and three-dimensional spaces, respectively.
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \transform_rotate:n, \transform_rotated:n,
%     \transform_rotate:nnn, \transform_rotated:nnn
%   }
%   \begin{syntax}
%     \cs{transform_rotate:n} \Arg{angle in radians}
%     \cs{transform_rotated:n} \Arg{angle in degrees}
%     \cs{transform_rotate:nnn} \Arg{x} \Arg{y} \Arg{angle in radians}
%     \cs{transform_rotated:nnn} \Arg{x} \Arg{y} \Arg{angle in degrees}
%   \end{syntax}
%   Adds a two-dimensional rotation about the origin or a specified point.
%   Positive angles indicate counterclockwise rotation.
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \transform_rotate_x:n, \transform_rotate_y:n, \transform_rotate_z:n,
%     \transform_rotated_x:n, \transform_rotated_y:n, \transform_rotated_z:n,
%     \transform_rotate_x:nnnn, \transform_rotate_y:nnnn, \transform_rotate_z:nnnn,
%     \transform_rotated_x:nnnn, \transform_rotated_y:nnnn, \transform_rotated_z:nnnn
%   }
%   \begin{syntax}
%     \cs{transform_rotate_x:n} \Arg{angle in radians}
%     \cs{transform_rotate_y:n} \Arg{angle in radians}
%     \cs{transform_rotate_z:n} \Arg{angle in radians}
%     \cs{transform_rotated_x:n} \Arg{angle in degrees}
%     \cs{transform_rotated_y:n} \Arg{angle in degrees}
%     \cs{transform_rotated_z:n} \Arg{angle in degrees}
%     \cs{transform_rotate_x:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in radians}
%     \cs{transform_rotate_y:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in radians}
%     \cs{transform_rotate_z:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in radians}
%     \cs{transform_rotated_x:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in degrees}
%     \cs{transform_rotated_y:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in degrees}
%     \cs{transform_rotated_z:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{angle in degrees}
%   \end{syntax}
%   Adds a three-dimensional rotation about the $x$, $y$, or $z$ axis.
%   The rotation center is either the origin or a specified point.
%   Positive angles follow the right-hand rule.
% \end{function}
%
%
% \subsubsection{Refreshing Transformations}
%
% When building transformations, the transformation commands only
% update the current transformation matrix. They do not automatically
% update the transformation functions used to calculate coordinates.
% The transformation functions must therefore be refreshed explicitly
% after the matrix has been constructed.
%
% \textbf{Refreshing uses local function definitions.}
% If a refresh is performed within a \TeX{} group, the transformation
% functions revert to their definitions from before the group
% when the group ends.
%
% \begin{function}[added=2026-11-21]{\transform_refresh:, \transform_refresh:N}
%   \begin{syntax}
%     \cs{transform_refresh:}
%     \cs{transform_refresh:N} \meta{matrix}
%   \end{syntax}
%   Refreshes the corresponding transformation functions using
%   the specified transformation matrix.
%   The specified matrix must be a valid homogeneous transformation
%   matrix: a $3\times3$ matrix for a two-dimensional transformation
%   or a $4\times4$ matrix for a three-dimensional transformation.
%   In either case, the bottom-right element must be equal to $1$.
% \end{function}
%
%
% \subsubsection{Applying Transformations}
%
% Unlike the transformation matrix, this module always maintains
% two sets of transformation functions: one for two-dimensional
% transformations and one for three-dimensional transformations.
% Therefore, two-dimensional and three-dimensional transformations
% can be used independently.
%
% \begin{function}[EXP, added=2026-11-21]{\transform_apply:nnn, \transform_apply:nnnn}
%   \begin{syntax}
%     \cs{transform_apply:nnn} \Arg{x} \Arg{y} \Arg{suffix}
%     \cs{transform_apply:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{suffix}
%   \end{syntax}
%   Applies the corresponding transformation to the specified
%   coordinates and outputs the result.
%   The coordinates should be plain numbers without units.
%   The resulting components are output in order, separated by commas,
%   and do not include the homogeneous component.
%   \meta{suffix} is appended to each transformed coordinate component,
%   for example, to add a unit. It may be left empty when no additional
%   content is required.
% \end{function}
%
% \begin{function}[EXP, added=2026-11-21]{\transform_apply:nn}
%   \begin{syntax}
%     \cs{transform_apply:nn} \Arg{coordinates} \Arg{suffix}
%   \end{syntax}
%   Applies the corresponding transformation to the specified
%   coordinates and outputs the result.
%   \meta{coordinates} is a comma-separated list of coordinate
%   components, containing either two or three components for
%   two-dimensional or three-dimensional transformations, respectively.
%   The coordinates should be plain numbers without units.
%   The resulting components are output in order, separated by commas,
%   and do not include the homogeneous component.
%   \meta{suffix} is appended to each transformed coordinate component,
%   for example, to add a unit. It may be left empty when no additional
%   content is required.
% \end{function}
% 
% \begin{function}[added=2026-11-23]{\transform_apply_inline:nnn, \transform_apply_inline:nnnn}
%   \begin{syntax}
%     \cs{transform_apply_inline:nnn}  \Arg{x} \Arg{y} \Arg{code}
%     \cs{transform_apply_inline:nnnn} \Arg{x} \Arg{y} \Arg{z} \Arg{code}
%   \end{syntax}
%   Applies the current transformation to the given coordinates and passes the
%   transformed coordinate components to \meta{code} for execution.
%   Within \meta{code}, the transformed coordinate components can be referenced
%   as |#1|, |#2|, and (for the three-dimensional variant only) |#3|.
%   The coordinates should be plain numbers without units.
%   This function cannot be called recursively (i.e., it must not be invoked
%   from within its own \meta{code} argument).
% \end{function}
%
%
% \subsection{\LaTeX2e Commands}
%
% This section describes the \LaTeX2e-style command interfaces.
%
% \subsubsection{Initialization and Saving or Restoring}
%
% \begin{function}[added=2026-11-21]{\TransformInit}
%   \begin{syntax}
%     \cs{TransformInit} \oarg{dimension}
%   \end{syntax}
%   Initializes the current transformation matrix to the identity matrix.
%   \meta{dimension} specifies the transformation dimension and must
%   be either |2| or |3|. If omitted, it defaults to |2|.
%   Two-dimensional transformations use a $3\times3$ identity matrix,
%   while three-dimensional transformations use a $4\times4$ identity matrix.
%   This command should be called to initialize the transformation
%   state at the beginning of each new sequence of transformations.
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformSave, \TransformRestore}
%   \begin{syntax}
%     \cs{TransformSave} \Arg{name}
%     \cs{TransformRestore} \Arg{name}
%   \end{syntax}
%   \cs{TransformSave} saves the current transformation matrix
%   under the specified \meta{name}, while \cs{TransformRestore}
%   restores the transformation matrix previously saved under
%   the same name.
%   The name distinguishes different saved records.
%   If no record exists for the specified name,
%   \cs{TransformRestore} performs no restoration.
% \end{function}
%
%
% \subsubsection{Translation, Scaling, and Shearing}
%
% \begin{function}[added=2026-11-21]{\TransformShift}
%   \begin{syntax}
%     \cs{TransformShift} \Arg{dx, dy}
%     \cs{TransformShift} \Arg{dx, dy, dz}
%   \end{syntax}
%   Adds a translation. The two-dimensional form uses horizontal
%   and vertical offsets |dx| and |dy|; the three-dimensional form
%   also includes the depth offset |dz|.
%   The number of parameters must match the current transformation dimension.
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformScale}
%   \begin{syntax}
%     \cs{TransformScale} \oarg{center} \Arg{scale}
%     \cs{TransformScale} \oarg{center} \Arg{sx, sy}
%     \cs{TransformScale} \oarg{center} \Arg{sx, sy, sz}
%   \end{syntax}
%   Adds a scaling transformation.
%   A single scaling factor applies the same scale to all directions;
%   two factors specify the scaling factors for the two-dimensional axes,
%   and three factors specify the scaling factors for the
%   three-dimensional axes.
%   The optional argument \meta{center} specifies the scaling center.
%   For two-dimensional transformations, it has the form |x,y|;
%   for three-dimensional transformations, it has the form |x,y,z|.
%   If omitted, the origin is used as the center.
%   The number of scaling factors must match the current
%   transformation dimension, except when a single uniform
%   scaling factor is supplied.
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformShear}
%   \begin{syntax}
%     \cs{TransformShear} \oarg{center} \Arg{a, b}
%     \cs{TransformShear} \oarg{center} \Arg{a, b, c, d, e, f}
%   \end{syntax}
%   Adds a shear transformation.
%   The two-dimensional form uses two coefficients and corresponds
%   to the transformation matrix
%   $\left[\begin{smallmatrix}1&a&0\\b&1&0\\0&0&1\end{smallmatrix}\right]$.
%   The three-dimensional form uses six coefficients and corresponds
%   to the transformation matrix
%   $\left[\begin{smallmatrix}1&a&b&0\\c&1&d&0\\e&f&1&0\\0&0&0&1\end{smallmatrix}\right]$.
%   The optional argument \meta{center} specifies the shear center.
%   Its format is the same as that used by \cs{TransformScale}.
%   If omitted, the origin is used as the center.
%   The number of coefficients must match the current
%   transformation dimension.
% \end{function}
%
%
% \subsubsection{Rotations}
%
% \begin{function}[added=2026-11-21]{\TransformRotate, \TransformRotated}
%   \begin{syntax}
%     \cs{TransformRotate}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotated} \oarg{center} \Arg{angle in degrees}
%   \end{syntax}
%   Adds a two-dimensional rotation. The current transformation
%   must be two-dimensional.
%   The optional argument \meta{center} specifies the rotation center
%   in the form |x, y|. If omitted, the rotation is performed
%   about the origin.
% \end{function}
%
% \begin{function}[added=2026-11-21]
%   {
%     \TransformRotateX, \TransformRotateY, \TransformRotateZ,
%     \TransformRotatedX, \TransformRotatedY, \TransformRotatedZ
%   }
%   \begin{syntax}
%     \cs{TransformRotateX}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotateY}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotateZ}  \oarg{center} \Arg{angle in radians}
%     \cs{TransformRotatedX} \oarg{center} \Arg{angle in degrees}
%     \cs{TransformRotatedY} \oarg{center} \Arg{angle in degrees}
%     \cs{TransformRotatedZ} \oarg{center} \Arg{angle in degrees}
%   \end{syntax}
%   Adds a three-dimensional rotation about the $x$, $y$, or $z$ axis.
%   The current transformation must be three-dimensional.
%   The optional argument \meta{center} specifies the rotation center
%   in the form |x, y, z|. If omitted, the rotation is performed
%   about the origin.
% \end{function}
%
%
% \subsubsection{Refreshing and Applying Transformations}
%
% Transformation commands only update the current transformation matrix.
% To convert the matrix coefficients into callable coordinate
% transformation functions, a refresh operation is required.
% The application commands can then be used to calculate coordinates.
%
% \begin{function}[added=2026-11-21]{\TransformRefresh}
%   \begin{syntax}
%     \cs{TransformRefresh}
%     \cs{TransformRefresh} \oarg{matrix}
%   \end{syntax}
%   Refreshes the internal coordinate transformation functions
%   using a transformation matrix.
%   If the optional argument is omitted, the current transformation
%   matrix is used. If \meta{matrix} is specified, the transformation
%   functions are extracted from that matrix instead.
%   A two-dimensional transformation matrix must be $3\times3$,
%   and a three-dimensional transformation matrix must be $4\times4$.
%   In both cases, the bottom-right element must be equal to $1$;
%   otherwise, an invalid transformation matrix error is reported.
% \end{function}
%
% \begin{function}[added=2026-11-21]{\TransformApply}
%   \begin{syntax}
%     \cs{TransformApply} \oarg{suffix} \Arg{coordinates}
%   \end{syntax}
%   Applies the corresponding transformation to the specified
%   coordinates and outputs the result.
%   \meta{coordinates} is a comma-separated list of coordinate
%   components, containing either two or three components for
%   two-dimensional or three-dimensional transformations, respectively.
%   Coordinate components must be numerical values without units.
%   The resulting components are output in order, separated by commas,
%   and do not include the homogeneous component.
%   \meta{suffix} is appended to each transformed coordinate component,
%   for example, to add a unit. It may be left empty when no additional
%   content is required.
% \end{function}
%
%
% \subsection{Error Conditions}
%
% The current implementation reports an error under the following
% conditions:
% \begin{itemize}
%   \item The number of rows or columns is negative when creating a matrix;
%   \item The matrix variable has already been defined when creating a matrix;
%   \item A constant matrix is used as the target of a modification operation;
%   \item The number of items in the element list exceeds the total number
%         of elements in the matrix when creating or setting a matrix;
%   \item The dimensions of the two operands do not match when performing
%         an element-wise binary operation between matrices;
%   \item The number of columns in the left operand does not match
%         the number of rows in the right operand when performing
%         matrix multiplication;
%   \item The number of arguments supplied to a geometric transformation
%         does not match the corresponding two-dimensional or
%         three-dimensional form;
%   \item The matrix supplied when refreshing a transformation is not
%         a valid $3\times3$ or $4\times4$ transformation matrix,
%         or its bottom-right element is not equal to $1$.
% \end{itemize}
%
% \cleardoublepage{}
%
% 
% \end{documentation}
%
%
% ^^A ---------------------------------- 实现部分 ----------------------------------
%
% \begin{implementation}
% 
% \changes{v0.2}{2026-11-20}{更新设计逻辑}
%
% \section{实现设计}
%
% 一个矩阵对象由矩阵描述符和矩阵数据两部分组成。
% 矩阵描述符中记录了矩阵的行数、列数、元素总数以及矩阵数据的逻辑索引；
% 矩阵数据则存储在某个 \pkg{fparray} 数组中。
% 
% 矩阵变量直接存储矩阵描述符，矩阵操作函数则通过矩阵描述符找到矩阵数据。
% 
% 由于 \pkg{fparray} 数组的修改采用全局操作，
% 为保证矩阵描述符与矩阵数据的更新保持同步，矩阵的创建和修改也均采用全局操作。
% 即矩阵的创建和修改不会因普通 \TeX{} 分组结束而自动恢复。
%
% \subsection{矩阵描述符}
%
% 矩阵描述符是一个特殊的凭据表，由以下四个整数字段组成：
% |<length>|、|<rows>|、|<columns>| 和 |<space-index>|。
% 各字段之间使用专用扫描标记分隔，并以结束标记结尾。
% 各字段的含义如下：
% \begin{description}
%   \item[|<length>|] 矩阵的元素总数，即行数与列数的乘积；
%   \item[|<rows>| / |<columns>|] 矩阵的行数和列数；
%   \item[|<space-index>|] 矩阵数据所在空间的逻辑索引。
% \end{description}
%
% \subsection{存储管理}
%
% 矩阵数据存储在 \pkg{fparray} 数组的一段连续空间中。
% 存储管理器负责这段空间的分配、回收与定位。
%
% \subsubsection{存储类别}
%
% 通常，存储管理器将固定长度的\pkg{fparray} 数组按照固定的
% 空间大小（|<size>|）划分为若干个独立空间，每个空间用于存储一个矩阵。
% 目前，存储管理器提供 $A$、$B$、$C$ 三种常规存储类别，
% 分别用于存储小型、中型和大型矩阵。 
% 对于超大型矩阵，则采用特殊的 $D$ 类存储，为每个矩阵单独创建数组。
%
% 各类别的当前参数如下：
% \begin{center}
%   \begin{tabular}{c|r|r|r}
%     类别 & 数组大小 & 空间的大小 & 独立空间数 \\
%     \hline
%     A & 4000 & 20 & 200 \\
%     B & 4000 & 100 & 40 \\
%     C & 4000 & 1000 & 4 \\
%     D & 矩阵长度& 矩阵长度 & 1 
%   \end{tabular}
% \end{center}
%
% \subsubsection{空间定位}
%
% 矩阵实际使用存储类别由其元素总数所决定，具体划分如下：
% 元素总数不超过 $20$ 时使用 $A$ 类；超过 $20$ 且不超过 $100$ 时使用 $B$ 类；
% 超过 $100$ 且不超过 $1000$ 时使用 $C$ 类；超过 $1000$ 时使用 $D$ 类。
%
% 存储类别与逻辑索引共同确定矩阵数据空间的实际位置：实际数组及偏移量。
% 
% 数组名称由存储类别字母和数组编号组成（|\g_@@_<class>_<sn>_fparray|）。
% 存储管理器根据逻辑索引以及该类别每个数组所能容纳的空间数（$C_{|<class>|}$），
% 计算矩阵所属的数组编号，其公式如下：
% \footnote{ \pkg{expl3} 宏包暂时还没有实现向上取整及向下取整，所以使用截断除来处理。}
% \[
%   |<sn>| = \left\lceil \frac{|<space-index>|} {C_{|<class>|}} \right\rceil 
%          = \left\lfloor \frac{|<space-index>| - 1} {C_{|<class>|}} \right\rfloor + 1
% \]
% 
% 逻辑索引对应数组的第 $k$ 个空间及其偏移量的公式如下：
% \[
% \begin{aligned}
%   k = |<space-index>| - (|<sn>| - 1)&C_{|<class>|}\\
%   |<offset>| = (k - 1)&S_{|<class>|}
% \end{aligned}
% \] 
% 
% 对于 $D$ 类，这三个公式可以简化：
% \[
% \begin{aligned}
%   |<sn>| &= |<space-index>|\\
%   k &= 1\\
%   |<offset>| &= 0
% \end{aligned}
% \] 
% 
% \subsubsection{元素定位}
% 
% 矩阵数据是按行顺序存储的，因此，对于任意矩阵 $A$，其第 $i$ 行第 $j$ 列的元素，
% 其位置由以下公式确定：
% 
% \[
% \begin{aligned}
%   |<element-index>| &= (i - 1)|<columns>| + j\\
%   |<array-index>| &= |<offset>| + |<element-index>|
% \end{aligned}
% \] 
% 
% \subsubsection{分配与回收}
%
% 空间分配优先使用回收序列中的索引；没有可回收空间时，才使用该类别的
% 下一个索引。
% 元素总数为零时不分配实际空间。普通空矩阵使用索引 $0$，常量空矩阵使用索引
% $-1$；这两个值是空矩阵的逻辑标记，不对应任何数组或数组区间。
%
% 回收时，$A$、$B$、$C$ 类只将空间索引放回对应的回收序列，底层数组继续保留，
% 后续分配可以复用该空间。$D$ 类回收时，除登记索引外，还会删除对应的独立数组。
% 空间回收函数本身不判断矩阵是否为常量；常量空间是否允许回收由矩阵操作层保证。
%
%
% \subsection{常量矩阵}
%
% 常量矩阵不使用额外的常量标记字段。宏包使用一个全局序列保存常量矩阵的
% 完整描述符内容，并通过描述符内容比较判断矩阵是否为常量。
% 修改矩阵之前，矩阵操作层检查目标描述符是否存在于该序列中；匹配时报告
% 常量矩阵错误，并中止当前操作。
%
% 这种识别方式以描述符内容而不是控制序列名称为依据。因此，复制或等价引用
% 常量矩阵描述符后，只要描述符内容不变，仍会被识别为常量矩阵。
% 常量矩阵的非空数据仍按正常类别规则分配空间。
%
% 常量保护由矩阵操作接口实现，存储管理器不承担常量判断。该保护针对宏包
% 提供的矩阵修改接口，不等同于对底层 \pkg{fparray} 变量的访问控制。
%
% \subsection{零矩阵与空矩阵}
%
% 零矩阵是所有元素均为零的矩阵，不是一种单独的存储类别。零矩阵仍按其元素
% 总数选择 $A$、$B$、$C$ 或 $D$ 类空间；归零操作直接将其现有空间置零。
%
% 空矩阵是元素总数为零的合法矩阵，包括 $0\times0$、$m\times0$ 和
% $0\times n$。空矩阵不分配实际存储空间，但行数和列数仍保留在描述符中。
% 因此，不同维度的空矩阵仍是不同形状的矩阵，参与运算时仍须执行维度兼容性检查。
%
% 对于结果元素总数为零的运算，结果描述符仍会按目标维度更新，但不会分配空间。
%
%
% 
% \section{宏包准备}
%  
%    \begin{macrocode}
%<*package>
%<@@=matrix>
%    \end{macrocode}
%
%    \begin{macrocode}
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{lt3matrix}{2026-11-23}{0.2} 
  {a lightweight global matrix base-on fparray}
%    \end{macrocode}
%
%
% 定义一些 \pkg{expl3} 函数的常用变体。 
%    \begin{macrocode}
\cs_generate_variant:Nn \iow_log:n { V, e }
%    \end{macrocode}
%
%  
% \begin{variable}{\l_@@_tmpa_int, \l_@@_tmpb_int, \l_@@_tmpa_fp, \l_@@_tmpb_fp,
%                  \l_@@_tmpa_tl,  \l_@@_tmpb_tl, \g_@@_tmpa_tl, \g_@@_tmpb_tl}
% 定义一些通用的临时变量。
%    \begin{macrocode}
\int_new:N  \l_@@_tmpa_int
\int_new:N  \l_@@_tmpb_int
\fp_new:N   \l_@@_tmpa_fp
\fp_new:N   \l_@@_tmpb_fp
\tl_new:N   \l_@@_tmpa_tl
\tl_new:N   \l_@@_tmpb_tl
\tl_new:N   \g_@@_tmpa_tl
\tl_new:N   \g_@@_tmpb_tl
%    \end{macrocode}
% \end{variable}
%   
%  
%    
% \section{核心命令}
% 
% \subsection{消息}
%
% 
% \begin{variable}{negative-dimension, constant-matrix, too-many-elements, 
%   dimension-mismatch, invalid-transformation-matrix}
% 错误消息。
%    \begin{macrocode}
\msg_new:nnn { matrix } { negative-dimension }
  {
    Matrix~dimensions('#1')~must~be~non-negative:.
  }
\msg_new:nnn { matrix } { constant-matrix }
  {
    The~matrix('#1')~is~constant~and~cannot~be~modified.
  }
\msg_new:nnn { matrix } { too-many-elements }
  {
    The~input~list('#1')~contains~too~many~elements.
  }
\msg_new:nnn { matrix } { dimension-mismatch }
  {
    The~dimensions~of~the~operands('#1')~are~incompatible~for~this~operation.
  }
\msg_new:nnn { matrix } { invalid-transformation-matrix }
  { The~transformation~matrix('#1')~is~not~a~valid~transformation~matrix. }
%    \end{macrocode}
% \end{variable}
% 
% 
% \subsection{元信息}
%
% 矩阵的元信息包含总长度、行数、列数及实际数据索引，索引指向实际数据。
% 
% \subsubsection{基础变量}
% 
% \begin{variable}{\s_@@, \s_@@_mark, \s_@@_stop}
% 存储数据用的扫描标记。
%    \begin{macrocode}
\scan_new:N \s_@@
\scan_new:N \s_@@_mark
\scan_new:N \s_@@_stop
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\g_@@_const_seq}
% 常量矩阵登记表。
%    \begin{macrocode}
\seq_new:N \g_@@_const_seq
%    \end{macrocode}
% \end{variable}
% 
% 
% \begin{variable}{\l_@@_length_int, \l_@@_row_int, \l_@@_col_int, \l_@@_index_int}
% 保存矩阵信息的临时变量。
%    \begin{macrocode}
\int_new:N  \l_@@_length_int
\int_new:N  \l_@@_row_int
\int_new:N  \l_@@_col_int
\int_new:N  \l_@@_index_int
%    \end{macrocode}
% \end{variable}
% 
%
% \subsubsection{设置信息}
% 
% \begin{macro}{\@@_meta_gset:N, \@@_meta_gset:Nnnnn}
% 将 |#1| 的值设置为矩阵基本信息。
%    \begin{macrocode}
\cs_new:Nn \@@_meta_gset:Nnnnn
  {
    \tl_gset:Nn #1 
      { 
        \s_@@      #2 % Length
        \s_@@_mark #3 % Row Count
        \s_@@_mark #4 % Column Count
        \s_@@_mark #5 % Index
        \s_@@_stop 
      }
  }
\cs_generate_variant:Nn \@@_meta_gset:Nnnnn { NVVVV }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。   
%    \begin{macrocode}
\cs_new:Nn \@@_meta_gset:N
  {
    \@@_meta_gset:NVVVV #1
      \l_@@_length_int
      \l_@@_row_int
      \l_@@_col_int
      \l_@@_index_int
  }
%    \end{macrocode}
% \end{macro}
%
% 
% \subsubsection{解析信息}
% 
% \begin{macro}{\@@_meta_parse_aux:w}
% 解析矩阵信息，并结果设置到 |#5~#8|。
%    \begin{macrocode}
\cs_new:Npn \@@_meta_parse_aux:w
  \s_@@      #1 \s_@@_mark #2
  \s_@@_mark #3 \s_@@_mark #4
  \s_@@_stop #5#6#7#8
  {
    \int_set:Nn #5 {#1}
    \int_set:Nn #6 {#2}
    \int_set:Nn #7 {#3}
    \int_set:Nn #8 {#4}
  } 
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_meta_parse:N, \@@_meta_parse:NNNNN}
% 解析矩阵信息，并结果设置到 |#2~#5|。
%    \begin{macrocode}
\cs_new:Nn \@@_meta_parse:NNNNN 
  {
    \exp_last_unbraced:NV 
      \@@_meta_parse_aux:w #1 #2#3#4#5
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。   
%    \begin{macrocode}
\cs_new:Nn \@@_meta_parse:N
  {
    \exp_last_unbraced:NV 
      \@@_meta_parse_aux:w #1
      \l_@@_length_int
      \l_@@_row_int
      \l_@@_col_int
      \l_@@_index_int
  }
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{维度判定}
% 
% 
% \begin{macro}{\@@_meta_if_empty:w}
% 判定矩阵长度是否为空。
%    \begin{macrocode}
\cs_new:Npn \@@_meta_if_empty:w
  \s_@@      #1 \s_@@_mark #2
  \s_@@_mark #3 \s_@@_mark #4
  \s_@@_stop
  { \int_compare_p:nNn {#1} = {0} } 
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_meta_if_square:w}
% 判定矩阵是否为方阵。
%    \begin{macrocode}
\cs_new:Npn \@@_meta_if_square:w
  \s_@@      #1 \s_@@_mark #2
  \s_@@_mark #3 \s_@@_mark #4
  \s_@@_stop
  { \int_compare_p:nNn {#2} = {#3} }   
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_meta_if_same_dim:w}
% 判定两个矩阵的行列数是否相等。
%    \begin{macrocode}
\cs_new:Npn \@@_meta_if_same_dim:w
  \s_@@      #1 \s_@@_mark #2
  \s_@@_mark #3 \s_@@_mark #4
  \s_@@_stop
  \s_@@      #5 \s_@@_mark #6
  \s_@@_mark #7 \s_@@_mark #8
  \s_@@_stop
  { 
    \bool_lazy_and_p:nn
      { \int_compare_p:nNn {#2} = {#6} } 
      { \int_compare_p:nNn {#3} = {#7} }
  } 
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsection{数据池}
% 
% \changes{v0.2}{2026-11-19}{将空间基址改为空间偏移量}
% 
% 在实际实现中，每个 \texttt{fparray} 按固定大小划分为若干空间，
% 每个空间只存储一份矩阵数据。根据空间大小，预定义
% 小（$A$ 类）、中（$B$ 类）和大（$C$ 类）三种存储池，
% 以适应不同大小的矩阵。
% 
%
% 对于元素总数超过 $C$ 类空间容量的矩阵（$D$ 类），
% 则为每个矩阵单独分配一个数组。
% 
% \subsubsection{常量}
% 
% 
% \begin{variable}{\c_@@_size_a_int, \c_@@_size_b_int, \c_@@_size_c_int}
% 三种划分 \pkg{fparray} 对应的空间大小。
%    \begin{macrocode}
\int_const:Nn \c_@@_size_a_int { 20   }
\int_const:Nn \c_@@_size_b_int { 100  }
\int_const:Nn \c_@@_size_c_int { 1000 }
%    \end{macrocode}
% \end{variable}
% 
% 
% \begin{variable}{\c_@@_array_length_int}
% 每个 \pkg{fparray} 变量的长度。
%    \begin{macrocode}
\int_const:Nn \c_@@_array_length_int { 4000 }
%    \end{macrocode}
% \end{variable}
% 
%  
% \begin{variable}{\c_@@_count_a_int, \c_@@_count_b_int, \c_@@_count_c_int}
% 单个 \pkg{fparray} 实际划分出的空间数量。
%    \begin{macrocode}
\int_const:Nn \c_@@_count_a_int 
  { \c_@@_array_length_int / \c_@@_size_a_int }
\int_const:Nn \c_@@_count_b_int
  { \c_@@_array_length_int / \c_@@_size_b_int }
\int_const:Nn \c_@@_count_c_int
  { \c_@@_array_length_int / \c_@@_size_c_int }
%    \end{macrocode}
% \end{variable}
% 
% 
% \subsubsection{变量}
% 
% \begin{variable}{\g_@@_a_i_fparray, \g_@@_b_i_fparray, \g_@@_c_i_fparray, \g_@@_d_i_fparray}
% 四种数据池演示，当前不实际申请（使用时自动申请）。\\
% 数据池的变量名形式为：\cs[no-index]{g_@@_<class>_<array index>_faparray}。
%    \begin{macrocode}
%<demo>\fparray_new:Nn \g_@@_a_i_fparray { \c_@@_array_length_int }
%<demo>\fparray_new:Nn \g_@@_b_i_fparray { \c_@@_array_length_int }
%<demo>\fparray_new:Nn \g_@@_c_i_fparray { \c_@@_array_length_int }
%<demo>\fparray_new:Nn \g_@@_d_i_fparray { \l_@@_length_int }
%    \end{macrocode}
% \end{variable}
% 
%  
% 
% \begin{variable}{\g_@@_next_a_int, \g_@@_next_b_int, \g_@@_next_c_int, \g_@@_next_d_int}
% 数据池中的下一个空间索引。
%    \begin{macrocode}
\int_new:N    \g_@@_next_a_int
\int_new:N    \g_@@_next_b_int
\int_new:N    \g_@@_next_c_int
\int_new:N    \g_@@_next_d_int
%    \end{macrocode} 
%    \begin{macrocode}
\int_gset:Nn  \g_@@_next_a_int { 1 }
\int_gset:Nn  \g_@@_next_b_int { 1 }
\int_gset:Nn  \g_@@_next_c_int { 1 }
\int_gset:Nn  \g_@@_next_d_int { 1 }
%    \end{macrocode}
% \end{variable}
% 
% 
% \begin{variable}{\g_@@_free_a_seq, \g_@@_free_b_seq, \g_@@_free_c_seq, \g_@@_free_d_seq}
% 数据池回收的空间索引。
%    \begin{macrocode}
\seq_new:N    \g_@@_free_a_seq
\seq_new:N    \g_@@_free_b_seq
\seq_new:N    \g_@@_free_c_seq
\seq_new:N    \g_@@_free_d_seq
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\l_@@_space_class_tl, \l_@@_space_size_int}
% 用于存储空间属性的临时变量。
%    \begin{macrocode}
\tl_new:N   \l_@@_space_class_tl
\int_new:N  \l_@@_space_size_int
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\l_@@_fparray, \l_@@_offset_int}
% 用于存储实际分配的空间的信息的临时变量。
%    \begin{macrocode}
\cs_new:Npn \l_@@_fparray {}
\int_new:N  \l_@@_offset_int
%    \end{macrocode}
% \end{variable}
%
%
% \subsubsection{空间选择}
% 
% \begin{macro}{\@@_space_select:, \@@_space_select:n}
% 查询某个长度（|#1|）的矩阵对应的空间的相关属性（类型与大小）。
% 结果保存在 \cs{l_@@_space_type_tl} 与 \cs{l_@@_space_size_int} 中。
%    \begin{macrocode}
\cs_new:Nn \@@_space_select:n
  {
    \int_compare:nNnTF {#1} > { \c_@@_size_a_int }
      {
        \int_compare:nNnTF {#1} > { \c_@@_size_b_int }
          {
            \int_compare:nNnTF {#1} > { \c_@@_size_c_int }
              { % Class D
                \tl_set:Nn  \l_@@_space_class_tl {d}
                \int_set:Nn \l_@@_space_size_int {#1}
              }
              {  % Class C
                \tl_set:Nn  \l_@@_space_class_tl {c}
                \int_set:Nn \l_@@_space_size_int { \c_@@_size_c_int }
              }          
          }
          { % Class B
              \tl_set:Nn  \l_@@_space_class_tl {b}
              \int_set:Nn \l_@@_space_size_int { \c_@@_size_b_int }
          }
      }
      {
        \int_compare:nNnTF {#1} = { 0 }
          { % Empty           
            \tl_set:Nn  \l_@@_space_class_tl {}
            \int_set:Nn \l_@@_space_size_int { 0 }
          }
          { % Class A
            \tl_set:Nn  \l_@@_space_class_tl {a}
            \int_set:Nn \l_@@_space_size_int { \c_@@_size_a_int }
          }
      }
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。
%    \begin{macrocode}
\cs_new:Nn \@@_space_select:
  {
    \@@_space_select:n { \l_@@_length_int }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{空间分配}
%
% \begin{macro}{\@@_space_allocate_aux:n}
% 获取下一个可用空间索引。|#1| 空间类型，结果保存在 \cs{l_@@_index_int} 中。
%    \begin{macrocode}
\cs_new:Nn \@@_space_allocate_aux:n 
  {
    \seq_if_empty:cTF { g_@@_free_#1_seq }
      { % 无回收空间，使用下一个空间
        \int_set_eq:Nc \l_@@_index_int { g_@@_next_#1_int  }
        \int_set_eq:Nc \l_@@_tmpa_int  { c_@@_count_#1_int }
        \int_compare:nNnT
          { \int_mod:nn { \l_@@_index_int } { \l_@@_tmpa_int } } = { 1 }
          { % 需要新数组
            \int_set:Nn \l_@@_tmpb_int
              { \int_div_truncate:nn { \l_@@_index_int - 1} { \l_@@_tmpa_int } }
            \int_incr:N \l_@@_tmpb_int
            \exp_args:Nc \fparray_new:Nn 
              { g_@@_#1_ \int_to_roman:n { \l_@@_tmpb_int } _fparray }
              { \c_@@_array_length_int }
          }
          \int_gincr:c { g_@@_next_#1_int }
      }
      { % 有回收空间，直接使用
        \seq_gpop_left:cN { g_@@_free_#1_seq } \l_@@_tmpa_tl
        \int_set:Nn \l_@@_index_int { \l_@@_tmpa_tl }
      }
  }
\cs_generate_variant:Nn \@@_space_allocate_aux:n { V }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_space_allocate_aux_d:n}
% 获取下一个可用空间索引（|D| 类型）。|#1| 空间大小，结果保存在 \cs{l_@@_index_int} 中。
%    \begin{macrocode}
\cs_new:Nn \@@_space_allocate_aux_d:n
  {
    \seq_if_empty:NTF \g_@@_free_d_seq
      { % 无回收空间，使用下一个空间
        \int_set_eq:NN \l_@@_index_int \g_@@_next_d_int 
        \int_gincr:N \g_@@_next_d_int
      }
      { % 有回收空间，直接使用
        \seq_gpop_left:NN \g_@@_free_d_seq \l_@@_tmpa_tl
        \int_set:Nn \l_@@_index_int { \l_@@_tmpa_tl }
      }
      \exp_args:Nc \fparray_new:Nn 
        { g_@@_d_ \int_to_roman:n { \l_@@_index_int } _fparray }
        { #1 }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_space_allocate:, \@@_space_allocate:n}
% 申请空间。|#1| 空间大小，结果保存在 \cs{l_@@_index_int} 中。
%    \begin{macrocode}
\cs_new:Nn \@@_space_allocate:n 
  {
    \int_compare:nNnTF {#1} = { 0 }
      { \int_set:Nn \l_@@_index_int { 0 } } % Emtpy Matrices
      { 
        \@@_space_select:n {#1}
        \int_compare:nNnTF {#1} > { \c_@@_size_c_int }
          { \@@_space_allocate_aux_d:n {#1} } % Class D
          { \@@_space_allocate_aux:V \l_@@_space_class_tl } % Class A ~ C
      }
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。
%    \begin{macrocode}
\cs_new:Nn \@@_space_allocate:
  {
    \@@_space_allocate:n { \l_@@_length_int }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{空间回收}
% 
% \begin{macro}{\@@_space_free_aux:nn}
% 回收空间辅助函数。|#1| 空间大小，|#2| 空间索引。
%    \begin{macrocode}
\cs_new:Nn \@@_space_free_aux:nn 
  {
    \@@_space_select:n {#1}
    \seq_gput_right:cn { g_@@_free_ \l_@@_space_class_tl _seq } {#2}
    \int_compare:nNnT {#1} > { \c_@@_size_c_int }
      { \cs_undefine:c { g_@@_d_ \int_to_roman:n { #2 } _fparray } }
  }
\cs_generate_variant:Nn \@@_space_free_aux:nn { VV } 
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_space_free:, \@@_space_free:nn}
% 回收空间函数。|#1| 空间大小，|#2| 空间索引。不负责常量空间校对。
%    \begin{macrocode}
\cs_new:Nn \@@_space_free:nn 
  {
    \int_compare:nNnT {#1} > {0}
      {
        \int_set:Nn \l_@@_tmpa_int {#1}
        \int_set:Nn \l_@@_tmpb_int {#2}
        \@@_space_free_aux:VV \l_@@_tmpa_int \l_@@_tmpb_int
      }    
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。
%    \begin{macrocode}
\cs_new:Nn \@@_space_free:
  {
    \@@_space_free:nn      
      { \l_@@_length_int }
      { \l_@@_index_int  } 
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{查询定位}
% 
% \begin{macro}{\@@_space_query_aux:nn}
% 查询指定类型的空间的信息。|#1| 空间类型，|#2| 空间索引。
% 结果保存在 \cs{l_@@_fparray} 与 \cs{l_@@_offset_int} 中。
%    \begin{macrocode}
\cs_new:Nn \@@_space_query_aux:nn
  {
    \int_set_eq:Nc \l_@@_tmpa_int { c_@@_count_#1_int }
    \int_set:Nn    \l_@@_tmpb_int
      { \int_div_truncate:nn { #2 - 1 } { \l_@@_tmpa_int } }
    \int_incr:N    \l_@@_tmpb_int
    \cs_set_eq:Nc  \l_@@_fparray 
      { g_@@_#1_ \int_to_roman:n { \l_@@_tmpb_int } _fparray }
    \int_set:Nn    \l_@@_offset_int
      { 
        \int_mod:nn { #2 - 1 } { \l_@@_tmpa_int } * 
        \int_use:c  { c_@@_size_#1_int }
      }      
  }
\cs_generate_variant:Nn \@@_space_query_aux:nn { V }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_space_query:, \@@_space_query:nn}
% 查询空间信息。|#1| 空间大小，|#2| 空间索引。
% 结果保存在 \cs{l_@@_fparray} 与 \cs{l_@@_offset_int} 中。
%    \begin{macrocode}
\cs_new:Nn \@@_space_query:nn
  {
    \@@_space_select:n {#1}
    \int_compare:nNnTF {#1} = { 0 }
      { 
        \int_zero:N \l_@@_offset_int
        \cs_set:Npn \l_@@_fparray {}         
      }
      {
        \int_compare:nNnTF {#1} > { \c_@@_size_c_int } 
          { % Class D
            \int_set:Nn   \l_@@_offset_int { 0 }
            \cs_set_eq:Nc \l_@@_fparray
              { g_@@_d_ \int_to_roman:n {#2} _fparray }
          } 
          { \@@_space_query_aux:Vn \l_@@_space_class_tl {#2} } % Class A ~ C
      }
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。
%    \begin{macrocode}
\cs_new:Nn \@@_space_query:
  {
    \@@_space_query:nn      
      { \l_@@_length_int }
      { \l_@@_index_int  } 
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{数据设置}
% 
% 
% 
% \begin{macro}{\@@_data_gzero:, \@@_data_gzero:Nnn}
% 将指定 \pkg{fparray} 的指定区间的数据置零。
% |#1| 数组，|#2| 偏移量，|#3| 长度。
%    \begin{macrocode}
\cs_new:Nn \@@_data_gzero:Nnn
  {
    \int_step_inline:nnn { #2 + 1 } { #2 + #3 }      
      { \fparray_gset:Nnn #1 {##1} { 0 } }
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。
%    \begin{macrocode}
\cs_new:Nn \@@_data_gzero:
  {
    \@@_data_gzero:Nnn \l_@@_fparray
      { \l_@@_offset_int } 
      { \l_@@_space_size_int }
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_data_gset:n, \@@_data_gset:Nnnn}
% 使用指定数据将填充 \pkg{fparray} 的指定区间，数据不足时补零。
% |#1| 数组，|#2| 偏移量，|#3| 长度，|#4| 指定数据。
%    \begin{macrocode}
\cs_new:Nn \@@_data_gset:Nnnn
  {
    \int_set:Nn \l_@@_tmpa_int { #2 + 1 } 
    \clist_map_inline:nn {#4} % Fill Data
      {
        \fparray_gset:Nnn #1 { \l_@@_tmpa_int } {##1}
        \int_incr:N \l_@@_tmpa_int
      }
    \int_step_inline:nnn      % Fill 0
      { \l_@@_tmpa_int } { #2 + #3 }
      { \fparray_gset:Nnn #1 {##1} { 0 } }
  }
%    \end{macrocode}
% 这是一个使用临时变量的简化版本。
%    \begin{macrocode}
\cs_new:Nn \@@_data_gset:n
  {
    \@@_data_gset:Nnnn \l_@@_fparray
      { \l_@@_offset_int } 
      { \l_@@_space_size_int }
      {#1}
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_data_gcopy:NnNnn}
% 使两块区域的内容完成相等。
% |#1| 目标数组，|#2| 目标偏移量，|#3| 源数组，|#4| 源偏移量，|#4| 长度。
%    \begin{macrocode}
\cs_new:Nn \@@_data_gcopy:NnNnn
  {
    \int_step_inline:nn {#5}
      {
        \fparray_gset:Nnn #1 { #2 + ##1 }
          { \fparray_item:Nn #3 { #4 + ##1 } }
      }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% 
% \subsubsection{数据输出}
% 
% 
% \begin{macro}{\@@_data_typeset:Nnnn}
% 在公式环境中排版 \pkg{fparray} 内容，命令本身不会自动切换到公式环境。
% |#1| 数组，|#2| 偏移量，|#3| 长度，|#4| 每行列数。
%    \begin{macrocode}
\cs_new:Nn \@@_data_typeset:Nnnn
  {
    \tl_set:Nn \l_@@_tmpa_tl 
      { \left( \begin{array}{*{#4}{c}} }
    \int_step_inline:nnn { #2 + 1 } { #2 + #3 }
      { 
        \tl_put_right:Ne \l_@@_tmpa_tl 
          { \fparray_item:Nn #1 {##1} }
        \int_compare:nNnTF 
          { \int_mod:nn { ##1 - #2 } { #4 } } = { 0 }
          { \tl_put_right:Nn \l_@@_tmpa_tl { \\ } } 
          { \tl_put_right:Nn \l_@@_tmpa_tl { & } }
      }
    \regex_replace_once:nnN { \c{\\}\Z } {} \l_@@_tmpa_tl 
    \tl_put_right:Nn \l_@@_tmpa_tl 
      { \end{array} \right) } 
    \l_@@_tmpa_tl  
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_data_log:Nnnn}
% 在日志中查看 \pkg{fparray} 内容。|#1| 数组，|#2| 偏移量，|#3| 长度，|#4| 每行列数。
%    \begin{macrocode}
\cs_new:Nn \@@_data_log:Nnnn
  {
    \int_set:Nn \l_@@_tmpa_int { 1 }
    \tl_set:Ne \l_@@_tmpa_tl 
      { <\int_use:N \l_@@_tmpa_int>:~ }
    \int_step_inline:nnn { #2 + 1 } { #2 + #3 }
      { 
        \tl_put_right:Ne \l_@@_tmpa_tl 
          { \fparray_item:Nn #1 {##1} }
        \int_compare:nNnTF 
          { \int_mod:nn { ##1 - #2 } { #4 } } = { 0 }
          { 
            \iow_log:V  \l_@@_tmpa_tl 
            \int_incr:N \l_@@_tmpa_int
            \tl_set:Ne  \l_@@_tmpa_tl 
              { <\int_use:N \l_@@_tmpa_int>:~ }
          } 
          { \tl_put_right:Nn \l_@@_tmpa_tl  { ,~ } }
      }  
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_data_log:Nnnnn}
% 在日志中查看 \pkg{fparray} 内容。|#1| 数组，|#2| 偏移量，|#3| 长度，|#4| 每行列数, |#5| 空间长度。
%    \begin{macrocode}
\cs_new:Nn \@@_data_log:Nnnnn
  { 
    \@@_data_log:Nnnn #1 {#2} {#3} {#4}
    \tl_set:Nn \l_@@_tmpa_tl { <unused>: }
    \int_step_inline:nnn { #3 + 1 } { #5 }
      { 
        \tl_put_right:Ne \l_@@_tmpa_tl 
          { ~\fparray_item:Nn #1 { #2 + ##1 }, }
      }
    \int_compare:nNnTF {#3} = {#5}
      { \tl_put_right:Nn \l_@@_tmpa_tl { ~none. }  }
      { \regex_replace_once:nnN { ,\Z } { . } \l_@@_tmpa_tl  }
    \iow_log:V \l_@@_tmpa_tl 
  }
%    \end{macrocode}
% \end{macro}
% 
% \section{矩阵命令}
% 
% 
% \subsection{创建与初始化矩阵}
% 
% 
% \subsubsection{创建矩阵}
% 
% \changes{v0.2}{2026-11-18}{增加无维度的创建函数}
% 
% \begin{macro}{\matrix_new:N, \matrix_new:Nnn, \matrix_new:cnn}
% 定义新矩阵。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_new:Nnn
  {
    \bool_lazy_or:nnTF
      { \int_compare_p:nNn {#2} < {0} }
      { \int_compare_p:nNn {#3} < {0} }
      { \msg_error:nnn { matrix } { negative-dimension } {#2x#3} }  
      {
        \tl_new:N #1  % 防止变量已使用
        \int_set:Nn \l_@@_length_int { #2 * #3 }
        \int_set:Nn \l_@@_row_int {#2}
        \int_set:Nn \l_@@_col_int {#3}
        \int_compare:nNnTF 
          { \l_@@_length_int } = { 0 }
          { \int_set:Nn \l_@@_index_int { 0 } } % Empty Matrices
          { % Non-Empty Matrices
            \@@_space_allocate:
            \@@_space_query:
            \@@_data_gzero:
          }
        \@@_meta_gset:N #1
      }
  }
\cs_generate_variant:Nn \matrix_new:Nnn { c }
%    \end{macrocode}
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_new:N
  { \matrix_new:Nnn #1 {0} {0} }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_const:Nnnn, \matrix_const:cnnn}
% 定义新矩阵。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_const:Nnnn
  {
    \bool_lazy_or:nnTF
      { \int_compare_p:nNn {#2} < {0} }
      { \int_compare_p:nNn {#3} < {0} }
      { \msg_error:nnn { matrix } { negative-dimension } {#2x#3} } 
      {
        \int_set:Nn \l_@@_length_int { #2 * #3 }
        \int_set:Nn \l_@@_row_int {#2}
        \int_set:Nn \l_@@_col_int {#3}
        \int_compare:nNnTF 
          { \clist_count:n {#4} } > { \l_@@_length_int } 
          { \msg_error:nnn { matrix } { too-many-elements } {#4} }          
          { 
            \tl_new:N #1  % 防止变量已使用
            \int_compare:nNnTF 
              { \l_@@_length_int } = { 0 }
              { \int_set:Nn \l_@@_index_int { -1 } } % Empty Matrices
              { % Non-Empty Matrices
                \@@_space_allocate:
                \@@_space_query:
                \@@_data_gset:n {#4}
              } 
            \@@_meta_gset:N #1
            \seq_gput_right:NV \g_@@_const_seq #1        
          }        
      }
  }
\cs_generate_variant:Nn \matrix_const:Nnnn { c }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{设置矩阵}
% 
% \begin{macro}{\matrix_gzero:N, \matrix_gzero:c}
% 矩阵归零。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gzero:N
  {    
    \seq_if_in:NVTF \g_@@_const_seq #1
      { \msg_error:nnn { matrix } { constant-matrix } {#1} }
      {
        \@@_meta_parse:N #1
        \int_compare:nNnT
          { \l_@@_length_int } > { 0 }
          { \@@_space_query: \@@_data_gzero: }
      }
  }
\cs_generate_variant:Nn \matrix_gzero:N { c }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_gset:Nn, \matrix_gset:cn}
% 设置矩阵的值。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gset:Nn 
  {
    \seq_if_in:NVTF \g_@@_const_seq #1
      { \msg_error:nnn { matrix } { constant-matrix } {#1} }
      {        
        \@@_meta_parse:N #1
        \int_compare:nNnTF 
          { \clist_count:n {#2} } > { \l_@@_length_int } 
          { \msg_error:nnn { matrix } { too-many-elements } {#2} }
          { \@@_space_query: \@@_data_gset:n {#2} }    
      }
  }
\cs_generate_variant:Nn \matrix_gset:Nn { c, Ne, ce }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{矩阵条件句}
% 
% \changes{v0.2}{2026-11-19}{添加矩阵条件句}
% 
% \begin{macro}{\@@_fp_case:nnnnnn}
% 浮点数条件分支。
% |#1| 要判定的浮点数，|#2| 为零的动作，|#3| 为 $1$ 的动作，|#4| 为 $-1$ 的动作，
% |#5| 为正且非 $1$ 的动作，|#6| 为负且非 $-1$ 的动作。
%    \begin{macrocode}
\cs_set:Nn \@@_fp_case:nnnnnn
  {
    \fp_compare:nNnTF { abs(#1) } < { 1e-12 }
      {#2}
      {
        \fp_compare:nNnTF {#2} > { 0 }
          { \fp_compare:nNnTF { abs(#1 - 1) } < { 1e-12 } {#3} {#5} }
          { \fp_compare:nNnTF { abs(#1 + 1) } < { 1e-12 } {#4} {#6} }
      }
  }
%    \end{macrocode}
% \end{macro}  
% 
% \begin{macro}{\@@_fp_case:nnnn}
% 浮点数条件分支。
% |#1| 要判定的浮点数，|#2| 为零的动作，|#3| 为 $1$ 的动作，|#4| 其余情况。
%    \begin{macrocode}
\cs_set:Nn \@@_fp_case:nnnn
  {
    \fp_compare:nNnTF { abs(#1) } < { 1e-12 }
      {#2}
      { \fp_compare:nNnTF { abs(#1 - 1) } < { 1e-12 } {#3} {#4} }
  }
%    \end{macrocode}
% \end{macro} 
% 
% \begin{macro}[pTF]{\@@_fp_if_eq:nn}
% 浮点数相等条件句。|#1| 浮点数$1$，|#2| 浮点数$2$。
%    \begin{macrocode}
\prg_set_conditional:Nnn  \@@_fp_if_eq:nn { p, T, F, TF }
  { 
    \fp_compare:nNnTF { abs(#1 - #2) } < { 1e-12 }
      { \prg_return_true:  }
      { \prg_return_false: }
 }
%    \end{macrocode}
% \end{macro} 
% 
% 
% \begin{macro}[pTF]{\matrix_if_empty:N, \matrix_if_square:N, 
%               \matrix_if_same_dim:NN, \matrix_if_same_dim:Nnn}
% 纯维度相关的矩阵条件句。
%    \begin{macrocode}
\prg_set_conditional:Nnn \matrix_if_empty:N  { p, T, F, TF }
  {  
    \bool_if:nTF 
      { \exp_last_unbraced:NV \@@_meta_if_empty:w #1 }
      { \prg_return_true:  }
      { \prg_return_false: }
  }
\prg_set_conditional:Nnn \matrix_if_square:N { p, T, F, TF }
  {  
    \bool_if:nTF 
      { \exp_last_unbraced:NV \@@_meta_if_square:w #1 }
      { \prg_return_true:  }
      { \prg_return_false: }
  }  
\prg_set_conditional:Nnn \matrix_if_same_dim:NN  { p, T, F, TF }
  {  
    \bool_if:nTF 
      { \exp_last_unbraced:Ne \@@_meta_if_same_dim:w  { #1#2 } }
      { \prg_return_true:  }
      { \prg_return_false: }
  }  
\prg_set_conditional:Nnn \matrix_if_same_dim:Nnn { p, T, F, TF }
  {  
    \@@_meta_gset:Nnnnn \g_@@_tmpa_tl {#2*#3} {#2} {#3} {0}
    \bool_if:nTF 
      {         
        \exp_last_unbraced:Ne \@@_meta_if_same_dim:w  
          { #1 \g_@@_tmpa_tl } 
      }
      { \prg_return_true:  }
      { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}  
%
% \begin{macro}[TF]{\matrix_if_zero:N}
% 指定矩阵是否为零矩阵。
%    \begin{macrocode}
\prg_set_conditional:Nnn \matrix_if_zero:N { T, F, TF }
  {
    \@@_meta_parse:N #1
    \int_compare:nNnTF { \l_@@_length_int } = { 0 }
      { \prg_return_false: }
      {
        \@@_space_query:
        \int_zero:N \l_@@_tmpa_int
        \int_step_inline:nn { \l_@@_length_int }
          {
            \fp_set:Nn \l_@@_tmpa_fp
              { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + ##1 } }
            \@@_fp_if_eq:nnF { \l_@@_tmpa_fp } { 0 }
              { \int_incr:N \l_@@_tmpa_int \prg_break: }
          }
        \prg_break_point:
        \int_compare:nNnTF { \l_@@_tmpa_int } = { 0 }
          { \prg_return_true:  }
          { \prg_return_false: }
      }  
  }
%    \end{macrocode}
% \end{macro}  
%
% \begin{macro}[TF]{\matrix_if_identity:N }
% 指定矩阵是否为单位矩阵。
%    \begin{macrocode}
\prg_set_conditional:Nnn \matrix_if_identity:N { T, F, TF }
  {
    \@@_meta_parse:N #1
    \bool_lazy_and:nnTF
      { \int_compare_p:nNn \l_@@_length_int > { 0 } }
      { \int_compare_p:nNn \l_@@_row_int = \l_@@_col_int }  
      {
        \@@_space_query:
        \int_zero:N \l_@@_tmpa_int
        \int_step_inline:nn { \l_@@_length_int }
          {
            \fp_set:Nn \l_@@_tmpa_fp
              { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + ##1 } }
            \int_compare:nNnTF 
              { \int_mod:nn {##1} { \l_@@_col_int + 1 } } = {1}
              { \@@_fp_if_eq:nnF { \l_@@_tmpa_fp } { 1 } }
              { \@@_fp_if_eq:nnF { \l_@@_tmpa_fp } { 0 } }       
              { \int_incr:N \l_@@_tmpa_int \prg_break: }
          }
        \prg_break_point:
        \int_compare:nNnTF { \l_@@_tmpa_int } = { 0 }
          { \prg_return_true:  }
          { \prg_return_false: }
      }
      { \prg_return_false: }
  }
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{显示矩阵}
% 
% \begin{macro}{\matrix_use:N, \matrix_use:c}
% 显示矩阵。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_use:N
  {
    \@@_meta_parse:N #1   
    \int_compare:nNnTF
      { \l_@@_length_int } = { 0 }
      { \left(~<\int_use:N \l_@@_row_int>~x~<\int_use:N \l_@@_col_int>~\right) }
      { 
        \@@_space_query:
        \@@_data_typeset:Nnnn \l_@@_fparray 
          { \l_@@_offset_int } { \l_@@_length_int } { \l_@@_col_int }
      }
  }
\cs_generate_variant:Nn \matrix_use:N { c }
%    \end{macrocode}
% \end{macro}
%  
% 
% \begin{macro}{\matrix_log:N}
% 查看矩阵。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_log:N
  {
    \@@_meta_parse:N #1
    \iow_log:n { The~matrix's~base~information:~ }
    \iow_log:V #1
    \int_compare:nNnTF { \l_@@_length_int } > { 0 }
      {
        \@@_space_query:nn { \l_@@_length_int } { \l_@@_index_int }
        \iow_log:e { The~matrix's~data: <offset:~\int_use:N \l_@@_offset_int>~ }
        \@@_data_log:Nnnnn \l_@@_fparray
          { \l_@@_offset_int } { \l_@@_length_int } 
          { \l_@@_col_int  } { \l_@@_space_size_int }
      }
      { \iow_log:n { The~matrix~does~not~has~any~data~! } }    
  }
\cs_generate_variant:Nn \matrix_log:N { c }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{预定义矩阵}
% 
% \begin{macro}{\g_tmpa_matrix, \g_tmpb_matrix}
% 本宏包永不使用的临时变量。
%    \begin{macrocode}
\matrix_new:Nnn  \g_tmpa_matrix { 0 } { 0 }
\matrix_new:Nnn  \g_tmpb_matrix { 0 } { 0 }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\c_empty_matrix, \c_identity_two_matrix, 
%               \c_identity_three_matrix , \c_identity_four_matrix }
% 定义长度为零的特殊矩阵，零索引不指向任何实际数据。
%    \begin{macrocode}
\matrix_const:Nnnn \c_empty_matrix 
  { 0 } { 0 } {}
\matrix_const:Nnnn \c_identity_two_matrix 
  { 2 } { 2 } { 1, 0, 0, 1 }
\matrix_const:Nnnn \c_identity_three_matrix 
  { 3 } { 3 } { 1, 0, 0, 0, 1, 0, 0, 0, 1 }
\matrix_const:Nnnn \c_identity_four_matrix 
  { 4 } { 4 } 
  { 
    1, 0, 0, 0, 
    0, 1, 0, 0, 
    0, 0, 1, 0, 
    0, 0, 0, 1 
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsection{映射与运算}
% 
% \subsubsection{运算专用变量}
% 
% \begin{variable}
%   {   
%     \l_@@_opa_row_int, \l_@@_opb_row_int, 
%     \l_@@_opa_col_int, \l_@@_opb_col_int, 
%     \l_@@_old_length_int, \l_@@_old_index_int
%   }
% 矩阵运算时使用的临时元数据。
%    \begin{macrocode}
\int_new:N  \l_@@_opa_row_int
\int_new:N  \l_@@_opb_row_int
\int_new:N  \l_@@_opa_col_int
\int_new:N  \l_@@_opb_col_int
\int_new:N  \l_@@_old_length_int
\int_new:N  \l_@@_old_index_int
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\l_@@_opa_fparray,  \l_@@_opb_fparray,
%                  \l_@@_opa_offset_int, \l_@@_opb_offset_int}
% 矩阵运算时使用的临时数组信息。
%    \begin{macrocode}
\cs_new:Npn \l_@@_opa_fparray {}
\cs_new:Npn \l_@@_opb_fparray {}
\int_new:N  \l_@@_opa_offset_int
\int_new:N  \l_@@_opb_offset_int
%    \end{macrocode}
% \end{variable}
% 
% \subsubsection{运算准备与收尾}
% 
% \begin{macro}{\@@_left_operand_prepare:N, \@@_right_operand_prepare:N}
% 操作数准备。
%    \begin{macrocode}
\cs_new:Nn \@@_left_operand_prepare:N
  {
    \@@_meta_parse:N #1
    \int_set_eq:NN \l_@@_opa_row_int    \l_@@_row_int 
    \int_set_eq:NN \l_@@_opa_col_int    \l_@@_col_int
    \@@_space_query:
    \cs_set_eq:NN  \l_@@_opa_fparray    \l_@@_fparray
    \int_set_eq:NN \l_@@_opa_offset_int   \l_@@_offset_int
  }
\cs_new:Nn \@@_right_operand_prepare:N
  {
    \@@_meta_parse:N #1
    \int_set_eq:NN \l_@@_opb_row_int    \l_@@_row_int 
    \int_set_eq:NN \l_@@_opb_col_int    \l_@@_col_int
    \@@_space_query:
    \cs_set_eq:NN   \l_@@_opb_fparray    \l_@@_fparray
    \int_set_eq:NN  \l_@@_opb_offset_int   \l_@@_offset_int
  }
\cs_generate_variant:Nn \@@_left_operand_prepare:N  { c }
\cs_generate_variant:Nn \@@_right_operand_prepare:N { c }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_inplace_prepare:N}
% 原地修改准备，清空旧的空间信息以误回收。
%    \begin{macrocode}
\cs_new:Nn \@@_inplace_prepare:N
  {
    \seq_if_in:NVTF \g_@@_const_seq #1
      { 
        \msg_error:nnn { matrix } { constant-matrix } {#1} 
        \matrix_map_break:
      }
      {
        \@@_meta_parse:N #1
        \@@_space_query:
        \int_zero:N \l_@@_old_length_int
        \int_zero:N \l_@@_old_index_int
      }    
  }
\cs_generate_variant:Nn \@@_inplace_prepare:N { c }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_result_prepare:Nnn}
% 结果准备，额外提供行列数，需要重新申请空间。旧空间不保留暂不回收。
%    \begin{macrocode}
\cs_new:Nn \@@_result_prepare:Nnn
  {
    \seq_if_in:NVTF \g_@@_const_seq #1
      { 
        \msg_error:nnn { matrix } { constant-matrix } {#1} 
        \matrix_map_break:
      }
      {
        \@@_meta_parse:N #1
        \int_set_eq:NN \l_@@_old_length_int \l_@@_length_int
        \int_set_eq:NN \l_@@_old_index_int  \l_@@_index_int
        \int_set:Nn \l_@@_row_int {#2}
        \int_set:Nn \l_@@_col_int {#3}
        \int_set:Nn \l_@@_length_int { #2 * #3 }
        \@@_space_allocate:
        \@@_meta_gset:N #1
        \@@_space_query:
        \int_step_inline:nnn { \l_@@_length_int + 1 } { \l_@@_space_size_int }
          { \fparray_gset:Nnn \l_@@_fparray { \l_@@_offset_int + ##1 } { 0 } }      
      }    
  }
\cs_generate_variant:Nn \@@_result_prepare:Nnn { c }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_operation_cleanup:}
% 运算收尾。
%    \begin{macrocode}
\cs_new:Nn \@@_operation_cleanup:
  {
    \@@_space_free:nn 
      { \l_@@_old_length_int } { \l_@@_old_index_int } 
    \int_zero:N \l_@@_old_length_int
    \int_zero:N \l_@@_old_index_int
  }
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{通用映射函数}
% 
% \begin{variable}{\g_@@_map_depth_int}
% 映射嵌套深度变量。
%    \begin{macrocode}
\int_new:N  \g_@@_map_depth_int
%    \end{macrocode}
% \end{variable}
% 
% 
% \begin{variable}{\g_@@_map_intarray}
% 有限的嵌套变量。
%    \begin{macrocode}
\intarray_new:Nn \g_@@_map_intarray { 400 }
%    \end{macrocode}
% \end{variable}
% 
% \begin{macro}{\@@_map_state_save:, \@@_map_state_restore:}
% 有限的嵌套变量的保存与恢复。
%    \begin{macrocode}
\cs_new_protected:Nn \@@_map_state_save:
  {
    \int_set:Nn \l_@@_tmpa_int { (\g_@@_map_depth_int - 2) * 10 }
    \intarray_gset:Nnn \g_@@_map_intarray 
      { \l_@@_tmpa_int + 1 } { \l_@@_length_int }
    \intarray_gset:Nnn \g_@@_map_intarray 
      { \l_@@_tmpa_int + 2 } { \l_@@_row_int }
    \intarray_gset:Nnn \g_@@_map_intarray 
      { \l_@@_tmpa_int + 3 } { \l_@@_col_int }
    \intarray_gset:Nnn \g_@@_map_intarray 
      { \l_@@_tmpa_int + 4 } { \l_@@_index_int }
    \intarray_gset:Nnn \g_@@_map_intarray 
      { \l_@@_tmpa_int + 5 } { \l_@@_offset_int }
  }
\cs_new_protected:Nn \@@_map_state_restore:
  {
    \int_set:Nn \l_@@_tmpa_int { (\g_@@_map_depth_int - 2) * 10 }
    \int_set:Nn \l_@@_length_int
      { \intarray_item:Nn \g_@@_map_intarray { \l_@@_tmpa_int + 1 } }
    \int_set:Nn \l_@@_row_int
      { \intarray_item:Nn \g_@@_map_intarray { \l_@@_tmpa_int + 2 } }
    \int_set:Nn \l_@@_col_int
      { \intarray_item:Nn \g_@@_map_intarray { \l_@@_tmpa_int + 3 } }
    \int_set:Nn \l_@@_index_int
      { \intarray_item:Nn \g_@@_map_intarray { \l_@@_tmpa_int + 4 } }
    \int_set:Nn \l_@@_offset_int
      { \intarray_item:Nn \g_@@_map_intarray { \l_@@_tmpa_int + 5 } }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_map_item:n, \@@_map_push:, \@@_map_pop:}
% 当前映射元素处理函数。
%    \begin{macrocode}
\cs_new:Npn \@@_map_item:n #1 { }
%    \end{macrocode}
% 保存当前元素处理函数。
%    \begin{macrocode}
\cs_new_protected:Nn \@@_map_push:
  {
    \int_gincr:N \g_@@_map_depth_int
    \int_compare:nNnT
      { \g_@@_map_depth_int } > { 1 }
      {
        \cs_gset_eq:cN
          { @@_map_item_ \int_use:N \g_@@_map_depth_int :n }
          \@@_map_item:n
        \cs_gset_eq:cN
          { g_@@_map_\int_use:N \g_@@_map_depth_int _fparray }
          \l_@@_fparray
        \@@_map_state_save:
      }    
  }
%    \end{macrocode}
% 恢复上一级元素处理函数。
%    \begin{macrocode}
\cs_new_protected:Nn \@@_map_pop:
  {
    \int_compare:nNnT
      { \g_@@_map_depth_int } > { 1 }
      {
        \cs_gset_eq:Nc \@@_map_item:n
          { @@_map_item_ \int_use:N \g_@@_map_depth_int :n }
        \cs_set_eq:Nc \l_@@_fparray
          { g_@@_map_\int_use:N \g_@@_map_depth_int _fparray }          
        \@@_map_state_restore:
      }    
    \int_gdecr:N \g_@@_map_depth_int
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_map_break:, \matrix_map_break:n}
% 定义中断函数。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_map_break:
  { \prg_map_break:Nn \matrix_map_break: {  } }
\cs_new_protected:Nn \matrix_map_break:n
  { \prg_map_break:Nn \matrix_map_break: {#1} }
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\matrix_map_function:NN}
% 函数式映射函数。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_map_function:NN
  {
    \@@_map_push:
    \@@_meta_parse:N #1        
    \int_compare:nNnT { \l_@@_length_int } = { 0 }
      { \matrix_map_break: }
    \@@_space_query:
    \int_step_inline:nn { \l_@@_length_int }
      {
        \exp_args:Ne #2 
          { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + ##1 } } 
      }
    \prg_break_point:Nn \matrix_map_break: { \@@_map_pop: } 
  }
%    \end{macrocode}
% \end{macro} 
% 
% \begin{macro}{\matrix_map_inline:Nn}
% 内联式映射函数。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_map_inline:Nn
  {
    \@@_map_push:
    \@@_meta_parse:N #1 
    \cs_gset:Nn \@@_map_item:n {#2}
    \int_compare:nNnT { \l_@@_length_int } = { 0 }
      { \matrix_map_break: }
    \@@_space_query:
    \int_step_inline:nn { \l_@@_length_int }
      {
        \exp_args:Ne \@@_map_item:n
          { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + ##1 } } 
      }
    \prg_break_point:Nn \matrix_map_break: { \@@_map_pop: } 
  }
%    \end{macrocode}
% \end{macro} 
% 
% 
% \subsubsection{原地映射运算}
% 
% \begin{macro}{\@@_map_inplace:Nn}
% 通过映射修改矩阵的元素。
%    \begin{macrocode}
\cs_new:Nn \@@_map_inplace:Nn
  {    
    \@@_map_push:
    \@@_inplace_prepare:N #1    
    \int_compare:nNnT { \l_@@_length_int } = { 0 }
      { \matrix_map_break: }
    \cs_gset:Nn \@@_map_item:n {#2}
    \int_step_inline:nn { \l_@@_length_int }
      {
        \fparray_gset:Nnn \l_@@_fparray { \l_@@_offset_int + ##1 }
          {
            \exp_args:Ne \@@_map_item:n
              { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + ##1 } } 
          }            
      }
    \prg_break_point:Nn \matrix_map_break: { \@@_map_pop: } 
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\matrix_gnegate:N, \matrix_gabs:N, \matrix_gsign:N, \matrix_ground:Nn}
% 求 $-A$、$\vert A \vert$、$sign(A)$ 与 $round(A, n)$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gnegate:N
  { \@@_map_inplace:Nn #1 { -##1 } }
\cs_new_protected:Nn \matrix_gabs:N
  { \@@_map_inplace:Nn #1 { abs(##1) }  }
\cs_new_protected:Nn \matrix_gsign:N
  { \@@_map_inplace:Nn #1 { sign(##1) } }
\cs_new_protected:Nn \matrix_ground:Nn
  { \@@_map_inplace:Nn #1 { round(##1, #2) } }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\matrix_gadd:Nn, \matrix_gsub:Nn, 
%               \matrix_gmul:Nn, \matrix_gdiv:Nn, \matrix_gpower:Nn}
% 求 $A + c$、$A - c$、$cA$、$A / c$ 及 $A ^ c$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gadd:Nn
  { \@@_map_inplace:Nn #1 { ##1 + #2 } }
\cs_new_protected:Nn \matrix_gsub:Nn
  { \@@_map_inplace:Nn #1 { ##1 - #2 } }
\cs_new_protected:Nn \matrix_gmul:Nn
  { \@@_map_inplace:Nn #1 { ##1 * #2 } }
\cs_new_protected:Nn \matrix_gdiv:Nn
  { \@@_map_inplace:Nn #1 { ##1 / #2 } }
\cs_new_protected:Nn \matrix_gpower:Nn
  { \@@_map_inplace:Nn #1 { ##1 ^ #2 } }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{单矩阵映射运算}
% 
% 
% \begin{macro}{\@@_map_operate:NNn}
% 通过映射逐元素计算，操作数只有一个矩阵。
%    \begin{macrocode}
\cs_new:Nn \@@_map_operate:NNn
  { 
    \@@_map_push:    
    \@@_left_operand_prepare:N #2
    \@@_result_prepare:Nnn #1 { \l_@@_opa_row_int } { \l_@@_opa_col_int }
    \int_compare:nNnT { \l_@@_length_int } = { 0 }
      { \matrix_map_break: } 
    \cs_gset:Nn \@@_map_item:n {#3}
    \int_step_inline:nn { \l_@@_length_int }
      {
        \fparray_gset:Nnn \l_@@_fparray { \l_@@_offset_int + ##1 }
          {
            \exp_args:Ne \@@_map_item:n
              { \fparray_item:Nn \l_@@_opa_fparray { \l_@@_opa_offset_int + ##1 } } 
          }            
      }
    \prg_break_point:Nn \matrix_map_break: 
      { \@@_map_pop: \@@_operation_cleanup: }     
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_gset_eq:NN, \matrix_gset_eq:Nc, \matrix_gset_eq:cN}
% 矩阵赋值。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gset_eq:NN
  { \@@_map_operate:NNn #1 #2 {##1} }
\cs_generate_variant:Nn \matrix_gset_eq:NN { c, Nc }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_gnegate:NN, \matrix_gabs:NN, \matrix_gsign:NN, \matrix_ground:NNn}
% 求 $Z=-A$、$Z=\vert A \vert$ 与 $Z=sign(A)$ 与 $Z=round(A, n)$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gnegate:NN
  { \@@_map_operate:NNn #1 #2 {-##1} }
\cs_new_protected:Nn \matrix_gabs:NN
  { \@@_map_operate:NNn #1 #2 { abs(##1) }  }
\cs_new_protected:Nn \matrix_gsign:NN
  { \@@_map_operate:NNn #1 #2 { sign(##1) } }
\cs_new_protected:Nn \matrix_ground:NNn
  { \@@_map_operate:NNn #1 #2 { round(##1, #3) } }
%    \end{macrocode}
% \end{macro}
% 
% 
% 
% \begin{macro}{\matrix_gadd:Nn, \matrix_gsub:Nn, 
%               \matrix_gmul:Nn, \matrix_gdiv:Nn, \matrix_gpower:Nn}
% 求 $Z=A + c$、$Z=A - c$、$Z=cA$、$Z=A / c$ 及 $Z=A ^ c$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gadd:NNn
  { \@@_map_operate:NNn #1 #2 { ##1 + #3 } }
\cs_new_protected:Nn \matrix_gsub:NNn
  { \@@_map_operate:NNn #1 #2 { ##1 - #3 } }
\cs_new_protected:Nn \matrix_gmul:NNn
  { \@@_map_operate:NNn #1 #2 { ##1 * #3 } }
\cs_new_protected:Nn \matrix_gdiv:NNn
  { \@@_map_operate:NNn #1 #2 { ##1 / #3 } }
\cs_new_protected:Nn \matrix_gpower:NNn
  { \@@_map_operate:NNn #1 #2 { ##1 ^ #3 } }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{双矩阵映射运算}
% 
% 
% \begin{macro}{\@@_map_operate:NNNn}
% 通过映射逐元素计算，操作数有两个矩阵。
%    \begin{macrocode}
\cs_new:Nn \@@_map_operate:NNNn
  { 
    \@@_map_push:    
    \@@_left_operand_prepare:N  #2
    \@@_right_operand_prepare:N #3
    \bool_lazy_and:nnF
      { \int_compare_p:nNn \l_@@_opa_row_int = \l_@@_opb_row_int }
      { \int_compare_p:nNn \l_@@_opa_col_int = \l_@@_opb_col_int }
      { 
        \msg_error:nne { matrix } { dimension-mismatch }
          { 
            (\int_use:N \l_@@_opa_row_int, \int_use:N \l_@@_opa_col_int) vs. 
            (\int_use:N \l_@@_opb_row_int, \int_use:N \l_@@_opb_col_int)
          }
        \matrix_map_break: 
      } 
    \@@_result_prepare:Nnn #1 { \l_@@_opa_row_int } { \l_@@_opa_col_int }
    \int_compare:nNnT { \l_@@_length_int } = { 0 }
      { \matrix_map_break: }
    \cs_gset:Npn \@@_map_item:n ##1 ##2 {#4}
    \int_step_inline:nn { \l_@@_length_int }
      {
        \fparray_gset:Nnn \l_@@_fparray { \l_@@_offset_int + ##1 }
          {
            \exp_args:Nee \@@_map_item:n
              { \fparray_item:Nn \l_@@_opa_fparray { \l_@@_opa_offset_int + ##1 } } 
              { \fparray_item:Nn \l_@@_opb_fparray { \l_@@_opb_offset_int + ##1 } } 
          }            
      }
    \prg_break_point:Nn \matrix_map_break: 
      { \@@_map_pop: \@@_operation_cleanup: }     
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\matrix_gadd:NNN, \matrix_gsub:NNN}
% 求 $Z=A + B$、$Z=A - B$
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gadd:NNN
  { \@@_map_operate:NNNn #1 #2 #3 { ##1 + ##2 } }
\cs_new_protected:Nn \matrix_gsub:NNN
  { \@@_map_operate:NNNn #1 #2 #3 { ##1 - ##2 } }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_gadd:NN, \matrix_gsub:NN}
% 求 $A + B$、$A - B$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gadd:NN
  { \@@_map_operate:NNNn #1 #1 #2 { ##1 + ##2 } }
\cs_new_protected:Nn \matrix_gsub:NN
  { \@@_map_operate:NNNn #1 #1 #2 { ##1 - ##2 } }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsection{非映射运算}
% 
% \begin{macro}{\@@_multiply_aux:nn}
% 计算 $Z_{ij} = \sum_{k=1}^nA_{ik}B_{kj}$。注意，为减少运算，此处代码坐标从 $0$ 计。
%    \begin{macrocode}
\cs_new:Nn \@@_multiply_aux:nn
  {
    \fp_zero:N \l_@@_tmpa_fp
    \int_step_inline:nn { \l_@@_opa_col_int }
      {
        \fp_add:Nn \l_@@_tmpa_fp
          {
            \fparray_item:Nn \l_@@_opa_fparray 
              { \l_@@_opa_offset_int + (  #1 - 1 ) * \l_@@_opa_col_int + ##1 } 
            *
            \fparray_item:Nn \l_@@_opb_fparray 
              { \l_@@_opb_offset_int + ( ##1 - 1 ) * \l_@@_opb_col_int +  #2 }
          }         
      }
    \fparray_gset:Nnn \l_@@_fparray 
      { \l_@@_offset_int + ( #1 - 1 ) * \l_@@_col_int + #2 }
      { \l_@@_tmpa_fp }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_gmul:NNN}
% 计算 $Z = AB$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gmul:NNN
  { 
    \group_begin:
    \@@_left_operand_prepare:N  #2 
    \@@_right_operand_prepare:N #3
    \int_compare:nNnF
      { \l_@@_opa_col_int } = { \l_@@_opb_row_int }
      { 
        \msg_error:nne { matrix } { dimension-mismatch }
          { 
            (\int_use:N \l_@@_opa_row_int, \int_use:N \l_@@_opa_col_int) vs. 
            (\int_use:N \l_@@_opb_row_int, \int_use:N \l_@@_opb_col_int)
          }
        \matrix_map_break: 
      }
    \@@_result_prepare:Nnn #1 { \l_@@_opa_row_int } { \l_@@_opb_col_int }
    \int_compare:nNnT { \l_@@_length_int } = { 0 }
      { \matrix_map_break: }
    \int_step_inline:nn { \l_@@_opa_row_int }
      {
        \int_step_inline:nn { \l_@@_opb_col_int }
          {
            \@@_multiply_aux:nn {##1} {####1}
          }         
      }
    \prg_break_point:Nn \matrix_map_break: 
      { \@@_operation_cleanup: }   
    \group_end:  
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\matrix_gmul:NN}
% 求 $AB$。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_gmul:NN
  { \matrix_gmul:NNN #1 #1 #2 }
%    \end{macrocode}
% \end{macro}
% 
% \section{几何变换}
% 
% \changes{v0.2}{2026-11-18}{增加几何变换功能}
% 
% \subsection{提取变换}
% 
% \begin{macro}{\@@_append_term:Nnn}
% 向变换求值公式追加项目。
%    \begin{macrocode}
\cs_new:Nn \@@_append_term:Nnn
  {
    \fp_compare:nNnF { abs(#2) } < { 1e-12 }
      {
        \fp_compare:nNnTF {#2} < { 0 }
          {
            \fp_compare:nNnTF { abs(#2 + 1) } < { 1e-12 }
              { \tl_put_right:Nn #1 {    - #3 } }
              { \tl_put_right:Nn #1 { #2 * #3 } }
          }        
          { 
            \tl_if_empty:NF #1
              { \tl_put_right:Nn #1 { + } }
            \fp_compare:nNnTF { abs(#2 - 1) } < { 1e-12 }
              { \tl_put_right:Nn #1 {      #3 } }
              { \tl_put_right:Nn #1 { #2 * #3 } }
          }
      }
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_set_transform:Nnnnnn}
% 设置二维变换的某个变换函数。
%    \begin{macrocode}
\cs_new:Nn \@@_set_transform:Nnnnnn
  {
    \tl_clear:N \l_@@_tmpa_tl
    \tl_clear:N \l_@@_tmpb_tl
    \@@_append_term:Nnn \l_@@_tmpa_tl {1}  {#4}
    \@@_append_term:Nnn \l_@@_tmpa_tl {#2} {##1}
    \@@_append_term:Nnn \l_@@_tmpa_tl {#3} {##2}
    \@@_append_term:Nnn \l_@@_tmpb_tl {1}  {1}
    \@@_append_term:Nnn \l_@@_tmpb_tl {#5} {##1}
    \@@_append_term:Nnn \l_@@_tmpb_tl {#6} {##2}    
    \cs_set:Npe #1 ##1 ##2
      {
        \str_if_eq:VnTF \l_@@_tmpb_tl {1}
          { \exp_not:N \fp_eval:n { \l_@@_tmpa_tl } }
          { 
            \exp_not:N \fp_eval:n
              { (\l_@@_tmpa_tl) / (\l_@@_tmpb_tl) }
          }
      }
  }
  \cs_generate_variant:Nn \@@_set_transform:Nnnnnn { Neeeee }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_set_transform:Nnnnnnnn}
% 设置三维变换的某个变换函数。
%    \begin{macrocode}
\cs_new:Nn \@@_set_transform:Nnnnnnnn
  {
    \tl_clear:N \l_@@_tmpa_tl
    \tl_clear:N \l_@@_tmpb_tl
    \@@_append_term:Nnn \l_@@_tmpa_tl {1}  {#5}
    \@@_append_term:Nnn \l_@@_tmpa_tl {#2} {##1}
    \@@_append_term:Nnn \l_@@_tmpa_tl {#3} {##2}
    \@@_append_term:Nnn \l_@@_tmpa_tl {#4} {##3}
    \@@_append_term:Nnn \l_@@_tmpb_tl {1}  {1}
    \@@_append_term:Nnn \l_@@_tmpb_tl {#6} {##1}
    \@@_append_term:Nnn \l_@@_tmpb_tl {#7} {##2}   
    \@@_append_term:Nnn \l_@@_tmpb_tl {#8} {##3}   
    \cs_set:Npe #1 ##1 ##2 ##3
      {
        \str_if_eq:VnTF \l_@@_tmpb_tl {1}
          { \exp_not:N \fp_eval:n { \l_@@_tmpa_tl } }
          { 
            \exp_not:N \fp_eval:n
              { (\l_@@_tmpa_tl) / (\l_@@_tmpb_tl) }
          }
      }
  }
\cs_generate_variant:Nn \@@_set_transform:Nnnnnnnn { Neeeeeee }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\matrix_extract_transform:NNN}
% 提取二维变换。|#1| 变换矩阵，|#2#3| 变换函数。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_extract_transform:NNN
  { 
    \@@_meta_parse:N #1    
    \bool_lazy_and:nnF
      { \int_compare_p:nNn { \l_@@_row_int } = {3} }
      { \int_compare_p:nNn { \l_@@_col_int } = {3} }
      { 
        \msg_error:nnn { matrix } { invalid-transformation-matrix } {#1}
        \prg_break:
      }   
      \@@_space_query:
      \@@_fp_if_eq:nnF
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 9 } } {1}
        { 
          \msg_error:nnn { matrix } { invalid-transformation-matrix } {#1}
          \prg_break:
        }
      \@@_set_transform:Neeeee #2
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 1 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 2 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 3 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 7 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 8 } }
      \@@_set_transform:Neeeee #3
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 4 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 5 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 6 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 7 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 8 } }    
    \prg_break_point:  
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\matrix_extract_transform:NNNN}
% 提取三维变换。|#1| 变换矩阵，|#2#3#4| 变换函数。
%    \begin{macrocode}
\cs_new_protected:Nn \matrix_extract_transform:NNNN
  { 
    \@@_meta_parse:N #1    
    \bool_lazy_and:nnF
      { \int_compare_p:nNn { \l_@@_row_int } = {4} }
      { \int_compare_p:nNn { \l_@@_col_int } = {4} }
      { 
        \msg_error:nnn { matrix } { invalid-transformation-matrix } {#1}
        \prg_break:
      }   
      \@@_space_query:
      \@@_fp_if_eq:nnF
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 16 } } {1}
        { 
          \msg_error:nnn { matrix } { invalid-transformation-matrix } {#1}
          \prg_break:
        }
      \@@_set_transform:Neeeeeee #2        
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  1 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  2 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  3 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  4 } }        
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 13 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 14 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 15 } }
      \@@_set_transform:Neeeeeee #3        
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  5 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  6 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  7 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  8 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 13 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 14 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 15 } }
      \@@_set_transform:Neeeeeee #4        
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int +  9 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 10 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 11 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 12 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 13 } }
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 14 } }   
        { \fparray_item:Nn \l_@@_fparray { \l_@@_offset_int + 15 } } 
    \prg_break_point:
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsection{构建变换}
% 
% 切换变换专用前缀。
%    \begin{macrocode}
%<@@=trans>
%    \end{macrocode}
% 
% \begin{variable}{\g_@@_matrix, \g_@@_two_matrix, \g_@@_three_matrix, \g_@@_dim_int}
% 用于存储变换矩阵的数据。
%    \begin{macrocode}
\matrix_new:N   \g_@@_matrix
\matrix_new:Nnn \g_@@_two_matrix   {3} {3}
\matrix_new:Nnn \g_@@_three_matrix {4} {4}
\int_new:N      \g_@@_dim_int
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\g_@@_prop, \g_@@_tmp_matrix, \g_@@_empty_matrix}
% 用于处理变换矩阵保存与恢复相关功能。
%    \begin{macrocode}
\prop_new:N     \g_@@_prop
\matrix_new:N   \g_@@_tmp_matrix
\matrix_new:N   \g_@@_empty_matrix
%    \end{macrocode}
% \end{variable}
% 
% \begin{variable}{\l_@@_sin_fp, \l_@@_cos_fp}
% 用于计算变换矩阵的临时变量。
%    \begin{macrocode}
\fp_new:N       \l_@@_sin_fp
\fp_new:N       \l_@@_cos_fp
%    \end{macrocode}
% \end{variable}
% 
% 
% \begin{macro}{\@@_compose:N}
% 将新变换矩阵作用于当前变换矩阵。|#1| 新变换矩阵。
%    \begin{macrocode}
\cs_new_protected:Nn \@@_compose:N
  { \matrix_gmul:NNN \g_@@_matrix #1 \g_@@_matrix }
%    \end{macrocode}
% \end{macro}
%
% 
% \subsubsection{初始化与复用}
% 
% \begin{macro}{\transform_init:n, \TransformInit}
% 初始化一个不做任何变换的 |#1| 维变换矩阵，即单位矩阵。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_init:n
  {
    \int_case:nnTF {#1}
      {
        { 2 } { \matrix_gset_eq:NN \g_@@_matrix \c_identity_three_matrix }
        { 3 } { \matrix_gset_eq:NN \g_@@_matrix \c_identity_four_matrix }       
      }
      { \int_gset:Nn \g_@@_dim_int {#1} }
      { \msg_error:nnn { matrix } { dimension-mismatch } {#1} }
  }
\NewDocumentCommand \TransformInit { O{2} } 
  { \transform_init:n {#1} }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\transform_save:n, \transform_restore:n, \TransformSave, \TransformRestore}
% 保存与恢复变换矩阵。注意，保存的矩阵与当前矩阵不应当引用同一份数据，否则更新矩阵是会出问题。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_save:n
  { 
    \prop_get:NnNF \g_@@_prop {#1} \g_@@_tmp_matrix
      { \tl_gset_eq:NN \g_@@_tmp_matrix \g_@@_empty_matrix }
    \matrix_gset_eq:NN \g_@@_tmp_matrix \g_@@_matrix 
    \prop_gput:NnV \g_@@_prop {#1} \g_@@_matrix 
  }
\cs_new_protected:Nn \transform_restore:n
  { 
    \prop_get:NnNT \g_@@_prop {#1} \g_@@_tmp_matrix 
      { 
        \matrix_gset_eq:NN \g_@@_matrix \g_@@_tmp_matrix 
        \matrix_if_same_dim:NnnTF \g_@@_matrix {3} {3}
          { \int_gset:Nn \g_@@_dim_int {2} }
          { \int_gset:Nn \g_@@_dim_int {3} }
      }
  }
\cs_gset_eq:NN \TransformSave    \transform_save:n
\cs_gset_eq:NN \TransformRestore \transform_restore:n
%    \end{macrocode}
% \end{macro}
%
% 
% \subsubsection{旋转变换}
% 
% 
% \begin{macro}{\transform_rotate:n, \transform_rotated:n, \@@_rotate_aux:nn}
% 绕原点旋转平面。
%    \begin{macrocode}
\cs_new:Nn \@@_rotate_aux:nn
  { 
    \matrix_gset:Nn \g_@@_two_matrix 
      { 
        #2, -#1,  0, 
        #1,  #2,  0, 
         0,   0,  1 
      }
    \@@_compose:N \g_@@_two_matrix
  }
\cs_generate_variant:Nn \@@_rotate_aux:nn { VV }
%    \end{macrocode}
%    \begin{macrocode}
\cs_new_protected:Nn \transform_rotate:n
  {
    \fp_set:Nn \l_@@_sin_fp { sin(#1) }
    \fp_set:Nn \l_@@_cos_fp { cos(#1) }
    \@@_rotate_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }    
\cs_new_protected:Nn \transform_rotated:n
  { 
    \fp_set:Nn \l_@@_sin_fp { sind(#1) }
    \fp_set:Nn \l_@@_cos_fp { cosd(#1) }
    \@@_rotate_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_rotate_x:n, \transform_rotated_x:n, \@@_rotate_x_aux:nn}
% 绕原点放置X轴。
%    \begin{macrocode}
\cs_new:Nn \@@_rotate_x_aux:nn
  { 
    \matrix_gset:Nn \g_@@_three_matrix 
      { 
         1,   0,   0,  0,
         0,  #2, -#1,  0, 
         0,  #1,  #2,  0, 
         0,   0,   0,  1 
      }
    \@@_compose:N \g_@@_three_matrix 
  }
\cs_generate_variant:Nn \@@_rotate_x_aux:nn { VV }
%    \end{macrocode}
%    \begin{macrocode}
\cs_new_protected:Nn \transform_rotate_x:n
  {
    \fp_set:Nn \l_@@_sin_fp { sin(#1) }
    \fp_set:Nn \l_@@_cos_fp { cos(#1) }
    \@@_rotate_x_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }    
\cs_new_protected:Nn \transform_rotated_x:n
  { 
    \fp_set:Nn \l_@@_sin_fp { sind(#1) }
    \fp_set:Nn \l_@@_cos_fp { cosd(#1) }
    \@@_rotate_x_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_rotate_y:n, \transform_rotated_y:n, \@@_rotate_y_aux:nn}
% 绕原点放置Y轴。
%    \begin{macrocode}
\cs_new:Nn \@@_rotate_y_aux:nn
  { 
    \matrix_gset:Nn \g_@@_three_matrix 
      { 
         #2,  0,  #1,  0,
          0,  1,   0,  0, 
        -#1,  0,  #2,  0, 
          0,  0,   0,  1 
      }
    \@@_compose:N \g_@@_three_matrix 
  }
\cs_generate_variant:Nn \@@_rotate_y_aux:nn { VV }
%    \end{macrocode}
%    \begin{macrocode}
\cs_new_protected:Nn \transform_rotate_y:n
  {
    \fp_set:Nn \l_@@_sin_fp { sin(#1) }
    \fp_set:Nn \l_@@_cos_fp { cos(#1) }
    \@@_rotate_y_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }    
\cs_new_protected:Nn \transform_rotated_y:n
  { 
    \fp_set:Nn \l_@@_sin_fp { sind(#1) }
    \fp_set:Nn \l_@@_cos_fp { cosd(#1) }
    \@@_rotate_y_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_rotate_z:n, \transform_rotated_z:n, \@@_rotate_z_aux:nn}
% 绕原点放置Z轴。
%    \begin{macrocode}
\cs_new:Nn \@@_rotate_z_aux:nn
  { 
    \matrix_gset:Nn \g_@@_three_matrix 
      { 
        #2, -#1,  0,  0,
        #1,  #2,  0,  0, 
         0,   0,  1,  0, 
         0,   0,  0,  1 
      }
    \@@_compose:N \g_@@_three_matrix 
  }
\cs_generate_variant:Nn \@@_rotate_z_aux:nn { VV }
%    \end{macrocode}
%    \begin{macrocode}
\cs_new_protected:Nn \transform_rotate_z:n
  {
    \fp_set:Nn \l_@@_sin_fp { sin(#1) }
    \fp_set:Nn \l_@@_cos_fp { cos(#1) }
    \@@_rotate_z_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }    
\cs_new_protected:Nn \transform_rotated_z:n
  { 
    \fp_set:Nn \l_@@_sin_fp { sind(#1) }
    \fp_set:Nn \l_@@_cos_fp { cosd(#1) }
    \@@_rotate_z_aux:VV \l_@@_sin_fp \l_@@_cos_fp
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{绽放、平移、错切}
% 
% 
% \begin{macro}{\transform_scale:nn, \transform_scale:nnn}
% 添加缩放变换。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_scale:nn
  { 
    \matrix_gset:Nn \g_@@_two_matrix 
      {
        #1,  0, 0, 
         0, #2, 0, 
         0,  0, 1 
      }
    \@@_compose:N \g_@@_two_matrix
  }
\cs_new_protected:Nn \transform_scale:nnn
  { 
    \matrix_gset:Nn \g_@@_three_matrix 
      {
        #1,  0,  0, 0, 
         0, #2,  0, 0, 
         0,  0, #3, 0, 
         0,  0,  0, 1 
      }
    \@@_compose:N \g_@@_three_matrix
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\transform_shift:nn, \transform_shift:nnn}
% 添加平移变换。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_shift:nn
  { 
    \matrix_gset:Nn \g_@@_two_matrix 
      {
        1, 0, #1, 
        0, 1, #2, 
        0, 0,  1 
      }
    \@@_compose:N \g_@@_two_matrix
  }
\cs_new_protected:Nn \transform_shift:nnn
  { 
    \matrix_gset:Nn \g_@@_three_matrix 
      {
        1, 0, 0, #1, 
        0, 1, 0, #2, 
        0, 0, 1, #3, 
        0, 0, 0,  1 
      }
    \@@_compose:N \g_@@_three_matrix
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\transform_shear:nn, \transform_shear:nnnnnn}
% 添加错切变换。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_shear:nn
  { 
    \matrix_gset:Nn \g_@@_two_matrix 
      {
         1, #1, 0, 
        #2,  1, 0, 
         0,  0, 1 
      }
    \@@_compose:N \g_@@_two_matrix
  }
\cs_new_protected:Nn \transform_shear:nnnnnn
  { 
    \matrix_gset:Nn \g_@@_three_matrix 
      {
         1, #1, #2, 0, 
        #3,  1, #4, 0, 
        #5, #6,  1, 0, 
         0,  0,  0, 1 
      }
    \@@_compose:N \g_@@_three_matrix
  }
%    \end{macrocode}
% \end{macro}
% 
% \subsubsection{组合变换}
% 
% \begin{macro}{\transform_rotate:nnn, \transform_rotated:nnn, \transform_scale:nnnn, \transform_shear:nnnn}
% 绕指定点旋转。|#1#2| 为中心点，|#3| 旋转角度。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_rotate:nnn
  {
    \transform_shift:nn  {-(#1)} {-(#2)}
    \transform_rotate:n  {#3}  
    \transform_shift:nn  {#1}    {#2}       
  }
\cs_new_protected:Nn \transform_rotated:nnn
  {
    \transform_shift:nn  {-(#1)} {-(#2)}
    \transform_rotated:n {#3}  
    \transform_shift:nn  {#1}    {#2}       
  }
%    \end{macrocode}
% 绕指定点缩放或错切。|#1#2| 为中心点，|#3#4| 变换参数。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_scale:nnnn
  {
    \transform_shift:nn  {-(#1)} {-(#2)}
    \transform_scale:nn  {#3}    {#4}
    \transform_shift:nn  {#1}    {#2}       
  }
\cs_new_protected:Nn \transform_shear:nnnn
  {
    \transform_shift:nn {-(#1)} {-(#2)}
    \transform_shear:nn {#3}    {#4}
    \transform_shift:nn {#1}    {#2}       
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_define_rotate_iii:NN, \@@_define_rotate_iii:n}
% 三维组合变换辅助定义函数。
%    \begin{macrocode}
\cs_new:Nn \@@_define_rotate_iii:NN
  {
    \cs_new_protected:Npn #1 ##1##2##3##4
      {
        \transform_shift:nnn {-(##1)} {-(##2)} {-(##3)}
        #2                   {##4}
        \transform_shift:nnn {##1}    {##2}    {##3}  
      }
  }
 \cs_generate_variant:Nn \@@_define_rotate_iii:NN { cc }
\cs_new:Nn \@@_define_rotate_iii:n
  { \@@_define_rotate_iii:cc {#1:nnnn} {#1:n} }  
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}
%   {
%     \transform_rotate_x:nnnn, \transform_rotate_y:nnnn, \transform_rotate_z:nnnn,
%     \transform_rotated_x:nnnn, \transform_rotated_y:nnnn, \transform_rotated_z:nnnn
%   }
% 绕指定点旋转。|#1#2#3| 为中心点，|#4| 旋转角度。
%    \begin{macrocode}
\@@_define_rotate_iii:n { transform_rotate_x }
\@@_define_rotate_iii:n { transform_rotate_y }
\@@_define_rotate_iii:n { transform_rotate_z }
\@@_define_rotate_iii:n { transform_rotated_x }
\@@_define_rotate_iii:n { transform_rotated_y }
\@@_define_rotate_iii:n { transform_rotated_z }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_scale:nnnnnn, \transform_shear:nnnnnnnnn}
% 绕指定点缩放或错切。|#1#2#3| 为中心点，|#4#5#6(#7#8#9)| 变换参数。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_scale:nnnnnn
  {
    \transform_shift:nnn {-(#1)} {-(#2)} {-(#3)}
    \transform_scale:nnn {#4}    {#5}    {#6} 
    \transform_shift:nnn {#1}    {#2}    {#3}    
  }
\cs_new_protected:Nn \transform_shear:nnnnnnnnn
  {
    \transform_shift:nnn {-(#1)} {-(#2)} {-(#3)}
    \transform_shear:nnnnnn {#4} {#5}    {#6}    {#7} {#8} {#9}
    \transform_shift:nnn    {#1} {#2}    {#3}    
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{\LaTeX2e 变换命令}
% 
% \begin{macro}{\@@_args_split:Nnn, \@@_args_split:Nnnn, \@@_args_split:Nnnnnnn,
%   \@@_args_split_negate:Nnn, \@@_args_split_negate:Nnnn}
% 拆分参数命令。
%    \begin{macrocode}
\cs_new:Npn \@@_args_split:Nnn  #1(#2,#3)
  { #1 {#2} {#3} }
\cs_new:Npn \@@_args_split:Nnnn #1(#2,#3,#4)
  { #1 {#2} {#3} {#4} }
\cs_new:Npn \@@_args_split:Nnnnnnn #1(#2,#3,#4,#5,#6,#7)
  { #1 {#2} {#3} {#4} {#5} {#6} {#7} }
\cs_new:Npn \@@_args_split_negate:Nnn  #1(#2,#3)
  { #1 { #2 * (-1) } { #3* (-1) } }
\cs_new:Npn \@@_args_split_negate:Nnnn #1(#2,#3,#4)
  { #1 { #2 * (-1) } { #3* (-1) } { #4* (-1) } }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_with_center:nn}
% 绕指定点变换。|#1| 中心点，|#2| 变换操作。
%    \begin{macrocode}
\cs_new_protected:Nn \@@_with_center:nn
  {
    \int_case:nnF {\clist_count:n {#1} } 
      {
        { 0 } {#2}        
        { 2 }
        { 
          \@@_args_split_negate:Nnn  \transform_shift:nn  (#1) 
          #2
          \@@_args_split:Nnn         \transform_shift:nn  (#1) 
        }
        { 3 }
        { 
          \@@_args_split_negate:Nnnn \transform_shift:nnn (#1) 
          #2
          \@@_args_split:Nnnn        \transform_shift:nnn (#1) 
        }
        { 1 } 
        { \msg_error:nnn { matrix } { dimension-mismatch } {#1} }
      }
      { \msg_error:nnn { matrix } { too-many-elements } {#1} }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\@@_choose:NNn}
% 根据参数数量选择操作。
%    \begin{macrocode}
\cs_new_protected:Nn \@@_choose:NNn
  {
    \int_case:nnF { \clist_count:n {#3} }
      {
        { 1 }
        { 
          \int_compare:nNnTF { \g_@@_dim_int } = { 2 }
            { #1 {#3} {#3} }
            { #2 {#3} {#3} {#3} }
        }
        { 2 }
        { \@@_args_split:Nnn  #1(#3) }
        { 3 }
        { \@@_args_split:Nnnn #2(#3) }
      }
      { \msg_error:nnn { matrix } { too-many-elements } {#3} }
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\TransformShift }
% 平移。
%    \begin{macrocode}
\NewDocumentCommand \TransformShift { m }
  {
    \int_case:nnF {\clist_count:n {#1} } 
      {     
        { 2 }
        { \@@_args_split:Nnn  \transform_shift:nn  (#1) }
        { 3 }
        { \@@_args_split:Nnnn \transform_shift:nnn (#1) }        
      }
      {
        \int_compare:nNnTF {\clist_count:n {#1} } > {3}
          { \msg_error:nnn { matrix } { too-many-elements }  {#1} }
          { \msg_error:nnn { matrix } { dimension-mismatch } {#1} }
      }         
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% 
% \begin{macro}{\TransformScale}
% 绕指定点缩放。|#1| 指定点为缩放中心，|#2| 缩放系数。
%    \begin{macrocode}
\NewDocumentCommand \TransformScale { O{} m }
  {
    \@@_with_center:nn {#1}
      {
        \@@_choose:NNn 
        \transform_scale:nn \transform_scale:nnn {#2}
      }          
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\TransformShear}
% 绕指定点错切。|#1| 指定点为错切中心，|#2| 错切系数。
%    \begin{macrocode}
\NewDocumentCommand \TransformShear { O{} m }
  {
    \@@_with_center:nn {#1}
      {
        \int_case:nnF {\clist_count:n {#2} } 
          {     
            { 2 }
            { \@@_args_split:Nnn     \transform_shear:nn     (#2) }
            { 6 }
            { \@@_args_split:Nnnnnnn \transform_shear:nnnnnn (#2) }        
          }
          {
            \int_compare:nNnTF {\clist_count:n {#2} } > {3}
              { \msg_error:nnn { matrix } { too-many-elements }  {#2} }
              { \msg_error:nnn { matrix } { dimension-mismatch } {#2} }
          } 
      }              
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\TransformRotate, \TransformRotated}
% 绕指定点旋转。|#1| 指定点为缩放中心，|#2| 放置角度。
%    \begin{macrocode}
\NewDocumentCommand \TransformRotate  { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotate:n  {#2} }          
  }
\NewDocumentCommand \TransformRotated { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotated:n {#2} }          
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\TransformRotateX,  \TransformRotateY,  \TransformRotateZ, 
%               \TransformRotatedX, \TransformRotatedY, \TransformRotatedZ}
% 绕指定点及指定轴向旋转。|#1| 指定点为缩放中心，|#2| 放置角度。
%    \begin{macrocode}
\NewDocumentCommand \TransformRotateX  { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotate_x:n  {#2} }          
  }
\NewDocumentCommand \TransformRotatedX { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotated_x:n {#2} }          
  }
\NewDocumentCommand \TransformRotateY  { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotate_y:n  {#2} }          
  }
\NewDocumentCommand \TransformRotatedY { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotated_y:n {#2} }          
  }
\NewDocumentCommand \TransformRotateZ  { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotate_z:n  {#2} }          
  }
\NewDocumentCommand \TransformRotatedZ { O{} m }
  {
    \@@_with_center:nn {#1} { \transform_rotated_z:n {#2} }          
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsection{执行变换}
% 
% \subsubsection{定义及刷新}
% 
% 
% \begin{macro}{\@@_apply_x:nn, \@@_apply_y:nn}
% 二维变换函数。
%    \begin{macrocode}
\cs_new:Nn \@@_apply_x:nn {#1}
\cs_new:Nn \@@_apply_y:nn {#2}
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\@@_apply_x:nnn, \@@_apply_y:nnn, \@@_apply_z:nnn}
% 三维变换函数。
%    \begin{macrocode}
\cs_new:Nn \@@_apply_x:nnn {#1}
\cs_new:Nn \@@_apply_y:nnn {#2} 
\cs_new:Nn \@@_apply_z:nnn {#3} 
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_refresh:, \transform_refresh:N, \TransformRefresh}
% 刷新变换函数。
%    \begin{macrocode}
\cs_new_protected:Nn \transform_refresh:N
  {
    \matrix_if_same_dim:NnnTF #1 {3} {3}
      { % 2D
        \matrix_extract_transform:NNN  #1 
          \@@_apply_x:nn \@@_apply_y:nn
      }
      { % 3D
        \matrix_extract_transform:NNNN #1 
          \@@_apply_x:nnn \@@_apply_y:nnn \@@_apply_z:nnn
      }
  }
\cs_new_protected:Nn \transform_refresh:
  { \transform_refresh:N \g_@@_matrix }
\NewDocumentCommand \TransformRefresh { O{\g_@@_matrix} }
  { \transform_refresh:N #1 }
%    \end{macrocode}
% \end{macro}
% 
% 
% \subsubsection{应用变换}
% 
% \begin{macro}{\transform_apply:nnn}
% 应用二维变换。
%    \begin{macrocode}
\cs_new:Nn \transform_apply:nnn 
  {
    \@@_apply_x:nn {#1} {#2} #3, 
    \@@_apply_y:nn {#1} {#2} #3
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_apply_inline:nnn}
% 应用二维变换。
%    \begin{macrocode}
\cs_new:Nn \transform_apply_inline:nnn 
  {
    \cs_set:Nn \@@_apply_inline:nn {#3}
    \@@_apply_inline:nn
      { \@@_apply_x:nn {#1} {#2} } 
      { \@@_apply_y:nn {#1} {#2} }
  }
%    \end{macrocode}
% \end{macro}
% 
% 
% \begin{macro}{\transform_apply:nnnn}
% 应用三维变换。
%    \begin{macrocode}
\cs_new:Nn \transform_apply:nnnn
  {
    \@@_apply_x:nnn {#1} {#2} {#3} #4, 
    \@@_apply_y:nnn {#1} {#2} {#3} #4,  
    \@@_apply_z:nnn {#1} {#2} {#3} #4
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_apply_inline:nnnn}
% 应用三维变换。
%    \begin{macrocode}
\cs_new:Nn \transform_apply_inline:nnnn
  {
    \cs_set:Nn \@@_apply_inline:nnn {#4}
    \@@_apply_inline:nnn
      { \@@_apply_x:nnn {#1} {#2} {#3} } 
      { \@@_apply_y:nnn {#1} {#2} {#3} }
      { \@@_apply_z:nnn {#1} {#2} {#3} }
  }
%    \end{macrocode}
% \end{macro}
% 
% \begin{macro}{\transform_apply:nn, \TransformApply}
% 应用变换。
%    \begin{macrocode}
\cs_new:Nn \transform_apply:nn 
  {
    \int_case:nnF { \clist_count:n {#1} }
      {
        { 2 }
        { \@@_args_split:Nnn \transform_apply:nnn (#1) {#2} }
        { 3 }
        { \@@_args_split:Nnnn \transform_apply:nnnn (#1) {#2} }     
      }
      {
        \int_compare:nNnTF {\clist_count:n {#1} } > {3}
          { \msg_error:nnn { matrix } { too-many-elements }  {#1} }
          { \msg_error:nnn { matrix } { dimension-mismatch } {#1} }
      }    
  }
\NewExpandableDocumentCommand \TransformApply { O{} m }
  { \transform_apply:nn {#2} {#1} } 
%    \end{macrocode}
% \end{macro}
% 
% 
%    \begin{macrocode}
%</package>
%    \end{macrocode}
%
%
% \end{implementation}
%
% \PrintChanges
% \PrintIndex
%
% 
\endinput