Macros for complex numbers for the PG language
# This subroutine compares complex numbers.
# Available prefilters include:
# each of these are called by cplx_cmp( answer, mode => '(prefilter name)' )
# 'std' The standard comparison method for complex numbers. This option
it the default
# and works with any combination of cartesian numbers, polar numbers,
and
# functions. The default display method is cartesian, for all methods,
but if
# the student answer is polar, even in part, then their answer will
be displayed
# that way.
# 'strict_polar' This is still under developement. The idea is to check to
make sure that there
# only a single term in front of the e and after it... but the method
does not
# check to make sure that the i is in the exponent, nor does it handle
cases
# where the polar has e** coefficients.
# 'strict_num_cartesian' This prefilter allows only complex numbers of the form
"a+bi" where a and b
# are strictly numbers.
# 'strict_num_polar' This prefilter allows only complex numbers of the form "ae^(bi)"
where a and b
# are strictly numbers.
# 'strict' This is a combination of strict_num_cartesian and strict_num_polar,
so it
# allows complex numbers of either the form "a+bi" or "ae^(bi)" where
a and b
# are strictly numbers.
# This is a filter: it accepts and returns an AnswerHash object.
#
# Usage: compare_cplx(ans_hash, %options)
#
# Compares two complex numbers by comparing their real and imaginary parts
#
# Checks a comma separated string of items against an array of evaluators.
# For example this is useful for checking all of the complex roots of an equation.
# Each student answer must be evaluated as correct by a DISTINCT answer evalutor.
#
# This answer checker will only work reliably if each answer checker corresponds
# to a distinct correct answer. For example if one answer checker requires
# any positive number, and the second requires the answer 1, then 1,2 might
# be judged incorrect since 1, satisifes the first answer checker, but 2 doesn't
# satisfy the second. 2,1 would work however. Avoid this type of use!!
#
# Including backtracking to fit the answers as best possible to each answer evaluator
# in the best possible way, is beyond the ambitions of this evaluator.
# for checking the form of a number or of the <student_ans> field in an answer
hash
# This subroutine takes in a string, which is a mathematical expresion, and determines
whether or not
# it is a single term. This is accoplished using a stack. Open parenthesis pluses
and minuses are all
# added onto the stack, and when a closed parenthesis is reached, the stack is
popped untill the open
# parenthesis is found. If the original was a single term, the stack should be
empty after
# evaluation. If there is anything left ( + or - ) then false is returned.
# Of course, the unary operator "-" must be handled... if it is a unary operator,
and not a regular -
# the only place it could occur unambiguously without being surrounded by parenthesis,
is the very
# first position. So that case is checked before the loop begins.
=cut
sub single_term{
my $term = shift;
my @stack;
$term = reverse $term;
if( length $term >= 1 )
{
my $temp = chop $term;
if( $temp ne "-" ){ $term .= $temp; }
}
while( length $term >= 1 ){
my $character = chop $term;
if( $character eq "+" || $character eq "-" || $character eq "(" ){
push @stack, $character;
}elsif( $character eq ")" ){
while( pop @stack ne "(" ){}
}
}
if( scalar @stack == 0 ){ return 1;}else{ return 0;}
}
# changes default to display as a polar
sub fix_for_polar_display{
my ($rh_ans, %options) = @_;
if( ref( $rh_ans->{student_ans} ) =~ /Complex/ && $rh_ans->{answer_form} eq 'polar'
){
$rh_ans->{student_ans}->display_format( 'polar');
## these lines of code have the polar displayed as re^(theta) instead of
[rho,theta]
$rh_ans->{student_ans} =~ s/,/*e^\(/;
$rh_ans->{student_ans} =~ s/\[//;
$rh_ans->{student_ans} =~ s/\]/i\)/;
}
$rh_ans;
}
# this does not seem to be in use, so I'm commenting it out. Mike Gage 6/27/05
# sub cplx_cmp2 {
####.............###########
# }
# this does not seem to be in use, so I'm commenting it out. Mike Gage 6/27/05
# sub cplx_cmp_mult {
####.............###########
# }
# this does not seem to be in use, so I'm commenting it out. Mike Gage 6/27/05
# sub answer_mult{
####.............###########
# }
#
# sub multi_cmp_old{
####.............###########
# }
# this does not seem to be in use, so I'm commenting it out. Mike Gage 6/27/05
# sub mult_cmp{
####.............###########
# }
1;
File path = /ww/webwork/pg/macros/PGcomplexmacros.pl
<| Post or View Comments |>
|