[system] / trunk / webwork / system / courseScripts / PGcomplexmacros.pl Repository:
ViewVC logotype

View of /trunk/webwork/system/courseScripts/PGcomplexmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 102 - (download) (as text) (annotate)
Wed Aug 8 02:24:16 2001 UTC (18 years, 2 months ago) by gage
File size: 4352 byte(s)
Minor  changes to the #!/usr/bin/perl line

    1 #!/usr/local/bin/webwork-perl
    2 # This file     is PGcomplexmacros.pl
    3 # This includes the subroutines for the ANS macros, that
    4 # is, macros allowing a more flexible answer checking
    5 ####################################################################
    6 # Copyright @ 1995-2001 The WeBWorK Team
    7 # All Rights Reserved
    8 ####################################################################
    9 #$Id$
   10 
   11 
   12 BEGIN{
   13   be_strict();
   14 
   15 }
   16 # export functions from Complex1.
   17 
   18 foreach my $f (@Complex1::EXPORT) {
   19     #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever --
   20                                                   # the original subroutines are destroyed
   21         next if $f eq 'sqrt';  #exporting the square root caused conflicts with the standard version
   22                                # You can still use Complex1::sqrt to take square root of complex numbers
   23     local($main::string) = qq{
   24        sub main::$f {
   25            &Complex1::$f;
   26        }
   27     };
   28     PG_restricted_eval($main::string);
   29 }
   30 
   31 # You need to add
   32 # sub i();  # to your problem or else to dangerousMacros.pl
   33 # in order to use expressions such as 1 +3*i;
   34 # Without this prototype you would have to write 1+3*i();
   35 # The prototype has to be defined at compile time, but dangerousMacros.pl is complied first.
   36 #Complex1::display_format('cartesian');
   37 
   38 sub polar{
   39   my $z = shift;
   40   my %options = @_;
   41     my $r = rho($z);
   42     my $theta = $z->theta;
   43     my $r_format = ':%0.3f';
   44     my $theta_format = ':%0.3f';
   45     $r_format=":" . $options{r_format} if defined($options{r_format});
   46     $theta_format = ":" . $options{theta_format} if defined($options{theta_format});
   47     "{$r$r_format} e^{i {$theta$theta_format}}";
   48 
   49 }
   50 sub cplx_cmp {
   51   my $correctAns = shift;
   52   my %options = @_;
   53   $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/;
   54   assign_option_aliases( \%options,
   55                 'reltol'    =>      'relTol',
   56     );
   57     set_default_options(\%options,
   58               'tolType'   =>  (defined($options{tol}) ) ? 'absolute' : 'relative',
   59                  # default mode should be relative, to obtain this tol must not be defined
   60               'tol'     =>  $main::numAbsTolDefault,
   61                     'relTol'    =>  $main::numRelPercentTolDefault,
   62             'zeroLevel'   =>  $main::numZeroLevelDefault,
   63             'zeroLevelTol'  =>  $main::numZeroLevelTolDefault,
   64             'format'    =>  $main::numFormatDefault,
   65             'debug'     =>  0,
   66 
   67     );
   68 
   69 
   70   my $ans_eval = sub {
   71                my $in = shift;
   72          my $rh_ans = new AnswerHash;
   73          $rh_ans->{correct_ans} = $correctAns;
   74          unless (defined($in) and $in =~/\S/ ) { #bail on empty answer
   75             $rh_ans->{student_ans} = "error: empty";
   76           return $rh_ans;
   77          }
   78          my($PG_errors,$PG_errors_long);
   79 
   80                  $rh_ans->input($in);
   81 
   82                  $rh_ans->{student_ans}=math_constants($rh_ans->{student_ans});
   83                  $rh_ans->{student_ans} =~ s/e\^/exp /g;  #try to handle exponents
   84                  $rh_ans=check_syntax($rh_ans);
   85                  $rh_ans->{student_ans} =~ s/\bi\b/(i)/g;  #try to keep -i being recognized as a file reference
   86                                                            # and recognized as a function whose output is an imaginary number
   87 
   88 
   89 
   90          warn $rh_ans->pretty_print() if defined($options{debug}) and $options{debug}==1;
   91          ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans});
   92                  $in = $in +0*i;   # force the input to be complex
   93          if ($PG_errors_long) {
   94           $rh_ans->{error}=1;
   95           $rh_ans->{ans_message} = $PG_errors;
   96          } else {
   97            $in->display_format('cartesian') if ref($in) =~/Complex/;
   98            $rh_ans->{student_ans}="$in";
   99            my $permitted_error;
  100           if (defined($options{tolType}) && $options{tolType} eq 'absolute')  {
  101             $permitted_error = $options{tol};
  102           } elsif ( abs($correctAns) <= $options{zeroLevel}) {
  103               $permitted_error = $options{zeroLevelTol};  ## want $tol  to be non zero
  104           } else {
  105               $permitted_error = abs(.01*$options{relTol}*$correctAns); # relative tolerance is given in per cent
  106           }
  107                   $rh_ans->{score} =  (abs($in - $correctAns)<$permitted_error )? 1:0;
  108 
  109                }
  110 
  111                  $rh_ans;
  112                  };
  113     $ans_eval;
  114 }
  115 
  116 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9