# code essentially from Davide Cervone 4/25/10############################# 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+2 if $value && $_[0] == 1; # force sqrt(1) to be incorrectreturn $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();$reducedSqrt = 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;};ANS($ans1->cmp(checker => $reducedSqrt,formatStudentAnswer=>"reduced"));
It might be possible to overwrite the functions for sine and cosine as Davide did for handling square root simplifications (just do this for sine and cosine instead of sqrt):