%%
%% This is file `simples-matrices.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% simples-matrices.dtx  (with options: `package')
%% 
%% Do not distribute this file without also distributing the
%% source files specified above.
%% 
%% Do not distribute a modified version of this file.
%% 
%% File: simples-matrices.dtx
%% Copyright (C) 2022 Yvon Henel aka Le TeXnicien de surface
%%
%% It may be distributed and/or modified under the conditions of the
%% LaTeX Project Public License (LPPL), either version 1.3c of this
%% license or (at your option) any later version.  The latest version
%% of this license is in the file
%%
%%    http://www.latex-project.org/lppl.txt
%%
\RequirePackage{expl3}[2020/01/12]
\GetIdInfo$Id: simples-matrices.dtx 1.0.1 2022-07-03 TdS $
  {}
\RequirePackage{l3keys2e,xparse}
\ProvidesExplPackage
{simples-matrices} {2022/06/08} {1.0} {defining and printing matrices}
\RequirePackage{amsmath}
\keys_define:nn {simples-matrices}
{
  envir .tl_set:N = \g__SMPLMTRC_nom_environnement_tl,
  envir .initial:n = {matrix},
  prefix .tl_set:N = \g__SMPLMTRC_prefix_environnement_tl,
  prefix .initial:n = {p},
  argopt .tl_set:N = \g__SMPLMTRC_argument_environnement_tl,
  argopt .initial:n = { },
  typeord .str_set:N = \g__SMPLMTRC_type_ordinaire_str,
  typeord .initial:n = { C },
}
\ProcessKeysOptions {simples-matrices}
\keys_define:nn {simples-matrices}
{
  out-of-box .meta:n = {prefix=p, envir=matrix, argopt=, typeord=C},
}
\regex_const:Nn \c__SMPLMTRC_xcas_debut_regexp {\[\[} %\]\]
\regex_const:Nn \c__SMPLMTRC_xcas_fin_regexp {\]\]}
\regex_const:Nn \c__SMPLMTRC_xcas_milieu_regexp{\],\ *\[} %\]
\msg_new:nnnn{simples-matrices}{errorIJST}
{The~number~of~coefficients~does~not~fit~the~matrix-type}
{I~was~expecting~#1~coefficients~and~obtain~only~#2.}

\msg_new:nnnn{simples-matrices}{errorx}
{The~data~do~not~seem~to~match~the~x-type}
{I~was~expecting~the~data~to~begin~with~"[["}
\tl_new:c {g__SMPLMTRC_contenu_anonyme*_tl}
\cs_new:Nn \__SMPLMTRC_creer_matrice_aux:
{
  \tl_gclear:c {g__SMPLMTRC_contenu_anonyme*_tl}

  \seq_map_indexed_inline:Nn \g_tmpa_seq
  {
    \tl_gput_right:cn {g__SMPLMTRC_contenu_anonyme*_tl} { ##2  }
    \int_compare:nNnTF { \int_mod:nn {##1} { \g_tmpa_int } } = { 0 }
    {
      \tl_gput_right:cn {g__SMPLMTRC_contenu_anonyme*_tl} { \\ }
    }
    {
      \tl_gput_right:cn {g__SMPLMTRC_contenu_anonyme*_tl} { &  }
    }
  }
}
\cs_generate_variant:Nn \regex_match:nnF { nVF }
\cs_new:Nn \__SMPLMTRC_creer_matrice:nn
{
  \tl_gclear:c {g__SMPLMTRC_contenu_anonyme*_tl}
  \str_set:Nx \l_tmpa_str { #1 }
  \str_if_eq:VnT \l_tmpa_str { O }
  {
    \str_set:NV \l_tmpa_str \g__SMPLMTRC_type_ordinaire_str
  }
  \str_case:VnF \l_tmpa_str
  {
    { x } {
      \tl_set:Nn \l_tmpa_tl { #2 }

      \regex_match:nVF {\A\s*\[\[} %\]\]
      \l_tmpa_tl
      {
          \msg_fatal:nn{simples-matrices} {errorx}
      }

      \regex_replace_all:NnN \c__SMPLMTRC_xcas_debut_regexp { } \l_tmpa_tl
      \regex_replace_all:NnN \c__SMPLMTRC_xcas_fin_regexp { \c{\\} } \l_tmpa_tl
      \regex_replace_all:NnN \c__SMPLMTRC_xcas_milieu_regexp { \c{\\} } \l_tmpa_tl
      \regex_replace_all:nnN { , }{ & } \l_tmpa_tl

      \tl_set_eq:cN {g__SMPLMTRC_contenu_anonyme*_tl} \l_tmpa_tl
    }
  }
  {
    \seq_set_from_clist:Nn \g_tmpa_seq { #2 }
    \seq_clear:N \l_tmpb_seq
    \str_case:VnF \l_tmpa_str
    {
      { C } {
        \int_set:Nn \g_tmpb_int { \seq_count:N \g_tmpa_seq }
        \int_set:Nn \g_tmpa_int { \fp_to_int:n { sqrt(\g_tmpb_int) } }
      }
      { D } {
        \int_set:Nn \g_tmpa_int { \seq_count:N \g_tmpa_seq }
        \seq_pop_left:NN \g_tmpa_seq \l_tmpa_tl
        \seq_put_right:NV \l_tmpb_seq \l_tmpa_tl

        \bool_until_do:nn { \seq_if_empty_p:N \g_tmpa_seq }
        {
          \int_step_inline:nn { \g_tmpa_int }
          {
            \seq_put_right:Nn \l_tmpb_seq { 0 }
          }
          \seq_pop:NN \g_tmpa_seq \l_tmpa_tl
          \seq_put_right:NV \l_tmpb_seq \l_tmpa_tl
        }
        \seq_set_eq:NN \g_tmpa_seq \l_tmpb_seq
      }
      { S } {
        \int_set:Nn \g_tmpb_int { \seq_count:N \g_tmpa_seq }
        \int_set:Nn \g_tmpa_int
        {
          \fp_to_int:n { ( sqrt(1 + 8*\g_tmpb_int) - 1 ) / 2 }
        }
        \int_compare:nNnF
        {2*\g_tmpb_int} = { \g_tmpa_int * (\g_tmpa_int + 1)}
        {
          \msg_fatal:nnxx{simples-matrices}
          {errorIJST}
          {\int_to_arabic:n{(\g_tmpa_int * (\g_tmpa_int + 1))/2}}
          {\int_to_arabic:n{\g_tmpb_int}}
        }

        \int_step_inline:nn { \g_tmpa_int }
        {
          \int_step_inline:nn { ##1 - 1 }
          {
            \seq_put_right:Nn \l_tmpb_seq {0}
          }
          \int_step_inline:nn { \g_tmpa_int - ##1 + 1 }
          {
            \seq_pop:NN \g_tmpa_seq \l_tmpa_tl
            \seq_put_right:NV \l_tmpb_seq \l_tmpa_tl
          }
        }

        \seq_set_eq:NN \g_tmpa_seq \l_tmpb_seq

      }
      { T } {
        \int_set:Nn \g_tmpb_int { \seq_count:N \g_tmpa_seq }
        \int_set:Nn \g_tmpa_int
        {
          \fp_to_int:n { (1 + sqrt(1 + 8*\g_tmpb_int) ) / 2 }
        }
        \int_compare:nNnF
        {2*\g_tmpb_int} = { \g_tmpa_int * (\g_tmpa_int - 1)}
        {
          \msg_fatal:nnxx{simples-matrices}
          {errorIJST}
          {\int_to_arabic:n{(\g_tmpa_int * (\g_tmpa_int - 1))/2}}
          {\int_to_arabic:n{\g_tmpb_int}}
        }
        \int_step_inline:nn { \g_tmpa_int }
        {
          \int_step_inline:nn { ##1 }
          {
            \seq_put_right:Nn \l_tmpb_seq {0}
          }
          \int_step_inline:nn { \g_tmpa_int - ##1 }
          {
            \seq_pop:NN \g_tmpa_seq \l_tmpa_tl
            \seq_put_right:NV \l_tmpb_seq \l_tmpa_tl
          }
        }
        \seq_set_eq:NN \g_tmpa_seq \l_tmpb_seq
      }
      { I } {
        \int_set:Nn \g_tmpb_int { \seq_count:N \g_tmpa_seq }
        \int_set:Nn \g_tmpa_int
        {
          \fp_to_int:n { ( sqrt(1 + 8*\g_tmpb_int) - 1 ) / 2 }
        }
        \int_compare:nNnF
        {2*\g_tmpb_int} = { \g_tmpa_int * (\g_tmpa_int + 1)}
        {
          \msg_fatal:nnxx{simples-matrices}
          {errorIJST}
          {\int_to_arabic:n{(\g_tmpa_int * (\g_tmpa_int + 1))/2}}
          {\int_to_arabic:n{\g_tmpb_int}}
        }
        \int_step_inline:nn { \g_tmpa_int }
        {
          \int_step_inline:nn { ##1 }
          {
            \seq_pop:NN \g_tmpa_seq \l_tmpa_tl
            \seq_put_right:NV \l_tmpb_seq \l_tmpa_tl
          }
          \int_step_inline:nn { \g_tmpa_int - ##1 }
          {
            \seq_put_right:Nn \l_tmpb_seq {0}
          }
        }
        \seq_set_eq:NN \g_tmpa_seq \l_tmpb_seq
      }
      { J } {
        \int_set:Nn \g_tmpb_int { \seq_count:N \g_tmpa_seq }
        \int_set:Nn \g_tmpa_int
        {
          \fp_to_int:n { (1 + sqrt(1 + 8*\g_tmpb_int) ) / 2 }
        }
        \int_compare:nNnF
        {2*\g_tmpb_int} = { \g_tmpa_int * (\g_tmpa_int - 1)}
        {
          \msg_fatal:nnxx{simples-matrices}
          {errorIJST}
          {\int_to_arabic:n{(\g_tmpa_int * (\g_tmpa_int - 1))/2}}
          {\int_to_arabic:n{\g_tmpb_int}}
        }
        \int_step_inline:nn { \g_tmpa_int }
        {
          \int_step_inline:nn { ##1 -1}
          {
            \seq_pop:NN \g_tmpa_seq \l_tmpa_tl
            \seq_put_right:NV \l_tmpb_seq \l_tmpa_tl
          }
          \int_step_inline:nn { \g_tmpa_int - ##1 + 1}
          {
            \seq_put_right:Nn \l_tmpb_seq {0}
          }
        }
        \seq_set_eq:NN \g_tmpa_seq \l_tmpb_seq
      }
    }
    {
      \int_set:Nn \g_tmpa_int { \l_tmpa_str }
    }
    \__SMPLMTRC_creer_matrice_aux:
  }
}
\cs_new:Npn \MatriceInterieur
{
  \tl_use:c { g__SMPLMTRC_contenu_anonyme*_tl }
}
\cs_new:Npn \LaMatriceInterieur #1
{
  \tl_use:c { g__SMPLMTRC_contenu_nom_#1_tl }
}
\cs_new:Nn \__SMPLMTRC_preparer_presentation:nn
{
  \IfValueT{#1}
  {
    \keys_set:nn {simples-matrices} {prefix = #1}
  }
  \IfValueT{#2}
  {
    \keys_set:nn {simples-matrices} { #2 }
  }
  \tl_set:NV \l_tmpa_tl \g__SMPLMTRC_prefix_environnement_tl
  \tl_put_right:NV \l_tmpa_tl \g__SMPLMTRC_nom_environnement_tl
  \tl_set:Nn \g_tmpa_tl { \begin{\l_tmpa_tl} }
  \tl_put_right:NV \g_tmpa_tl \g__SMPLMTRC_argument_environnement_tl
  \tl_set:Nn \g_tmpb_tl {\end{\l_tmpa_tl} }
}
\NewDocumentCommand{\matrice}{ d() d<> O{O} m }
{
  \__SMPLMTRC_creer_matrice:nn { #3 } { #4 }
  \__SMPLMTRC_preparer_presentation:nn { #1 } { #2 }
  \tl_use:N \g_tmpa_tl
  \tl_use:c { g__SMPLMTRC_contenu_anonyme*_tl }
  \tl_use:N \g_tmpb_tl
}
\NewDocumentCommand{\simplesmatricessetup}{m}
{
  \keys_set:nn {simples-matrices} { #1 }
}
\NewDocumentCommand{\declarermatrice}{ s d() d<> m  O{O} m }
{
  \__SMPLMTRC_creer_matrice:nn { #5 } { #6 }
  \tl_gset_eq:cc {g__SMPLMTRC_contenu_nom_#4_tl} {g__SMPLMTRC_contenu_anonyme*_tl}
  \IfBooleanT { #1 }
  {
    \__SMPLMTRC_preparer_presentation:nn { #2 } { #3 }
    \tl_use:N \g_tmpa_tl
    \tl_use:c { g__SMPLMTRC_contenu_anonyme*_tl }
    \tl_use:N \g_tmpb_tl
  }
}
\NewDocumentCommand{\lamatrice}{ d() d<> m }
{
  \__SMPLMTRC_preparer_presentation:nn { #1 } { #2 }
  \tl_use:N \g_tmpa_tl
  \tl_use:c { g__SMPLMTRC_contenu_nom_#3_tl }
  \tl_use:N \g_tmpb_tl
}
\cs_generate_variant:Nn \__SMPLMTRC_creer_matrice:nn { nV }
\cs_new:Nn \__SMPLMTRC_creer_matrice_identite:nn
{
  \clist_clear:N \l_tmpa_clist
  \int_step_inline:nn { #2 }
  {
    \clist_put_left:Nn \l_tmpa_clist { #1 }
  }
  \__SMPLMTRC_creer_matrice:nV {D} \l_tmpa_clist
}
\NewDocumentCommand{\matid}{ d() d<> O{1} m}
{
  \__SMPLMTRC_creer_matrice_identite:nn { #3 } { #4 }
  \__SMPLMTRC_preparer_presentation:nn  { #1 } { #2 }
  \tl_use:N \g_tmpa_tl
  \tl_use:c { g__SMPLMTRC_contenu_anonyme*_tl }
  \tl_use:N \g_tmpb_tl
}
\cs_new:Nn \__SMPLMTRC_creer_matrice_nulle:nn
{
  \clist_clear:N \l_tmpa_clist
  \int_step_inline:nn { #2*#2 }
  {
    \clist_put_left:Nn \l_tmpa_clist { #1 }
  }
  \__SMPLMTRC_creer_matrice:nV {C} \l_tmpa_clist
}
\NewDocumentCommand{\matnulle}{ d() d<> O{0} m}
{
  \__SMPLMTRC_creer_matrice_nulle:nn {#3} {#4}
  \__SMPLMTRC_preparer_presentation:nn { #1 } { #2 }
  \tl_use:N \g_tmpa_tl
  \tl_use:c { g__SMPLMTRC_contenu_anonyme*_tl }
  \tl_use:N \g_tmpb_tl
}
\endinput
%%
%% End of file `simples-matrices.sty'.
