(* 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 *)
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]]];
TTSt[eq_]:=ToString[TeXForm[StandardForm[eq]]];
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]]}];
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]]];

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

(* select pairs of matrices *)
QtList={}; For[ct=1,ct≤300,ct++,
Cn=1;
pr=RandomChoice[{p,q,r,s,t}];
col=RandomChoice[{3,3,3,4,4,5}];
row=RandomChoice[{2,2,3,3,3,3,4}];
MA=Table[Table[RandomChoice[{-3,-2,-1,-1,-1,0,0,1,1,2,2,3}],col],row];
(* While[MatrixRank[MA]≠col,MA=Table[Table[RandomChoice[{-3,-2,-1,-1,-1,0,0,1,1,2,2,3}],col],row]]; *)
QtList=Append[QtList,{Cn,{pr,row,col,MA}}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

StringRiffle[Table[ToString[i],{i,1,3}],","]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,pr,pS,row,col,MA,vv,vS,pVS,i,ct,ret},
{Cn,{pr,row,col,MA}}=QtList[[mm]];
pS=StringRiffle[Table[TOT[pr]<>"_"<>TOT[i],{i,1,col-1}],","];
pVS=StringRiffle[Table[TOT[pr]<>"_"<>TOT[i]<>"{\\mathbf b}_"<>TOT[i],{i,1,col-1}],"+"];
vv=MA.Prepend[Table[ToExpression[TOT[pr]<>TOT[i]],{i,1,col-1}],1];
vS="\\begin{pmatrix}";
For[ct=1,ct≤row,ct++,
vS=vS<>ci2tex[ToString[pr],TTSt[vv[[ct]]],""];If[ct<row,vS=vS<>"\\\\"]];vS=vS<>"\\end{pmatrix}";
ret="文字\\("<>pS<>"\\)を成分に含むベクトル\\("<>vS<>"\\)を\\("<>pS<>"\\)について整理すると"
<>"\\({\\mathbf a}+"<>pVS<>"\\)と書ける. \\({\\mathbf a},{\\mathbf b}_1,\\dots\\)"
<>"の組み合わせとして正しいものを選べ\n";
Return[ret]];

CListF[mm_]:=Module[{Cn,pr,row,col,MA,Va,Vbi,ct,fs,ret={}},
{Cn,{pr,row,col,MA}}=QtList[[mm]];fs="正解です.";Vbi={};
Va=Col[MA,1];For[ct=2,ct≤col,ct++,Vbi=Append[Vbi,Col[MA,ct]]];
ret=Append[ret,{"\\("<>TOT[Va]<>","
<>StringRiffle[Table[TOT[Vbi[[i]]],{i,1,col-1}],","]
<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,pr,row,col,MA,MA1,i,j,parm,Va,Vbi,Va1,Vbi1,fs1,fs2,ret={}},
{Cn,{pr,row,col,MA}}=QtList[[mm]];
fs1="\\(p=0\\)とおいて得られるベクトルが\\({\\mathbf a}\\)です.";
fs2="検算として例えば\\("<>TOT[pr]
<>"_1=1,"<>TOT[pr]<>"_2=\\cdots=0\\)を代入したものと\\({\\mathbf a}+{\\mathbf b_1}\\)が一致しているか確かめよう．";
Va=Col[MA,1];Vbi=Table[Col[MA,ct],{ct,2,col}];
Va1=Col[MA,col];Vbi1=Table[Col[MA,ct],{ct,1,col-1}];
ret=Append[ret,
{"\\("<>TOT[Va1]<>","<>StringRiffle[Table[TOT[Vbi1[[i]]],{i,1,col-1}],","]<>"\\)",fs1}];
parm=RandomSample[Permutations[Range[col]] /. Range[1,col]->Nothing,4];
For[i=1,i≤4,i++,
Va1=Col[MA,parm[[i,1]]];
Vbi1=Table[Col[MA,parm[[i,j]]],{j,2,col}]];
ret=Append[ret,
{"\\("<>TOT[Va1]<>","<>StringRiffle[Table[TOT[Vbi1[[i]]],{i,1,col-1}],","]<>"\\)",fs1}];
For[ct=1,ct≤5,ct++,
MA1=MA+Table[Table[RandomInteger[1],col],row];
While[MA1==MA,MA1=MA+Table[Table[RandomInteger[1],col],row]];
Va1=Col[MA1,1];Vbi1=Table[Col[MA1,ct],{ct,2,col}];
ret=Append[ret,
{"\\("<>TOT[Va1]<>","<>StringRiffle[Table[TOT[Vbi1[[i]]],{i,1,col-1}],","]<>"\\)",fs2}]
];ret=DeleteDuplicates[ret];
Return[RandomSample[ret,SELNUM-Cn]]];

For[Ct=1,Ct≤NUM,Ct++,QtextF[Ct];CListF[Ct];ICListF[Ct]]; (* Error Check *)

GenerateMCxml[DFname[1],Category<>Title<>"(MC1)",Title,NUM,Single,QtextF,CListF,ICListF]

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

col=5

Permutations[Range[2,5]] /. Range[2,5]->Nothing

ICListF[30]