SimplifiedSquareRoots: Difference between revisions

From WeBWorK_wiki
Jump to navigation Jump to search
(New page: <h2>Your title here: PG Code Snippet</h2> <!-- Header for these sections -- no modification needed --> <p style="background-color:#eeeeee;border:black solid 1px;padding:3px;"> <em>Thi...)
 
No edit summary
Line 47: Line 47:
<td style="background-color:#ffffdd;border:black 1px dashed;">
<td style="background-color:#ffffdd;border:black 1px dashed;">
<pre>
<pre>
# code essentially from Davide Cervone 4/25/10
###########################
###########################
#
#
Line 87: Line 86:
#
#
LimitedPowers::OnlyPositiveIntegers();
LimitedPowers::OnlyPositiveIntegers();
$expr = "\sqrt{12 x^2}";


$f = Compute("x*sqrt(6)");
$f = Compute("x*sqrt(6)");
Line 94: Line 95:
<p>
<p>
<b>Setup:</b>  
<b>Setup:</b>  
We specify that the Context should be <code>......</code>, and define the answer to be a formula.
This code is from Davide Cervone (4/25/10). See the discussion [[http://webwork.maa.org/moodle/mod/forum/discuss.php?d=6416 simplifying radical expressions]] for more information.
</p>
<p>
Notes: on using this and related Contexts.
</p>
</p>
</td>
</td>
</tr>
</tr>
Line 109: Line 106:
<pre>
<pre>
BEGIN_TEXT
BEGIN_TEXT
Simplify \( \sqrt{6 x^2} \) assuming that \( x \geq 0 \).
Simplify \( $expr \) assuming that \( x \geq 0 \).
Do not enter fractional exponents.
$BR
$BR
$BR
$BR
\( \sqrt{6 x^2} \) = \{ans_rule(20)\}
\( $expr \) = \{ans_rule(20)\}
END_TEXT
END_TEXT


Line 131: Line 129:
$showPartialCorrectAnswers = 1;
$showPartialCorrectAnswers = 1;


#
# Use a custom checker to check that the answers are equivalent
# and that they are still equivalent when sqrt() is replaced by 1
# (so the stuff outside the sqrt() is equal in both)
#
ANS( $f-> cmp( checker => sub {
ANS( $f-> cmp( checker => sub {
   my ($correct,$student,$ans) = @_;
   my ($correct,$student,$ans) = @_;
Line 163: Line 156:
<p>
<p>
<b>Answer Evaluation:</b>
<b>Answer Evaluation:</b>
As is the answer.
Use a custom checker to check that the answers are equivalent
and that they are still equivalent when sqrt() is replaced by 1
(so the stuff outside the sqrt() is equal in both).
</p>
</p>
</td>
</td>

Revision as of 21:14, 25 April 2010

Your title here: PG Code Snippet


This PG code shows how to check student answers that are equations. Note that this is an insertion, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

Problem Techniques Index

PG problem file Explanation
DOCUMENT(); 

loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"contextLimitedPowers.pl"
);

TEXT(beginproblem());

Initialization: We need to include the macros file contextLimitedPowers.pl.

###########################
#
# Subclass the numeric functions
#
package my::Function::numeric;
our @ISA = ('Parser::Function::numeric');

#
# Override sqrt() to return a special value (usually 1) when evaluated
# effectively eliminating it from the product.
#
sub sqrt {
  my $self = shift;
  my $value = $self->context->flag("setSqrt");
  return $value+1 if $value && $_[0] == 1; # force sqrt(1) to be incorrect
  return $value if $value;
  return $self->SUPER::sqrt(@_);
}

#
# end of subclass
#
package main;

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

Context("Numeric")->variables->are(
x => ["Real", limits => [0,2]],    # only needed if x is used in the square roots
);
#
# make sqrt() use our subclass
#
Context()->functions->set(sqrt=>{class=>'my::Function::numeric'});
Context()->flags->set(reduceConstantFunctions=>0);
#
# Don't allow fractional powers (avoids 1/2 power)
# [Could subclass exponentiation to handle that as well]
#
LimitedPowers::OnlyPositiveIntegers();

$expr = "\sqrt{12 x^2}";

$f = Compute("x*sqrt(6)");

Setup: This code is from Davide Cervone (4/25/10). See the discussion [simplifying radical expressions] for more information.

BEGIN_TEXT
Simplify \( $expr \) assuming that \( x \geq 0 \).
Do not enter fractional exponents.
$BR
$BR
\( $expr \) = \{ans_rule(20)\}
END_TEXT

Main Text: The problem text section of the file is as we'd expect.

$showPartialCorrectAnswers = 1;

ANS( $f-> cmp( checker => sub {
  my ($correct,$student,$ans) = @_;
  return 0 if $ans->{isPreview} || $correct != $student;
  #
  #  Get parsed formula for student and correct answers
  #
  $student = $ans->{student_formula};
  $correct = $correct->{original_formula} if defined $correct->{original_formula};
  #
  # check if equal when sqrt's are replaced by 1
  #
  Context()->flags->set(setSqrt => 1);
  delete $correct->{test_values}, $student->{test_values};
  my $OK = ($correct == $student);
  Context()->flags->set(setSqrt => 0);
  #
  Value::Error("Check to see if your answer is simplified.") unless $OK;
  return $OK;
  }, formatStudentAnswer=>"reduced"
  )
);

ENDDOCUMENT();

Answer Evaluation: Use a custom checker to check that the answers are equivalent and that they are still equivalent when sqrt() is replaced by 1 (so the stuff outside the sqrt() is equal in both).

Problem Techniques Index