(* 15_01 *)

(* 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);
RowCat[CL_]:=Join@@(CL);
PtString[M_]:=StringReplace[ToString[M],{"{"->"(","}"->")"}];
ai2tex[s_]:=StringReplace[TOT[s],Table["\\text{a"<>TOT[i]<>"}"->"{\\mathbf a}_"<>TOT[i],{i,1,5}]];
TpStr[L_]:="\\("<>TOT[L[[1]]]<>"\\times"<>TOT[L[[2]]]<>"\\)";
TpTpse[L_]:=If[L=={},{},{L[[2]],L[[1]]}];
PType[A_,B_]:=If[A=={}||B=={},Return[{}],
If[A[[2]]===B[[1]],Return[{A[[1]],B[[2]]}],Return[{}]]];
PTypeL[L_]:=Module[{ct,len,tmp},len=Length[L];
Which[len==0,Return[{}],len==1,Return[L[[1]]],
len==2,Return[PType[L[[1]],L[[2]]]],
True,tmp=L[[1]];For[ct=2,ct≤len,ct++,tmp=PType[tmp,L[[ct]]]];Return[tmp]]];
Category="LA/15-01.";
Title="行列とベクトルの積と1次写像";
SELNUM=6;
Single="true";
NUM=200;

(* select pairs of matrices *)
QtList={}; For[ct=1,ct≤300,ct++,
Cn=1;
row=RandomChoice[{2,2,3,3,4}];
col=RandomChoice[{2,3,3,4,4}];
While[col==row,col=RandomChoice[{2,3,3,4,4}]];
MA=Table[Table[RandomInteger[8]-4,col],row];
QtList=Append[QtList,{Cn,{row,col,MA}}]];
(* QtList=DeleteDuplicates[QtList]; *)
QtList=RandomSample[QtList,NUM];Length[QtList]

Clear[QtextF];QtextF[mm_]:=Module[{Cn,row,col,Sv,Tv,MA},
{Cn,{row,col,MA}}=QtList[[mm]];
Sv=RandomSample[{x,y,z,w},col];
(* Which[col==2,Sv=RandomSample[{x,y},2],col==3,Sv={x,y,z},col==4,Sv={w,x,y,z}]; *)
Return["ベクトル\\({\\mathbf u}="<>TOTT[{Sv}]<>"\\)に対し, \\({\\mathbf v}="
<>TOTT[{MA.Sv}]<>"はある行列\\(A\\)で\\(A"<>TOTT[{Sv}]<>"と表せる."
<>"\\(A\\)を選べ"]];

CListF[mm_]:=Module[{Cn,row,col,Sv,Tv,MA,fs,ret={}},
{Cn,{row,col,MA}}=QtList[[mm]];fs="正解です.";
ret=Append[ret,{"\\("<>TOT[MA]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,row,col,pm,Sv,Tv,MA,GM,MA1,ct,fs,ret={}},
{Cn,{row,col,MA}}=QtList[[mm]];
fs="行列の型をよく確認しよう.";
MA1=Transpose[MA];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs}];
MA1=GetRnij[col,1,2].Transpose[MA]; 
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs}];
MA1=Transpose[MA].GetRnij[row,1,2];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs}];
fs="変数の順序をよく確認しよう.";
pm=Permutations[Range[col],{2}];
For[ct=1,ct≤Length[pm],ct++,
MA1=MA.GetRnij[col,pm[[ct,1]],pm[[ct,2]]];
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs}]];
pm=Permutations[Range[row],{2}];
For[ct=1,ct≤Length[pm],ct++,
MA1=GetRnij[row,pm[[ct,1]],pm[[ct,2]]].MA;
ret=Append[ret,{"\\("<>TOT[MA1]<>"\\)",fs}]];
ret=DeleteDuplicates[ret];
Return[RandomSample[ret,SELNUM-Cn]]];

ICListF[5]

Sort@Permutations[Range[3],{2}]

MA={{a,b},{c,d}}

MA[[{1,2},1]]

ICListF[10]

For[ct=1,ct≤NUM,ct++,QtextF[ct];ICListF[ct]] (* Error check *)

GenerateMCxml[DFname[],Category<>Title<>"(MC1)",Title,NUM,Single,QtextF,CListF,ICListF]

ICListF[3]

Permutations[{1,2,3,4},{2}] /. Permutations[{1,2,3},{2}]Nothing

#->Nothing &/@ Permutations[{1,2,3},{2}]

MA={{3,5},{7,9}}

MA1=MA;MA1[[{1,2},1]]={{1},{0}};

MA1

RowCat[{IdentityMatrix[2],MA}]

A={{1,2,3},{3,4,5}}

Su={x,y,z}
A.Su

3y+4w

? Less

? MonomialOrder

? MonomialOrder