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

(* Global Options *)
Shuffle="false";Numbering="abc";
(* Numbers of select options and questions *)
SELNUM=8;Single="false";NUM=400;Title="2次行列式の定義と計算";
GeneralFeedback="";

TOT[mt_]:=ToString[OneLineTeXForm[mt]];
TOTT[mt_]:=ToString[OneLineTeXForm[Transpose[mt]]];
TTSt[eq_]:=ToString[TeXForm[StandardForm[eq]]];
TOC[mt_,c_]:=TOTT[{Transpose[mt][[c]]}];
TTD[r_]:=StringReplace[TT[r],"\\frac"->"\\dfrac"];
MFT[m_]:=MatrixForm[Transpose[m]];
MF[m_]:=MatrixForm[m];
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,8}]];
ci2tex[c_,s_]:=StringReplace[s,Table["\\text{"<>c<>TOT[i]<>"}"->"{\\mathbf "<>c<>"}_"<>TOT[i],{i,1,5}]];
ci2tex[c_,s_,tx_]:=StringReplace[s,Table["\\text{"<>c<>TOT[i]<>"}"->"{"<>tx<>c<>"}_"<>TOT[i],{i,1,8}]];
TOTci2tex[c_,s_]:=StringReplace[TOT[s],Table["\\text{"<>c<>TOT[i]<>"}"->"{\\mathbf "<>c<>"}_"<>TOT[i],{i,1,8}]];
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]]];
GCDL[L_]:=Module[{ct,ret=0},For[ct=1,ct≤Length[L],ct++,ret=GCD[ret,L[[ct]]]];Return[ret]];

Clear[p,q,r,s,t,u,v,x,y,z]

(* select pairs of matrices *)
MAlist=Range[-6,6];
QtList={}; For[ct=1,ct≤NUM*1.05,ct++,
Cn=RandomChoice[{2,2,3,3,3,4,4,4,4,4,5,5,5,6}];
Dval=RandomChoice[Range[-8,8]];
While[Det[MA=Table[Table[Rnz[3],2],2]]≠Dval];
GML={};While[Length[GML]<Cn,While[Det[GM=Table[Table[Rnz[1],2],2]]≠1];GML=Append[GML,GM]];
QtList=Append[QtList,{Cn,Dval,MA,GML}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,Dval,MA,GML,ret},
{Cn,Dval,MA,GML}=QtList[[mm]];ret=IfDeb[{Dval,MatrixForm[MA]},
"以下の2次行列式のうち, 値が\\("<>TOT[Dval]<>"\\)になるものをすべて選びなさい."];
Return[ret]];

AListF[mm_]:=Module[{Cn,Dval,MA,GML,GM,MA1,ct,Tmp,fsL={},retC={},retI={}},
{Cn,Dval,MA,GML}=QtList[[mm]];
fsL=Append[fsL,"正解です."];fsL=Append[fsL,"計算を確認してみよう."];
For[ct=1,ct≤Cn,ct++,GM=GML[[ct]];
Tmp=GM.MA;retC=Append[retC,{"C",IfDeb[{Det[Tmp],MF[Tmp]},"\\("<>TOT[Tmp]<>"\\)"],fsL[[1]]}];
Tmp=MA.GM;retC=Append[retC,{"C",IfDeb[{Det[Tmp],MF[Tmp]},"\\("<>TOT[Tmp]<>"\\)"],fsL[[1]]}];
retC=DeleteDuplicates[retC]];
While[Length[retC]<Cn,
While[Det[Tmp=Table[Table[Rnz[3],2],2]]≠Dval];
retC=Append[retC,{"C",IfDeb[{Det[Tmp],MF[Tmp]},"\\("<>TOT[Tmp]<>"\\)"],fsL[[1]]}];
retC=DeleteDuplicates[retC]];
retC=RandomSample[retC,Cn];
While[Length[retI]<SELNUM-Cn,
While[Det[MA1=Table[Table[Rnz[3],2],2]]==Dval];
Tmp=MA1;retI=Append[retI,{"I",IfDeb[{Det[Tmp],MF[Tmp]},"\\("<>TOT[Tmp]<>"\\)"],fsL[[2]]}];
retI=DeleteDuplicates[retI]];
Return[Join[{{Cn,SELNUM-Cn}},retC,retI]]];

Print[QtextF[15]];AListF[15]

(* For[ct=1,ct≤NUM,ct++,Print[ct];QtextF[ct];AListF[ct]]; (* Error Check *) *)

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

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