(* 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],{"{"->"(","}"->")"}];
Title="行列の成分";
SELNUM=6;NUM=200;

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤1000,ct++,
i=Rpos[3];j=Rpos[3];
While[j==i,j=Rpos[3]];
QtList=Append[QtList,{Rpos[1]+1,{i,j,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,NrcL,M,k,l,fs,ret={}},
{Cn,{i,j,c}}=QtList[[mm]];fs="正解です.";
NrcL=RandomSample[Flatten[Table[{i+k,j+l},{k,1,3},{l,1,3}],1],3];
(* set 1 *)
{Nr,Nc}=NrcL[[1]];
M=Table[Table[(x-1)*Nr+(y-1),{y,1,Nc}],{x,1,Nr}];
M[[i,j]]=c;
ret=Append[ret,{"\\("<>TOT[M]<>"\\)",fs}];
(* set 2 *)
{Nr,Nc}=NrcL[[2]];
M=Table[Table[Rnz[3],{y,1,Nc}],{x,1,Nr}];
M[[i,j]]=c;
ret=Append[ret,{"\\("<>TOT[M]<>"\\)",fs}];
(* set 3 *)
{Nr,Nc}=NrcL[[3]];
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,ct,s1,s2,x,y,i,j,ij,c,c1,c2,fs,fs1,fs2,fs3,fs4,Nr,Nc,NrcL,M,ret={}},
{Cn,{i,j,c}}=QtList[[mm]];ij=Max[i,j];
NrcL=RandomChoice[DeleteDuplicates[
Flatten[Table[{Min[5,Max[i+k,2]],Min[5,Max[j+l,2]]},
{k,{-2,-1,1,2,3}},{l,{-2,-1,1,2,3}}],1]],5];
s1="\\(("<>TOT[i]<>","<>TOT[j]<>")\\)";
s2="\\(("<>TOT[j]<>","<>TOT[i]<>")\\)";
c1=c+Rnz[3];c2=c+Rnz[3];
fs1="\\("<>s1<>"\\)成分は\\("<>TOT[c]<>"\\)ではなく,\\("<>s2<>"\\)成分が\\("<>TOT[c]<>"\\)です.";
fs2="この行列は\\("<>s1<>"\\)成分を持ちません.";
fs3="この行列の\\("<>s1<>"\\)成分も\\("<>s2<>"\\)成分も\\("<>TOT[c]<>"\\)ではありません.";
fs4="この行列の\\("<>s1<>"\\)成分は\\("<>TOT[c1]<>"\\)です.";
For[ct=1,ct≤Length[NrcL],ct++,
{Nr,Nc}=NrcL[[ct]];M=Table[Table[RandomInteger[10]-5,{y,1,Nc}],{x,1,Nr}];
If[i≠j,
Which[
Nr≥ij&&Nc≥ij,If[RandomInteger[1]==0,
M[[i,j]]=c2;M[[j,i]]=c;fs=fs1,M[[i,j]]=c1;M[[j,i]]=c2;fs=fs3<>fs4],
Nr<i&&Nc≥j,M[[Nr,j]]=c;fs=fs2,
Nr≥i&&Nc<j,M[[i,Nc]]=c;fs=fs2,
Nr<i&&Nc<j,M[[Nr,Nc]]=c;fs=fs2,
True,M[[i,j]]=c1;fs=fs4],
Which[
Nr<i&&Nc≥j,M[[Nr,j]]=c;fs=fs2,
Nr≥i&&Nc<j,M[[i,Nc]]=c;fs=fs2,
Nr<i&&Nc<j,M[[Nr,Nc]]=c;fs=fs2,
M[[i,j]]=c1;fs=fs3
]];
ret=Append[ret,{"\\("<>TOT[M]<>"\\)",fs}]];
Return[RandomSample[ret,SELNUM-Cn]]];

GenerateMCxml[DFname[],"LA/11-02."<>Title<>"(MC1)",Title,NUM,"false",QtextF,CListF,ICListF]

i=2;j=3;RandomSample[Table[{i+k,j+l},{k,1,3},{l,1,3}],3]

Flatten[Table[{i+k,j+l},{k,1,3},{l,1,3}],1]

RandomSample[Flatten[Table[{i+k,j+l},{k,1,3},{l,1,3}],1],3]

Table[k,{k,{-2,-1,1,2,3}}]

QtextF[30]

CListF[45]

ICListF[45]

NrcL=RandomChoice[DeleteDuplicates[
Flatten[Table[{Max[i+k,2],Max[j+l,2]},{k,{-2,-1,1,2,3}},{l,{-2,-1,1,2,3}}],1]],5]