(* 11_02 *)

(* Require XMLgen_MC.nb ver >=0.04 *)
SetDirectory[NotebookDirectory[]];
<<XMLgenMC.mx;
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],{"{"->"(","}"->")"}];
SELNUM=5;NUM=200;

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤1000,ct++,
Nr=Rpos[2]+1;Nc=Rpos[3]+1;
While[Nc==Nr,Nc=Rpos[3]+1];
QtList=Append[QtList,{RandomChoice[{0,1,1,1,1,2,2}]+1,{Nr,Nc,Rbtw[5]}}]
(* QtList=DeleteDuplicates[QtList]; *)
];QtList=RandomSample[QtList,NUM];Length[QtList]

QtextF[mm_]:=Module[{Cn,i,j,c},{Cn,{i,j,c}}=QtList[[mm]];
Return["次の行列の中で\\(("<>TOT[i]<>","<>TOT[j]<>")\\)成分が\\("<>TOT[c]<>"\\)の行列を"
<>"すべて選べ"]];

CListF[mm_]:=Module[{Cn,x,y,i,j,c,Nr,Nc,M,fs,ret={}},
{Cn,{i,j,c}}=QtList[[mm]];fs="正解です.";
(* set 1 *)
Nr=i+Rpos[2]-1;Nc=j+Rpos[3]-1;
M=Table[Table[x*Nr+j,{y,1,Nc}],{x,1,Nr}];
M[[i,j]]=c;
ret=Append[ret,{"\\("<>TOT[M]<>"\\)",fs}];
(* set 2 *)
Nr=i+Rpos[2]-1;Nc=j+Rpos[3]-1;
M=Table[Table[Rnz[3],{y,1,Nc}],{x,1,Nr}];
M[[i,j]]=c;
ret=Append[ret,{"\\("<>TOT[M]<>"\\)",fs}];
(* set 3 *)
Nr=i+Rpos[2]-1;Nc=j+Rpos[3]-1;
M=Table[Table[Rnz[(x-1)*(y-1)],{y,1,Nc}],{x,1,Nr}];
M[[i,j]]=c;
ret=Append[ret,{"\\("<>TOT[M]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,x,y,s1,s2,i,j,c,c1,c2,fs1,fs2,fs3,Nr,Nc,M,ret={}},
{Cn,{i,j,c}}=QtList[[mm]];
s1="\\(("<>TOT[i]<>","<>TOT[j]<>")\\)";
s2="\\(("<>TOT[j]<>","<>TOT[i]<>")\\)";
fs1=s1<>"成分が\\("<>TOT[c]<>"\\)なのではなく,"<>s2>"成分が\\("<>TOT[c]<>"\\)です.";
fs2="この行列は"<>s1<>"成分を持ちません.";
fs3="この行列の"<>s1<>"成分も"<>s2<>"成分も"<>TOT[c]<>"ではありません.";
c1=c+Rnz[2];c2=c+Rnz[2];



Xc=Nc+Rnz[2];While[Xc<2,Xc=Nc+Rnz[2]];
Xr=Nr+Rnz[3];While[Xr<2,Xr=Nr+Rnz[3]];
fs1="行の数はあっていますのが列の数が異なります.";
fs2="列の数はあっていますのが行の数が異なります.";
fs3="行と列が逆です.";
fs4="行の数, 列の数ともに一致しません.";
MM=Table[Table[Rnz[2],{j,1,Xc}],{i,1,Nr}];
ret=Append[ret,{"\\("<>TOT[MM]<>"\\)",fs1}];
MM=Table[Table[Rnz[2],{j,1,Nc}],{i,1,Xr}];
ret=Append[ret,{"\\("<>TOT[MM]<>"\\)",fs2}];
MM=Table[Table[Rnz[2],{j,1,Nr}],{i,1,Nc}];
ret=Append[ret,{"\\("<>TOT[MM]<>"\\)",fs3}];
MM=Table[Table[Rnz[2],{j,1,Nr}],{i,1,Nc}];
ret=Append[ret,{"\\("<>TOT[MM]<>"\\)",fs3}];
If[{Nr,Nc}≠{Xc,Xr},
MM=Table[Table[Rnz[2],{j,1,Xc}],{i,1,Xr}];
ret=Append[ret,{"\\("<>TOT[MM]<>"\\)",fs4}],
MM=Table[Table[Abs[i-j],{j,1,Nr}],{i,1,Nc}];
ret=Append[ret,{"\\("<>TOT[MM]<>"\\)",fs3}]];
Return[RandomSample[ret,SELNUM-Cn]]];

GenerateMCxml[DFname[],"LA/11-02.行列の成分/MC-1","行列の成分",NUM,"false",QtextF,CListF,ICListF]