(* Require XMLgen_MC.nb ver >=0.20 *)
SetDirectory[NotebookDirectory[]];
<<XMLgenMC.mx;
DebugMode=False;
ThisNo=StringReplace[StringSplit[ NotebookFileName[],"/"][[-1]],{"LA_"->"",".nb"->"","_"->"-"}];
Category="LA/"<>ThisNo<>".";ThisNo

(* Global Options *)
Shuffle="true";Numbering="abc";Single="false";
(* Numbers of select options and questions *)
SELNUM=8;NUM=200;Title="被約階段行列の定義(サイズ固定)";

TTS[mt_]:=ToString[TeXForm[StandardForm[mt]]];
TOT[mt_]:=ToString[OneLineTeXForm[mt]];
TOTT[mt_]:=ToString[OneLineTeXForm[Transpose[mt]]];
TOC[mt_,c_]:=TOTT[{Transpose[mt][[c]]}];
TTD[r_]:=StringReplace[TT[r],"\\frac"->"\\dfrac"];
Rnz[m_]:=(RandomInteger[m]+1)*(2*RandomInteger[1]-1); (* \\pm 1,\pm2,...,\pm(m+1) *)
Rpos[m_]:=RandomInteger[m]+1; (* 1,2,...,m+1 *)
Rbtw[m_]:=RandomInteger[2m]-m;
TTC[A_,n_]:=TOTT[{(Transpose[A])[[n]]}];
Col[A_,n_]:=Transpose[{A[[Range[Length[A]],n]]}];
ColCat[CL_]:=Join@@@(Transpose@CL);
RowCat[CL_]:=Join@@(CL);
PtString[M_]:=StringReplace[ToString[M],{"{"->"(","}"->")"}];
ai2tex[s_]:=StringReplace[TOT[s],Table["\\text{a"<>TOT[i]<>"}"->"{\\mathbf a}_"<>TOT[i],{i,1,5}]];
TpStr[L_]:="\\("<>TOT[L[[1]]]<>"\\times"<>TOT[L[[2]]]<>"\\)";
TpTpse[L_]:=If[L=={},{},{L[[2]],L[[1]]}];
PType[A_,B_]:=If[A=={}||B=={},Return[{}],
If[A[[2]]===B[[1]],Return[{A[[1]],B[[2]]}],Return[{}]]];
PTypeL[L_]:=Module[{ct,len,tmp},len=Length[L];
Which[len==0,Return[{}],len==1,Return[L[[1]]],
len==2,Return[PType[L[[1]],L[[2]]]],
True,tmp=L[[1]];For[ct=2,ct≤len,ct++,tmp=PType[tmp,L[[ct]]]];Return[tmp]]];

(* select pairs of matrices *)
QtList={}; For[ct=1,ct≤300,ct++,
Cn=RandomChoice[{1,2,2,2,3,3,3,3,4,4,4,4,5}];
row=RandomChoice[{3,3,4,4,4,5}];col=RandomChoice[{4,4,4,5,5,5,5,6}];
QtList=Append[QtList,{Cn,{row,col}}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

(* Clear[QtextF];QtextF[mm_]:=Module[{Cn,row,col,MA},
{Cn,{row,col}}=QtList[[mm]];
Return[""]]; *)
QtextF="次の行列のうち，被約階段行列をすべて選びなさい.";

CListF[mm_]:=Module[{Cn,row,col,rk,rkm,RE,MA,i,j,fs,ret={}},
{Cn,{row,col}}=QtList[[mm]];fs="正解です.";rkm=Min[row,col];
While[Length[ret]≤2*Cn,
For[rk=1,rk≤rkm,rk++,
RE=RandomEchelonRank[row,col,rk,{-2,3}];
ret=Append[ret,{"\\("<>TOT[RE]<>"\\)",fs}];
ret=DeleteDuplicates[ret]]];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,row,col,rk,rkm,RE,MA,Pn,i,j,tL={},ct,fs,fs1,fs2,fs3,fsL={},ret={}},
{Cn,{row,col}}=QtList[[mm]];rkm=Min[row,col];
fs1="被約階段行列の段の成分は\\(1\\)でなければなりません.";
fs2="被約階段行列の段の上も\\(0\\)でなければなりません. 各段の列は基本ベクトルになっていなければなりません.";
fs3="そもそも階段行列になっていないようです.";
While[Length[ret]<SELNUM-Cn+2,
For[rk=1,rk≤rkm,rk++,
RE=RandomEchelonRank[row,col,rk,{-2,3}];
If[rk>1,{i,j}=Sort[RandomSample[Range[rk],2]];
Pn=GetPnijc[row,i,j,Rnz[2]];
MA=Pn.RE;ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs2}];
Pn=GetRnij[row,i,j];
MA=Pn.RE;ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs3}]];
Pn=GetQnic[row,RandomChoice[Range[rk]],RandomChoice[{-3,-2,-1,2,3}]];
MA=Pn.RE;ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs1}]];
ret=DeleteDuplicates[ret]];
Return[RandomSample[ret,SELNUM-Cn]]];

For[ct=1,ct≤NUM,ct++,QtextF[ct];CListF[ct]] (* Error check *)

GenerateMCxml[DFname[],Category<>Title<>"(MC1)",Title,NUM,Single,QtextF,CListF,ICListF]

#->Nothing &/@ Permutations[{1,2,3},{2}]