Skip to content

Commit

Permalink
Merge pull request #1161 from gajennings/main
Browse files Browse the repository at this point in the history
Fix bug 4833, use math objects and PGML
  • Loading branch information
gajennings authored Jul 3, 2024
2 parents 0c3899b + 0d12902 commit badd0e4
Showing 1 changed file with 76 additions and 279 deletions.
355 changes: 76 additions & 279 deletions OpenProblemLibrary/TCNJ/TCNJ_Eigenvalues/problem6.pg
Original file line number Diff line number Diff line change
Expand Up @@ -20,290 +20,87 @@
DOCUMENT(); # This should be the first executable line in the problem.
loadMacros(
"PGstandard.pl",
"PGchoicemacros.pl",
"PGgraphmacros.pl",
"PGmatrixmacros.pl",
"PGnumericalmacros.pl",
"PGmorematrixmacros.pl",
"PGcomplexmacros.pl",
"MathObjects.pl",
"parserPopUp.pl",
"PGML.pl",
"PGcourse.pl"
);

TEXT(beginproblem());
# Do not show which answers are incorrect.
$showPartialCorrectAnswers = 1;

# FIRST MATRIX
$A= new Matrix(4,4);
$a[1][1] = random(-2,2,1);
$a[2][1] = random(-1,1,2);
$a[3][1] = random(-1,1,2);
$a[4][1] = random(-1,1,1);
$b1 = random(-1,1,2);
foreach $i (1..4) {
$a[$i][2] = $b1 * $a[$i][1]; }
$p = random(-1,1,2);
$a[2][2] = $a[2][2] + $p;
$c = random(-1,1,1);
$d = random(-1,1,2);
$n = random(-1,1,2);
foreach $i (1..4) {
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
$n = random(-1,1,2);
$a[1][3] = $a[1][3] + $n;
$f = random(-1,1,2);
$g = random(-1,1,1);
$h = random(-1,1,1);
foreach $i (1..4) {
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
$q = random(-1,1,2);
$a[4][4] = $a[4][4] + $q;
$det = - $a[3][1] * $p * $n * $q;
foreach $i (1..4) {
foreach $j (1..4) {
$A->assign($i,$j, $a[$i][$j]);
}
}
$A_lr = $A->decompose_LR();
$A_det = $A_lr->det_LR();
$b = $A_lr->invert_LR();
$e = new Matrix(4,4);
$e->one();
$eig2 = non_zero_random(-4,4,1);
$e->assign(2,2, $eig2);
$eig1 = random(-1,1,2);
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
$e->assign(1,1, $eig1);
$eig3 = - $eig1;
$e->assign(3,3, $eig3);
$eig4 = - $eig2;
$e->assign(4,4, $eig4);
$ans1 = $eig2;
$matrix = $A * $e *$b;
$matrix_lr = $matrix->decompose_LR();
$matrix_det = $matrix_lr->det_LR();
foreach $i (1..4) {
foreach $j (1..4) {
$m[$i][$j] = $matrix->element($i,$j);
$m[$i][$j] = round($m[$i][$j]);
$matrix -> assign($i,$j,$m[$i][$j]);
}
}

# SECOND MATRIX
$A= new Matrix(4,4);
$a[1][1] = random(-2,2,1);
$a[2][1] = random(-1,1,2);
$a[3][1] = random(-1,1,2);
$a[4][1] = random(-1,1,1);
$b1 = random(-1,1,2);
foreach $i (1..4) {
$a[$i][2] = $b1 * $a[$i][1]; }
$p = random(-1,1,2);
$a[2][2] = $a[2][2] + $p;
$c = random(-1,1,1);
$d = random(-1,1,2);
$n = random(-1,1,2);
foreach $i (1..4) {
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
$n = random(-1,1,2);
$a[1][3] = $a[1][3] + $n;
$f = random(-1,1,2);
$g = random(-1,1,1);
$h = random(-1,1,1);
foreach $i (1..4) {
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
$q = random(-1,1,2);
$a[4][4] = $a[4][4] + $q;
$det = - $a[3][1] * $p * $n * $q;
foreach $i (1..4) {
foreach $j (1..4) {
$A->assign($i,$j, $a[$i][$j]);
}
}
$A_lr = $A->decompose_LR();
$A_det = $A_lr->det_LR();
$b = $A_lr->invert_LR();
$e = new Matrix(4,4);
$e->one();
$eig2 = non_zero_random(-4,4,1);
$e->assign(2,2, $eig2);
$eig1 = random(-1,1,2);
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
$e->assign(1,1, $eig1);
$eig3 = - $eig1;
$e->assign(3,3, $eig3);
$eig4 = - $eig2;
$e->assign(4,4, $eig4);
$ans2 = random(-8,8,1);
while($eig1==$ans2||$eig2==$ans2||$eig3==$ans2||$eig4==$ans2) {
$ans2 = random(-8,8,1); }
$matrix2 = $A * $e *$b;
$matrix2_lr = $matrix2->decompose_LR();
$matrix2_det = $matrix2_lr->det_LR();
foreach $i (1..4) {
foreach $j (1..4) {
$m[$i][$j] = $matrix2->element($i,$j);
$m[$i][$j] = round($m[$i][$j]);
$matrix2 -> assign($i,$j,$m[$i][$j]);
}
}

# THIRD MATRIX
$A= new Matrix(4,4);
$a[1][1] = random(-2,2,1);
$a[2][1] = random(-1,1,2);
$a[3][1] = random(-1,1,2);
$a[4][1] = random(-1,1,1);
$b1 = random(-1,1,2);
foreach $i (1..4) {
$a[$i][2] = $b1 * $a[$i][1]; }
$p = random(-1,1,2);
$a[2][2] = $a[2][2] + $p;
$c = random(-1,1,1);
$d = random(-1,1,2);
$n = random(-1,1,2);
foreach $i (1..4) {
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
$n = random(-1,1,2);
$a[1][3] = $a[1][3] + $n;
$f = random(-1,1,2);
$g = random(-1,1,1);
$h = random(-1,1,1);
foreach $i (1..4) {
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
$q = random(-1,1,2);
$a[4][4] = $a[4][4] + $q;
$det = - $a[3][1] * $p * $n * $q;
foreach $i (1..4) {
foreach $j (1..4) {
$A->assign($i,$j, $a[$i][$j]);
}
}
$A_lr = $A->decompose_LR();
$A_det = $A_lr->det_LR();
$b = $A_lr->invert_LR();
$e = new Matrix(4,4);
$e->one();
$eig2 = non_zero_random(-4,4,1);
$e->assign(2,2, $eig2);
$eig1 = random(-1,1,2);
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
$e->assign(1,1, $eig1);
$eig3 = - $eig1;
$e->assign(3,3, $eig3);
$eig4 = - $eig2;
$e->assign(4,4, $eig4);
$ans3 = $eig1;
$matrix3 = $A * $e *$b;
$matrix3_lr = $matrix3->decompose_LR();
$matrix3_det = $matrix3_lr->det_LR();
foreach $i (1..4) {
foreach $j (1..4) {
$m[$i][$j] = $matrix3->element($i,$j);
$m[$i][$j] = round($m[$i][$j]);
$matrix3 -> assign($i,$j,$m[$i][$j]);
}
}

# FOURTH MATRIX
$A= new Matrix(4,4);
$a[1][1] = random(-2,2,1);
$a[2][1] = random(-1,1,2);
$a[3][1] = random(-1,1,2);
$a[4][1] = random(-1,1,1);
$b1 = random(-1,1,2);
foreach $i (1..4) {
$a[$i][2] = $b1 * $a[$i][1]; }
$p = random(-1,1,2);
$a[2][2] = $a[2][2] + $p;
$c = random(-1,1,1);
$d = random(-1,1,2);
$n = random(-1,1,2);
foreach $i (1..4) {
$a[$i][3] = $c * $a[$i][1] + $d * $a[$i][2]; }
$n = random(-1,1,2);
$a[1][3] = $a[1][3] + $n;
$f = random(-1,1,2);
$g = random(-1,1,1);
$h = random(-1,1,1);
foreach $i (1..4) {
$a[$i][4] = $f * $a[$i][1] + $g * $a[$i][2] + $h * $a[$i][3]; }
$q = random(-1,1,2);
$a[4][4] = $a[4][4] + $q;
$det = - $a[3][1] * $p * $n * $q;
foreach $i (1..4) {
foreach $j (1..4) {
$A->assign($i,$j, $a[$i][$j]);
}
}
$A_lr = $A->decompose_LR();
$A_det = $A_lr->det_LR();
$b = $A_lr->invert_LR();
$e = new Matrix(4,4);
$e->one();
$eig2 = non_zero_random(-4,4,1);
$e->assign(2,2, $eig2);
$eig1 = random(-1,1,2);
if (abs($eig1) == abs($eig2)) { $eig2 = random(-2,2,4);}
$e->assign(1,1, $eig1);
$eig3 = - $eig1;
$e->assign(3,3, $eig3);
$eig4 = - $eig2;
$e->assign(4,4, $eig4);
$ans4 = random(-8,8,1);
while($eig1==$ans4||$eig2==$ans4||$eig3==$ans4||$eig4==$ans4) {
$ans4 = random(-8,8,1); }
$matrix4 = $A * $e *$b;
$matrix4_lr = $matrix4->decompose_LR();
$matrix4_det = $matrix4_lr->det_LR();
foreach $i (1..4) {
foreach $j (1..4) {
$m[$i][$j] = $matrix4->element($i,$j);
$m[$i][$j] = round($m[$i][$j]);
$matrix4 -> assign($i,$j,$m[$i][$j]);
}
}

# Make a new checkbox multiple choice
$tf = new_pop_up_select_list();
$tf->ra_pop_up_list(["?"=>"Select an Answer", "Yes" => "Yes", "No" => "No"]);
# $cmc now "contains" the checkbox multiple choice object.

# Insert some questions and matching answers in the q/a list

$tf -> qa (

mbox( '\( A = \)', display_matrix($matrix), ', \( \lambda = $ans1\)' ),
"Yes",

mbox( '\( A = \)', display_matrix($matrix2), ', \( \lambda = $ans2\)' ),
"No",

mbox( '\( A = \)', display_matrix($matrix3), ', \( \lambda = $ans3\)' ),
"Yes",

mbox( '\( A = \)', display_matrix($matrix4), ', \( \lambda = $ans4\)' ),
"No",
);

$tf->choose(3);

# Insert some incorrect answers

# Print the text using $mc->print_q for the questions and
# $mc->print_a to print the answers.
BEGIN_TEXT

Determine if \( \lambda \) is an eigenvalue of the matrix \(A\).

$BR

\{ $tf -> print_q \}

END_TEXT

# Enter the correct answers to be checked against the answers to the students.
ANS(str_cmp( $tf->ra_correct_ans ) ) ;
Context("Matrix");

# randomized invertible matrix

sub randInvMat{
my $P = Matrix(
[ [non_zero_random(-2,2,1), 0, 0, 0],
[random(-1,1,2), random(-1,1,2), 0, 0],
[random(-1,1,2), 0, random(-1,1,2), 0],
[random(-1,1,2), 0, 0, random(-1,1,2)]
]
);
my $Q = Matrix(
[ [1, random(-1,1,2),random(-1,1,2), random(-1,1,2) ],
[0, 1, random(-1,1,2), random(-1,1,2) ],
[0, 0, 1, random(-1,1,2) ],
[0, 0, 0, 1]
]
);
return $P*$Q;
};

# make four random matrices that have known eigenvalues

for ($i=0; $i<4; $i++){
$eig2[$i] = (-4,-3,-2,2,3,4)[random(0,5,1)];
$eig1[$i] = random(-1,1,2);
my $e = Matrix([
[ $eig1[$i], 0, 0, 0],
[ 0, $eig2[$i], 0, 0],
[ 0, 0, -$eig1[$i], 0],
[ 0, 0, 0, -$eig2[$i]]
]);
my $A = randInvMat();
my $Ainv = $A->inverse;
$M[$i]=$A*$e*$Ainv;
};

$lambda[0]=$eig2[0];
$choice[0]=DropDown(["Yes","No"],"Yes",placeholder=>"Select an answer");

do {
$lambda[1]=random(-8,8,1);
} until (abs($lambda[1]) != abs($eig2[1]) && abs($lambda[1]) != 1);
$choice[1]=DropDown(["Yes","No"],"No",placeholder=>"Select an answer");

$lambda[2]=$eig1[2];
$choice[2]=DropDown(["Yes","No"],"Yes",placeholder=>"Select an answer");

do {
$lambda[3]=random(-8,8,1);
} until (abs($lambda[3]) != abs($eig2[3]) && abs($lambda[3]) != 1);
$choice[3]=DropDown(["Yes","No"],"No",placeholder=>"Select an answer");

# reindex to make questions appear in a random order

@q = random_subset(4,(0,1,2,3));

BEGIN_PGML
Determine if [`\lambda`] is an eigenvalue of the matrix [`A`]:

[`A = [$M[$q[0]]],\quad \lambda=[$lambda[$q[0]]]\quad`][_____]{$choice[$q[0]]}

[`A = [$M[$q[1]]],\quad \lambda=[$lambda[$q[1]]]\quad`][_____]{$choice[$q[1]]}

[`A = [$M[$q[2]]],\quad \lambda=[$lambda[$q[2]]]\quad`][_____]{$choice[$q[2]]}

[`A = [$M[$q[3]]],\quad \lambda=[$lambda[$q[3]]]\quad`][_____]{$choice[$q[3]]}
END_PGML

BEGIN_PGML_SOLUTION
[`\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.
END_PGML_SOLUTION

ENDDOCUMENT();

0 comments on commit badd0e4

Please sign in to comment.