summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/physics2/tex/phy-diagmat.sty
blob: c917f797b76e3ac33ad4f5425d964deb7e739ebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
%% 
%% This is file `phy-diagmat.sty',
%% the `diagmat' module of `physics2' package.
%% 
%% ******************************************************
%% * This work may be distributed and/or modified under *
%% * the conditions of the LaTeX Project Public License *
%% *                                                    *
%% *     http://www.latex-project.org/lppl.txt          *
%% *                                                    *
%% * either version 1.3c of this license or any later   *
%% * version.                                           *
%% ******************************************************
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is Zhang Tingxuan.
%% 
\ProvidesExplFile{phy-diagmat.sty}{2023/1/25}{}
  {`diagmat' module of `physics2.sty'}
\phy@requiremodule { explsetup }
\RequirePackage { amsmath }
\phy@define@key { diagmat } { empty } [ 0 ] { \tl_gset:Nn \l__phy_mat_empty_tl { #1 } }
% New variables
\clist_new:N \l__phy_mat_diag_clist
\clist_new:N \l__phy_mat_tmpa_clist
\clist_new:N \l__phy_mat_tmpb_clist
\clist_new:N \l__phy_mat_diagpos_clist
\int_new:N \l__phy_mat_dim_int
\tl_new:N \l__phy_mat_line_tl
\tl_new:N \l__phy_diagmat_tl
\tl_new:N \l__phy_mat_empty_tl
\tl_gset:Nn \l__phy_mat_empty_tl { 0 }
\phy@processkeyopt { diagmat }
\keys_define:nn { phy/diagmat }
  {
    empty .tl_set:N = \l__phy_mat_empty_tl ,
  }
% Document Commands
\DeclareDocumentCommand \diagmat { O{} m }
  { \__phy_diagmat_type:nnn { } { #1 } { #2 } }
\DeclareDocumentCommand \pdiagmat { O{} m }
  { \__phy_diagmat_type:nnn { p } { #1 } { #2 } }
\DeclareDocumentCommand \bdiagmat { O{} m }
  { \__phy_diagmat_type:nnn { b } { #1 } { #2 } }
\DeclareDocumentCommand \Bdiagmat { O{} m }
  { \__phy_diagmat_type:nnn { B } { #1 } { #2 } }
\DeclareDocumentCommand \vdiagmat { O{} m }
  { \__phy_diagmat_type:nnn { v } { #1 } { #2 } }
\DeclareDocumentCommand \Vdiagmat { O{} m }
  { \__phy_diagmat_type:nnn { V } { #1 } { #2 } }
% Internal function
\cs_new:Npn \__phy_diagmat_type:nnn #1#2#3
  {
    \group_begin:
    \clist_set:Nn \l__phy_mat_diag_clist { #3 }
    \int_set:Nn \l__phy_mat_dim_int { \clist_count:N \l__phy_mat_diag_clist }
    \int_compare:nNnT { \l__phy_mat_dim_int } > { \value { MaxMatrixCols } }
      { \setcounter { MaxMatrixCols } { \l__phy_mat_dim_int } }
    \keys_set:nn { phy/diagmat } { #2 }
    \tl_gclear:N \l__phy_diagmat_tl
    \int_step_inline:nnn { 0 } { \l__phy_mat_dim_int - 1 }
      {
        \int_step_inline:nnn { 0 } { \l__phy_mat_dim_int - 1 }
          {
            \int_compare:nNnTF { ##1 } = { ####1 }
              {
                \clist_gpop:NN \l__phy_mat_diag_clist \l__phy_tmpa_tl
                \tl_if_empty:NTF \l__phy_tmpa_tl
                  { \tl_gput_right:Nn \l__phy_mat_line_tl { \l__phy_mat_empty_tl } }
                  { \tl_gput_right:Nx \l__phy_mat_line_tl { \l__phy_tmpa_tl } }
              }
              { \tl_gput_right:Nn \l__phy_mat_line_tl { \l__phy_mat_empty_tl } }
            % The `&' and `\\' between entries.
            \int_compare:nNnTF { ####1 } = { \l__phy_mat_dim_int - 1 }
              {
                \tl_gput_right:Nn \l__phy_mat_line_tl { \\ }
              }
              {
                \tl_gput_right:Nn \l__phy_mat_line_tl { & }
              }
          }
        \tl_gput_right:Nx \l__phy_diagmat_tl { \l__phy_mat_line_tl }
        \tl_gclear:N \l__phy_mat_line_tl
      }
    \begin { #1 matrix }
      \tl_use:N \l__phy_diagmat_tl
    \end { #1 matrix }
    \group_end:
  }
\endinput
%% End of file `phy-diagmat.sty'.