File Coverage

blib/lib/Math/Calculus/TaylorEquivalent.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             # ########################################################################################
2             # A TAYLOR SERIES EQUIVALENCE OBJECT
3             # Copyright (C) Jonathan Worthington 2005
4             # This module may be used and distributed under the same terms as Perl.
5             # ########################################################################################
6            
7             package Math::Calculus::TaylorEquivalent;
8 1     1   14276 use Math::Calculus::Expression;
  0            
  0            
9             use strict;
10             our $VERSION = '0.1';
11             our @ISA = qw/Math::Calculus::Expression/;
12             our $DEFAULTCOMPTERMS = 5;
13             our $DEFAULTERROR = 0;
14            
15             =head1 NAME
16            
17             Math::Calculus::TaylorEquivalent - Estimating expression equivalence by decomposition
18             into basis functions.
19            
20             =head1 SYNOPSIS
21            
22             use Math::Calculus::TaylorEquivalent;
23            
24             # Create an object.
25             my $exp1 = Math::Calculus::TaylorEquivalent->new;
26             my $exp2 = Math::Calculus::TaylorEquivalent->new;
27            
28             # Set variables and expressions.
29             $exp1->addVariable('x');
30             $exp1->setExpression('(x + 1)*(x - 1)') or die $exp1->getError;
31             $exp2->addVariable('x');
32             $exp2->setExpression('x^2 - 1') or die $exp2->getError;
33            
34             # Check equivalence.
35             my $result = $exp1->taylorEquivalent($exp2, 'x', 0);
36             die $exp1->getError unless defined $result;
37             print $result; # Prints 1
38            
39             # Example where they are not equivalent.
40             $exp2->addVariable('x');
41             $exp2->setExpression('x^2 + 1') or die $exp2->getError;
42            
43             # Check equivalence.
44             my $result = $exp1->taylorEquivalent($exp2, 'x', 0);
45             die $exp1->getError unless defined $result;
46             print $result; # Prints 0
47            
48            
49             =head1 DESCRIPTION
50            
51             This module provides an expression object with a Taylor Equivalent method, which
52             decomposes the expression and another expression into the first N terms of their
53             Taylor series and compares the co-efficients so try and decide whether the expressions
54             are equivalent.
55            
56             It understands expressions containing any of the operators +, -, *, / and ^ (raise to
57             power), bracketed expressions to enable correct precedence and the functions ln,
58             exp, sin, cos, tan, sec, cosec, cot, sinh, cosh, tanh, sech, cosech, coth, asin,
59             acos, atan, asinh, acosh and atanh.
60            
61             =head1 EXPORT
62            
63             None by default.
64            
65             =head1 METHODS
66            
67             =cut
68            
69             # Constructor
70             # ###########
71            
72             =item new
73            
74             $exp = Math::Calculus::TaylorSeries->new;
75            
76             Creates a new instance of the Taylor Series object, which can hold an individual
77             expression.
78            
79             =item addVariable
80            
81             $exp->addVariable('x');
82            
83             Sets a certain named value in the expression as being a variable. A named value must be
84             an alphabetic chracter.
85            
86             =item setExpression
87            
88             $exp->setExpression('x^2 + 5*x);
89            
90             Takes an expression in human-readable form and stores it internally as a tree structure,
91             checking it is a valid expression that the module can understand in the process. Note that
92             the engine is strict about syntax. For example, note above that you must write 5*x and not
93             just 5x. Whitespace is allowed in the expression, but does not have any effect on precedence.
94             If you require control of precedence, use brackets; bracketed expressions will always be
95             evaluated first, as you would normally expect. The module follows the BODMAS precedence
96             convention. Returns undef on failure and a true value on success.
97            
98             =item getExpression
99            
100             $expr = $exp->getExpression;
101            
102             Returns a textaul, human readable representation of the expression that is being stored.
103            
104             =cut
105            
106            
107             # Taylor Equivalent.
108             # ##################
109            
110             =item taylorEquivalent
111            
112             $boolean = $exp1->taylorEquivalent($exp2, $variable, $about);
113             $boolean = $exp1->taylorEquivalent($exp2, $variable, $about, $compTerms);
114             $boolean = $exp1->taylorEquivalent($exp2, $variable, $about, $compTerms, $maxError);
115            
116             Takes the current expression and another expression and calculates the first
117             $compTerms (default 5) terms of their Taylor Series. Tnese terms are then
118             compared, and if the difference between the co-efficients in each is no greater
119             than $maxError (default 0) then it returns true. This suggests that the expressions
120             are equivalent. The Taylor series is taken with respect to the variable $variable
121             and about $about. 0 is often a good value.
122             =cut
123            
124             sub taylorEquivalent {
125             # Get invocant and parameters.
126             my ($self, $exp2, $variable, $about, $compTerms, $error) = @_;
127            
128             # Clear error and traceback.
129             $self->{'error'} = $self->{'traceback'} = '';
130            
131             # Check variable is in the list of variables.
132             unless (grep { $_ eq $variable } @{$self->{'variables'}})
133             {
134             $self->{'error'} = 'Function variable was not declared.';
135             return undef;
136             }
137            
138             # Check number of terms is sane.
139             unless ($compTerms =~ /^\d+$/)
140             {
141             $compTerms = $DEFAULTCOMPTERMS;
142             }
143            
144             # Check error condition is OK or use default.
145             unless ($error =~ /^\d+(?:\.\d+)?$/)
146             {
147             $error = $DEFAULTERROR;
148             }
149            
150             # Check about value is sane.
151             unless ($about =~ /^[\-\d\.]+$/)
152             {
153             $self->{'error'} = 'Attempt to evaluate Taylor series about an invalid value.';
154             return undef;
155             }
156            
157             # Now calculate co-efficients for each expression.
158             my @coeffs1 = $self->taylorSeries_coeffs($variable, $compTerms, $about);
159             my @coeffs2 = $exp2->taylorSeries_coeffs($variable, $compTerms, $about);
160            
161             # Do comparrison.
162             my $result = 1;
163             for (my $i = 0; $i < $compTerms; $i++) {
164             if (abs($coeffs1[$i] - $coeffs2[$i]) > $error) {
165             $result = 0;
166             last;
167             }
168             }
169            
170             # Return the result if no errors.
171             if ($self->{'error'}) {
172             return undef;
173             } else {
174             return $result;
175             }
176             }
177            
178            
179             =item getTraceback
180            
181             $exp->getTraceback;
182            
183             When setExpression and taylorSeries are called, a traceback is generated to describe
184             what these functions did. If an error occurs, this traceback can be extremely useful
185             in helping track down the source of the error.
186            
187             =item getError
188            
189             $exp->getError;
190            
191             When any method other than getTraceback is called, the error message stored is cleared, and
192             then any errors that occur during the execution of the method are stored. If failure occurs,
193             call this method to get a textual representation of the error.
194            
195             =head1 SEE ALSO
196            
197             The author of this module has a website at L, which has
198             the latest news about the module and a web-based frontend to allow you to test the module
199             out for yourself.
200            
201             =head1 AUTHOR
202            
203             Jonathan Worthington, Ejonathan@jwcs.netE
204            
205             =head1 COPYRIGHT AND LICENSE
206            
207             Copyright (C) 2004 by Jonathan Worthington
208            
209             This library is free software; you can redistribute it and/or modify
210             it under the same terms as Perl itself, either Perl version 5.8.1 or,
211             at your option, any later version of Perl 5 you may have available.
212            
213             =cut
214            
215            
216             # Factorial routine.
217             sub fact {
218             return $_[1] == 0 ? 1 : $_[1] * $_[0]->fact($_[1] - 1);
219             }
220            
221            
222             1;