(* 11_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],{"{"->"(","}"->")"}];
Title="行列とベクトルの積";
SELNUM=6;NUM=200;

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤1000,ct++,
Vr=Table[Rnz[5],3];
QtList=Append[QtList,{RandomChoice[{2,2,3,3,3,3,3,4}],Vr}]
(* QtList=DeleteDuplicates[QtList]; *)
];QtList=RandomSample[QtList,NUM];Length[QtList]

(* This question is canceled *)

QtextF[mm_]:=Module[{Cn,Vr},{Cn,Vr}=QtList[[mm]];
Return["次の行列とベクトルの積のうち積が定義され，かつ\\("<>TOTT[{Vr}]<>"\\)となるものをすべて選べ"]];

CListF[mm_]:=Module[{Cn,Vr,a,R,C1,cl,Nr,Nc,M,V,V1,ME,fs,ret={}},
{Cn,Vr}=QtList[[mm]];fs="正解です.";
Nr=Length[Vr];R=Range[Nr];
(* col=2 *)
Nc=2;ME=IdentityMatrix[Nc];
M=Table[Table[Rnz[3],Nc],Nr];
cl=RandomInteger[Nc-1]+1;M[[R,cl]]=Vr;V=Col[ME,cl];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs}];
(* col=3 *)
Nc=3;ME=IdentityMatrix[Nc];
M=Table[Table[Rnz[3],Nc],Nr];
cl=RandomInteger[Nc-1]+1;M[[R,cl]]=Vr;V=Col[ME,cl];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs}];
(* col=4 *)
Nc=4;ME=IdentityMatrix[Nc];
M=Table[Table[Rnz[3],Nc],Nr];
cl=RandomInteger[Nc-1]+1;M[[R,cl]]=Vr;V=Col[ME,cl];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs}];
(* col=3 and generic vec *)
Nc=3;ME=IdentityMatrix[Nc];
M=Table[Table[Rnz[3],Nc],Nr];
cl=RandomInteger[Nc-1]+1;M[[R,cl]]=Vr;V=Col[ME,cl];
V1=Table[Rnz[3],Nc];
C1=Range[Nc]/.cl->Nothing;M[[R,C1[[1]]]]=V1;M[[R,C1[[2]]]]=-V1;
a=Rnz[3];V[[C1[[1]],1]]=a;V[[C1[[2]],1]]=a;
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,Vr,a,R,C1,cl,Nr,Nc,Nc1,M,V,V1,ME,fs1,fs2,fs3,ret={}},
{Cn,Vr}=QtList[[mm]];
Nr=Length[Vr];R=Range[Nr];
fs1="この積は定義されません.";
fs2="積は定義されますが, 指定されたベクトル"<>TOTT[{Vr}]<>"になりません.";
(* col=2 *)
Nc=2;ME=IdentityMatrix[Nc];Nc1=Nc+Rpos[1];
M=Table[Table[Rnz[3],Nc1],Nr];
cl=RandomInteger[Nc1-1]+1;M[[R,cl]]=Vr;
If[cl≤Nc,V=Col[ME,cl],V=Col[ME,1]];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs1}];
(* col=3 *)
Nc=3;ME=IdentityMatrix[Nc];Nc1=Nc+Rnz[0];
M=Table[Table[Rnz[3],Nc1],Nr];
cl=RandomInteger[Nc1-1]+1;M[[R,cl]]=Vr;
If[cl≤Nc,V=Col[ME,cl],V=Col[ME,1]];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs1}];
(* col=4 *)
Nc=4;ME=IdentityMatrix[Nc];Nc1=Nc+Rnz[1];
M=Table[Table[Rnz[3],Nc1],Nr];
cl=RandomInteger[Nc1-1]+1;M[[R,cl]]=Vr;
If[cl≤Nc,V=Col[ME,cl],V=Col[ME,1]];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs1}];
(* col=3 and random vec *)
Nc=3;M=Table[Table[Rnz[3],Nc],Nr];
V=Transpose[{Table[Rnz[3],Nc]}];
While[M.V==Vr,V=Transpose[{Table[Rnz[3],Nc]}]];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs2}];
(* col=4 and random vec *)
Nc=4;M=Table[Table[Rnz[3],Nc],Nr];
V=Transpose[{Table[Rnz[3],Nc]}];
While[M.V==Vr,V=Transpose[{Table[Rnz[3],Nc]}]];
ret=Append[ret,{"\\("<>TOT[M]<>TOT[V]<>"\\)",fs2}];
 Return[RandomSample[ret,SELNUM-Cn]]];

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

ICListF[45]

{1,2,3} /. 2->Nothing

Nr=3;Nc=4;

Table[Table[Rnz[3],Nc+Rpos[1]],Nr]