(* 11_03 *)

(* 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++,
M3a={Table[Rnz[3],3]};
M3b=Transpose[{Table[Rnz[3],3]}];
a=(M3a.M3b)[[1,1]];
QtList=Append[QtList,{RandomChoice[{2,2,3,3,3,3,3,4,4}],{a,M3a,M3b}}]
(* QtList=DeleteDuplicates[QtList]; *)
];QtList=RandomSample[QtList,NUM];Length[QtList]

(* This question is canceled *)

QtextF[mm_]:=Module[{Cn,a,M3a,M3b},{Cn,{a,M3a,M3b}}=QtList[[mm]];
Return["次の行列の積のうち，積が定義されかつ\\("<>TOT[a]<>"\\)と一致するものを"<>"すべて選べ"]];

CListF[mm_]:=Module[{Cn,pr,M3a,M3b,Nr,Nc,M1,M2,fs,ret={}},
{Cn,{pr,M3a,M3b}}=QtList[[mm]];fs="正解です.";
ret=Append[ret,{"\\("<>TOT[M3a]<>TOT[M3b]<>"\\)",fs}];
(* len=2 *)
Nc=2;Nr=Nc;M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}];
While[M1.M2!={{pr}},
M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs}];
(* another len=3 *)
Nc=3;Nr=Nc;M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}];
While[M1.M2!={{pr}},
M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs}];
(* len=4 *)
Nc=4;Nr=Nc;M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}];
While[M1.M2!={{pr}},
M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs}];
(* len=5 *)
Nc=5;Nr=Nc;M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}];
While[M1.M2!={{pr}},
M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,pr,M3a,M3b,Nr,Nc,M1,M2,fs1,fs2,fs3,ret={}},
{Cn,{pr,M3a,M3b}}=QtList[[mm]];
fs1="行の次数と列の次数が違うので積が定義されません.";
fs2="\\(n\\)次列ベクトル\\(\\times n\\)次行ベクトルは行列になりスカラーにはなりません.";
fs3="行列の積は定義されますが, 計算しても\\("<>TOT[pr]<>"\\)になりません.";
M1=M3b;M2=M3a;
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs2}];
(* len=4 *)
Nc=4;Nr=Nc;M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}];
While[M1.M2!={{pr}},
M1={Table[Rnz[4],Nc]};M2=Transpose[{Table[Rnz[4],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M2]<>TOT[M1]<>"\\)",fs2}];
(* case fs1 *)
Nr=Rpos[3]+1;Nc=Rpos[3]+1;While[Nc==Nr,Nc=Rpos[3]+1];
M1={Table[Rnz[3],Nc]};M2=Transpose[{Table[Rnz[3],Nr]}];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs1}];
(* case fs3 *)
Nr=2;Nc=2;
M1={Table[Rnz[3],Nc]};M2=Transpose[{Table[Rnz[3],Nr]}];
While[M1.M2=={{pr}},M2=Transpose[{Table[Rnz[3],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs3}];
Nr=2+Rpos[1];Nc=Nr;
M1={Table[Rnz[3],Nc]};M2=Transpose[{Table[Rnz[3],Nr]}];
While[M1.M2=={{pr}},M2=Transpose[{Table[Rnz[3],Nr]}]];
ret=Append[ret,{"\\("<>TOT[M1]<>TOT[M2]<>"\\)",fs3}];
Return[RandomSample[ret,SELNUM-Cn]]];

GenerateMCxml[DFname[],"LA/11-04."<>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[45]

CListF[45]

ICListF[45]

Table[Rnz[3],4]

Nr=Nc=5;

M1={Table[Rnz[3],Nc]};
M2=Transpose[{Table[Rnz[3],Nr]}];

M1

M2

(M1.M2)[[1,1]]

QtextF[3]

CListF[3]