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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6539 - (view) (download) (as text)

1 : sh002i 5556 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4 : gage 6262 # $CVSHeader: pg/macros/contextLimitedPolynomial.pl,v 1.24 2010/04/01 00:21:45 dpvc Exp $
5 : sh002i 5556 #
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 : sh002i 5551 =head1 NAME
18 : gage 4997
19 : sh002i 5551 contextLimitedPolynomial.pl - Allow only entry of polynomials.
20 : dpvc 3294
21 : sh002i 5551 =head1 DESCRIPTION
22 : dpvc 3294
23 : sh002i 5551 Implements a context in which students can only enter (expanded)
24 :     polynomials (i.e., sums of multiples of powers of x).
25 : gage 4997
26 : sh002i 5551 Select the context using:
27 : dpvc 3294
28 : sh002i 5555 Context("LimitedPolynomial");
29 : sh002i 5551
30 :     If you set the "singlePowers" flag, then only one monomial of each
31 :     degree can be included in the polynomial:
32 :    
33 : sh002i 5555 Context("LimitedPolynomial")->flags->set(singlePowers=>1);
34 : sh002i 5551
35 :     There is also a strict limited context that does not allow
36 :     operations even within the coefficients. Select it using:
37 :    
38 : sh002i 5555 Context("LimitedPolynomial-Strict");
39 : sh002i 5551
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 : gage 6539
47 :     Uses library module lib/LimitedPolynomial.pm
48 :    
49 : dpvc 5051 =cut
50 :    
51 : sh002i 5551 loadMacros("MathObjects.pl");
52 :    
53 : gage 6539 sub _contextLimitedPolynomial_init{
54 :     Init();
55 :     } # don't load it again
56 : sh002i 5551
57 : dpvc 5392 sub Init {
58 :     #
59 :     # Build the new context that calls the
60 :     # above classes rather than the usual ones
61 :     #
62 : dpvc 3294
63 : dpvc 5392 my $context = $main::context{LimitedPolynomial} = Parser::Context->getCopy("Numeric");
64 : dpvc 5441 $context->{name} = "LimitedPolynomial";
65 : dpvc 5392 $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 : dpvc 3294 );
80 : dpvc 5392 #
81 :     # Remove these operators and functions
82 :     #
83 :     $context->lists->set(
84 :     AbsoluteValue => {class => 'LimitedPolynomial::List::AbsoluteValue'},
85 : dpvc 3294 );
86 : dpvc 5392 $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 : dpvc 5484 #
104 : dpvc 5580 # Don't convert -ax-b to -(ax+b), or -ax+b to b-ax, etc.
105 : dpvc 5484 #
106 : dpvc 5580 $context->reduction->set("(-x)-y"=>0,"(-x)+y"=>0);
107 : dpvc 5392
108 : dpvc 5518 #
109 :     # A context where coefficients can't include operations
110 :     #
111 :     $context = $main::context{"LimitedPolynomial-Strict"} = $context->copy;
112 : dpvc 5575 $context->flags->set(strictCoefficients=>1, singlePowers=>1, reduceConstants=>0);
113 : dpvc 5518 $context->functions->disable("All"); # can be re-enabled if needed
114 :    
115 : dpvc 5392 main::Context("LimitedPolynomial"); ### FIXME: probably should require author to set this explicitly
116 : dpvc 3294 }
117 : dpvc 5373 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9