%%
%%% ��������Ϥ������ޥ���
%%% shige
%%% 07/24 1999 ver 1.0
%%% 06/05 2000 ver 1.1
%%% 04/22 2001 ver 1.2
%%% 01/27 2005 ver 1.3
%%% 08/27 2005 ver 1.4
%%% 12/06 2006 ver 1.5
%%% mymatrix.sty
%%%
%%% \matrixR[1]: �Ԥ��˹�������Ϥ���
%%%   #1: ��������� (a11,a12,...,a1n:a21,a22,...)
%%%   ��: \matrixR{1,2,3:4,5,6}
%%%       ==> 
%%%        [ 1 2 3 ]
%%%        [ 4 5 6 ]
%%% 
%%% \matrixC[1]: ����˹�������Ϥ���
%%%   #1: ��������� (a11,a21,...,an1:a12,a22,...)
%%%   ��: \matrixC{1,2,3:4,5,6}
%%%        [ 1 4 ]
%%%        [ 2 5 ]
%%%        [ 3 6 ]
%%%
%%% \����ݳ��: \matrix{R,C} �γ�̤�ݳ�̤ˤ���
%%% \����ѳ��: \matrix{R,C} �γ�̤�ѳ�̤ˤ��� (default)
%%%
%%% \marumatrix{R,C}[1]: �ݳ�̤ι��� (���Ʊ��)
%%% \kakumatrix{R,C}[1]: �ѳ�̤ι��� (���Ʊ��)
%%% \nomatrix{R,C}[1]  : ��̤ʤ��ι��� (���Ʊ��)
%%% \determ{R,C}[1]    : ���� (���Ʊ��)
%%%
%%% \���󺸹�: ��������Ǥ򺸤˹�碌��
%%% \���󱦹�: ��������Ǥ򱦤˹�碌��
%%% \�������: ��������Ǥ�����˴󤻤�
%%% \DefArrayFormatUnit[1]: ��� l,r,c �ǻ���
%%% 
%%% \���������[1] : ����ιԤβ��������ѹ� (default: 0pt)
%%%
%%% 07/24 1999
%%% 1)  �ޤ��ϡ�\matrixR{3}{1,2,3, 4,5,6} �Τ褦�ˡ���Ȥ��ƻ��ꤹ���
%%%     �Τ� size ����ꤷ�����٤� def �� matching ��ǽ�����Ѥ������Ǥ�
%%%     ʬ�䤹���Τ��ä� (ver 0.5)��������������������Ǥο��� 9 ��
%%%     �ʲ��Ǥʤ��ȻȤ��ʤ��Ȥ������¤����ä������θ塢':' �Ѥ� \@for 
%%%     ��ƣ���ú��LaTeX �ޥ�����Ȭ����p53 �Ǹ��Ĥ��ơ������Ȥ���
%%%     �ȤǤ������¤�Ϥ��������ǤΤ߽񤯤褦�ˤ�����
%%% 06/05 2000
%%% 2)  array �� c ���ѹ��Ǥ���褦�ˤ�����
%%%     \���󺸹�: ��������Ǥ򺸤˹�碌��
%%%     \���󱦹�: ��������Ǥ򱦤˹�碌��
%%%     \�������: ��������Ǥ�����˴󤻤�
%%%     \DefArrayFormatUnit[1]: ��� l,r,c �ǻ���
%%%     ����ˡ�format ʸ����� manual �ǽ񤱤�褦�ˤ��Ƥ⤤�������Τ�
%%%     �ʤ���\@Setoneline ��ˤ������ꤷ�Ƥ��Ф����Τ����餽������
%%%     �Ǥ�ʤ�������ޤ���default �� r ���������䤹�����ʤΤǤ������
%%%     ���롣
%%% 04/22 2001
%%% 3)  �������Ǥβ��������Ѥ����褦�ˤ�������������Ǥ�ʬ���ξ���
%%%     \displaystyle ��ɬ�פˤʤ뤬������ϻȤ����Ǥ�äƤ�餦���Ȥ�
%%%     ���뤫��
%%% 01/27 2005
%%% 4)	bug fix. 
%%% 08/27 2005
%%% 5)	amopn �ȤȤ�˻Ȥ������Զ��ν��� (\edef -> \protected@edef
%%%     ������¿ʬ LaTeX2e ����)��Thanks . ���� (TeX QandA 3767{1,2})
%%% 12/06 2006
%%% 6)	��̤Τʤ��������Ǥ������
%%%
%%%%%%%%%% main %%%%%%%%%%
%%%
%%% test
%%%
% \documentstyle{jarticle}
% \tracingmacros=1
% \makeatletter

\newif\if@ParentKaku
\@ParentKakutrue

\def\@����ݳ��#1{\left(#1\right)}
\def\@����ѳ��#1{\left[#1\right]}
\def\@���󼰳��#1{\left|#1\right|}
\def\@DefaultParent#1{
  \if@ParentKaku\@����ѳ��{#1}\else\@����ݳ��{#1}\fi}
\def\@���������{0pt}

%%%
%%% \@for for :
%%% cf. ƣ���ú��LaTeX �ޥ�����Ȭ����p53 
%%%
%\def\@Colonfor#1:=#2\do#3{\edef\@fortmp{#2}\ifx\@fortmp\@empty \else
\def\@Colonfor#1:=#2\do#3{\protected@edef\@fortmp{#2}\ifx\@fortmp\@empty \else
    \expandafter\@Colonforloop#2:\@nil:\@nil\@@#1{#3}\fi}
\def\@Colonforloop#1:#2:#3\@@#4#5{\def#4{#1}\ifx #4\@nnil \else
       #5\def#4{#2}\ifx #4\@nnil \else#5\@Coloniforloop #3\@@#4{#5}\fi\fi}
\def\@Coloniforloop#1:#2\@@#3#4{\def#3{#1}\ifx #3\@nnil 
       \let\@nextwhile=\@fornoop \else
      #4\relax\let\@nextwhile=\@Coloniforloop\fi\@nextwhile#2\@@#3{#4}}

%%
%% �Ԥ���������
%% R (���˸���) ==> �Ԥο� == (: fields)
%%                  ��ο� == ((, fields)-1)/(: fields)+1
%% C (�Ĥ˸���) ==> �Ԥο� == ((, fields)-1)/(: fields)+1
%%                  ��ο� == (: fields)
%% #1 ==> : fields
%% #2 ==> ((, fields)-1)/(: fields)+1
%%
\newcount{\@CommaCount}
\newcount{\@ColonCount}
\def\@MyMatrixSize#1#2#3{
  \@ColonCount=0
  \@Colonfor\S:=#3\do{\advance\@ColonCount by 1}
%  \edef#1{\the\@ColonCount}
  \protected@edef#1{\the\@ColonCount}
  \@CommaCount=-1
  \@for\S:=#3\do{\advance\@CommaCount by 1}
  \divide\@CommaCount by \@ColonCount
  \advance\@CommaCount by 1
%  \edef#2{\the\@CommaCount}
  \protected@edef#2{\the\@CommaCount}
}

%%%
%%% array �Ѥ����
%%% 
%%% \@Oneline #1,#2,...,#(n+1): ==> #2&#3&...#(n+1) (#1 ���ɤ߼Τ�)
%%% \@Onelinejust #1,#2,...,#n: ==> #1&#2&...#n 
%%% \@ArrayFormat ==> cc...c (n ��)
%%%
%\def\@DefaultArrayFormatUnit{c}
\def\@DefaultArrayFormatUnit{r}
\gdef\@AFU{\@DefaultArrayFormatUnit}

\def\@Setoneline#1{%
  \ifnum#1=1
    \def\@Oneline##1,##2:{##2}
    \def\@Onelinejust##1:{##1}
    %\def\@ArrayFormat{c}
    \def\@ArrayFormat{\@AFU}
  \fi  
  \ifnum#1=2
    \def\@Oneline##1,##2,##3:{##2&##3}
    \def\@Onelinejust##1,##2:{##1&##2}
    %\def\@ArrayFormat{cc}
    \def\@ArrayFormat{\@AFU\@AFU}
  \fi  
  \ifnum#1=3
    \def\@Oneline##1,##2,##3,##4:{##2&##3&##4}
    \def\@Onelinejust##1,##2,##3:{##1&##2&##3}
    %\def\@ArrayFormat{ccc}
    \def\@ArrayFormat{\@AFU\@AFU\@AFU}
  \fi  
  \ifnum#1=4
    \def\@Oneline##1,##2,##3,##4,##5:{##2&##3&##4&##5}
    \def\@Onelinejust##1,##2,##3,##4:{##1&##2&##3&##4}
    %\def\@ArrayFormat{cccc}
    \def\@ArrayFormat{\@AFU\@AFU\@AFU\@AFU}
  \fi  
  \ifnum#1=5
    \def\@Oneline##1,##2,##3,##4,##5,##6:{##2&##3&##4&##5&##6}
    \def\@Onelinejust##1,##2,##3,##4,##5:{##1&##2&##3&##4&##5}
    %\def\@ArrayFormat{ccccc}
    \def\@ArrayFormat{\@AFU\@AFU\@AFU\@AFU\@AFU}
  \fi  
  \ifnum#1=6
    \def\@Oneline##1,##2,##3,##4,##5,##6,##7:{##2&##3&##4&##5&##6&##7}
    \def\@Onelinejust##1,##2,##3,##4,##5,##6:{##1&##2&##3&##4&##5&##6}
    %\def\@ArrayFormat{cccccc}
    \def\@ArrayFormat{\@AFU\@AFU\@AFU\@AFU\@AFU\@AFU}
  \fi  
}

%%%
%%% 1*X ==> not yet
%%%
% 1*X C
\def\@MymainsizeiC#1;{
  \let\@tmpi\empty
%  \gdef\@myspliti##1:{\edef\@tmpi{\@tmpi,##1}}
%    \@Colonfor\@Row:=#1\do{\expandafter\@myspliti\@Row:}
%    \@Colonfor\@Row:=#1\do{\edef\@tmpi{\@tmpi,\@Row}}
    \@Colonfor\@Row:=#1\do{\protected@edef\@tmpi{\@tmpi,\@Row}}
  \begin{array}{\@ArrayFormat}
    \expandafter\@Oneline\@tmpi:
  \end{array}
}

% 1*X R
\def\@MymainsizeiR#1;{
  \begin{array}{\@ArrayFormat}
    \@Onelinejust #1:
  \end{array}
}

%%%
%%% 2*X
%%%
% 2*X C
\def\@MymainsizeiiC#1;{
  \let\@tmpi\empty\let\@tmpii\empty
  \gdef\@mysplitii##1,##2:{%
%    \edef\@tmpi{\@tmpi,##1}\edef\@tmpii{\@tmpii,##2}}
    \protected@edef\@tmpi{\@tmpi,##1}\protected@edef\@tmpii{\@tmpii,##2}}
  \@Colonfor\@Row:=#1\do{\expandafter\@mysplitii\@Row:}
  \begin{array}{\@ArrayFormat}
    \expandafter\@Oneline\@tmpi:\\[\@���������]
    \expandafter\@Oneline\@tmpii:
  \end{array}
}

% 2*X R
\def\@MymainsizeiiR#1:#2;{
  \begin{array}{\@ArrayFormat}
    \@Onelinejust #1:\\[\@���������]
    \@Onelinejust #2:
  \end{array}
}

%%%
%%% 3*X
%%%
% 3*X C
\def\@MymainsizeiiiC#1;{
  \let\@tmpi\empty\let\@tmpii\empty\let\@tmpiii\empty
  \gdef\@mysplitiii##1,##2,##3:{%
%    \edef\@tmpi{\@tmpi,##1}\edef\@tmpii{\@tmpii,##2}
%    \edef\@tmpiii{\@tmpiii,##3}}
    \protected@edef\@tmpi{\@tmpi,##1}\protected@edef\@tmpii{\@tmpii,##2}
    \protected@edef\@tmpiii{\@tmpiii,##3}}
  \@Colonfor\@Row:=#1\do{\expandafter\@mysplitiii\@Row:}
  \begin{array}{\@ArrayFormat}
    \expandafter\@Oneline\@tmpi:\\[\@���������]
    \expandafter\@Oneline\@tmpii:\\[\@���������]
    \expandafter\@Oneline\@tmpiii:
  \end{array}
}

% 3*X R
\def\@MymainsizeiiiR#1:#2:#3;{
  \begin{array}{\@ArrayFormat}
    \@Onelinejust #1:\\[\@���������]
    \@Onelinejust #2:\\[\@���������]
    \@Onelinejust #3:
  \end{array}
}

%%%
%%% 4*X
%%%
% 4*X C
\def\@MymainsizeivC#1;{
  \let\@tmpi\empty\let\@tmpii\empty\let\@tmpiii\empty\let\@tmpiv\empty  
  \gdef\@mysplitiv##1,##2,##3,##4:{%
%    \edef\@tmpi{\@tmpi,##1}\edef\@tmpii{\@tmpii,##2}
%    \edef\@tmpiii{\@tmpiii,##3}\edef\@tmpiv{\@tmpiv,##4}}
    \protected@edef\@tmpi{\@tmpi,##1}\protected@edef\@tmpii{\@tmpii,##2}
    \protected@edef\@tmpiii{\@tmpiii,##3}\protected@edef\@tmpiv{\@tmpiv,##4}}
  \@Colonfor\@Row:=#1\do{\expandafter\@mysplitiv\@Row:}
  \begin{array}{\@ArrayFormat}
    \expandafter\@Oneline\@tmpi:\\[\@���������]
    \expandafter\@Oneline\@tmpii:\\[\@���������]
    \expandafter\@Oneline\@tmpiii:\\[\@���������]
    \expandafter\@Oneline\@tmpiv:
  \end{array}
}

% 4*X R
\def\@MymainsizeivR#1:#2:#3:#4;{
  \begin{array}{\@ArrayFormat}
    \@Onelinejust #1:\\[\@���������]
    \@Onelinejust #2:\\[\@���������]
    \@Onelinejust #3:\\[\@���������]
    \@Onelinejust #4:
  \end{array}
}

%%%
%%% 5*X
%%%
% 5*X C
\def\@MymainsizevC#1;{
  \let\@tmpi\empty\let\@tmpii\empty\let\@tmpiii\empty\let\@tmpiv\empty  
  \let\@tmpv\empty  
  \gdef\@mysplitv##1,##2,##3,##4,##5:{%
%    \edef\@tmpi{\@tmpi,##1}\edef\@tmpii{\@tmpii,##2}
%    \edef\@tmpiii{\@tmpiii,##3}\edef\@tmpiv{\@tmpiv,##4}
%    \edef\@tmpv{\@tmpv,##5}}
    \protected@edef\@tmpi{\@tmpi,##1}\protected@edef\@tmpii{\@tmpii,##2}
    \protected@edef\@tmpiii{\@tmpiii,##3}\protected@edef\@tmpiv{\@tmpiv,##4}
    \protected@edef\@tmpv{\@tmpv,##5}}
  \@Colonfor\@Row:=#1\do{\expandafter\@mysplitv\@Row:}
  \begin{array}{\@ArrayFormat}
    \expandafter\@Oneline\@tmpi:\\[\@���������]
    \expandafter\@Oneline\@tmpii:\\[\@���������]
    \expandafter\@Oneline\@tmpiii:\\[\@���������]
    \expandafter\@Oneline\@tmpiv:\\[\@���������]
    \expandafter\@Oneline\@tmpv:
  \end{array}
}

% 5*X R
\def\@MymainsizevR#1:#2:#3:#4:#5;{
  \begin{array}{\@ArrayFormat}
    \@Onelinejust #1:\\[\@���������]
    \@Onelinejust #2:\\[\@���������]
    \@Onelinejust #3:\\[\@���������]
    \@Onelinejust #4:\\[\@���������]
    \@Onelinejust #5:
  \end{array}
}

%%%
%%% 6*X
%%%
% 6*X C
\def\@MymainsizeviC#1;{
  \let\@tmpi\empty\let\@tmpii\empty\let\@tmpiii\empty\let\@tmpiv\empty  
  \let\@tmpv\empty\let\@tmpvi\empty  
  \gdef\@mysplitvi##1,##2,##3,##4,##5,##6:{%
%    \edef\@tmpi{\@tmpi,##1}\edef\@tmpii{\@tmpii,##2}
%    \edef\@tmpiii{\@tmpiii,##3}\edef\@tmpiv{\@tmpiv,##4}
%    \edef\@tmpv{\@tmpv,##5}\edef\@tmpvi{\@tmpvi,##6}}
    \protected@edef\@tmpi{\@tmpi,##1}\protected@edef\@tmpii{\@tmpii,##2}
    \protected@edef\@tmpiii{\@tmpiii,##3}\protected@edef\@tmpiv{\@tmpiv,##4}
    \protected@edef\@tmpv{\@tmpv,##5}\protected@edef\@tmpvi{\@tmpvi,##6}}
  \@Colonfor\@Row:=#1\do{\expandafter\@mysplitvi\@Row:}
  \begin{array}{\@ArrayFormat}
    \expandafter\@Oneline\@tmpi:\\[\@���������]
    \expandafter\@Oneline\@tmpii:\\[\@���������]
    \expandafter\@Oneline\@tmpiii:\\[\@���������]
    \expandafter\@Oneline\@tmpiv:\\[\@���������]
    \expandafter\@Oneline\@tmpv:\\[\@���������]
    \expandafter\@Oneline\@tmpvi:
  \end{array}
}

% 6*X R
\def\@MymainsizeviR#1:#2:#3:#4:#5:#6;{
  \begin{array}{\@ArrayFormat}
    \@Onelinejust #1:\\[\@���������]
    \@Onelinejust #2:\\[\@���������]
    \@Onelinejust #3:\\[\@���������]
    \@Onelinejust #4:\\[\@���������]
    \@Onelinejust #5:\\[\@���������]
    \@Onelinejust #6:
  \end{array}
}

%%%
%%% \@mymatrix{R,C}
%%%
\def\@mymatrixR#1{%
  \@MyMatrixSize{\@Rows}{\@Columns}{#1}
  \@Setoneline{\@Columns}
  \ifnum\@Rows=1 \@MymainsizeiR #1;\fi
  \ifnum\@Rows=2 \@MymainsizeiiR #1;\fi
  \ifnum\@Rows=3 \@MymainsizeiiiR #1;\fi
  \ifnum\@Rows=4 \@MymainsizeivR #1;\fi
  \ifnum\@Rows=5 \@MymainsizevR #1;\fi
  \ifnum\@Rows=6 \@MymainsizeviR #1;\fi
}
\def\@mymatrixC#1{
  \@MyMatrixSize{\@Columns}{\@Rows}{#1}
  \@Setoneline{\@Columns}
  \ifnum\@Rows=1 \@MymainsizeiC #1;\fi
  \ifnum\@Rows=2 \@MymainsizeiiC #1;\fi
  \ifnum\@Rows=3 \@MymainsizeiiiC #1;\fi
  \ifnum\@Rows=4 \@MymainsizeivC #1;\fi
  \ifnum\@Rows=5 \@MymainsizevC #1;\fi
  \ifnum\@Rows=6 \@MymainsizeviC #1;\fi
}

%%%
%%% \matrix{R,C}
%%% \marumatrix{R,C}
%%% \kakumatrix{R,C}
%%% \nomatrix{R,C}
%%% \determ{R,C}
%%% \default{kaku,maru}kakko
%%%
\def\matrixR#1{\@DefaultParent{\@mymatrixR{#1}}}
\def\matrixC#1{\@DefaultParent{\@mymatrixC{#1}}}
\def\marumatrixR#1{\@����ݳ��{\@mymatrixR{#1}}}
\def\marumatrixC#1{\@����ݳ��{\@mymatrixC{#1}}}
\def\kakumatrixR#1{\@����ѳ��{\@mymatrixR{#1}}}
\def\kakumatrixC#1{\@����ѳ��{\@mymatrixC{#1}}}
\def\nomatrixR#1{\@mymatrixR{#1}}
\def\nomatrixC#1{\@mymatrixC{#1}}
\def\determR#1{\@���󼰳��{\@mymatrixR{#1}}}
\def\determC#1{\@���󼰳��{\@mymatrixC{#1}}}

\def\����ѳ��{\@ParentKakutrue}
\def\����ݳ��{\@ParentKakufalse}
\def\defaultkakukakko{\@ParentKakutrue}
\def\defaultmarukakko{\@ParentKakufalse}

\def\���󺸹�{\gdef\@AFU{l}}
\def\���󱦹�{\gdef\@AFU{r}}
\def\�������{\gdef\@AFU{c}}
\def\DefArrayFormatUnit#1{\gdef\@AFU{#1}}
\def\���������#1{\gdef\@���������{#1}}

%%%
%%% test
%%%
%\begin{document}
%\[
%\@MymainsizeivC 1,2,3,4:5,6,7,8:9,10,11,12:13,14,15,16:17,18,19,20;
%\]
%\@Setoneline{5}
%\[
%\@MymainsizeivR 1,2,3,4,5:6,7,8,9,10:11,12,13,14,15:16,17,18,19,20;
%\]
%\edef\L{1,2,3,4,5:6,7,8,9,10:11,12,13,14,15:16,17,18,19,20}
%\@MyMatrixSize{\F}{\S}{\L}
%\typeout{\L ==> \F,\S}
%\edef\L{1,2}
%\@MyMatrixSize{\F}{\S}{\L}
%\typeout{\L ==> \F,\S}
%\edef\L{1:2}
%\@MyMatrixSize{\F}{\S}{\L}
%\typeout{\L ==> \F,\S}
%\[
%\matrixR{1,2,3,4,5:6,7,8,9,10:11,12,13,14,15:16,17,18,19,20}
%\matrixC{1,2,3,4,5:6,7,8,9,10:11,12,13,14,15:16,17,18,19,20}
%\]
%\makeatother
%\end{document}

%%% Local Variables: 
%%% TeX-command-default: "jLaTeX"
%%% TeX-master: t
%%% End: 
