<?xml version="1.0" encoding="UTF-8"?>
<quiz>
  <question type="stack">
    <name>
      <text>Orthogonally diagonalise a matrix with a repeated eigenvalue</text>
    </name>
    <questiontext format="html">
      <text><![CDATA[<p>Orthogonally diagonalise the matrix \[ A = {@AA@}. \] That is, find and orthogonal matrix \(Q\) and diagonal matrix \(D\) such that \(QDQ^T = A\).</p>
<p>The eigenspaces of \(A\) are: \[ E_{{@L2/ss@}} = \operatorname{span}\left({@v3@}\right),\qquad E_{{@L1/ss@}} = \operatorname{span}\left({@v1@}, {@v2@}\right) \]</p>
<div>\(Q = \) [[input:stu_Q]] [[validation:stu_Q]]</div>
<div>\(D = \) [[input:stu_D]] [[validation:stu_D]]</div>
<p>[[feedback:prt_QDQT]] <br>[[hint title="How might I check my answer?"]]</p>
<p>If your \(Q\) is orthogonal (check that \(Q^TQ = I\) and \(Q\) is square), your \(D\) is diagonal, and \(QDQ^T = A\) then your answer must be correct!</p>
<p>[[/hint]]</p>]]></text>
    </questiontext>
    <generalfeedback format="html">
      <text/>
    </generalfeedback>
    <defaultgrade>1</defaultgrade>
    <penalty>0.5</penalty>
    <hidden>0</hidden>
    <idnumber/>
    <stackversion>
      <text>2025042500</text>
    </stackversion>
    <questionvariables>
      <text><![CDATA[stack_linear_algebra_declare(true);
stack_include_contrib("matrix.mac");
stack_include_contrib("rand_matrix.mac");

[L1, L2]: random_permutation([1,rand([2,3])]);
sol_D: matrix([L1,0,0],[0,L1,0],[0,0,L2]);
sol_Q: radcan(rand_orth(3,2));

AA: radcan(sol_Q. sol_D. transpose(sol_Q));
ss: lreduce(lambda([ex1,ex2],gcd(ex1,ex2)),list_matrix_entries(AA));
sol_D: sol_D/ss;
AA: radcan(sol_Q. sol_D. transpose(sol_Q));

v3: expand(scale_nicely(col(sol_Q,3)));
v1: expand(scale_nicely(col(sol_Q,1)));
v2: expand(scale_nicely(col(sol_Q,1)) + rand([1,-1])*scale_nicely(col(sol_Q,2)));]]></text>
    </questionvariables>
    <specificfeedback format="html">
      <text/>
    </specificfeedback>
    <questionnote format="moodle_auto_format">
      <text>Orthogonally diagonalise the matrix 
  \[
  	A = {@AA@} \rightarrow {@sol_Q@}, {@sol_D@} 
  \]</text>
    </questionnote>
    <questiondescription format="html">
      <text/>
    </questiondescription>
    <questionsimplify>1</questionsimplify>
    <assumepositive>0</assumepositive>
    <assumereal>0</assumereal>
    <prtcorrect format="html">
      <text><![CDATA[<p><i class="fa fa-check"></i> Correct answer, well done.</p>]]></text>
    </prtcorrect>
    <prtpartiallycorrect format="html">
      <text><![CDATA[<p><i class="fa fa-adjust"></i> Your answer is partially correct.</p>]]></text>
    </prtpartiallycorrect>
    <prtincorrect format="html">
      <text><![CDATA[<p><i class="fa fa-times"></i> Incorrect answer.</p>]]></text>
    </prtincorrect>
    <decimals>.</decimals>
    <scientificnotation>*10</scientificnotation>
    <multiplicationsign>onum</multiplicationsign>
    <sqrtsign>1</sqrtsign>
    <complexno>i</complexno>
    <inversetrig>cos-1</inversetrig>
    <logicsymbol>lang</logicsymbol>
    <matrixparens>[</matrixparens>
    <isbroken>0</isbroken>
    <variantsselectionseed/>
    <input>
      <name>stu_D</name>
      <type>matrix</type>
      <tans>sol_D</tans>
      <boxsize>15</boxsize>
      <strictsyntax>1</strictsyntax>
      <insertstars>0</insertstars>
      <syntaxhint/>
      <syntaxattribute>0</syntaxattribute>
      <forbidwords/>
      <allowwords/>
      <forbidfloat>1</forbidfloat>
      <requirelowestterms>0</requirelowestterms>
      <checkanswertype>0</checkanswertype>
      <mustverify>1</mustverify>
      <showvalidation>3</showvalidation>
      <options/>
    </input>
    <input>
      <name>stu_Q</name>
      <type>matrix</type>
      <tans>sol_Q</tans>
      <boxsize>15</boxsize>
      <strictsyntax>1</strictsyntax>
      <insertstars>0</insertstars>
      <syntaxhint/>
      <syntaxattribute>0</syntaxattribute>
      <forbidwords/>
      <allowwords/>
      <forbidfloat>1</forbidfloat>
      <requirelowestterms>0</requirelowestterms>
      <checkanswertype>0</checkanswertype>
      <mustverify>1</mustverify>
      <showvalidation>3</showvalidation>
      <options/>
    </input>
    <prt>
      <name>prt_QDQT</name>
      <value>1.0000000</value>
      <autosimplify>1</autosimplify>
      <feedbackstyle>1</feedbackstyle>
      <feedbackvariables>
        <text/>
      </feedbackvariables>
      <node>
        <name>0</name>
        <description/>
        <answertest>AlgEquiv</answertest>
        <sans>orth_matrixp(stu_Q)</sans>
        <tans>true</tans>
        <testoptions/>
        <quiet>0</quiet>
        <truescoremode>=</truescoremode>
        <truescore>0</truescore>
        <truepenalty/>
        <truenextnode>1</truenextnode>
        <trueanswernote>prt_QDQT-1-T</trueanswernote>
        <truefeedback format="html">
          <text><![CDATA[<p dir="ltr" style="text-align: left;">Your \(Q\) is orthogonal, good job! </p>]]></text>
        </truefeedback>
        <falsescoremode>-</falsescoremode>
        <falsescore>1</falsescore>
        <falsepenalty/>
        <falsenextnode>1</falsenextnode>
        <falseanswernote>prt_QDQT-1-F</falseanswernote>
        <falsefeedback format="html">
          <text><![CDATA[<p dir="ltr" style="text-align: left;">Your \(Q\) is not orthogonal.</p>]]></text>
        </falsefeedback>
      </node>
      <node>
        <name>1</name>
        <description/>
        <answertest>AlgEquiv</answertest>
        <sans>diagp(stu_D)</sans>
        <tans>true</tans>
        <testoptions/>
        <quiet>0</quiet>
        <truescoremode>+</truescoremode>
        <truescore>0</truescore>
        <truepenalty/>
        <truenextnode>2</truenextnode>
        <trueanswernote>prt_QDQT-2-T</trueanswernote>
        <truefeedback format="html">
          <text><![CDATA[<p dir="ltr" style="text-align: left;">Your \(D\) is diagonal, good job!</p>]]></text>
        </truefeedback>
        <falsescoremode>-</falsescoremode>
        <falsescore>1</falsescore>
        <falsepenalty/>
        <falsenextnode>2</falsenextnode>
        <falseanswernote>prt_QDQT-2-F</falseanswernote>
        <falsefeedback format="html">
          <text><![CDATA[<p dir="ltr" style="text-align: left;">Your \(D\) is not diagonal. </p>]]></text>
        </falsefeedback>
      </node>
      <node>
        <name>2</name>
        <description/>
        <answertest>AlgEquiv</answertest>
        <sans>radcan(stu_Q . stu_D . transpose(stu_Q))</sans>
        <tans>AA</tans>
        <testoptions/>
        <quiet>1</quiet>
        <truescoremode>+</truescoremode>
        <truescore>1</truescore>
        <truepenalty/>
        <truenextnode>-1</truenextnode>
        <trueanswernote>prt_QDQT-3-T</trueanswernote>
        <truefeedback format="html">
          <text><![CDATA[<p dir="ltr" style="text-align: left;">Your \(Q\) and \(D\) satisfy \(QDQ^T = A\), good job! </p>]]></text>
        </truefeedback>
        <falsescoremode>-</falsescoremode>
        <falsescore>0</falsescore>
        <falsepenalty/>
        <falsenextnode>-1</falsenextnode>
        <falseanswernote>prt_QDQT-3-F</falseanswernote>
        <falsefeedback format="html">
          <text><![CDATA[<p dir="ltr" style="text-align: left;">Your \(Q\) and \(D\) do not satisfy \(QDQ^T = A\).</p>]]></text>
        </falsefeedback>
      </node>
    </prt>
    <deployedseed>369475573</deployedseed>
    <deployedseed>18932765</deployedseed>
    <deployedseed>1535827746</deployedseed>
    <deployedseed>361203422</deployedseed>
    <deployedseed>507548407</deployedseed>
    <deployedseed>26696499</deployedseed>
    <deployedseed>522886401</deployedseed>
    <deployedseed>2054052720</deployedseed>
    <deployedseed>249866155</deployedseed>
    <qtest>
      <testcase>1</testcase>
      <description>Test case assuming the teacher's input gets full marks.</description>
      <testinput>
        <name>stu_D</name>
        <value>sol_D</value>
      </testinput>
      <testinput>
        <name>stu_Q</name>
        <value>sol_Q</value>
      </testinput>
      <expected>
        <name>prt_QDQT</name>
        <expectedscore>1.0000000</expectedscore>
        <expectedpenalty>0.0000000</expectedpenalty>
        <expectedanswernote>prt_QDQT-3-T</expectedanswernote>
      </expected>
    </qtest>
    <qtest>
      <testcase>2</testcase>
      <description/>
      <testinput>
        <name>stu_D</name>
        <value>matrix([L1/ss,0,0],[0,L2/ss,0],[0,0,L1/ss])</value>
      </testinput>
      <testinput>
        <name>stu_Q</name>
        <value>ev(addcol(col(sol_Q,2),col(sol_Q,3),-col(sol_Q,1)),simp)</value>
      </testinput>
      <expected>
        <name>prt_QDQT</name>
        <expectedscore>1.0000000</expectedscore>
        <expectedpenalty>0.0000000</expectedpenalty>
        <expectedanswernote>prt_QDQT-3-T</expectedanswernote>
      </expected>
    </qtest>
  </question>
</quiz>
