(* Require XMLgenMC.mx (ver>0.30) and Categories.mx >=0.30 *)
SetDirectory[NotebookDirectory[]];
<<XMLgenMC.mx;
<<Categories.mx;
ThisNo=StringReplace[StringSplit[ NotebookFileName[],"/"][[-1]],{"LA_"->"",".nb"->"","_"->"-"}];
CatNo=StringReplace[ThisNo,Join["-0"<>ToString[#]->"" &/@Range[1,9],"-1"<>ToString[#]->""&/@Range[0,9]]];
Category="LA/"<>CatNo<>"."<>CatNoAssoc[CatNo]<>"/"<>ThisNo<>".";
eTitle="Ext. coeff. matrix";
Title="拡大係数行列";

(* Global Options *)
DebugMode=False;Shuffle="true";Numbering="abc";
(* Numbers of select options and questions *)
Single="false";SELNUM=4;NUM=300;

(* Require XMLgen_MC.nb ver >=0.04 *)
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],{"{"->"(","}"->")"}];
(* xi2tex[fm_]:=StringReplace[TOT[fm],{"\\text{x"->"x_","}"->""}]; *)
xi2tex[s_]:=StringReplace[TOT[s],Table["\\text{x"<>TOT[i]<>"}"->"x_"<>TOT[i],{i,1,5}]];

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤400,ct++,
m=RandomChoice[{2,2,2,3,3}];
n=RandomChoice[{3,4,4,4,4,5,5,5,6,6,6}];
Which[n==3,vlist={x,y,z},n==4,vlist={x,y,z,w},n==5,vlist={v,w,x,y,z},n==6,vlist={u,v,w,x,y,z}];
MC=Table[Table[Rnz[3],{j,1,n}],{i,1,m}];
QtList=Append[QtList,{2,{m,n,vlist,MC}}];
QtList=DeleteDuplicates[QtList]];QtList=RandomSample[QtList,NUM];Length[QtList]

QtList[[6]]

QtextF[mm_]:=Module[{Cn,m,n,vlist,MC},{Cn,{m,n,vlist,MC}}=QtList[[mm]];
Return["次の連立1次方程式のうち,拡大係数行列が\\(A="<>TOT[MC]
<>"\\)となるものをえらびなさい."]];

xi2tex[s_]:=StringReplace[TOT[s],Table["\\text{x"<>TOT[i]<>"}"->"x_"<>TOT[i],{i,1,10}]];

Clear[x,y,z,v,w,x1,x2,x3,x4,x5]; 
(* b3=c1*b1+c2*b2; a=a1*b1+a2*b2+k(c1*b1+c2*b2-b3); *)

CListF[mm_]:=Module[{Cn,m,n,vlist,vl1,vl2,MC,MC0,lhs,rhs,ct,ct2,fs,Frm1,Frm2,ret={}},
{Cn,{m,n,vlist,MC}}=QtList[[mm]];fs="正解です.";
Frm1="\\(\\left\\{\\begin{array}{ccc}";
Frm2="\\end{array}\\right.\\)";
(* MC0=MC[[Table[i,{i,1,m}],Table[j,{j,1,n-1}]]]; *)
MC0=Transpose[Take[Transpose[MC],n-1]];
vl1=Take[vlist,-(n-1)];
vl2=ToExpression[Table["x"<>ToString[i],{i,1,n-1}]];
For[ct=1,ct≤1,ct++,lhs="";rhs=(Transpose[MC])[[n]];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>TOT[MC0[[ct2]].vl1]<>"&=&"<>TOT[rhs[[ct2]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[TOT[vl1],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs}]];
For[ct=1,ct≤1,ct++,lhs="";rhs=(Transpose[MC])[[n]];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>xi2tex[MC0[[ct2]].vl2]<>"&=&"<>TOT[rhs[[ct2]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vl2],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs}]];
Return[RandomSample[ret,Cn]]]

ICListF[mm_]:=Module[{Cn,m,n,vlist,vlist0,vlist2,vl,MC,MC0,
jL,lhs,rhs,ct,ct2,fs1,fs2,Frm1,Frm2,ret={}},
{Cn,{m,n,vlist,MC}}=QtList[[mm]];
Frm1="\\(\\left\\{\\begin{array}{ccc}";
Frm2="\\end{array}\\right.\\)";
MC0=Transpose[Take[Transpose[MC],n-1]];
vlist2=ToExpression[Table["x"<>ToString[i],{i,1,n-1}]];
vlist0=Take[vlist,-(n-1)];
fs1="拡大係数行列の最後の列は右辺値を並べた列ベクトルがそのまま入ります.";
fs2="拡大係数行列の最後の列は右辺値を並べた列ベクトルであり,変数の係数ではありません.";
(* case fs1 *)
vl=RandomChoice[{vlist0,vlist2}];
lhs="";rhs=(Transpose[MC])[[n]];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>xi2tex[MC0[[ct2]].vl]<>"&=&"<>TOT[-rhs[[ct2]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vl],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs1}];
(* case fs2 *)
vlist2=ToExpression[Table["x"<>ToString[i],{i,1,n}]];
vl=RandomChoice[{vlist,vlist2}];
lhs="";rhs=Table[0,m];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>xi2tex[MC[[ct2]].vl]<>"&=&"<>TOT[0];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vl],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs2}];
Return[RandomSample[ret,SELNUM-Cn]]];

ID=2;DebOff[];GenerateMCxml[DFname[ID],Category<>Title<>"(MC"<>ToString[ID]<>")",Title,NUM,Single,QtextF,CListF,ICListF];