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

(* Global Options *)
Shuffle="true";Numbering="abc";Single="false";
(* Numbers of select options and questions *)
SELNUM=8;NUM=300;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]]}];
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[{2,2,3,3,3,3,4,4,4,5}];
row=RandomChoice[{2,3,3,3,3,3,4,4,4,5}];col=RandomChoice[{3,3,4,4,4,4,5,5,5,5,6}];
rk=Min[RandomChoice[{0,1,1,2,2,2,2,2,3,3,3,3,4,4,5}],Min[row,col]];
QtList=Append[QtList,{Cn,{row,col,rk}}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

? RandomEchelonPV

Clear[QtextF];QtextF[mm_]:=Module[{Cn,row,col,rk},
{Cn,{row,col,rk}}=QtList[[mm]];
Return["次の行列のうち，階数が\\("<>TOT[rk]<>"\\)であるものをすべて選びなさい."]];

CListF[mm_]:=Module[{Cn,row,col,rk,MA,GM,RE,ct,fs,ret={}},
{Cn,{row,col,rk}}=QtList[[mm]];fs="正解です.";
While[Length[ret]≤Cn,
RE=RandomEchelonRank[row,col,rk,{-1,1}];
GM=Table[Table[RandomInteger[2]-1,row],row];
While[Abs[Det[GM]]!=1,GM=Table[Table[RandomInteger[2]-1,row],row]];
MA=GM.RE;
ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs}];
(* ret=Append[ret,{MatrixForm[MA],MatrixRank[MA]}]; *)
];
Return[RandomSample[ret,Cn]]];

QtextF[8]

ICListF[mm_]:=Module[{Cn,row,col,rk,rk1,MA,GM,RE,k,ct,fs,fsL={},ret={}},
{Cn,{row,col,rk}}=QtList[[mm]];
(*
fs="階数は\\(1\\)です. よく確認してみよう.";
For[k=1,k≤2,k++,
RE=RandomEchelonRank[row,col,1,{-1,1}];
GM=Table[Table[RandomInteger[2]-1,row],row];
While[Abs[Det[GM]]≠0,GM=Table[Table[RandomInteger[2]-1,row],row]];
MA=GM.RE;
ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs}]];
*)
For[k=1,k≤SELNUM,k++,
For[ct=1,ct≤Min[row,col],ct++,
fs="階数は\\("<>TOT[ct]<>"\\)で, \\("<>TOT[rk]<>"\\)ではありません.";
If[ct≠rk,RE=RandomEchelonRank[row,col,ct,{-1,1}];
GM=Table[Table[RandomInteger[2]-1,row],row];
While[Det[GM]==0,GM=Table[Table[RandomInteger[2]-1,row],row]];
MA=GM.RE;
ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs}]
(* ret=Append[ret,{MatrixForm[MA],fs}]; *)
]]];
 Return[RandomSample[ret,SELNUM-Cn]]];

ICListF[10]

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

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

ICListF[3]

Permutations[{1,2,3,4},{2}] /. Permutations[{1,2,3},{2}]Nothing

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

MA={{3,5},{7,9}}

MA1=MA;MA1[[{1,2},1]]={{1},{0}};

MA1

RowCat[{IdentityMatrix[2],MA}]

(* failed code *)
While[Length[tL]<2,
rk=RandomInteger[rkm-1]+1;
js=Sort[RandomChoice[Range[col],rk]];
If[rk>1,While[js==DeleteDuplicates[js],js=Sort[RandomChoice[Range[col],rk]]]];
tL=Append[tL,{js,fs1}];tL=DeleteDuplicates[tL]];
While[Length[tL]<4,
rk=RandomInteger[rkm-1]+1;
js=RandomSample[Range[col],rk];
If[rk>1,While[js==Sort[js],js=RandomSample[Range[col],rk]]];
tL=Append[tL,{js,fs2}];tL=DeleteDuplicates[tL]];
While[Length[tL]<6,
rk=RandomInteger[rkm-1]+1;
js=RandomChoice[Range[col],rk];
If[rk>1,While[js==Sort[DeleteDuplicates[js]],
js=RandomChoice[Range[col],rk]]];
tL=Append[tL,{js,fs3}];tL=DeleteDuplicates[tL]]; *)