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

Diff of /trunk/pg/macros/contextPiecewiseFunction.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 5550 Revision 5551
1=head1 NAME
2
3contextPiecewiseFunction.pl - Allow usage of piecewise functions.
4
5=head1 DESCRIPTION
6
7This file implements a context in which piecewise-defined functions
8can be specified by students and problem authors. To use it, add
9
10 loadMacros("contextPiecewiseFunction.pl");
11
12and then use
13
14 Context("PiecewiseFuntion");
15
16to select the context for piecewise functions. There are several
17ways to produce a piecewise function. For example:
18
19 $f = Compute("x if x >= 0 else -x");
20 $f = Compute("x if x >= 0 else -x if x < 0");
21 $f = Formula("x+1 if x > 2 else 4 if x = 2 else 1-x");
22 $f = PiecewiseFunction("x^2 if 1 < x <= 2 else 2x+1");
23 $f = PiecewiseFunction("1 < x <= 2" => "x^2", "2x+1");
24 $f = PiecewiseFunction("(1,2]" => "x^2", "2x+1");
25 $f = PiecewiseFunction(Interval("(1,2]") => "x^2", "2x+1");
26
27You can use either Compute() or Formula() interchangeably to
28convert a string containing "if" and "else" to the corresponding
29PiecewiseFunction. The PiecewiseFunction() constructor can
30also do this, or you can pass it a list of interval=>formula
31pairs that specify the various branches. If there is an
32unpaired final formula, it represents the "otherwise" portion
33of the function (the formula to use of the input is not in
34any of the given intervals).
35
36Note that you can use Inveral, Set, or Union objects in place of
37the intervals in the specification of a piecewise function.
38
39The PiecewiseFunction object TeXifies using a LaTeX "cases"
40environment, so you can use these objects to produce nice
41output even if you are not asking a student to enter one.
42For example:
43
44 Context("PiecewiseFunction");
45
46 $f = Formula("1-x if x > 0 else 4 if x = 0 else 1+x if x < 0");
47 $a = random(-2,2,.1);
48
49 Context()->texStrings;
50 BEGIN_TEXT
51 Suppose \(f(x)=$f\). Then \(f($a)\) = \{ans_rule(20)\}.
52 END_TEXT
53 Context()->normalStrings;
54
55 ANS($f->eval(x=>$a)->cmp);
56
57=cut
58
1loadMacros("MathObjects.pl"); 59loadMacros("MathObjects.pl");
2loadMacros("contextInequalities.pl"); 60loadMacros("contextInequalities.pl");
3 61
4sub _contextPiecewiseFunction_init {PiecewiseFunction::Init()} 62sub _contextPiecewiseFunction_init {PiecewiseFunction::Init()}
5
6=head1 Context("PiecewiseFunction");
7
8 ######################################################################
9 #
10 # This file implements a context in which piecewise-defined functions
11 # can be specified by students and problem authors. To use it, add
12 #
13 # loadMacros("contextPiecewieFunction.pl");
14 #
15 # and then use
16 #
17 # Context("PiecewiseFuntion");
18 #
19 # to select the context for piecewise functions. There are several
20 # ways to produce a piecewise function. For example:
21 #
22 # $f = Compute("x if x >= 0 else -x");
23 # $f = Compute("x if x >= 0 else -x if x < 0");
24 # $f = Formula("x+1 if x > 2 else 4 if x = 2 else 1-x");
25 # $f = PiecewiseFunction("x^2 if 1 < x <= 2 else 2x+1");
26 # $f = PiecewiseFunction("1 < x <= 2" => "x^2", "2x+1");
27 # $f = PiecewiseFunction("(1,2]" => "x^2", "2x+1");
28 # $f = PiecewiseFunction(Interval("(1,2]") => "x^2", "2x+1");
29 #
30 # You can use either Compute() or Formula() interchangeably to
31 # convert a string containing "if" and "else" to the corresponding
32 # PiecewiseFunction. The PiecewiseFunction() constructor can
33 # also do this, or you can pass it a list of interval=>formula
34 # pairs that specify the various branches. If there is an
35 # unpaired final formula, it represents the "otherwise" portion
36 # of the function (the formula to use of the input is not in
37 # any of the given intervals).
38 #
39 # Note that you can use Inveral, Set, or Union objects in place of
40 # the intervals in the specification of a piecewise function.
41 #
42 # The PiecewiseFunction object TeXifies using a LaTeX "cases"
43 # environment, so you can use these objects to produce nice
44 # output even if you are not asking a student to enter one.
45 # For example:
46 #
47 # Context("PiecewiseFunction");
48 #
49 # $f = Formula("1-x if x > 0 else 4 if x = 0 else 1+x if x < 0");
50 # $a = random(-2,2,.1);
51 #
52 # Context()->texStrings;
53 # BEGIN_TEXT
54 # Suppose \(f(x)=$f\). Then \(f($a)\) = \{ans_rule(20)\}.
55 # END_TEXT
56 # Context()->normalStrings;
57 #
58 # ANS($f->eval(x=>$a)->cmp);
59 #
60 ######################################################################
61
62=cut
63 63
64package PiecewiseFunction; 64package PiecewiseFunction;
65 65
66# 66#
67# Create the needed context and the constructor function 67# Create the needed context and the constructor function

Legend:
Removed from v.5550  
changed lines
  Added in v.5551

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9