%% 
%% 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/01/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'.
