Skip to content

Commit 001fb83

Browse files
authored
Merge pull request #1156 from gajennings/main
Fix bug 4828, use MathObjects, PGML, add solution
2 parents 02d7600 + 6e10194 commit 001fb83

File tree

1 file changed

+35
-26
lines changed
  • OpenProblemLibrary/Rochester/setLinearAlgebra19QRfactorization

1 file changed

+35
-26
lines changed

OpenProblemLibrary/Rochester/setLinearAlgebra19QRfactorization/ur_la_19_1.pg

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,50 +23,59 @@ DOCUMENT(); # This should be the first executable line in the problem.
2323
loadMacros(
2424
"PGstandard.pl",
2525
"MathObjects.pl",
26+
"PGML.pl",
27+
"parserMultiAnswer.pl",
2628
"PGcourse.pl"
2729
);
2830

29-
TEXT(beginproblem());
3031
$showPartialCorrectAnswers = 1;
3132

3233
Context('Matrix');
3334

34-
$norm = 0;
35+
$normsq = 0;
3536

36-
foreach $i (1..3) {
37+
foreach $i (0..2) {
3738
$myq[$i] = non_zero_random(-3,3,1);
38-
$norm = $norm + $myq[$i] * $myq[$i];
39+
$normsq = $normsq + $myq[$i] * $myq[$i];
3940
}
4041

41-
$myQ = Matrix([ [$myq[1]], [$myq[2]], [$myq[3]] ]);
42+
$norm = sqrt($normsq);
4243

43-
$norm = sqrt($norm);
44+
$mult = non_zero_random(1,5,1);
4445

45-
$myR = Matrix([non_zero_random(1,5,1)]);
46+
$Q = Matrix([[$myq[0]],[$myq[1]],[$myq[2]]])/$norm;
4647

47-
$M = $myQ * $myR;
48+
$R = Matrix([[$norm*$mult]]);
4849

49-
foreach $i (1..3) {
50-
$q[$i] = $myq[$i]/$norm;
51-
}
50+
$M = Matrix([[$myq[0]],[$myq[1]],[$myq[2]]])*$mult;
51+
52+
$multAns = MultiAnswer($Q,$R)->with(
53+
singleResult => 1,
54+
checker => sub {
55+
my ($correct, $student, $self) = @_;
56+
my ($Qstu, $Rstu) = @{$student};
57+
my ($Qcor, $Rcor) = @{$correct};
58+
if ( ($Qstu == $Qcor && $Rstu == $Rcor) or
59+
($Qstu == -$Qcor && $Rstu == -$Rcor) )
60+
{ return 1; }
61+
else { return 0;}
62+
}
63+
);
5264

53-
$Q = Matrix([ [$q[1]], [$q[2]], [$q[3]] ]);
65+
BEGIN_PGML
66+
Find the QR factorization of the given matrix.
5467

55-
$R = $myR * $norm;
68+
[``[$M] = ``] [_____]*{$multAns} [_____]*{$multAns}
69+
END_PGML
5670

57-
Context()->texStrings;
58-
BEGIN_TEXT
59-
Find the \(QR\) factorization of the given matrix.
60-
$BR
61-
$BR
62-
$BCENTER
63-
\($M = \) \{ $Q->ans_array \} \{ $R->ans_array \}
64-
$ECENTER
65-
END_TEXT
66-
Context()->normalStrings;
71+
BEGIN_PGML_SOLUTION
72+
The [`3\times 1`] matrix M is equivalent to a vector and Q is supposed to be an orthogonal matrix, that is, a sequence of orthonormal column vectors, so Q is equivalent to a single unit vector that is parallel to M. The length of M is [`[$mult]\sqrt{[$normsq]}`]
73+
so
74+
[``Q = (\pm M)\big(\frac{1}{[$mult]\sqrt{[$normsq]}}\big)``]
75+
and
76+
[``R = \pm [$mult]\sqrt{[$normsq]} ``]
77+
with the same plus or minus sign for both.
78+
END_PGML_SOLUTION
6779

68-
ANS($Q->cmp);
69-
ANS($R->cmp);
70-
;
7180
ENDDOCUMENT(); # This should be the last executable line in the problem.
7281

0 commit comments

Comments
 (0)