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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6539 - (download) (as text) (annotate)
Sat Nov 20 23:47:38 2010 UTC (9 years, 2 months ago) by gage
File size: 4281 byte(s)
moving code from contextLimitedPolynomial.pl to LimitedPolynomial.pm to avoid @ISA   error.



    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/contextLimitedPolynomial.pl,v 1.24 2010/04/01 00:21:45 dpvc Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 contextLimitedPolynomial.pl - Allow only entry of polynomials.
   20 
   21 =head1 DESCRIPTION
   22 
   23 Implements a context in which students can only enter (expanded)
   24 polynomials (i.e., sums of multiples of powers of x).
   25 
   26 Select the context using:
   27 
   28   Context("LimitedPolynomial");
   29 
   30 If you set the "singlePowers" flag, then only one monomial of each
   31 degree can be included in the polynomial:
   32 
   33   Context("LimitedPolynomial")->flags->set(singlePowers=>1);
   34 
   35 There is also a strict limited context that does not allow
   36 operations even within the coefficients.  Select it using:
   37 
   38   Context("LimitedPolynomial-Strict");
   39 
   40 In addition to disallowing operations within the coefficients,
   41 this context does not reduce constant operations (since they are
   42 not allowed), and sets the singlePowers flag automatically.  In
   43 addition, it disables all the functions, though they can be
   44 re-enabled, if needed.
   45 
   46 
   47 Uses library module   lib/LimitedPolynomial.pm
   48 
   49 =cut
   50 
   51 loadMacros("MathObjects.pl");
   52 
   53 sub _contextLimitedPolynomial_init{
   54   Init();
   55 } # don't load it again
   56 
   57 sub Init {
   58   #
   59   #  Build the new context that calls the
   60   #  above classes rather than the usual ones
   61   #
   62 
   63   my $context = $main::context{LimitedPolynomial} = Parser::Context->getCopy("Numeric");
   64   $context->{name} = "LimitedPolynomial";
   65   $context->operators->set(
   66      '+' => {class => 'LimitedPolynomial::BOP::add'},
   67      '-' => {class => 'LimitedPolynomial::BOP::subtract'},
   68      '*' => {class => 'LimitedPolynomial::BOP::multiply'},
   69     '* ' => {class => 'LimitedPolynomial::BOP::multiply'},
   70     ' *' => {class => 'LimitedPolynomial::BOP::multiply'},
   71      ' ' => {class => 'LimitedPolynomial::BOP::multiply'},
   72      '/' => {class => 'LimitedPolynomial::BOP::divide'},
   73     ' /' => {class => 'LimitedPolynomial::BOP::divide'},
   74     '/ ' => {class => 'LimitedPolynomial::BOP::divide'},
   75      '^' => {class => 'LimitedPolynomial::BOP::power'},
   76     '**' => {class => 'LimitedPolynomial::BOP::power'},
   77     'u+' => {class => 'LimitedPolynomial::UOP::plus'},
   78     'u-' => {class => 'LimitedPolynomial::UOP::minus'},
   79   );
   80   #
   81   #  Remove these operators and functions
   82   #
   83   $context->lists->set(
   84     AbsoluteValue => {class => 'LimitedPolynomial::List::AbsoluteValue'},
   85   );
   86   $context->operators->undefine('_','!','U');
   87   $context->functions->disable("atan2");
   88   #
   89   #  Hook into the numeric, trig, and hyperbolic functions
   90   #
   91   foreach ('ln','log','log10','exp','sqrt','abs','int','sgn') {
   92     $context->functions->set(
   93       "$_"=>{class => 'LimitedPolynomial::Function::numeric'}
   94     );
   95   }
   96   foreach ('sin','cos','tan','sec','csc','cot',
   97            'asin','acos','atan','asec','acsc','acot') {
   98     $context->functions->set(
   99        "$_"=>{class => 'LimitedPolynomial::Function::trig'},
  100        "${_}h"=>{class => 'LimitedPolynomial::Function::hyperbolic'}
  101     );
  102   }
  103   #
  104   #  Don't convert -ax-b to -(ax+b), or -ax+b to b-ax, etc.
  105   #
  106   $context->reduction->set("(-x)-y"=>0,"(-x)+y"=>0);
  107 
  108   #
  109   #  A context where coefficients can't include operations
  110   #
  111   $context = $main::context{"LimitedPolynomial-Strict"} = $context->copy;
  112   $context->flags->set(strictCoefficients=>1, singlePowers=>1, reduceConstants=>0);
  113   $context->functions->disable("All");  # can be re-enabled if needed
  114 
  115   main::Context("LimitedPolynomial");  ### FIXME:  probably should require author to set this explicitly
  116 }
  117 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9