[system] / trunk / pg / macros / contextIntegerFunctions.pl Repository:
ViewVC logotype

View of /trunk/pg/macros/contextIntegerFunctions.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5551 - (download) (as text) (annotate)
Tue Oct 2 20:48:05 2007 UTC (12 years, 2 months ago) by sh002i
File size: 1645 byte(s)
improved formatting for docs -- these were in pod sections but were all
formatted as verbatim sections, and i moved them into normal paragraphs,
lists, etc. should make things more readable from the web.

    1 =head1 NAME
    2 
    3 contextIntegerFunctions.pl - adds integer related functions C(n,r) and P(n,r).
    4 
    5 =head1 DESCRIPTION
    6 
    7 This is a Parser context that adds integer related functions C(n,r)
    8 and P(n,r).  They can be used by the problem author and also by
    9 students if the answer checking is done by Parser.  The latter is
   10 the main purpose of this file.
   11 
   12 B<Note:> by default, webwork problems do not permit students to use
   13 C(n,r) and P(n,r) functions.  Problems which do permit this
   14 should alert the student in their text.
   15 
   16 =head1 USAGE
   17 
   18     Context("IntegerFunctions")
   19 
   20     $b = random(2, 5); $a = $b+random(0, 5);
   21     $c = C($a, $b);
   22     ANS(Compute("P($a, $b)")->cmp);
   23 
   24 B<Note:> If the context is set to something else, such as Numeric, it
   25 can be set back with Context("IntegerFunctions").
   26 
   27 =cut
   28 
   29 loadMacros('MathObjects.pl');
   30 
   31 sub _contextIntegerFunctions_init {context::IntegerFunctions2::Init()}; # don't reload this file
   32 
   33 package context::IntegerFunction2;
   34 our @ISA = qw(Parser::Function::numeric2); # checks for 2 numeric inputs
   35 
   36 sub C {
   37   shift; my ($n,$r) = @_; my $C = 1;
   38   return (0) if($r>$n);
   39   $r = $n-$r if ($r > $n-$r); # find the smaller of the two
   40   for (1..$r) {$C = ($C*($n-$_+1))/$_}
   41   return $C
   42 }
   43 
   44 sub P {
   45   shift; my ($n,$r) = @_; my $P = 1;
   46   return (0) if($r>$n);
   47   for (1..$r) {$P *= ($n-$_+1)}
   48   return $P
   49 }
   50 
   51 sub Init {
   52   my $context = $main::context{IntegerFunctions} = Parser::Context->getCopy("Numeric");
   53   $context->{name} = "IntegerFunctions";
   54 
   55   $context->functions->add(
   56     C => {class => 'context::IntegerFunction2'},
   57     P => {class => 'context::IntegerFunction2'},
   58   );
   59 
   60   main::Context("IntegerFunctions");
   61 }
   62 
   63 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9