[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 321 - (download) (as text) (annotate)
Thu May 30 19:21:16 2002 UTC (17 years, 5 months ago) by gage
File size: 4857 byte(s)
Fixed bug in PGcomplexmacros.pl having to do with the lack of a
=cut line in the pod documentation.

The loadMacros eval doesn't fail gracefully when this kind of mistake
occurs.  More work needs to be done on loadMacros.

Also changed the _PGcomplexmacros_init subroutine to include the
exporting of variables from Complex.

I think this is what it was supposed to do in any case, but perhaps not.
When using the forking process it was only necessary to export the
variables in the parent.

    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 
   13 
   14 
   15 
   16 
   17 
   18 
   19 =head1 NAME
   20 
   21   Macros for complex numbers for the PG language
   22 
   23 =head1 SYNPOSIS
   24 
   25 
   26 
   27 =head1 DESCRIPTION
   28 
   29 =cut
   30 
   31 
   32 BEGIN{
   33   be_strict();
   34 
   35 }
   36 sub _PGcomplexmacros_init {
   37 
   38 # export functions from Complex1.
   39 
   40   foreach my $f (@Complex1::EXPORT) {
   41       #PG_restricted_eval("\*$f = \*Complex1::$f"); # this is too clever --
   42                               # the original subroutines are destroyed
   43       next if $f eq 'sqrt';  #exporting the square root caused conflicts with the standard version
   44                    # You can still use Complex1::sqrt to take square root of complex numbers
   45       next if $f eq 'log';  #exporting loq caused conflicts with the standard version
   46                    # You can still use Complex1::log to take square root of complex numbers
   47 
   48       my $string = qq{
   49          sub main::$f {
   50            &Complex1::$f;
   51          }
   52       };
   53       PG_restricted_eval($string);
   54   }
   55 }
   56 
   57 # You need to add
   58 # sub i();  # to your problem or else to dangerousMacros.pl
   59 # in order to use expressions such as 1 +3*i;
   60 # Without this prototype you would have to write 1+3*i();
   61 # The prototype has to be defined at compile time, but dangerousMacros.pl is complied first.
   62 #Complex1::display_format('cartesian');
   63 
   64 
   65 =head4 polar
   66 
   67   Usage polar($complex_number,r_format=>"%0.3f",theta_format=>"%0.3f")
   68 
   69   Output is text displaying the complex number in "e to the i theta" form.  The
   70   formats for the argument theta is determined by the option C<theta_format> and the
   71   format for the modulus is determined by the C<r_format> option.
   72 
   73 =cut
   74 
   75 
   76 sub polar{
   77   my $z = shift;
   78   my %options = @_;
   79   set_default_options(\%options,  r_format  => ':%0.3f',
   80                   theta_format  => ':%0.3f',
   81   );
   82   my $r = rho($z);
   83   my $theta = $z->theta;
   84   my $r_format=":" . $options{r_format};
   85   my $theta_format = ":" . $options{theta_format});
   86   "{$r$r_format} e^{i {$theta$theta_format}}";
   87 
   88 }
   89 
   90 sub cplx_cmp {
   91   my $correctAns = shift;
   92   my %options = @_;
   93   $correctAns = cplx($correctAns,0) unless ref($correctAns) =~/Complex/;
   94   assign_option_aliases( \%options,
   95                 'reltol'    =>      'relTol',
   96     );
   97     set_default_options(\%options,
   98           'tolType'   =>  (defined($options{tol}) ) ? 'absolute' : 'relative',
   99           # default mode should be relative, to obtain this tol must not be defined
  100       'tol'     =>  $main::numAbsTolDefault,
  101                   'relTol'    =>  $main::numRelPercentTolDefault,
  102       'zeroLevel'   =>  $main::numZeroLevelDefault,
  103       'zeroLevelTol'    =>  $main::numZeroLevelTolDefault,
  104       'format'    =>  $main::numFormatDefault,
  105       'debug'     =>  0,
  106 
  107     );
  108 
  109 
  110   my $ans_eval = sub {
  111                my $in = shift;
  112       my $rh_ans = new AnswerHash;
  113          $rh_ans->{correct_ans} = $correctAns;
  114          unless (defined($in) and $in =~/\S/ ) { #bail on empty answer
  115             $rh_ans->{student_ans} = "error: empty";
  116           return $rh_ans;
  117          }
  118          my($PG_errors,$PG_errors_long);
  119 
  120                  $rh_ans->input($in);
  121 
  122                  $rh_ans->{student_ans}=math_constants($rh_ans->{student_ans});
  123                  $rh_ans->{student_ans} =~ s/e\^/exp /g;  #try to handle exponents
  124                  $rh_ans=check_syntax($rh_ans);
  125                  $rh_ans->{student_ans} =~ s/\bi\b/(i)/g;  #try to keep -i being recognized as a file reference
  126                                                            # and recognized as a function whose output is an imaginary number
  127 
  128 
  129 
  130          warn $rh_ans->pretty_print() if defined($options{debug}) and $options{debug}==1;
  131          ($in,$PG_errors,$PG_errors_long) = PG_restricted_eval($rh_ans->{student_ans});
  132                  $in = $in +0*i;   # force the input to be complex
  133          if ($PG_errors_long) {
  134           $rh_ans->{error}=1;
  135           $rh_ans->{ans_message} = $PG_errors;
  136          } else {
  137            $in->display_format('cartesian') if ref($in) =~/Complex/;
  138            $rh_ans->{student_ans}="$in";
  139            my $permitted_error;
  140           if (defined($options{tolType}) && $options{tolType} eq 'absolute')  {
  141             $permitted_error = $options{tol};
  142           } elsif ( abs($correctAns) <= $options{zeroLevel}) {
  143               $permitted_error = $options{zeroLevelTol};  ## want $tol  to be non zero
  144           } else {
  145               $permitted_error = abs(.01*$options{relTol}*$correctAns); # relative tolerance is given in per cent
  146           }
  147                   $rh_ans->{score} =  (abs($in - $correctAns)<$permitted_error )? 1:0;
  148 
  149                }
  150 
  151                  $rh_ans;
  152                  };
  153     $ans_eval;
  154 }
  155 
  156 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9