(* 12-01 *)

(* Require XMLgen_MC.nb ver >=0.04 *)
SetDirectory[NotebookDirectory[]];
<<XMLgenMC.mx;
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);
PtString[M_]:=StringReplace[ToString[M],{"{"->"(","}"->")"}];
Title="正方行列の積";
SELNUM=6;NUM=200;

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤300,ct++,
sz=RandomChoice[{2,2,2,2,3,3,3}];
MA=Table[Table[Rnz[5],{j,1,sz}],{i,1,sz}];
QtList=Append[QtList,{RandomInteger[1]+2,{sz,MA}}];
QtList=DeleteDuplicates[QtList]];QtList=RandomSample[QtList,NUM];Length[QtList]

QtextF[mm_]:=Module[{Cn,sz,MA},{Cn,{sz,MA}}=QtList[[mm]];
Return["次の正方行列の積のうち, \\("<>TOT[MA]<>"\\)と一致するものをすべて選べ"]];

CListF[mm_]:=Module[{Cn,sz,MA,i,j,c,P1,M1,fs,ret={}},
{Cn,{sz,MA}}=QtList[[mm]];fs="正解です.";
(* Pn(i,j;1) *)
{i,j}=RandomSample[Range[sz],2];
P1=GetPnijc[sz,i,j,Rnz[1]];M1=Inverse[P1].MA;
ret=Append[ret,{"\\("<>TOT[P1]<>TOT[M1]<>"\\)",fs}];
{i,j}=RandomSample[Range[sz],2];
P1=GetPnijc[sz,i,j,Rnz[1]];M1=MA.Inverse[P1];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[P1]<>"\\)",fs}];
(* Qn(i;c) *)
P1=GetQnic[sz,RandomChoice[Range[sz]],-1];
M1=Inverse[P1].MA;
ret=Append[ret,{"\\("<>TOT[P1]<>TOT[M1]<>"\\)",fs}];
P1=GetQnic[sz,RandomChoice[Range[sz]],-1];
M1=MA.Inverse[P1];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[P1]<>"\\)",fs}];
(* Rn(i;c) *)
{i,j}=Sort[RandomSample[Range[sz],2]];
P1=GetRnij[sz,i,j];M1=Inverse[P1].MA;
ret=Append[ret,{"\\("<>TOT[P1]<>TOT[M1]<>"\\)",fs}];
P1=GetRnij[sz,i,j];M1=MA.Inverse[P1];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[P1]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,sz,MA,i,j,c,P1,M1,fs1,ret={}},
{Cn,{sz,MA}}=QtList[[mm]];
fs1="すべての成分が一致しているか確認しよう.";
(* Pn(i,j;1) *)
{i,j}=RandomSample[Range[sz],2];
P1=GetPnijc[sz,i,j,Rnz[1]];M1=P1.MA; (* should be P1^(-1) *)
ret=Append[ret,{"\\("<>TOT[P1]<>TOT[M1]<>"\\)",fs1}];
{i,j}=RandomSample[Range[sz],2];
P1=GetPnijc[sz,i,j,Rnz[1]];M1=MA.P1;
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[P1]<>"\\)",fs1}];
(* Qn(i;c) *)
P1=GetQnic[sz,RandomChoice[Range[sz]],-1];
M1=Inverse[P1].MA;
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[P1]<>"\\)",fs1}];
(* Rn(i;c) *)
{i,j}=Sort[RandomSample[Range[sz],2]];
P1=GetRnij[sz,i,j];M1=P1.MA;
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[P1]<>"\\)",fs1}];
P1=GetRnij[sz,i,j];M1=MA.P1;
ret=Append[ret,{"\\("<>TOT[P1]<>TOT[M1]<>"\\)",fs1}];
Return[RandomSample[ret,SELNUM-Cn]]];

QtextF[3]

QtList[[5]]

ICListF[5]

GenerateMCxml[DFname[],"LA/12-01."<>Title<>"(MC1)",Title,NUM,"false",QtextF,CListF,ICListF]

Col[{{1,2},{3,4}},1]

Join[{a,b},{c,d}]

RandomSample[Append[{a,b},c],2]

RandomSample[Range[3],2]

MA={{2,4,6},{3,6,9},{5,10,15}}

GCD[Flatten[MA[[2]]]]

? GCD

? Flatten

? GCD

GCD[MA[[2,1]],MA[[2,2]],MA[[2,3]]]