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

(* Global Options *)
Shuffle="true";Numbering="abc";
(* Numbers of select options and questions *)
SELNUM=6;Single="true";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;
MF[m_]:=MatrixForm[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);
MatrixDenom[M_]:=Module[{den,l,ct},l=Denominator[Flatten[M]];
den=1;For[ct=1,ct≤Length[l],ct++,den=LCM[den,l[[ct]]]];Return[den]];
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 / It may take a while *)
QtList={}; For[ct=1,ct≤NUM*1.05,ct++,Cn=1;
Cn=RandomChoice[{0,1,1,1,1}];
row=RandomChoice[{3,3,4,4,4,4,4,4,5}];
col=RandomChoice[{4,5,5,5,5,5,5,6,6,6}];
rk=RandomChoice[{2,2,2,2,3,3,3}];
While[(pv=Sort[RandomSample[Range[col-1],rk]])[[1]]>2];
RE=RandomEchelonPV2[row,col,pv,{-3,-2,-2,-2,-3/2,-1,-1,-1/2,0,1,1,1,1/2,3/2,2,2,3}];
GM=Table[Table[Rnz[5],row],row];
While[Det[GM]==0||MatrixDenom[GM.RE]≠1,GM=Table[Table[Rnz[5],row],row]];
MA=GM.RE;
QtList=Append[QtList,{Cn,{row,col,pv},{MA,RE,GM}}]];
 QtList=DeleteDuplicates[QtList];
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,row,col,pv,MA,RE,GM,ret},
{Cn,{row,col,pv},{MA,RE,GM}}=QtList[[mm]];
ret=IfDeb[{MF[MA],MF[RE]},"行列\\("<>TOT[MA]<>"\\)を被約階段行列に変形すると, 次のいずれになるか."];
Return[ret]];

AListF[mm_]:=Module[{Cn,row,v0,col,pv,MA,RE,RE1,GM,i,j,En,MA1,EM1,fsL={},retC={},retI={}},
{Cn,{row,col,pv},{MA,RE,GM}}=QtList[[mm]];fsL=Append[fsL,"正解です."];
fsL=Append[fsL,"正解です. この中には正解はありません. 正解は\\("<>TOT[RE]<>"\\)ですね."];
fsL=Append[fsL,"まだ被約になっていないようです. 最後まで計算しよう."];
fsL=Append[fsL,"少し計算間違いかもしれません."];
fsL=Append[fsL,"いいえ, この中に正解があります."];
v0=Transpose[{Table[0,row]}];En=IdentityMatrix[row];
If[Cn==0,retC=Append[retC,{"C","この中に正解はない.",fsL[[2]]}],
retC=Append[retC,{"C",IfDeb[MF[RE],"\\("<>TOT[RE]<>"\\)"],fsL[[1]]}];
retI=Append[retI,{"I",IfDeb["No Correct answer.","この中に正解はない."],fsL[[5]]}]];
(* ct=1;While[Col[MA,ct++]==v0]; ct=pv[[1]]*)
{MA1,EM1}=ZSweepOut1[MA,pv[[1]],1];
While[EM1≠En, {MA1,EM1}=ZSweepOut1[MA1,pv[[1]],1]];
If[MA1≠RE,retI=Append[retI,{"I",IfDeb[MF[MA1],"\\("<>TOT[MA1]<>"\\)"],fsL[[3]]}]];
If[rk≥2,{MA1,EM1}=ZSweepOut1[MA1,pv[[2]],2];
While[EM1≠En,{MA1,EM1}=ZSweepOut1[MA1,pv[[2]],2]];
If[MA1≠RE,retI=Append[retI,{"I",IfDeb[MF[MA1],"\\("<>TOT[MA1]<>"\\)"],fsL[[3]]}]]];
(* noises *)
(* RE1=RE;RE1[[1,pv[[1]]]]=RE1[[1,pv[[1]]]]+Rnz[0];
If[RE1≠RE,retI=Append[retI,{"I",IfDeb[MF[RE1],"\\("<>TOT[RE1]<>"\\)"],fsL[[4]]}]]; *)
(* If[rk>1,RE1=RE;RE1[[2,pv[[2]]]]=RE1[[2,pv[[2]]]]+Rnz[0];
If[RE1≠RE,retI=Append[retI,{"I",IfDeb[MF[RE1],"\\("<>TOT[RE1]<>"\\)"],fsL[[4]]}]]];
If[rk>1,RE1=RE;RE1[[2,pv[[2]]]]=-RE1[[2,pv[[2]]]];
If[RE1≠RE,retI=Append[retI,{"I",IfDeb[MF[RE1],"\\("<>TOT[RE1]<>"\\)"],fsL[[4]]}]]]; *)
While[Length[retI]<SELNUM+1,
RE1=RandomEchelonPV2[row,col,pv,Range[-10,10]/(RandomInteger[2]+1)];
If[RE1≠RE,retI=Append[retI,{"I",IfDeb[MF[RE1],"\\("<>TOT[RE1]<>"\\)"],fsL[[4]]}]]];
retI=DeleteDuplicates[retI];
retI=RandomSample[retI,SELNUM-1];
Return[Join[{{1,SELNUM-1}},retC,retI]]];

DebOn[];Print[QtextF[80]];AListF[80]

DebOn[];For[ct=1,ct≤NUM,ct++,AListF[ct]]

AListF[2]

QtextF[2]

GCDL[{0,0,0,0}]

DebOff[];GenMCxmlV2[DFname[2],Category<>Title<>"(MC2)",Title,NUM,Single,QtextF,AListF]

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

RandomEchelonRank[3,4,0,{-1,1}]

QtextF[10]