(* 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 *)
Numbering="abc";Single="true";Shuffle="true";
(* Numbers of select options and questions *)
SELNUM=4;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_]:=Icf[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}]; *)
Cn=1;row=RandomChoice[{3,3,3,4}];col=RandomChoice[{4,4,4,5,5}];
rc=RandomChoice[{"row","col"}];
MA=Table[Table[Rnz[3],col],row];
i=RandomChoice[Range[row]];j=RandomChoice[Range[col]];
ij=RandomChoice[{2,2,2,1,1,-1,-1,-1,-2,-2,3,3}];
If[rc=="row",
For[k=1,k≤col,k++,
If[k==j,MA[[i,k]]=ij,
MA[[i,k]]=LCM[ij,MA[[i,k]]]]],
For[k=1,k≤row,k++,
If[k==i,MA[[k,j]]=ij,
MA[[k,j]]=LCM[ij,MA[[k,j]]]]]];
QtList=Append[QtList,{Cn,{row,col},{i,j,rc},MA}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,row,col,i,j,rc,MA},
{Cn,{row,col},{i,j,rc},MA}=QtList[[mm]];
Return["次の行列\\("<>TOT[MA]<>"\\)を\\(("<>TOT[i]<>","<>TOT[j]<>")\\)に関して第"<>
If[rc=="row",TOT[i]<>"行",TOT[j]<>"列"]<>"を掃き出しなさい."]];

QtextF[40]

CListF[mm_]:=Module[{Cn,row,col,i,j,rc,MA,MA1,k,l,rt,fs,ret={}},
{Cn,{row,col},{i,j,rc},MA}=QtList[[mm]];fs="正解です.";MA1=MA;
If[rc=="row",
For[k=1,k≤col,k++,
If[k≠j,rt=MA1[[i,k]]/MA1[[i,j]];
For[l=1,l≤row,l++,MA1[[l,k]]=MA1[[l,k]]-rt*MA1[[l,j]]]]],
For[k=1,k≤row,k++,
If[k≠i,rt=MA1[[k,j]]/MA1[[i,j]];
For[l=1,l≤col,l++,MA1[[k,l]]=MA1[[k,l]]-rt*MA1[[i,l]]]]]];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];
QtextF[9]

CListF[9]

ICListF[mm_]:=Module[{Cn,row,col,i,j,rc,MA,MA1,k,l,rt,fs1,fs2,fs3,ret={}},
{Cn,{row,col},{i,j,rc},MA}=QtList[[mm]];
fs1="行について掃き出す計算と列について掃き出す計算を間違えないようにしよう.";
MA1=MA;
If[rc=="col", (* row/col reversed *)
For[k=1,k≤col,k++,
If[k≠j,rt=MA1[[i,k]]/MA1[[i,j]];
For[l=1,l≤row,l++,MA1[[l,k]]=MA1[[l,k]]-rt*MA1[[l,j]]]]],
For[k=1,k≤row,k++,
If[k≠i,rt=MA1[[k,j]]/MA1[[i,j]];
For[l=1,l≤col,l++,MA1[[k,l]]=MA1[[k,l]]-rt*MA1[[i,l]]]]]];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs1}];
fs2="計算をよく確認しよう";MA1=MA; (* 計算ミス ケース *)
If[rc=="row",
For[k=1,k≤col,k++,
If[k≠j,rt=MA1[[i,k]]/MA1[[i,j]];
For[l=1,l≤row,l++,MA1[[l,k]]=MA1[[l,k]]+rt*MA1[[l,j]]]]],
For[k=1,k≤row,k++,
If[k≠i,rt=MA1[[k,j]]/MA1[[i,j]];
For[l=1,l≤col,l++,MA1[[k,l]]=MA1[[k,l]]+rt*MA1[[i,l]]]]]];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs2}];
MA1=MA;
If[rc=="row",
For[k=1,k≤col,k++,
If[k≠j,rt=MA1[[i,k]]/MA1[[i,j]]+Rnz[0];
For[l=1,l≤row,l++,MA1[[l,k]]=MA1[[l,k]]-rt*MA1[[l,j]]]]],
For[k=1,k≤row,k++,
If[k≠i,rt=MA1[[k,j]]/MA1[[i,j]]+Rnz[0];
For[l=1,l≤col,l++,MA1[[k,l]]=MA1[[k,l]]-rt*MA1[[i,l]]]]]];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs2}];
MA1=MA;
If[rc=="row",
fs3="\\("<>TOT[j]<>"\\)列は消えません.";
For[k=1,k≤col,k++,
rt=MA[[i,k]]/MA[[i,j]];
For[l=1,l≤row,l++,MA1[[l,k]]=MA[[l,k]]-rt*MA[[l,j]]]],
fs3="\\("<>TOT[i]<>"\\)行は消えません.";
For[k=1,k≤row,k++,
rt=MA1[[k,j]]/MA[[i,j]];
For[l=1,l≤col,l++,MA1[[k,l]]=MA[[k,l]]-rt*MA[[i,l]]]]];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs3}];
Return[RandomSample[ret,SELNUM-Cn]]];

QtList[[5]]

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

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]]; *)