Skip to content

Commit badd0e4

Browse files
authored
Merge pull request openwebwork#1161 from gajennings/main
Fix bug 4833, use math objects and PGML
2 parents 0c3899b + 0d12902 commit badd0e4

File tree

1 file changed

+76
-279
lines changed

1 file changed

+76
-279
lines changed

OpenProblemLibrary/TCNJ/TCNJ_Eigenvalues/problem6.pg

Lines changed: 76 additions & 279 deletions
Original file line numberDiff line numberDiff line change
@@ -20,290 +20,87 @@
2020
DOCUMENT(); # This should be the first executable line in the problem.
2121
loadMacros(
2222
"PGstandard.pl",
23-
"PGchoicemacros.pl",
24-
"PGgraphmacros.pl",
25-
"PGmatrixmacros.pl",
26-
"PGnumericalmacros.pl",
27-
"PGmorematrixmacros.pl",
28-
"PGcomplexmacros.pl",
23+
"MathObjects.pl",
24+
"parserPopUp.pl",
25+
"PGML.pl",
2926
"PGcourse.pl"
3027
);
3128

32-
TEXT(beginproblem());
3329
# Do not show which answers are incorrect.
3430
$showPartialCorrectAnswers = 1;
3531

36-
# FIRST MATRIX
37-
$A= new Matrix(4,4);
38-
$a[1][1] = random(-2,2,1);
39-
$a[2][1] = random(-1,1,2);
40-
$a[3][1] = random(-1,1,2);
41-
$a[4][1] = random(-1,1,1);
42-
$b1 = random(-1,1,2);
43-
foreach $i (1..4) {
44-
$a[$i][2] = $b1 * $a[$i][1]; }
45-
$p = random(-1,1,2);
46-
$a[2][2] = $a[2][2] + $p;
47-
$c = random(-1,1,1);
48-
$d = random(-1,1,2);
49-
$n = random(-1,1,2);
50-
foreach $i (1..4) {
51-
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
52-
$n = random(-1,1,2);
53-
$a[1][3] = $a[1][3] + $n;
54-
$f = random(-1,1,2);
55-
$g = random(-1,1,1);
56-
$h = random(-1,1,1);
57-
foreach $i (1..4) {
58-
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
59-
$q = random(-1,1,2);
60-
$a[4][4] = $a[4][4] + $q;
61-
$det = - $a[3][1] * $p * $n * $q;
62-
foreach $i (1..4) {
63-
foreach $j (1..4) {
64-
$A->assign($i,$j, $a[$i][$j]);
65-
}
66-
}
67-
$A_lr = $A->decompose_LR();
68-
$A_det = $A_lr->det_LR();
69-
$b = $A_lr->invert_LR();
70-
$e = new Matrix(4,4);
71-
$e->one();
72-
$eig2 = non_zero_random(-4,4,1);
73-
$e->assign(2,2, $eig2);
74-
$eig1 = random(-1,1,2);
75-
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
76-
$e->assign(1,1, $eig1);
77-
$eig3 = - $eig1;
78-
$e->assign(3,3, $eig3);
79-
$eig4 = - $eig2;
80-
$e->assign(4,4, $eig4);
81-
$ans1 = $eig2;
82-
$matrix = $A * $e *$b;
83-
$matrix_lr = $matrix->decompose_LR();
84-
$matrix_det = $matrix_lr->det_LR();
85-
foreach $i (1..4) {
86-
foreach $j (1..4) {
87-
$m[$i][$j] = $matrix->element($i,$j);
88-
$m[$i][$j] = round($m[$i][$j]);
89-
$matrix -> assign($i,$j,$m[$i][$j]);
90-
}
91-
}
92-
93-
# SECOND MATRIX
94-
$A= new Matrix(4,4);
95-
$a[1][1] = random(-2,2,1);
96-
$a[2][1] = random(-1,1,2);
97-
$a[3][1] = random(-1,1,2);
98-
$a[4][1] = random(-1,1,1);
99-
$b1 = random(-1,1,2);
100-
foreach $i (1..4) {
101-
$a[$i][2] = $b1 * $a[$i][1]; }
102-
$p = random(-1,1,2);
103-
$a[2][2] = $a[2][2] + $p;
104-
$c = random(-1,1,1);
105-
$d = random(-1,1,2);
106-
$n = random(-1,1,2);
107-
foreach $i (1..4) {
108-
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
109-
$n = random(-1,1,2);
110-
$a[1][3] = $a[1][3] + $n;
111-
$f = random(-1,1,2);
112-
$g = random(-1,1,1);
113-
$h = random(-1,1,1);
114-
foreach $i (1..4) {
115-
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
116-
$q = random(-1,1,2);
117-
$a[4][4] = $a[4][4] + $q;
118-
$det = - $a[3][1] * $p * $n * $q;
119-
foreach $i (1..4) {
120-
foreach $j (1..4) {
121-
$A->assign($i,$j, $a[$i][$j]);
122-
}
123-
}
124-
$A_lr = $A->decompose_LR();
125-
$A_det = $A_lr->det_LR();
126-
$b = $A_lr->invert_LR();
127-
$e = new Matrix(4,4);
128-
$e->one();
129-
$eig2 = non_zero_random(-4,4,1);
130-
$e->assign(2,2, $eig2);
131-
$eig1 = random(-1,1,2);
132-
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
133-
$e->assign(1,1, $eig1);
134-
$eig3 = - $eig1;
135-
$e->assign(3,3, $eig3);
136-
$eig4 = - $eig2;
137-
$e->assign(4,4, $eig4);
138-
$ans2 = random(-8,8,1);
139-
while($eig1==$ans2||$eig2==$ans2||$eig3==$ans2||$eig4==$ans2) {
140-
$ans2 = random(-8,8,1); }
141-
$matrix2 = $A * $e *$b;
142-
$matrix2_lr = $matrix2->decompose_LR();
143-
$matrix2_det = $matrix2_lr->det_LR();
144-
foreach $i (1..4) {
145-
foreach $j (1..4) {
146-
$m[$i][$j] = $matrix2->element($i,$j);
147-
$m[$i][$j] = round($m[$i][$j]);
148-
$matrix2 -> assign($i,$j,$m[$i][$j]);
149-
}
150-
}
151-
152-
# THIRD MATRIX
153-
$A= new Matrix(4,4);
154-
$a[1][1] = random(-2,2,1);
155-
$a[2][1] = random(-1,1,2);
156-
$a[3][1] = random(-1,1,2);
157-
$a[4][1] = random(-1,1,1);
158-
$b1 = random(-1,1,2);
159-
foreach $i (1..4) {
160-
$a[$i][2] = $b1 * $a[$i][1]; }
161-
$p = random(-1,1,2);
162-
$a[2][2] = $a[2][2] + $p;
163-
$c = random(-1,1,1);
164-
$d = random(-1,1,2);
165-
$n = random(-1,1,2);
166-
foreach $i (1..4) {
167-
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
168-
$n = random(-1,1,2);
169-
$a[1][3] = $a[1][3] + $n;
170-
$f = random(-1,1,2);
171-
$g = random(-1,1,1);
172-
$h = random(-1,1,1);
173-
foreach $i (1..4) {
174-
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
175-
$q = random(-1,1,2);
176-
$a[4][4] = $a[4][4] + $q;
177-
$det = - $a[3][1] * $p * $n * $q;
178-
foreach $i (1..4) {
179-
foreach $j (1..4) {
180-
$A->assign($i,$j, $a[$i][$j]);
181-
}
182-
}
183-
$A_lr = $A->decompose_LR();
184-
$A_det = $A_lr->det_LR();
185-
$b = $A_lr->invert_LR();
186-
$e = new Matrix(4,4);
187-
$e->one();
188-
$eig2 = non_zero_random(-4,4,1);
189-
$e->assign(2,2, $eig2);
190-
$eig1 = random(-1,1,2);
191-
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
192-
$e->assign(1,1, $eig1);
193-
$eig3 = - $eig1;
194-
$e->assign(3,3, $eig3);
195-
$eig4 = - $eig2;
196-
$e->assign(4,4, $eig4);
197-
$ans3 = $eig1;
198-
$matrix3 = $A * $e *$b;
199-
$matrix3_lr = $matrix3->decompose_LR();
200-
$matrix3_det = $matrix3_lr->det_LR();
201-
foreach $i (1..4) {
202-
foreach $j (1..4) {
203-
$m[$i][$j] = $matrix3->element($i,$j);
204-
$m[$i][$j] = round($m[$i][$j]);
205-
$matrix3 -> assign($i,$j,$m[$i][$j]);
206-
}
207-
}
208-
209-
# FOURTH MATRIX
210-
$A= new Matrix(4,4);
211-
$a[1][1] = random(-2,2,1);
212-
$a[2][1] = random(-1,1,2);
213-
$a[3][1] = random(-1,1,2);
214-
$a[4][1] = random(-1,1,1);
215-
$b1 = random(-1,1,2);
216-
foreach $i (1..4) {
217-
$a[$i][2] = $b1 * $a[$i][1]; }
218-
$p = random(-1,1,2);
219-
$a[2][2] = $a[2][2] + $p;
220-
$c = random(-1,1,1);
221-
$d = random(-1,1,2);
222-
$n = random(-1,1,2);
223-
foreach $i (1..4) {
224-
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
225-
$n = random(-1,1,2);
226-
$a[1][3] = $a[1][3] + $n;
227-
$f = random(-1,1,2);
228-
$g = random(-1,1,1);
229-
$h = random(-1,1,1);
230-
foreach $i (1..4) {
231-
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
232-
$q = random(-1,1,2);
233-
$a[4][4] = $a[4][4] + $q;
234-
$det = - $a[3][1] * $p * $n * $q;
235-
foreach $i (1..4) {
236-
foreach $j (1..4) {
237-
$A->assign($i,$j, $a[$i][$j]);
238-
}
239-
}
240-
$A_lr = $A->decompose_LR();
241-
$A_det = $A_lr->det_LR();
242-
$b = $A_lr->invert_LR();
243-
$e = new Matrix(4,4);
244-
$e->one();
245-
$eig2 = non_zero_random(-4,4,1);
246-
$e->assign(2,2, $eig2);
247-
$eig1 = random(-1,1,2);
248-
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
249-
$e->assign(1,1, $eig1);
250-
$eig3 = - $eig1;
251-
$e->assign(3,3, $eig3);
252-
$eig4 = - $eig2;
253-
$e->assign(4,4, $eig4);
254-
$ans4 = random(-8,8,1);
255-
while($eig1==$ans4||$eig2==$ans4||$eig3==$ans4||$eig4==$ans4) {
256-
$ans4 = random(-8,8,1); }
257-
$matrix4 = $A * $e *$b;
258-
$matrix4_lr = $matrix4->decompose_LR();
259-
$matrix4_det = $matrix4_lr->det_LR();
260-
foreach $i (1..4) {
261-
foreach $j (1..4) {
262-
$m[$i][$j] = $matrix4->element($i,$j);
263-
$m[$i][$j] = round($m[$i][$j]);
264-
$matrix4 -> assign($i,$j,$m[$i][$j]);
265-
}
266-
}
267-
268-
# Make a new checkbox multiple choice
269-
$tf = new_pop_up_select_list();
270-
$tf->ra_pop_up_list(["?"=>"Select an Answer", "Yes" => "Yes", "No" => "No"]);
271-
# $cmc now "contains" the checkbox multiple choice object.
272-
273-
# Insert some questions and matching answers in the q/a list
274-
275-
$tf -> qa (
276-
277-
mbox( '\( A = \)', display_matrix($matrix), ', \( \lambda = $ans1\)' ),
278-
"Yes",
279-
280-
mbox( '\( A = \)', display_matrix($matrix2), ', \( \lambda = $ans2\)' ),
281-
"No",
282-
283-
mbox( '\( A = \)', display_matrix($matrix3), ', \( \lambda = $ans3\)' ),
284-
"Yes",
285-
286-
mbox( '\( A = \)', display_matrix($matrix4), ', \( \lambda = $ans4\)' ),
287-
"No",
288-
);
289-
290-
$tf->choose(3);
291-
292-
# Insert some incorrect answers
293-
294-
# Print the text using $mc->print_q for the questions and
295-
# $mc->print_a to print the answers.
296-
BEGIN_TEXT
297-
298-
Determine if \( \lambda \) is an eigenvalue of the matrix \(A\).
299-
300-
$BR
301-
302-
\{ $tf -> print_q \}
303-
304-
END_TEXT
305-
306-
# Enter the correct answers to be checked against the answers to the students.
307-
ANS(str_cmp( $tf->ra_correct_ans ) ) ;
32+
Context("Matrix");
33+
34+
# randomized invertible matrix
35+
36+
sub randInvMat{
37+
my $P = Matrix(
38+
[ [non_zero_random(-2,2,1), 0, 0, 0],
39+
[random(-1,1,2), random(-1,1,2), 0, 0],
40+
[random(-1,1,2), 0, random(-1,1,2), 0],
41+
[random(-1,1,2), 0, 0, random(-1,1,2)]
42+
]
43+
);
44+
my $Q = Matrix(
45+
[ [1, random(-1,1,2),random(-1,1,2), random(-1,1,2) ],
46+
[0, 1, random(-1,1,2), random(-1,1,2) ],
47+
[0, 0, 1, random(-1,1,2) ],
48+
[0, 0, 0, 1]
49+
]
50+
);
51+
return $P*$Q;
52+
};
53+
54+
# make four random matrices that have known eigenvalues
55+
56+
for ($i=0; $i<4; $i++){
57+
$eig2[$i] = (-4,-3,-2,2,3,4)[random(0,5,1)];
58+
$eig1[$i] = random(-1,1,2);
59+
my $e = Matrix([
60+
[ $eig1[$i], 0, 0, 0],
61+
[ 0, $eig2[$i], 0, 0],
62+
[ 0, 0, -$eig1[$i], 0],
63+
[ 0, 0, 0, -$eig2[$i]]
64+
]);
65+
my $A = randInvMat();
66+
my $Ainv = $A->inverse;
67+
$M[$i]=$A*$e*$Ainv;
68+
};
69+
70+
$lambda[0]=$eig2[0];
71+
$choice[0]=DropDown(["Yes","No"],"Yes",placeholder=>"Select an answer");
72+
73+
do {
74+
$lambda[1]=random(-8,8,1);
75+
} until (abs($lambda[1]) != abs($eig2[1]) && abs($lambda[1]) != 1);
76+
$choice[1]=DropDown(["Yes","No"],"No",placeholder=>"Select an answer");
77+
78+
$lambda[2]=$eig1[2];
79+
$choice[2]=DropDown(["Yes","No"],"Yes",placeholder=>"Select an answer");
80+
81+
do {
82+
$lambda[3]=random(-8,8,1);
83+
} until (abs($lambda[3]) != abs($eig2[3]) && abs($lambda[3]) != 1);
84+
$choice[3]=DropDown(["Yes","No"],"No",placeholder=>"Select an answer");
85+
86+
# reindex to make questions appear in a random order
87+
88+
@q = random_subset(4,(0,1,2,3));
89+
90+
BEGIN_PGML
91+
Determine if [`\lambda`] is an eigenvalue of the matrix [`A`]:
92+
93+
[`A = [$M[$q[0]]],\quad \lambda=[$lambda[$q[0]]]\quad`][_____]{$choice[$q[0]]}
94+
95+
[`A = [$M[$q[1]]],\quad \lambda=[$lambda[$q[1]]]\quad`][_____]{$choice[$q[1]]}
96+
97+
[`A = [$M[$q[2]]],\quad \lambda=[$lambda[$q[2]]]\quad`][_____]{$choice[$q[2]]}
98+
99+
[`A = [$M[$q[3]]],\quad \lambda=[$lambda[$q[3]]]\quad`][_____]{$choice[$q[3]]}
100+
END_PGML
101+
102+
BEGIN_PGML_SOLUTION
103+
[`\lambda`] is an eigenvalue of [`A`] if and only if the matrix [`\lambda I - A`] is singular. One may check for singularity by row reducing the matrix [`\lambda I - A`], or by computing its determinant, or some other convenient method.
104+
END_PGML_SOLUTION
308105

309106
ENDDOCUMENT();

0 commit comments

Comments
 (0)