(* 10-05 *)

(* 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],{"{"->"(","}"->")"}];
Rv[m_]:={Rnz[m],Rnz[m]};
Title="2次正方行列同士の積の性質";
SELNUM=6;NUM=200;

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤300,ct++,
MA={{Rnz[4],Rnz[4]},{Rnz[4],Rnz[4]}};
RC=RandomChoice[{"R1","R2","C1","C2"}];
QtList=Append[QtList,{RandomInteger[1]+3,MA,RC}];
QtList=DeleteDuplicates[QtList]];QtList=RandomSample[QtList,NUM];Length[QtList]

QtextF[mm_]:=Module[{Cn,MA,RC},{Cn,MA,RC}=QtList[[mm]];
Which[
RC=="R1",MA[[2]]={"*","*"},
RC=="R2",MA[[1]]={"*","*"},
RC=="C1",MA[[1,2]]="*";MA[[2,2]]="*",
RC=="C2",MA[[1,1]]="*";MA[[2,1]]="*"];
Return["次の行列の積のうち, 計算するとベクトルの積の結果が必ず\\("<>TOT[MA]<>"\\)となるものをすべて選べ"]];

CListF[mm_]:=Module[{Cn,MA,RC,LR,RV,MMs={},MAs={},RR,Tmp,i,a,b,fs,ret={}},
{Cn,MA,RC}=QtList[[mm]];fs="正解です.";RR={1,2};
a=Rpos[2]+1;b=Rpos[2]+1;
Which[
(* case a11 & a12 \\ * & * *)
RC=="R1",LR="L"; 
MMs=Append[MMs,{{1,0},{"*","*"}}];Tmp=MA;
Tmp[[2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{0,1},{"*","*"}}];Tmp={{0,1},{1,0}}.MA;
Tmp[[1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,1},{"*","*"}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=MA[[1]]-Tmp[[1]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,-b},{"*","*"}}];Tmp=MA;
Tmp[[2]]=Rv[3];Tmp[[1]]=MA[[1]]+b*Tmp[[2]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{a,-1},{"*","*"}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=-MA[[1]]+a*Tmp[[1]];MAs=Append[MAs,Tmp],
(* case * & * \\ a21 & a22 *)
RC=="R2",LR="L";
MMs=Append[MMs,{{"*","*"},{0,1}}];Tmp=MA;
Tmp[[1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{1,0}}];Tmp={{0,1},{1,0}}.MA;
Tmp[[2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{1,1}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=MA[[2]]-Tmp[[1]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{1,-b}}];Tmp=MA;
Tmp[[2]]=Rv[3];Tmp[[1]]=MA[[2]]+b*Tmp[[2]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{a,-1}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=-MA[[2]]+a*Tmp[[1]];MAs=Append[MAs,Tmp],
(* case a11 & * \\ a21 & * *)
RC=="C1",LR="R";
MMs=Append[MMs,{{1,"*"},{0,"*"}}];Tmp=MA;
Tmp[[RR,2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{0,"*"},{1,"*"}}];Tmp=MA.{{0,1},{1,0}};
Tmp[[RR,1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,"*"},{1,"*"}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,1]]-Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,"*"},{-b,"*"}}];Tmp=MA;
Tmp[[RR,2]]=Rv[3];Tmp[[RR,1]]=MA[[RR,1]]+b*Tmp[[RR,2]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{a,"*"},{-1,"*"}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=-MA[[RR,1]]+a*Tmp[[RR,1]];
MAs=Append[MAs,Tmp],
(* case * & a12 \\ * & a22 *)
RC=="C2",LR="R";
MMs=Append[MMs,{{"*",0},{"*",1}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",1},{"*",0}}];Tmp=MA.{{0,1},{1,0}};
Tmp[[RR,2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",1},{"*",1}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,2]]-Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",-b},{"*",1}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,2]]+b*Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",-1},{"*",a}}];Tmp=MA;
Tmp[[RR,2]]=Rv[3];Tmp[[RR,1]]=-MA[[RR,2]]+a*Tmp[[RR,2]];
MAs=Append[MAs,Tmp]
];
For[i=1,i≤Length[MMs],i++,
If[LR=="L",
ret=Append[ret,{"\\("<>TOT[MMs[[i]]]<>TOT[MAs[[i]]]<>"\\)",fs}],
ret=Append[ret,{"\\("<>TOT[MAs[[i]]]<>TOT[MMs[[i]]]<>"\\)",fs}]]];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,MA,RC,LR,RV,MMs={},MAs={},RR,Tmp,i,a,b,fs,ret={}},
{Cn,MA,RC}=QtList[[mm]];fs="必ず指定の形になるものを選んで下さい.";RR={1,2};
a=Rpos[2]+1;b=Rpos[2]+1;
Which[
(* case a11 & a12 \\ * & * *)
RC=="R1",LR="L"; 
MMs=Append[MMs,{{1,0},{"*","*"}}];Tmp=MA;
Tmp[[2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{0,1},{"*","*"}}];Tmp={{0,1},{1,0}}.MA;
Tmp[[1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,1},{"*","*"}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=MA[[1]]-Tmp[[1]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,-b},{"*","*"}}];Tmp=MA;
Tmp[[2]]=Rv[3];Tmp[[1]]=MA[[1]]+b*Tmp[[2]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{a,-1},{"*","*"}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=-MA[[1]]+a*Tmp[[1]];MAs=Append[MAs,Tmp],
(* case * & * \\ a21 & a22 *)
RC=="R2",LR="L";
MMs=Append[MMs,{{"*","*"},{0,1}}];Tmp=MA;
Tmp[[1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{1,0}}];Tmp={{0,1},{1,0}}.MA;
Tmp[[2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{1,1}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=MA[[2]]-Tmp[[1]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{1,-b}}];Tmp=MA;
Tmp[[2]]=Rv[3];Tmp[[1]]=MA[[2]]+b*Tmp[[2]];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*","*"},{a,-1}}];Tmp=MA;
Tmp[[1]]=Rv[3];Tmp[[2]]=-MA[[2]]+a*Tmp[[1]];MAs=Append[MAs,Tmp],
(* case a11 & * \\ a21 & * *)
RC=="C1",LR="R";
MMs=Append[MMs,{{1,"*"},{0,"*"}}];Tmp=MA;
Tmp[[RR,2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{0,"*"},{1,"*"}}];Tmp=MA.{{0,1},{1,0}};
Tmp[[RR,1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,"*"},{1,"*"}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,1]]-Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{1,"*"},{-b,"*"}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,1]]+b*Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{a,"*"},{-1,"*"}}];Tmp=MA;
Tmp[[RR,2]]=Rv[3];Tmp[[RR,1]]=-MA[[RR,1]]+a*Tmp[[RR,2]];
MAs=Append[MAs,Tmp],
(* case * & a12 \\ * & a22 *)
RC=="C2",LR="R";
MMs=Append[MMs,{{"*",0},{"*",1}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",1},{"*",0}}];Tmp=MA.{{0,1},{1,0}};
Tmp[[RR,2]]=Rv[3];MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",1},{"*",1}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,2]]-Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",-b},{"*",1}}];Tmp=MA;
Tmp[[RR,1]]=Rv[3];Tmp[[RR,2]]=MA[[RR,2]]+b*Tmp[[RR,1]];
MAs=Append[MAs,Tmp];
MMs=Append[MMs,{{"*",-1},{"*",a}}];Tmp=MA;
Tmp[[RR,2]]=Rv[3];Tmp[[RR,1]]=-MA[[RR,2]]+a*Tmp[[RR,2]];
MAs=Append[MAs,Tmp]
];
For[i=1,i≤Length[MMs],i++,
If[LR=="L",
ret=Append[ret,{"\\("<>TOT[MAs[[i]]]<>TOT[MMs[[i]]]<>"\\)",fs}],
ret=Append[ret,{"\\("<>TOT[MMs[[i]]]<>TOT[MAs[[i]]]<>"\\)",fs}]]];
Return[RandomSample[ret,SELNUM-Cn]]];

GenerateMCxml[DFname[],"LA/10-05."<>Title<>"(MC2)",Title,NUM,"false",QtextF,CListF,ICListF]

Col[{{1,2},{3,4}},1]

Join[{a,b},{c,d}]

Range[2]

MA[[{1,2},2]]={Rnz[3],Rnz[3]}

MA