File Coverage

blib/lib/Math/SymbolicX/NoSimplification.pm
Criterion Covered Total %
statement 24 24 100.0
branch n/a
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 36 36 100.0


line stmt bran cond sub pod time code
1             package Math::SymbolicX::NoSimplification;
2              
3 2     2   165848 use 5.006;
  2         11  
  2         74  
4 2     2   10 use strict;
  2         4  
  2         56  
5 2     2   9 use warnings;
  2         5  
  2         65  
6              
7             # We don't need exports
8 2     2   910 use Math::Symbolic qw();
  2         170120  
  2         337  
9              
10             # But we might export ourselves
11             require Exporter;
12             our @ISA = qw(Exporter);
13             our %EXPORT_TAGS = ( 'all' => [ qw(
14             dont_simplify
15             do_simplify
16             ) ] );
17             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
18             our @EXPORT = qw();
19              
20             our $VERSION = '1.01';
21              
22             # This is what we do instead of simplifying: Cloning
23             sub _Minimum_Simplification_Sub {
24             # Minimum simplification method clones.
25 2     2   4697 return $_[0]->new();
26             };
27              
28             # This is where we save the simplification routines for
29             # later reinstallation using 'do_simplify()'
30             sub _Simplification_Sub_Cache {}
31             {
32             # no warnings since we're redefining the simplify routine.
33             # It is sufficient to redefine the one in ::Operator since that
34             # is the only one that does anything but cloning.
35 2     2   17 no warnings;
  2         4  
  2         135  
36             *_Simplification_Sub_Cache = \&Math::Symbolic::Operator::simplify;
37             }
38              
39             # A call to this will replace the simplify() routine in M::S::Operator with
40             # one that just clones (see above)
41             sub dont_simplify {
42 2     2   8 no warnings;
  2         4  
  2         92  
43 3     3 1 2635 *Math::Symbolic::Operator::simplify =
44             \&Math::SymbolicX::NoSimplification::_Minimum_Simplification_Sub;
45             }
46              
47             # A call to this routine will undo all the damage dont_simplify() may have
48             # done by restoring the simplification routine in M::S::Operator from the
49             # backup in this module's &Simplification_Sub_Cache.
50             sub do_simplify {
51 2     2   7 no warnings;
  2         4  
  2         138  
52 1     1 1 776 *Math::Symbolic::Operator::simplify = \&Math::SymbolicX::NoSimplification::_Simplification_Sub_Cache;
53             }
54              
55             # By default, if you load this module, we don't simplify.
56             dont_simplify();
57              
58             1;
59             __END__
60              
61             =head1 NAME
62              
63             Math::SymbolicX::NoSimplification - Turn off Math::Symbolic simplification
64              
65             =head1 SYNOPSIS
66              
67             use Math::SymbolicX::NoSimplification qw(:all);
68             # ... code that uses Math::Symbolic ...
69             # Won't use the builtin simplification routines.
70             # ...
71             do_simplify();
72             # ... code that uses Math::Symbolic ...
73             # Will use the builtin simplification routines.
74             # ...
75             dont_simplify();
76             # ... you get the idea ...
77              
78             =head1 DESCRIPTION
79              
80             This module offers facilities to turn off the builtin Math::Symbolic
81             simplification routines and replace them with routines that just clone
82             the objects. You may want to do this in cases where the simplification
83             routines fail to simplify the Math::Symbolic trees and waste a lot of
84             CPU time. (For example, calculating the first order Taylor polynomial of
85             a moderately complex test function was sped up by 100% on my machine.)
86              
87             A word of caution, however: If you turn off the simplification routines,
88             some procedures may produce very, very large trees. One such procedure
89             would be the consecutive application of many derivatives to a product
90             without intermediate simplification. This would yield exponential
91             growth of nodes. (And may, in fact, still do if you keep the simplification
92             heuristics turned on because most expressions cannot be simplified
93             significantly.)
94              
95             =head2 USAGE
96              
97             Just load the module to turn off simplification. To turn it back on, you
98             can call C<Math::SymbolicX::NoSimplification->do_simplify()> and to
99             turn it off again, you may call
100             C<Math::SymbolicX::NoSimplification->do_simplify()>. Since the module's name
101             is quite long, you may choose to import C<do_simplify()> and/or
102             C<dont_simplify()> into your namespace using standard C<Exporter> semantics.
103             See below.
104              
105             =head2 CLASS METHODS
106              
107             =over 2
108              
109             =item do_simplify
110              
111             Turn simplification back on.
112              
113             =item dont_simplify
114              
115             Turn simplification off.
116              
117             =back
118              
119             =head2 EXPORT
120              
121             None by default, but you may choose to import either the routines
122             C<do_simplify()> and/or C<dont_simplify()> or both by using the
123             C<:all> exporter group. See also: L<Exporter>
124              
125              
126             =head1 AUTHOR
127              
128             Steffen Mueller, E<lt>smueller@cpan.orgE<gt>
129              
130             Please send feedback, bug reports, and support requests to the Math::Symbolic
131             support mailing list:
132             math-symbolic-support at lists dot sourceforge dot net. Please
133             consider letting us know how you use Math::Symbolic. Thank you.
134              
135             If you're interested in helping with the development or extending the
136             module's functionality, please contact the developers' mailing list:
137             math-symbolic-develop at lists dot sourceforge dot net.
138              
139             =head1 COPYRIGHT AND LICENSE
140              
141             Copyright (C) 2005-2006 Steffen Mueller
142              
143             This library is free software; you can redistribute it and/or modify
144             it under the same terms as Perl itself.
145              
146             =head1 SEE ALSO
147              
148             New versions of this module can be found on
149             http://steffen-mueller.net or CPAN.
150              
151             L<Math::Symbolic>,
152              
153             =cut