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

Legend:
Removed from v.5446  
changed lines
  Added in v.5457

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9