(* 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=4;Single="false";NUM=300;Title="係数行列";

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;
MF[m_]:=MatrixForm[m];MFT[m_]:=MatrixForm[Transpose[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}]];
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++,Cn=RandomChoice[{1,2,2,2,2,3}];
m=RandomChoice[{2,2,2,3,3}];
n=RandomChoice[{3,3,3,3,4,4,5}];
Which[n==3,vlist={x,y,z},n==4,vlist={w,x,y,z},n==5,vlist={v,w,x,y,z}];
MCbase=RandomSample[Range[-Floor[n*m/2]-1,Floor[n*m/2]+1],n*m];
MC=Table[Table[MCbase[[j+(i-1)*n]],{j,1,n}],{i,1,m}];
QtList=Append[QtList,{Cn,{m,n,vlist,MC}}];
QtList=DeleteDuplicates[QtList]];QtList=RandomSample[QtList,NUM];Length[QtList]

QtList[[32]]

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

DebOn[];QtextF[RandomInteger[NUM-1]+1]

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,vlist2,MC,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.\\)";
vlist2=ToExpression[Table["x"<>ToString[i],{i,1,n}]];
For[ct=1,ct≤Cn,ct++,lhs="";rhs=Table[Rnz[3],m];
For[ct2=1,ct2≤m,ct2++,lhs=lhs<>TOT[MC[[ct2]].vlist]<>"&=&"<>TOT[rhs[[ct2]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[TOT[vlist],{"\\{"->"","\\}"->""}]<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs}]];
For[ct=1,ct≤Cn,ct++,lhs="";rhs=Table[Rnz[3],m];
For[ct2=1,ct2≤m,ct2++,lhs=lhs<>xi2tex[MC[[ct2]].vlist2]<>"&=&"<>TOT[rhs[[ct2]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vlist2],{"\\{"->"","\\}"->""}]<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs}]];
Return[RandomSample[ret,Cn]]];

CListF[30]

ICListF[mm_]:=Module[{Cn,m,n,vlist,vlist2,vl,MC,MC1,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.\\)";
vlist2=ToExpression[Table["x"<>ToString[i],{i,1,n}]];
fs1="変数の順序をよく確認しよう. ";
fs2="係数行列は変数の係数のみからなる行列です. 右辺値は無関係であることに注意.";
(* case fs2 *)
MC1=MC;MC1[[Table[i,{i,1,m}],n]]=Table[0,m];lhs="";
vl=RandomChoice[{vlist,vlist2}];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>xi2tex[MC1[[ct2]].vl]<>"&=&"<>TOT[MC[[ct2,n]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vl],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs2}];
(* case fs1 *)
MC1=MC;MC1[[m,n-1]]=MC[[m,n]];MC1[[m,n]]=MC[[m,n-1]];
lhs="";rhs=Table[Rnz[3],m];vl=RandomChoice[{vlist,vlist2}];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>xi2tex[MC1[[ct2]].vl]<>"&=&"<>TOT[rhs[[m]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vl],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs1}];
(* case fs1 -2 *)
MC1=MC;jL=RandomSample[Table[i,{i,1,n}],2];
MC1[[Table[i,{i,1,m}],jL[[1]]]]=MC[[Table[i,{i,1,m}],jL[[2]]]];MC1[[Table[i,{i,1,m}],jL[[2]]]]=MC[[Table[i,{i,1,m}],jL[[1]]]];
lhs="";rhs=Table[Rnz[3],m];vl=RandomChoice[{vlist,vlist2}];
For[ct2=1,ct2≤m,ct2++,
lhs=lhs<>xi2tex[MC1[[ct2]].vl]<>"&=&"<>TOT[rhs[[m]]];
If[ct2<m,lhs=lhs<>"\\\\"]];
ret=Append[ret,{"変数\\("<>StringReplace[xi2tex[vl],{"\\{"->"","\\}"->""}]
<>"\\)に関する方程式"<>Frm1<>lhs<>Frm2,fs1}];
Return[IfDeb[ret,RandomSample[ret,SELNUM-Cn]]]];

ICListF[37]

Print[ToString[3]<>"\"<>ToString[4]]

? Print

For[ct=1,ct≤NUM,ct++,PrintTemporary[ct];CListF[ct]]

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

StringReplace[xi2tex[vlist2],{"\\{"->"","\\}"->""}]

xi2tex[vlist2]

TeXForm[HoldForm[3x+4y+5z+3v+7w]]

a={1,2,3,4,5};vlist={x,y,z,v,w};

? Order

a.vlist

?

? CPOrdering

Table[Rnz[3],3]