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

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,5}]];
ci2tex[c_,s_]:=StringReplace[s,Table["\\text{"<>c<>TOT[i]<>"}"->"{\\mathbf "<>c<>"}_"<>TOT[i],{i,1,5}]];
TOTci2tex[c_,s_]:=StringReplace[TOT[s],Table["\\text{"<>c<>TOT[i]<>"}"->"{\\mathbf "<>c<>"}_"<>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_]:=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]]];
MF[m_]:=MatrixForm[m];MFT[m_]:=MatrixForm[Transpose[m]];

(* Global Options *)
col=4;rk=2;Numbering="abc";Title="連立1次方程式の解法(v"<>TOT[col]<>"r"<>TOT[rk]<>")";
(* Numbers of select options and questions *)
Shuffle="false";Single="true";SELNUM=8;NUM=500;
GeneralFeedback="";

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

RElist={-3,-2,-1,1,2,3,4,5,6,7};
Which[
col==4,pvL={{1,2},{1,2},{1,2},{1,2},{1,3},{1,3},{2,3}},
col==5,Which[
rk==2,pvL={{1,2},{1,3},{1,3},{1,4},{1,4},{2,3},{2,4}},
rk==3,pvL={{1,2,3},{1,2,3},{1,2,3},{1,2,4},{1,3,4}}],
col==6,Which[
rk==2,pvL={{1,2},{1,3},{1,3},{1,4},{1,4},{1,5},{2,4},{2,5}},
rk==3,pvL={{1,2,3},{1,2,4},{1,2,5},{1,3,4},{1,3,5},{2,3,5}}]];
(* select pairs of matrices *)
QtList={}; For[ct=1,ct≤NUM*1.02,ct++,
Cn=RandomChoice[{0,1,1,1,1,1,1,1,1}]; (* -1 means eq has no sol *)
tp=RandomChoice[{0,0,2,2,2,2,2}]; (* 0:不能,2:不定(rk=2) *)
row=3+RandomInteger[1];pv=RandomChoice[pvL];
While[Abs[Det[GM=Table[Table[Rnz[2],row],row]]]≠1];
RE=RandomEchelonPV2[row,col,pv,RElist];
Mb=Table[Rnz[2],row];
If[tp==2,Mb[[Range[3,row]]]=0];
QtList=Append[QtList,{Cn,tp,pv,RE,Transpose[{Mb}],GM}]];
QtList=DeleteDuplicates[QtList];
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,tp,pv,RE,Mb,GM,Wx,ret},
{Cn,tp,pv,RE,Mb,GM}=QtList[[mm]];Wx="{\\mathbf x}";
ret=IfDeb[{Cn,tp,MF[RE],MF[Mb]},
"以下の係数行列\\(A\\)とベクトル\\({\\mathbf b}\\)に対し,"
<>"方程式\\(A{\\mathbf x}={\\mathbf b}\\)の解について"
<>"正しいものを<u>1つ</u>選べ.<br/>\n"
<>"\\(A="<>TOT[GM.RE]<>",{\\mathbf b}="<>TOT[GM.Mb]<>"\\)"];
Return[ret]];

RC[m_]:=RandomChoice[m];RI[m_]:=RandomInteger[m];
RS[m_]:=RandomSample[m];RS[L_,k_]:=RandomSample[L,k];
TP[M_]:=Transpose[M];DebPrint[a_]:=If[DebugMode,Print[a]];

AListF[mm_]:=Module[{Cn,tp,pv,rk,row,vn,kn,RE,Mb,Mz,GM,ct,Wx,Vc,Vc1,VcL={},NB,Vu,Vu1,VuL={},
sL={},fsL={},asL,s1,s2,s3,aret,Tmp,ret={}},
{Cn,tp,pv,RE,Mb,GM}=QtList[[mm]];rk=Length[pv];row=Length[RE];vn=Length[RE[[1]]];
DebPrint[ {MF[RE],MF[Mb],Cn,tp},""];
Mz=TP[{Table[0,row]}];kn=vn-rk;
Wx="\\begin{pmatrix}x\\\\y\\\\z\\end{pmatrix}=";
fsL=Append[fsL,"正解です."]; (*1*)
fsL=Append[fsL,"正解です. この方程式は解を持ちません."]; (*2*)
fsL=Append[fsL,"正解です. 上の選択肢に正しい解はありません."]; (*3*)
fsL=Append[fsL,"残念. パラメータの数が違います."];(*4*)
fsL=Append[fsL,"残念. パラメータ表示の各ベクトルは条件を満たしていますか?"];(*5*)
fsL=Append[fsL,"残念. この方程式は解を持ちます."];(*6*)
fsL=Append[fsL,"残念. 選択肢に正解があります."];(*7*)
fsL=Append[fsL,"残念. 違います."];(*8*)
Vc=Table[0,vn];For[ct=1,ct≤rk,ct++,Vc[[pv[[ct]]]]=Mb[[ct,1]]];Vc=TP[{Vc}];
NB=NullSpace[RE];If[Length[NB]≠vn-rk,Print["Logical Error"]];
Vu=Reverse[TP[{#}]&/@NB];VcL=VuL={};
For[ct=1,ct≤rk,ct++,Vc1=TP[{RE[[ct]]}];If[RE.Vc1≠Mb,VcL=Append[VcL,Vc1]]];
For[ct=1,ct≤rk,ct++,Vu1=TP[{RE[[ct]]}];If[RE.Vu1≠Mz,VuL=Append[VuL,Vu1]]];
ct=1;While[Length[VcL]<kn*2&&ct<3^kn,ct++;
While[RE.(Vc1=Vc*TP[{Table[Rnz[0],vn]}])==Mb];VcL=Append[VcL,Vc1];
While[RE.(Vc1=Vc+TP[{Table[RC[{0,0,1}],vn]}])==Mb];VcL=Append[VcL,Vc1];
VcL=DeleteDuplicates[VcL]];
ct=1;While[Length[VuL]<kn*2&&ct<3^kn,ct++;
While[RE.(Vu1=RC[Vu]*TP[{Table[Rnz[0],vn]}])==Mz];VuL=Append[VuL,Vu1];
While[RE.(Vu1=RC[Vu]+TP[{Table[RC[{0,0,1}],vn]}])==Mz];VuL=Append[VuL,Vu1];
VuL=DeleteDuplicates[VuL]];
(* Print[{VcL,VuL}]; *)
If[MemberQ[VuL,Vu],Print["VuL:Error"]];
(* Print[{Length[VcL],Length[VuL]}]; *)
asL[vc_,vu_]:=Module[{pars,k,l,s=""},
l=Length[vu];pars=RS[{"p","q","r","s","t","u"},l];
For[k=1,k≤l,k++,s=s<>pars[[k]]<>TOT[vu[[k]]];If[k<l,s=s<>"+"]];
sL=Append[sL,IfDeb[Join[{MF[vc]},MF[#]&/@vu],
"\\("<>Wx<>TOT[vc]<>"+"<>s<>",\\ "
<>StringRiffle[pars,","]<>"\\in{\\mathbb R}\\)"]];
sL=DeleteDuplicates[sL]];
sL=Append[sL,IfDeb["No sol","不能(解なし)"]];(*1:correct for tp=0 case *)
sL=Append[sL,IfDeb[MF[Vc],"\\("<>Wx<>TOT[Vc]<>"\\) (一意解)"]];(*2:correct for tp=1 *)
asL[Vc,Rnz[0]*Vu]; (* 3:correct for tp=2 case *)
sL=Append[sL,IfDeb["No opts.","上の選択肢に正解はない."]]; s1=Length[sL]; (*4*)
For[ct=1,ct<kn,ct++,(* less parameters case *)
If[RI[1]==0,asL[Vc,RS[Vu,ct]],asL[Vc,RS[VuL,ct]]];
asL[RC[VcL],RS[VuL,ct]];
asL[Vc,Vc+#&/@RS[Vu,ct]];
asL[RC[Vu],RS[Append[VcL,Vc],ct]]];s2=Length[sL];
asL[Vc,Vc+#&/@Vu];asL[Vc,RS[VuL,kn]];
asL[RC[Join[VcL,Vu]],Vu] ;asL[RC[Join[VcL,Vu]],RS[VuL,kn]];s3=Length[sL];
aret[ic_,s_,n_]:=Module[{},ret=DeleteDuplicates[Append[ret,{ic,sL[[s]],fsL[[n]]}]]];
Which[tp==0,For[ct=s1+1,ct≤s3,ct++,aret["I",ct,8]],
tp==1,For[ct=s1+1,ct≤s3,aret["I",ct,4]], (* 想定外 *)
tp==2,aret["I",1,6];aret["I",2,4];
For[ct=s1+1,ct≤s2,ct++,aret["I",ct,4]];For[ct=s2+1,ct≤s3,ct++,aret["I",ct,5]]];
If[Cn==1,ret=RS[ret,SELNUM-2];Which[tp==0,aret["C",1, 2],(* tp==1,aret["C",2,1],*)tp==2,aret["C",3,1]],
ret=RS[ret,SELNUM-1]];
ret=RS[ret];If[Length[ret]≠SELNUM-1,Print["Length:ERROR"]];
If[Cn==0,aret["C",4,3],aret["I",4,7]];
Return[Join[{{1,SELNUM-1}},ret]]];

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

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