Symbolic/Compiler.pm

filter: Statementstmt 92.2% 71 / 77 Branchbran 68.1% 15 / 22 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 85.8% 103 / 120 CC 16 SCAR 17.1
CC
16
Cov
86%
compile_to_code
9.3
_rec_ms_to_sub
6.3
_compile_sub
3.1
CRAP 5.5
« Symbolic/VectorCalculus.pm ↑ Summary Symbolic/MiscAlgebra.pm »
1 =encoding utf8
2  
3 =head1 NAME
4  
5 Math::Symbolic::Compiler - Compile Math::Symbolic trees to Perl code
6  
7 =head1 SYNOPSIS
8  
9 use Math::Symbolic::Compiler;
10
11 # A tree to compile
12 my $tree = Math::Symbolic->parse_from_string('a^2 + b * c * 2');
13
14 # The Math::Symbolic::Variable 'a' will be evaluated to $_[1], etc.
15 my $vars = [qw(b a c)];
16
17 my ($closure, $code, $trees) =
18 Math::Symbolic::Compiler->compile($tree, $vars);
19
20 print $closure->(2, 3, 5); # (b, a, c)
21 # prints 29 (= 3^2 + 2 * 5 * 2)
22
23 # or:
24 ($closure, $trees) =
25 Math::Symbolic::Compiler->compile_to_sub($tree, $vars);
26
27 ($code, $trees) = Math::Symbolic::Compiler->compile_to_code($tree, $vars);
28  
29 =head1 DESCRIPTION
30  
31 This module allows one to compile Math::Symbolic trees to Perl code and/or
32 anonymous subroutines whose arguments will be positionally mapped to the
33 variables of the compiled Math::Symbolic tree.
34  
35 The reason you'd want to do this is that evaluating a Math::Symbolic tree to
36 its numeric value is extremely slow. So is compiling, but once you've done all
37 necessary symbolic calculations, you can take advantage of the speed gain
38 of invoking a closure instead of evaluating a tree.
39  
40 =head2 UNCOMPILED LEFTOVER TREES
41  
42 Not all, however, is well in the land of compiled Math::Symbolic trees.
43 There may occasionally be trees that cannot be compiled (such as a derivative)
44 which need to be included into the code as trees. These trees will be
45 returned in a referenced array by the compile*() methods. The closures
46 will have access to
47 the required trees as a special variable '@_TREES inside the closure's scope,
48 so you need not worry about them in that case. But if you plan to use the
49 generated code itself, you need to supply an array named @_TREES that
50 contains the trees as returned by the compile*() methods in the scope of
51 the eval() you evaluate the code with.
52  
53 Note that you give away all performance benefits compiling the tree might have
54 if the closure contains uncompiled trees. You can tell there are any by
55 checking the length of the referenced array that contains the trees. If it's
56 0, then there are no trees left to worry about.
57  
58 =head2 AVOIDING LEFTOVER TREES
59  
60 In most cases, this is pretty simple. Just apply all derivatives in the tree
61 to make sure that there are none left in the tree. As of version 0.130, there
62 is no operator except derivatives that cannot be compiled. There may, however,
63 be some operators you cannot get rid of this easily some time in the future.
64 If you have problems getting a tree to compile, try using the means of
65 simplification provided by Math::Symbolic::* to get a simpler tree for
66 compilation.
67  
68 =head2 EXPORT
69  
70 None by default, but you may choose to import the compile(), compile_to_sub(),
71 and compile_to_code() subroutines to your namespace using the standard
72 Exporter semantics including the ':all' tag.
73  
74 =head1 SUBROUTINES
75  
76 =cut
77  
78 package Math::Symbolic::Compiler;
79  
80 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 23 our @ISA = qw(Exporter);
87  
88 23 our %EXPORT_TAGS = (
89 'all' => [
90 qw(
91 compile
92 compile_to_sub
93 compile_to_code
94 )
95 ]
96 );
97  
98 23 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
99  
100 23 our @EXPORT = qw();
101  
102 23 our $VERSION = '0.613';
103  
104 =head2 ($code, $trees) = compile_to_code($tree, $vars)
105  
106 The compile_to_code() class method takes one mandatory argument which is
107 the Math::Symbolic tree to be compiled. Second argument is optional
108 and an array reference to an array of variable mappings.
109 See L<VARIABLE PASSING STYLES> for details on how this works.
110  
111 compile_to_code() returns a string and an array reference. The string
112 contains the compiled Perl code that uses the values stored in @_ as described
113 in the section on positional variable passing. It also accesses a special
114 variable @_TREES if there were any sub-trees (inside the tree that has been
115 compiled) that were impossible to compile. The array reference returned by this
116 method contains any of the aforementioned trees that failed to compile.
117  
118 If there are any such trees that did not compile, you may put them into the
119 @_TREES variable in scope of the eval() that evaluates the compiled code
120 in the same order that they were returned by this method. If you do that, the
121 code will run and determine the value of the tree at run-time. Needless to say,
122 that is slow.
123  
124 =cut
125  
126 21 ▶sub compile_to_code {
Subroutine compile_to_code: called CC=9 SCAR=22.3
Pod documented
127 21 my $tree = shift;
128 21 ▶ $tree = shift if not ref $tree and $tree eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $tree and $tree eq __PACKAGE__ 21 0 21
Condition33%1/3
not ref $tree and $tree eq __PACKAGE__
!ll&&!rl&&r
0021
MC/DC0%0/2
not ref $tree and $tree eq __PACKAGE__
not ref $tree $tree eq __PACKAGE__
Truth table33%1/3
not(A) and B
A= not ref $treeB= $tree eq __PACKAGE__
ABresult
0X0
100
111
129  
130 21 ▶ my $order = shift || [];
Condition100%2/2
shift() || []
l!l
192
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
131 21 my %order;
132 21 ▶ if (ref($order) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $order eq 'HASH') { } 0 21 21
elsif (ref $order eq 'ARRAY') { } 21 0 21
133 0 %order = %$order;
134 }
135 elsif (ref($order) eq 'ARRAY') {
136 21 my $count = 0;
137 21 %order = map { ( $_, $count++ ) } @$order;
138 }
139  
140 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
141  
142 21 my $vars = [ $tree->explicit_signature() ];
143  
144 21 my %vars;
145 my @not_placed;
146 21 foreach (@$vars) {
147 51 my $pos = $order{$_};
148 51 ▶ if ( defined $pos ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos) { } 9 42 51
149 9 $vars{$_} = $pos;
150 }
151 else {
152 42 push @not_placed, $_;
153 }
154 }
155  
156 21 my $count = 0;
157 21 foreach ( sort @not_placed ) {
158 42 $vars{$_} = @$vars - @not_placed + $count++;
159 }
160  
161 # The user is to do that himself. Left in to show that it would be
162 # a sensible (if slow) thing to do.
163 # $tree = $tree->simplify();
164 # $tree = $tree->apply_derivatives();
165 # $tree = $tree->simplify();
166  
167 21 my @trees;
168  
169 21 my $code = _rec_ms_to_sub( $tree, \%vars, \@trees );
170  
171 21 return ( $code, \@trees );
172 }
173  
174 =head2 ($sub, $trees) = compile_to_sub($tree, $vars)
175  
176 The compile_to_sub() class method takes one mandatory argument which is
177 the Math::Symbolic tree to be compiled. Second argument is optional
178 and an array reference to an array of variable mappings.
179 See L<VARIABLE PASSING STYLES> for details on how this works.
180  
181 compile_to_sub() returns a list of two elements, the first being the compiled
182 anonymous subroutine. For details on the second element, please refer to
183 the docs on the compile_to_code() subroutine.
184  
185 =cut
186  
187 10 ▶sub compile_to_sub {
Subroutine compile_to_sub: called CC=1 SCAR=0.0
Pod documented
188 10 my ( $code, $trees ) = Math::Symbolic::Compiler::compile_to_code(@_);
189 10 my $sub = _compile_sub( 'sub {' . $code . '}', @$trees );
190 10 return ( $sub, $trees );
191 }
192  
193 =head2 ($sub, $code, $trees) = compile($tree, $vars)
194  
195 The compile() class method takes one mandatory argument which is
196 the Math::Symbolic tree to be compiled. Second argument is optional
197 and an array reference to an array of variable mappings.
198 See L<POSITIONAL VARIABLE PASSING> for details on how this works.
199  
200 compile() returns a list of three elements, the first being the compiled
201 anonymous subroutine, the second being the compiled code. For details on the
202 second and third elements, please refer to the docs on the compile_to_code()
203 subroutine.
204  
205 =cut
206  
207 1 ▶sub compile {
Subroutine compile: called CC=1 SCAR=0.0
Pod documented
208 1 my ( $code, $trees ) = Math::Symbolic::Compiler::compile_to_code(@_);
209 1 my $sub = _compile_sub( 'sub {' . $code . '}', @$trees );
210 1 return ( $sub, $code, $trees );
211 }
212  
213 11 ▶sub _compile_sub {
Subroutine _compile_sub: called CC=3 SCAR=11.3
214 11 my @_TREES;
215 11 ▶ @_TREES = @_[ 1 .. $#_ ] if @_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 1 0 11 11
216 11 my $sub = eval $_[0];
217 11 ▶ die "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
218 11 return $sub;
219 }
220  
221 349 ▶sub _rec_ms_to_sub {
Subroutine _rec_ms_to_sub: called CC=6 SCAR=18.5
222 349 my $tree = shift;
223 349 my $vars = shift;
224 349 my $trees = shift;
225  
226 349 my $code = '';
227 349 my $ttype = $tree->term_type();
228  
229 349 ▶ if ( $ttype == T_CONSTANT ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ttype == 1) { } 83 266 349
elsif ($ttype == 2) { } 95 171 266
230 83 $code .= $tree->value();
231 }
232 elsif ( $ttype == T_VARIABLE ) {
233 95 $code .= '$_[' . $vars->{ $tree->name() } . ']';
234 }
235 else {
236 171 my $type = $tree->type();
237 171 my $otype = $Math::Symbolic::Operator::Op_Types[$type];
238 171 my $app = $otype->{application};
239 171 ▶ if ( ref($app) eq 'CODE' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $app eq 'CODE') { } 0 171 171
240 0 push @$trees, $tree->new();
241 my $arg_str = join( ', ',
242 0 map { "'$_' => \$_[" . $vars->{$_} . ']' } keys %$vars );
243 0 my $index = $#$trees;
244 0 $code .= <<HERE
245 (\$_TREES[$index]->value($arg_str))
246 HERE
247 }
248 else {
249 171 my @app = split /\$_\[(\d+)\]/, $app;
250 171 ▶ if ( @app > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@app > 1) 171 0 171
251 171 ▶ for ( my $i = 1 ; $i < @app ; $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @app) 328 171 499
252 $app[$i] = '('
253 328 . _rec_ms_to_sub( $tree->{operands}[ $app[$i] ],
254 $vars, $trees )
255 . ')';
256 }
257 }
258 171 $code .= join '', @app;
259 }
260 }
261 349 return $code;
262 }
263  
264 23 1;
265 __END__
« Symbolic/VectorCalculus.pm ↑ Summary Symbolic/MiscAlgebra.pm »