(* 06_06 *)

(* 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],{"{"->"(","}"->")"}];
TOPt[A_]:="\\(("<>TOT[A[[1]]]<>","<>TOT[A[[2]]]<>","<>TOT[A[[3]]]<>")\\)";
Title="3点を通る平面の方程式";
SELNUM=6;NUM=200;

(* Select matrices satisfying some mapping conditions *)
QtList={}; For[ct=1,ct≤300,ct++,
Wp={Rnz[1],Rnz[2],Rnz[1]};
Wn={Rnz[1],Rnz[2],Rnz[1]};
{Wu1,Wv1}=NullSpace[{Wn}];
GM2={{Rnz[1],Rnz[1]},{Rnz[1],Rnz[1]}};
While[Det[GM2]≠1,GM2={{Rnz[1],Rnz[2]},{Rnz[2],Rnz[1]}}];
{Wu,Wv}=GM2.{Wu1,Wv1};
QtList=Append[QtList,{RandomChoice[{2,2,3,3,3,3,4}],{Wp,Wn,Wu,Wv}}];
QtList=DeleteDuplicates[QtList]];QtList=RandomSample[QtList,NUM];Length[QtList]

QtList[[3]]

QtextF[mm_]:=Module[{Cn,Wn,Wu,Wv,Wp,A,B,C},
{Cn,{Wp,Wn,Wu,Wv}}=QtList[[mm]];
A=Wp;B=Wp+Wu;C=Wp+Wv;
Return["3点A"<>TOPt[A]<>",B"<>TOPt[B]<>",C"<>TOPt[C]<>"を通る平面の方程式として"
<>"正しいものをすべて選びなさい."]];

CListF[mm_]:=Module[{Cn,Wx,Wp,Wn,Wa,Wu,Wv,d,k1,k2,k3,fs,ret={}},
{Cn,{Wp,Wn,Wu,Wv}}=QtList[[mm]];fs="正解です.";
{k1,k2,k3}=RandomSample[{-2,-1,2,3,4},3];
While[(k1==k2)||(k1==k3)||(k2==k3),{k1,k2,k3}=RandomChoice[{-2,-1,2,3,4},3]];
Wx={x,y,z};d=Wp.Wn;
ret=Append[ret,{"\\("<>TOT[Expand[Wn.Wx]]<>"="<>TOT[d]<>"\\)",fs}];
ret=Append[ret,{"\\("<>TOT[Expand[Wn.Wx-d]]<>"=0\\)",fs}];
ret=Append[ret,{"\\("<>TOT[Expand[k1*Wn.Wx]]<>"="<>TOT[k1*d]<>"\\)",fs}];
ret=Append[ret,{"\\("<>TOT[Expand[k2*Wn.Wx-k2*d]]<>"=0\\)",fs}];
ret=Append[ret,{"\\("<>TOT[Expand[k3*Wn.Wx]]<>"="<>TOT[k3*d]<>"\\)",fs}];
Return[RandomSample[ret,Cn]]];

ICListF[mm_]:=Module[{Cn,Wx,Wp,Wn,Wu,Wv,Wn1,k1,k2,A,B,C,d,fs1,fs2,fs3,ret={}},
{Cn,{Wp,Wn,Wu,Wv}}=QtList[[mm]];
fs1="1点は通りますが, 他の2点は通らないようです.";
fs2="2点は通りますが, 残る1点は通らないようです.";
fs3="法線ベクトルは合っていますが, 平行にずれているようです.";
{A,B,C}=RandomSample[{Wp,Wp+Wu,Wp+Wv},3];
{k1,k2}=RandomSample[{-2,-1,-1,1,1,1,2,3},2];
Wx={x,y,z};
(* case fs2*)
(* Wn1={Rnz[4],Rnz[4],Rnz[4]};
While[A.Wn1==B.Wn1 || C.Wn1==B.Wn1,Wn1={Rnz[4],Rnz[4],Rnz[4]}];d=B.Wn1; *)
Wn1=({{Rnz[2],Rnz[2]}}.NullSpace[{B-A}])[[1]];
While[MatrixRank[{Wn1,Wn}]==1,Wn1=({{Rnz[2],Rnz[2]}}.NullSpace[{B-A}])[[1]]];
ret=Append[ret,{"\\("<>TOT[Expand[k1*Wn1.Wx]-k1*A.Wn1]<>"=0\\)",fs2}];
Wn1=({{Rnz[2],Rnz[2]}}.NullSpace[{C-A}])[[1]];
While[MatrixRank[{Wn1,Wn}]==1,Wn1=({{Rnz[2],Rnz[2]}}.NullSpace[{C-A}])[[1]]];
ret=Append[ret,{"\\("<>TOT[Expand[k2*Wn1.Wx]]<>"="<>TOT[k2*A.Wn1]<>"\\)",fs2}];
(* case fs1 *)
Wn1={Rnz[2],Rnz[2],Rnz[2]};
While[B.Wn1==A.Wn1 || C.Wn1==A.Wn1,Wn1={Rnz[2],Rnz[2],Rnz[2]}];d=A.Wn1;
ret=Append[ret,{"\\("<>TOT[Expand[k1*Wn1.Wx]-k1*d]<>"=0\\)",fs1}];
Wn1={Rnz[2],Rnz[2],Rnz[2]};
While[C.Wn1==B.Wn1 || A.Wn1==B.Wn1,Wn1={Rnz[2],Rnz[2],Rnz[2]}];d=B.Wn1;
ret=Append[ret,{"\\("<>TOT[Expand[k2*Wn1.Wx]]<>"="<>TOT[k2*d]<>"\\)",fs1}];
(* case fs3 *)
d=A.Wn+Rnz[3];
ret=Append[ret,{"\\("<>TOT[Expand[k1*Wn.Wx]-k1*d]<>"=0\\)",fs3}];
ret=Append[ret,{"\\("<>TOT[Expand[k2*Wn.Wx]]<>"="<>TOT[k2*d]<>"\\)",fs3}];
Return[RandomSample[ret,SELNUM-Cn]]];

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

QtextF[130]

Timing[CListF[130]]

Timing[ICListF[130]]

{{2,-1}}.NullSpace[{{1,2,3}}]

{1,1,3}.{0,-1,1}

{-6,-2,2}.{0,-1,1}

{-1,1,2}.{1,-3,2}

{-2,-2,-2}.{0,-1,1}