(* 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 *)
Numbering="abc";Title="3次行列式の余因子展開";
(* Numbers of select options and questions *)
Shuffle="true";Single="true";SELNUM=6;NUM=300;
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}]];
TeXArray2Matrix[s_,mtx_]:=StringReplace[s,Join[{"array"->mtx,"\\left("->"","\\right)"->""},#->"" &/@ ("{"<>StringRepeat["c",#]<>"}" &/@ Reverse[Range[20]])]];
TAM[s_,mtx_]:=TeXArray2Matrix[TOT[s],mtx];
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=Join[Range[-9,-1],Range[2,12]];sz=3;
QtList={}; For[ct=1,ct≤NUM*1.02,ct++,
Cn=1;RC=RandomChoice[{"R","C"}];
k=RandomChoice[Range[sz]];
MA=ArrayReshape[RandomSample[MAList,sz*sz],{sz,sz}];
QtList=Append[QtList,{Cn,sz,MA,RC,k}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[a,b,c,QtextF];QtextF[mm_]:=Module[{Cn,sz,MA,RC,k,ret,ct},
{Cn,sz,MA,RC,k}=QtList[[mm]];
ret="以下の3次行列式に対する第"<>TOT[k]<>If[RC=="R","行","列"]<>"に関して展開したものとして正しいものを選びなさい.\n<br/>"
<>"\\("<>TAM[MA,"vmatrix"]<>"\\)";
Return[IfDeb[{MF[MA],RC,k},ret]]];

SM1[M_,i_,j_]:=Flatten[Col[M[[Range[Length[M]] /.i->Nothing]],Range[Length[M[[1]]]]/. j->Nothing],1];
TOTp[x_]:=If[x<0,TOT[x],"+"<>TOT[x]];
TAv[M_]:=TAM[M,"vmatrix"];
DspTemp[M_,RC_,k_,SL_]:=Module[{ct,s1,s2,s3},{s1,s2,s3}=SL;
Return[If[RC=="R",TOT[s1*M[[k,1]]]<>TAv[SM1[M,k,1]]
<>TOTp[s2*M[[k,2]]]<>TAv[SM1[M,k,2]]<>TOTp[s3*M[[k,3]]]<>TAv[SM1[M,k,3]],
TOT[s1*M[[1,k]]]<>TAv[SM1[M,1,k]]<>TOTp[s2*M[[2,k]]]<>TAv[SM1[M,2,k]]<>TOTp[s3*M[[3,k]]]<>TAv[SM1[M,3,k]]]]];

AListF[mm_]:=Module[{Cn,sz,MA,RC,RC1,k,Trm={},i,j,ct,SL,fsL={},ret={},IL={}},
{Cn,sz,MA,RC,k}=QtList[[mm]];If[RC=="R",RC1="C",RC1="R"];
fsL=Append[fsL,"正解です."];fsL=Append[fsL,"違います. 線形性で展開した後, 符号を考えましょう."];
fsL=Append[fsL,"違います. 行と列を間違えていませんか?"];
fsL=Append[fsL,"違います. 行と列, 符号を間違えていませんか?"];
SL={-1,1,-1}*(-1)^k;
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{1,1,-1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{1,-1,1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{-1,1,1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{1,-1,-1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{-1,1,-1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{-1,-1,1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC,k,SL*{-1,-1,-1}]<>"\\)",fsL[[2]]}];
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL]<>"\\)",fsL[[3]]}];
(* ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{1,1,-1}]<>"\\)",fsL[[4]]}]; *)
(* ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{1,-1,1}]<>"\\)",fsL[[4]]}]; *)
(* ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{-1,1,1}]<>"\\)",fsL[[4]]}]; *)
 ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{1,-1,-1}]<>"\\)",fsL[[4]]}];
(* ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{-1,1,-1}]<>"\\)",fsL[[4]]}]; *)
(* ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{-1,-1,1}]<>"\\)",fsL[[4]]}]; *)
ret=Append[ret,{"I","\\("<>DspTemp[MA,RC1,k,SL*{-1,-1,-1}]<>"\\)",fsL[[4]]}];
ret=RandomSample[ret,SELNUM-1];
ret=Append[ret,{"C","\\("<>DspTemp[MA,RC,k,SL]<>"\\)",fsL[[1]]}];
Return[Join[{{Cn,SELNUM-Cn}},ret]]];

DebOn[];QtextF[8]

AListF[8]

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

(* 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]