## PREP 2014 Question Authoring - Archived

### How can an answer be a list of linear equations? ### How can an answer be a list of linear equations?

by Mary Cameron -
Number of replies: 8
I would like to be able to have a list of zero, one or two linear equations (like:  x=2, x=4) be entered and checked.  I can get one equation to work, but when trying to use a list, I am unsuccessful.  How can this be done?  Below is the code I am using

Thanks,
Mary Cameron

DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"parserPopUp.pl",
"compoundProblem.pl",
"problemRandomize.pl",   # to get more problem practice
"parserImplicitPlane.pl",    # for VA and HA linear equations
);
TEXT(beginproblem());

# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1; ProblemRandomize(when => "Correct", onlyAfterDue => 0); Context("Numeric"); ############################## Setup # Choose two different vertical asymptotes @va = ();$va = random(-9,9,1);
do {   $va = random(-9,9,1); } until ($va != $va) ;$denom = Formula( "x^2 - ($va +$va) * x + $va *$va"  ) -> reduce ( "(-x)-y" => 0, "(-x)+y" => 0 );
$f = Formula("1/$denom");

################### The following WORKS -- checking just ONE linear equation:
#Context("ImplicitPlane")->variables->are(x=>"Real",y=>"Real");
#$v1 = ImplicitPlane("x=$va")  ;  # vertical asymptote equation
#$answers = ImplicitPlane("x=$va")  ;  # vertical asymptote equation  WORKS

#$answers = ImplicitPlane($v1 )  ;              # also works (if put $v1 defined before this) ################### The following FAILS -- Want to check Two linear equation answers as a list of two linear equations Context("ImplicitPlane")->variables->are(x=>"Real",y=>"Real");$v1 = ImplicitPlane("x=$va") ; # vertical asymptote equation$v2 = ImplicitPlane("x=$va") ; # vertical asymptote equation$answers = List( $v1,$v2  );

################### more FAILURES
#$answers = List ( ImplicitPlane($v1 ) ,  ImplicitPlane( $v2 ) ); # FAILS Can't locate object method "blank" via package "ImplicitPlane::formula" #$answers = List (   $v1,$v2 );                                #  FAILS ... "blank"
#$answers = List ( "$v1" );                                 #  also FAILS with just one... "blank"
#$answers = List ( "x=$va",   "x=$va" ); # FAILS ... "blank" #$answers = List (  $va,$va );     # correct answers are like 4, 5  rather than "x=4", and "x=5".
#$answers = List ( "$va",   "$va" ); # same problem here -- 4,5 is correct "$ans_eval = $answers ->cmp(); ################################# Text #1 Context()->texStrings; BEGIN_TEXT${BBOLD}Part 1 of 3:${EBOLD} Enter the equation(s) for the vertical asymptotes of $$f(x) = \displaystyle f$$$BR If there is more than one vertical asymptote, separate the equations with commas.
$BR If there are no vertical asymptotes, enter "NONE"$BR  The equations for the vertical asymptotes of $$f(x)$$ are
\{ ans_rule(20) \}
END_TEXT
Context()->normalStrings;
ANS( $ans_eval ); ENDDOCUMENT(); In reply to Mary Cameron ### Re: How can an answer be a list of linear equations? by Davide Cervone - This was fixed in a recent patch to parserImplicitPlane.pl, but you'd have to upgrade your copy of PG to get it. I've attached the updated copy here. You can put this in the templates/macros directory of your course until the PG at your institution is updated (then remove it). In reply to Davide Cervone ### Re: How can an answer be a list of linear equations? by Mary Cameron - Yay! It works. Thank you! However, I put the file in the macros directory of this course for now. I hope this is ok.... In reply to Mary Cameron ### Re: How can an answer be a list of linear equations? by Paul Pearson - Hi Mary, Since your equations are of the form x = constant, you could have used parserAssignment.pl instead of parserImplicitPlane.pl Context("Numeric"); parser::Assignment->Allow;$a1 = Formula("x=2");
$a2 = Formula("x=3");$answer = List($a1,$a2);

Best regards,

Paul Pearson ### Re: How can an answer be a list of linear equations?

by Robin Cruz -
Hi,
I tried Paul's suggestion and I get an error: No value given for variable 'x'
Here's the code snippet.  Thing is, I wrote 5 problems and on some of them it works.
-----------------------------------------------
Context("Numeric")->variables->are(x=>"Real",y=>"Real");
parser::Assignment->Allow;

$answer1 = Formula("x = -1*$a")->reduce;
$answer2 = Formula("x =$a")->reduce;
$answer3 = Formula("y =-1* x")->reduce;$ans = List( $answer1,$answer2, $answer3 ); ANS($ans->cmp() );
-----------------------------------------------

I also tried these with the ImplicitPlane method using the macro Davide attached to an earlier note in this thread. It works but I like the way the answer displays a little better with Assignment: "y=-x" instead of "x+y=0"

I got one error on a problem using the ImplicitPlane method:

### Warning messages

• Use of uninitialized value $ld in numeric eq (==) at line 169 of [TMPL]/macros/parserImplicitPlane.pl Code segment: --------------------------------------------- Context("ImplicitPlane")->variables->are(x=>"Real",y=>"Real"); #NOT AS NICE DISPLAY$answer1 = ImplicitPlane("y = $c/$d");
$ans = List($answer1 );
ANS($ans->cmp() ); ------------------------------------------------ Thanks for any advice you can give me on this. --rac In reply to Robin Cruz ### Re: How can an answer be a list of linear equations? by Davide Cervone - I am not able to reproduce the errors you are getting with either code snippet. Can you give the complete problem and seed, and the answer you are entering? In reply to Davide Cervone ### Re: How can an answer be a list of linear equations? by Robin Cruz - Davide, The complete code is at the end of this note and a seed that has produced the warning message: ### Warning messages • Use of uninitialized value$ld in numeric eq (==) at line 169 of [TMPL]/macros/parserImplicitPlane.pl

There is another weird thing that is happening. I've had three people report the same problem:  They submit an incorrect answer and then re-submit the correct answer.  The answers are correctly checked, the first "incorrect" and the second "correct", but on the second submission they get a message:
This answer is equivalent to the one you just submitted.
The code for this problem and a seed on one of these is at the end of this note too.

Thanks - rac

#-------Some people get a warning message: Seeds: 1886--------------------------
DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"parserImplicitPlane.pl",
"PGchoicemacros.pl"
);

TEXT(beginproblem());
#######################################
#  Set-up

Context("Numeric");

$a = non_zero_random(-5,5,1);$b = random(1,5,1); if ($a==$b) {$b =$b+1;}
$d = list_random(1, 2, 4, 5); #---Makes nicer denominators$c =  random(1,5,1); if ($c==$d) {$c =$c+1;}
$f = Compute("($c* x^2 + $a)/($d* x^2 + $b)")->reduce;$mc = new_checkbox_multiple_choice();
$mc -> qa ( "Is the following function even or odd or periodic? Select all that apply:", "Even" );$mc -> makeLast("Even","Odd", "Periodic","None of these");

#######################################
#  Main

Context()->texStrings;
BEGIN_TEXT
(a) \{ $mc -> print_q() \} $f(x) = f$ \{$mc -> print_a() \}
$BR (b) Find all asymptotes of $$f(x)$$: $$y = b$$ or $$x = a$$ or $$y = mx+b$$ or${BITALIC}NONE${EITALIC}.$BR
Asymptotes: \{ ans_rule(30) \}
END_TEXT
Context()->normalStrings;

#######################################

$showPartialCorrectAnswers = 0; ANS( checkbox_cmp($mc->correct_ans() ) );

Context("ImplicitPlane")->variables->are(x=>"Real",y=>"Real");

$answer1 = ImplicitPlane("y =$c/$d");$ans = List(  $answer1 ); ANS($ans->cmp() );

ENDDOCUMENT();

#---------Gives weird message on a second submission: Seed 1087, 4830-------
DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"parserImplicitPlane.pl",
"PGchoicemacros.pl"
);

TEXT(beginproblem());
#######################################
#  Set-up

Context("Numeric");

$a = list_random(-5,-3, -2, 1, 3, 4, 5);$b = non_zero_random(-5,5,1); if ($a ==$b) {$b = 6;}$f = Compute("(x^2 + $a)/(x +$b)")->reduce;

$mc = new_checkbox_multiple_choice();$mc -> qa (
"Is the following function even or odd or periodic? Select all that apply:",
"None of these"
);
$mc -> makeLast("Even","Odd", "Periodic","None of these"); ####################################### # Main Context()->texStrings; BEGIN_TEXT (a) \{$mc -> print_q() \}
$f(x) = f$
\{ $mc -> print_a() \}$BR
(b) Find all asymptotes of $$f(x)$$: $$y = b$$ or $$x = a$$ or $$y = mx+b$$ or ${BITALIC}NONE${EITALIC}.
$BR Asymptotes: \{ ans_rule(30) \} END_TEXT Context()->normalStrings; ####################################### # Answer$showPartialCorrectAnswers = 0;

ANS( checkbox_cmp( $mc->correct_ans() ) ); Context("ImplicitPlane")->variables->are(x=>"Real",y=>"Real");$answer1 = ImplicitPlane("y =  x - $b");$answer2 = ImplicitPlane("x = -1*$b");$ans = List(  $answer1,$answer2 );
ANS($ans->cmp() ); ENDDOCUMENT(); In reply to Robin Cruz ### Re: How can an answer be a list of linear equations? by Davide Cervone - OK, got it. Both problems (and some others, too) are being caused by the check to see if the student has entered an equivalent expression to the last one they entered (e.g., just added redundant parentheses). The initial error about undefined $ld was because that code forces the earlier answer to be treated as a Formula and so it loses the fact that it is an ImplicitPlane object and the data associated with that. I have fixed that in an updated copy of parserImplicitPlane.pl. There is a "raw" button on the linked page that you can use to obtain a copy of the file (use your browser "Save" menu to save it to disk and them move it to your course macros directory).

This patch also fixes a problem when the student enters "NONE" when an ImplicitPlane was the correct answer (it used to give an error message about the answer not being an implicit plane; now it is marked incorrect silently).

The second issue, about the message saying the answer is the same as the previous one, is more difficult to fix, and I haven't done that yet. This comes from the fact that you have a list of implicit planes. Because the planes are formulas, the list is converted from a list of formulas to a formula returning a list. That is fine for the student answer checking (since formulas returning lists are broken up again during answer checking). But the check to see if a formula is equivalent to the previous one is done by a simple equality check of the two formulas, and that means the lists are evaluated at the test points for the formula and the results are compared to see if the lists agree at each test point.

The equal signs in the formulas produce the result 1 if the two operands are equal and 0 if not, and for random points, the equation of the plane usually is not equal, so you get lists with just a bunch of zeros as the entries, and that means that the two lists do agree on the test points. That is why the message is produced. The upshot is that the implicit planes are not being checked for equality as they should be in that case, and that leads to the incorrect message.

I'm not sure what the best way is to address the problem, as the code that is handing this is not part of the parserImplicitPlane.pl file. I have to think harder about how to overcome the problem. Probably the real solution is to not convert lists of formulas to a formula returning a list, but that is a fairly fundamental change to MathObjects that would need to be handled very carefully. It is not a quick fix. I'll see if I can come up with something better for the time being. 