Symbolic/Derivative.pm

filter: Statementstmt 91.8% 258 / 281 Branchbran 75.5% 148 / 196 Conditioncond 86.6% 13 / 15 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 20 / 20 Podpod 100% 2 / 2 totaltotal 85.3% 447 / 524 CC 106 SCAR 26.5
CC
106
Cov
85%
_product_rule
26.6
_quotient_rule
19.8
total_derivative
16.6
CRAP 14.2
« Symbolic/Base.pm ↑ Summary Symbolic/VectorCalculus.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Derivative - Derive Math::Symbolic trees
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic::Derivative qw/:all/;
11 $derived = partial_derivative($term, $variable);
12 # or:
13 $derived = total_derivative($term, $variable);
14  
15 =head1 DESCRIPTION
16  
17 This module implements derivatives for Math::Symbolic trees.
18 Derivatives are Math::Symbolic::Operators, but their implementation
19 is drawn from this module because it is significantly more complex
20 than the implementation of most operators.
21  
22 Derivatives come in two flavours. There are partial- and total derivatives.
23  
24 Explaining the precise difference between partial- and total derivatives is
25 beyond the scope of this document, but in the context of Math::Symbolic,
26 the difference is simply that partial derivatives just derive in terms of
27 I<explicit> dependency on the differential variable while total derivatives
28 recongnize implicit dependencies from variable signatures.
29  
30 Partial derivatives are faster, have been tested more thoroughly, and
31 are probably what you want for simpler applications anyway.
32  
33 =head2 EXPORT
34  
35 None by default. But you may choose to import the total_derivative()
36 and partial_derivative() functions.
37  
38 =cut
39  
40 package Math::Symbolic::Derivative;
41  
42 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 23 ▶no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 23 require Exporter;
52  
53 23 our @ISA = qw(Exporter);
54  
55 23 our %EXPORT_TAGS = (
56 'all' => [
57 qw(
58 &total_derivative
59 &partial_derivative
60 )
61 ]
62 );
63  
64 23 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
65  
66 23 our @EXPORT = qw();
67  
68 23 our $VERSION = '0.613';
69  
70 =head1 CLASS DATA
71  
72 The package variable %Partial_Rules contains partial
73 derivative rules as key-value pairs of names and subroutines.
74  
75 =cut
76  
77 # lookup-table for derivative rules for various operators.
78 23 our %Rules = (
79 'each operand' => \&_each_operand,
80 'product rule' => \&_product_rule,
81 'quotient rule' => \&_quotient_rule,
82 'logarithmic chain rule after ln' => \&_logarithmic_chain_rule_after_ln,
83 'logarithmic chain rule' => \&_logarithmic_chain_rule,
84 'derivative commutation' => \&_derivative_commutation,
85 'trigonometric derivatives' => \&_trigonometric_derivatives,
86 'inverse trigonometric derivatives' => \&_inverse_trigonometric_derivatives,
87 'inverse atan2' => \&_inverse_atan2,
88 );
89  
90 # References to derivative subroutines
91 # Will be assigned a reference after subroutine compilation.
92 23 our $Partial_Sub;
93 23 our $Total_Sub;
94  
95 our @Constant_Simplify = (
96 # B_SUM
97 33 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
98 33 my $tree = shift;
99 33 my ($op1, $op2) = @{$tree->{operands}};
100 33 my ($t1, $t2) = ($op1->term_type(), $op2->term_type());
101 33 ▶ if ($t1 == T_CONSTANT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t1 == 1) { } 14 19 33
elsif ($t2 == 1) { } 2 17 19
102 14 ▶ return $op2 if $op1->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op1->{"value"} == 0 10 4 14
103 4 ▶ if ($t2 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t2 == 1) 3 1 4
104 3 return Math::Symbolic::Constant->new($op1->{value} + $op2->{value});
105 }
106 }
107 elsif ($t2 == T_CONSTANT) {
108 2 ▶ return $op1 if $op2->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op2->{"value"} == 0 2 0 2
109 }
110  
111 18 return $tree;
112 },
113  
114 # B_DIFFERENCE
115 6 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=19.7
116 6 my $tree = shift;
117 6 my ($op1, $op2) = @{$tree->{operands}};
118 6 my ($t1, $t2) = ($op1->term_type(), $op2->term_type());
119 6 ▶ if ($t1 == T_CONSTANT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t1 == 1) { } 2 4 6
elsif ($t2 == 1) { } 4 0 4
120 2 ▶ $op2 *= -1, return $op2 if $op1->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op1->{"value"} == 0 2 0 2
121 0 ▶ if ($t2 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t2 == 1) 0 0 0
122 0 return Math::Symbolic::Constant->new($op1->{value} - $op2->{value});
123 }
124 }
125 elsif ($t2 == T_CONSTANT) {
126 4 ▶ return $op1 if $op2->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $op2->{"value"} == 0 2 2 4
127 2 $op2->{value} *= -1;
128 2 return Math::Symbolic::Operator->new('+', $op1, $op2);
129 }
130 0 return $tree;
131 },
132
133 # B_PRODUCT
134 undef, # implemented inline
135 # B_DIVISION
136 undef, # not implemented
137  
138 # U_MINUS
139 24 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
140 24 my $tree = shift;
141 24 my $op = $tree->{operands}[0];
142 24 ▶ if ($op->term_type == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->term_type == 1) 4 20 24
143 4 return Math::Symbolic::Constant->new(-$op->{value});
144 }
145 20 return $tree;
146 },
147  
148 #... not implemented
149 23 );
150  
151 =begin comment
152  
153 The following subroutines are helper subroutines that apply a
154 specific rule to a tree.
155  
156 =end comment
157  
158 =cut
159  
160 63 ▶sub _each_operand {
Subroutine _each_operand: called CC=3 SCAR=11.2
161 63 my ( $tree, $var, $cloned, $d_sub ) = @_;
162 63 foreach ( @{ $tree->{operands} } ) {
163 102 $_ = $d_sub->( $_, $var, 1 );
164 }
165  
166 63 my $type = $tree->type();
167 63 my $simplifier = $Constant_Simplify[$type];
168 63 ▶ return $simplifier->($tree) if $simplifier;
Branch: 1 branch
BranchTrueFalseTotal
if $simplifier 63 0 63
169  
170 0 return $tree;
171 }
172  
173  
174 157 ▶sub _product_rule {
Subroutine _product_rule: called CC=22 SCAR=32.8
175 157 my ( $tree, $var, $cloned, $d_sub ) = @_;
176 157 my $ops = $tree->{operands};
177 157 my ($o1, $o2) = @$ops;
178 157 my ($to1, $to2) = ($o1->term_type(), $o2->term_type());
179  
180 # one of the terms is a constant, don't derive it
181 157 ▶ if ($to1 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to1 == 1) 88 69 157
182 88 ▶ return Math::Symbolic::Constant->zero() if $o1->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o1->{"value"} == 0 0 88 88
183 88 my $deriv = $d_sub->( $o2, $var, 0 );
184 88 ▶ return $deriv if $o1->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o1->{"value"} == 0 0 88 88
185 return Math::Symbolic::Constant->new($deriv->{value}*$o1->{value})
186 88 ▶ if $deriv->term_type == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
if $deriv->term_type == 1 58 30 88
187 }
188 99 ▶ if ($to2 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to2 == 1) 1 98 99
189 1 ▶ return Math::Symbolic::Constant->zero() if $o2->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o2->{"value"} == 0 0 1 1
190 1 my $deriv = $d_sub->( $o1, $var, 0 );
191 1 ▶ return $deriv if $o2->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $o2->{"value"} == 0 0 1 1
192 return Math::Symbolic::Constant->new($deriv->{value}*$o2->{value})
193 1 ▶ if $deriv->term_type == T_CONSTANT;
Branch: 1 branch
BranchTrueFalseTotal
if $deriv->term_type == 1 1 0 1
194 }
195
196 98 my $do1 = $d_sub->( $o1, $var, 0 );
197 98 my $do2 = $d_sub->( $o2, $var, 0 );
198  
199 98 my ($tdo1, $tdo2) = ($do1->term_type(), $do2->term_type());
200  
201 98 my ($m1, $m2);
202 # check for const*const
203 98 ▶ if ($tdo1 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tdo1 == 1) { } 69 29 98
204 69 ▶ if ($to2 == T_CONSTANT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($to2 == 1) { } 0 69 69
elsif ($do1->{'value'} == 0) { } 37 32 69
elsif ($do1->{'value'} == 1) { } 10 22 32
205 0 $m1 = $do1->new($o2->{value} * $do1->{value}); # const
206 } elsif ($do1->{value} == 0) {
207 37 $m1 = $do1->zero(); # 0
208 } elsif ($do1->{value} == 1) {
209 10 $m1 = $o2;
210 } else {
211 22 $m1 = $do1*$o2; # c*tree
212 }
213 }
214 else {
215 29 $m1 = $o2*$do1;
216 }
217  
218 98 ▶ if ($tdo2 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tdo2 == 1) { } 16 82 98
219 16 ▶ if ($to1 == T_CONSTANT) {
Branch: 3 branches
BranchTrueFalseTotal
if ($to1 == 1) { } 0 16 16
elsif ($do2->{'value'} == 0) { } 8 8 16
elsif ($do2->{'value'} == 1) { } 8 0 8
220 0 $m2 = $do2->new($o1->{value} * $do2->{value}); # const
221 } elsif ($do2->{value} == 0) {
222 8 $m2 = $do2->zero(); # 0
223 } elsif ($do2->{value} == 1) {
224 8 $m2 = $o1;
225 } else {
226 0 $m2 = $do2*$o1; # c*tree
227 }
228 }
229 else {
230 82 $m2 = $o1*$do2;
231 }
232  
233 # 0's or 2 consts in +
234 98 ▶ if ($m1->term_type == T_CONSTANT) {
Branch: 2 branches
BranchTrueFalseTotal
if ($m1->term_type == 1) { } 37 61 98
elsif ($m2->term_type == 1) { } 6 55 61
235 37 ▶ return $m2 if $m1->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m1->{"value"} == 0 37 0 37
236 0 ▶ if ($m2->term_type == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m2->term_type == 1) 0 0 0
237 0 return $m2->new($m1->{value}*$m2->{value});
238 }
239 }
240 elsif ($m2->term_type == T_CONSTANT) {
241 6 ▶ return $m1 if $m2->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m2->{"value"} == 0 6 0 6
242 }
243  
244 55 return Math::Symbolic::Operator->new( '+', $m1, $m2 );
245 }
246  
247 32 ▶sub _quotient_rule {
Subroutine _quotient_rule: called CC=18 SCAR=29.9
248 32 my ( $tree, $var, $cloned, $d_sub ) = @_;
249  
250 32 my ($op1, $op2) = @{$tree->{operands}};
251  
252 32 my ($do1, $do2);
253  
254 # y = f(x)/c; y' = f'/c
255 32 ▶ if ($op2->is_simple_constant()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op2->is_simple_constant) { } 3 29 32
elsif ($op1->is_simple_constant) { } 13 16 29
256 3 $do1 = $d_sub->( $op1, $var, 0 );
257 3 my $val = $op2->value();
258  
259 3 ▶ if ($val == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val == 0) { } 0 3 3
elsif ($val == 1) { } 0 3 3
260 0 return $tree->new('/', $do1, $op2->new()); # inf!
261 }
262 elsif ($val == 1) {
263 0 return $do1; # f/1
264 }
265 3 return $tree->new('*', Math::Symbolic::Constant->new(1/$val), $do1);
266 }
267 # y = c/f(x) => y' = -c*f'(x)/f^2(x)
268 elsif ($op1->is_simple_constant()) {
269 13 $do2 = $d_sub->( $op2, $var, 0 );
270 13 my $val = $op1->value();
271
272 13 ▶ if ($val == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val == 0) 0 13 13
273 0 return Math::Symbolic::Constant->zero(); # 0*f'/f
274 }
275  
276 13 my $tdo2 = $do2->term_type();
277 13 ▶ if ($tdo2 == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tdo2 == 1) { } 5 8 13
278 5 ▶ return $do2->zero() if $do2->{value} == 0; # c*0/f
Branch: 1 branch
BranchTrueFalseTotal
if $do2->{"value"} == 0 1 4 5
279 return $tree->new(
280 4 '/', $do2->new(-1.*$val*$do2->{value}),
281 $tree->new('^', $op2, 2)
282 );
283 }
284 else {
285 8 return $tree->new(
286 '*', Math::Symbolic::Constant->new(-1*$val),
287 $tree->new('/', $do2, $tree->new('^', $op2, Math::Symbolic::Constant->new(2)))
288 )
289 }
290 }
291  
292 16 ▶ $do1 = $d_sub->( $op1, $var, 0 ) if not $do1;
Branch: 1 branch
BranchTrueFalseTotal
unless $do1 16 0 16
293 16 ▶ $do2 = $d_sub->( $op2, $var, 0 ) if not $do2;
Branch: 1 branch
BranchTrueFalseTotal
unless $do2 16 0 16
294  
295 16 my $m1 = Math::Symbolic::Operator->new( '*', $do1, $op2 );
296 16 my $m2 = Math::Symbolic::Operator->new( '*', $op1, $do2 );
297  
298 # f' = 0
299 16 ▶ if ($do1->is_zero()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($do1->is_zero) { } 1 15 16
elsif ($do1->is_one) { } 3 12 15
300 1 $m1 = undef;
301 }
302 # f' = 1
303 elsif ($do1->is_one()) {
304 3 $m1 = $op2->new();
305 }
306  
307 # g' = 0
308 16 ▶ if ($do2->is_zero()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($do2->is_zero) { } 0 16 16
elsif ($do2->is_one) { } 1 15 16
309 0 $m2 = undef;
310 }
311 elsif ($do2->is_one()) {
312 1 $m2 = $op1->new();
313 }
314  
315 16 my $upper;
316 # -g'f / g^2
317 16 ▶ if (not defined $m1) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $m1) { } 1 15 16
elsif (not defined $m2) { } 0 15 15
318 # f'=g'=0
319 1 ▶ return Math::Symbolic::Constant->zero() if not defined $m2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m2 0 1 1
320 1 $upper = $tree->new('neg', $m2);
321 }
322 # f'g / g^2 = f'/g
323 elsif (not defined $m2) {
324 0 return $tree->new('/', $do1, $op2);
325 }
326  
327 16 my $m3 = $tree->new('^', $op2, Math::Symbolic::Constant->new(2));
328 16 ▶ if (not defined $upper) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $upper) 15 1 16
329 15 $upper = Math::Symbolic::Operator->new( '-', $m1, $m2 );
330 }
331 16 return Math::Symbolic::Operator->new( '/', $upper, $m3 );
332 }
333  
334 67 ▶sub _logarithmic_chain_rule_after_ln {
Subroutine _logarithmic_chain_rule_after_ln: called CC=7 SCAR=19.5
335 67 my ( $tree, $var, $cloned, $d_sub ) = @_;
336  
337 # y(x)=u^v
338 # y'(x)=y*(d/dx ln(y))
339 # y'(x)=y*(d/dx (v*ln(u)))
340 67 my ($u, $v) = @{$tree->{operands}};
341  
342 # This is a special case:
343 # y(x)=u^CONST
344 # y'(x)=CONST*y* d/dx ln(u)
345 # y'(x)=CONST*y* u' / u
346 67 ▶ if ($v->term_type() == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->term_type == 1) 40 27 67
347  
348 # y=VAR^CONST
349 40 ▶ if ($u->term_type() == T_VARIABLE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u->term_type == 2) 13 27 40
350 13 my $d = $d_sub->($u, $var, 0);
351 13 my $dtt = $d->term_type();
352 13 ▶ if ($dtt == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dtt == 1) 13 0 13
353 # not our var
354 13 ▶ return Math::Symbolic::Constant->zero() if $d->{value} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $d->{"value"} == 0 0 13 13
355 # our var
356 13 ▶ return Math::Symbolic::Constant->one() if $v->{value} == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $v->{"value"} == 1 0 13 13
357 13 ▶ return $tree->new('*', $v->new(), $u->new()) if $v->{value} == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $v->{"value"} == 2 5 8 13
358 8 return $tree->new('*', $v->new(), $tree->new('^', $u->new(), $v->new($v->{value}-1)));
359 }
360 # otherwise: signature contains $var
361 }
362 27 return Math::Symbolic::Operator->new(
363 '*',
364 Math::Symbolic::Operator->new(
365 '*', $v->new(), $tree
366 ),
367 Math::Symbolic::Operator->new(
368 '/', $d_sub->($u, $var, 0), $u->new()
369 )
370 );
371 }
372  
373 27 my $e = Math::Symbolic::Constant->euler();
374 27 my $ln = Math::Symbolic::Operator->new( 'log', $e, $u );
375 27 my $mul1 = $ln->new( '*', $v, $ln );
376 27 my $dmul = $d_sub->( $mul1, $var, 0 );
377 27 $tree = $ln->new( '*', $tree, $dmul );
378 27 return $tree;
379 }
380  
381 30 ▶sub _logarithmic_chain_rule {
Subroutine _logarithmic_chain_rule: called CC=3 SCAR=11.0
382 30 my ( $tree, $var, $cloned, $d_sub ) = @_;
383  
384 #log_a(y(x))=>y'(x)/(ln(a)*y(x))
385 30 my ($a, $y) = @{$tree->{operands}};
386 30 my $dy = $d_sub->( $y, $var, 0 );
387  
388 # This would be y'/y
389 30 ▶ if ($a->term_type() == T_CONSTANT and $a->{special} eq 'euler') {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->term_type == 1 and $a->{"special"} eq "euler") 26 4 30
Condition100%3/3
$a->term_type == 1 and $a->{"special"} eq "euler"
!ll&&!rl&&r
1326
MC/DC100%2/2
$a->term_type == 1 and $a->{"special"} eq "euler"
$a->term_type == 1 $a->{"special"} eq "euler"
Truth table100%3/3
A and B
A= $a->term_type == 1B= $a->{"special"} eq "euler"
ABresult
0X0
100
111
390 26 return Math::Symbolic::Operator->new('/', $dy, $y);
391 }
392
393 4 my $e = Math::Symbolic::Constant->euler();
394 4 my $ln = Math::Symbolic::Operator->new( 'log', $e, $a );
395 4 my $mul1 = $ln->new( '*', $ln, $y->new() );
396 4 $tree = $ln->new( '/', $dy, $mul1 );
397 4 return $tree;
398 }
399  
400 7 ▶sub _derivative_commutation {
Subroutine _derivative_commutation: called CC=1 SCAR=0.0
401 7 my ( $tree, $var, $cloned, $d_sub ) = @_;
402 7 $tree->{operands}[0] = $d_sub->( $tree->{operands}[0], $var, 0 );
403 7 return $tree;
404 }
405  
406 99 ▶sub _trigonometric_derivatives {
Subroutine _trigonometric_derivatives: called CC=11 SCAR=24.1
407 99 my ( $tree, $var, $cloned, $d_sub ) = @_;
408 99 my $op = Math::Symbolic::Operator->new();
409 99 my $d_inner = $d_sub->( $tree->{operands}[0], $var, 0 );
410 99 my $trig;
411 99 my $type = $tree->type();
412 99 ▶ if ( $type == U_SINE ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($type == 9) { } 29 70 99
elsif ($type == 10) { } 52 18 70
elsif ($type == 17) { } 5 13 18
elsif ($type == 18) { } 5 8 13
elsif ($type == 11 or $type == 12) { } 8 0 8
413 29 $trig = $op->new( 'cos', $tree->{operands}[0] );
414 }
415 elsif ( $type == U_COSINE ) {
416 52 $trig = $op->new( 'neg', $op->new( 'sin', $tree->{operands}[0] ) );
417 }
418 elsif ( $type == U_SINE_H ) {
419 5 $trig = $op->new( 'cosh', $tree->{operands}[0] );
420 }
421 elsif ( $type == U_COSINE_H ) {
422 5 $trig = $op->new( 'sinh', $tree->{operands}[0] );
423 }
424 ▶ elsif ( $type == U_TANGENT or $type == U_COTANGENT ) {
Condition66%2/3
$type == 11 or $type == 12
l!l&&r!l&&!r
710
MC/DC0%0/2
$type == 11 or $type == 12
$type == 11 $type == 12
Truth table66%2/3
A or B
A= $type == 11B= $type == 12
ABresult
1X1
011
000
425 8 $trig = $op->new(
426 '/',
427 Math::Symbolic::Constant->one(),
428 $op->new(
429 '^',
430 $op->new( 'cos', $tree->op1() ),
431 Math::Symbolic::Constant->new(2)
432 )
433 );
434 8 ▶ $trig = $op->new( 'neg', $trig ) if $type == U_COTANGENT;
Branch: 1 branch
BranchTrueFalseTotal
if $type == 12 1 7 8
435 }
436 else {
437 0 die "Trigonometric derivative applied to invalid operator.";
438 }
439 99 ▶ if ($d_inner->term_type() == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d_inner->term_type == 1) 99 0 99
440 99 my $spec = $d_inner->special();
441 99 ▶ if ($spec eq 'zero') {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec eq 'zero') { } 1 98 99
elsif ($spec eq 'one') { } 73 25 98
442 1 return $d_inner;
443 }
444 elsif ($spec eq 'one') {
445 73 return $trig;
446 }
447 }
448 25 return $op->new( '*', $d_inner, $trig );
449 }
450  
451 6 ▶sub _inverse_trigonometric_derivatives {
Subroutine _inverse_trigonometric_derivatives: called CC=13 SCAR=26.3
452 6 my ( $tree, $var, $cloned, $d_sub ) = @_;
453 6 my $op = Math::Symbolic::Operator->new();
454 6 my $d_inner = $d_sub->( $tree->{operands}[0], $var, 0 );
455 6 my $trig;
456 6 my $type = $tree->type();
457 6 ▶ if ( $type == U_ARCSINE or $type == U_ARCCOSINE ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == 13 or $type == 14) { } 2 4 6
elsif ($type == 15 or $type == 16) { } 2 2 4
elsif ($type == 19 or $type == 20) { } 2 0 2
Condition100%3/3
$type == 13 or $type == 14
l!l&&r!l&&!r
114
MC/DC100%2/2
$type == 13 or $type == 14
$type == 13 $type == 14
Truth table100%3/3
A or B
A= $type == 13B= $type == 14
ABresult
1X1
011
000
458 2 ▶ my $one = $type == U_ARCSINE
Branch: 1 branch
BranchTrueFalseTotal
$type == 13 ? : 1 1 2
459 ? Math::Symbolic::Constant->one()
460 : Math::Symbolic::Constant->new(-1);
461 2 $trig = $op->new( '/', $one,
462 $op->new( '-', $one->new(1), $op->new( '^', $tree->op1(), $one->new(2) ) )
463 );
464 }
465 ▶ elsif ($type == U_ARCTANGENT
Condition100%3/3
$type == 15 or $type == 16
l!l&&r!l&&!r
112
MC/DC100%2/2
$type == 15 or $type == 16
$type == 15 $type == 16
Truth table100%3/3
A or B
A= $type == 15B= $type == 16
ABresult
1X1
011
000
466 or $type == U_ARCCOTANGENT )
467 {
468 2 ▶ my $one = $type == U_ARCTANGENT
Branch: 1 branch
BranchTrueFalseTotal
$type == 15 ? : 1 1 2
469 ? Math::Symbolic::Constant->one()
470 : Math::Symbolic::Constant->new(-1);
471 2 $trig = $op->new( '/', $one,
472 $op->new( '+', $one->new(1), $op->new( '^', $tree->op1(), $one->new(2) ) )
473 );
474 }
475 ▶ elsif ($type == U_AREASINE_H
Condition66%2/3
$type == 19 or $type == 20
l!l&&r!l&&!r
110
MC/DC0%0/2
$type == 19 or $type == 20
$type == 19 $type == 20
Truth table66%2/3
A or B
A= $type == 19B= $type == 20
ABresult
1X1
011
000
476 or $type == U_AREACOSINE_H )
477 {
478 2 my $one = Math::Symbolic::Constant->one();
479 2 ▶ $trig = $op->new(
Branch: 1 branch
BranchTrueFalseTotal
$tree->type == 19 ? : 1 1 2
480 '/', $one,
481 $op->new(
482 '^',
483 $op->new(
484 ( $tree->type() == U_AREASINE_H ? '+' : '-' ),
485 $op->new( '^', $tree->op1(), $one->new(2) ),
486 $one
487 ),
488 $one->new(0.5)
489 )
490 );
491 }
492 else {
493 0 die "Inverse trig. derivative applied to invalid operator.";
494 }
495  
496 6 ▶ if ($d_inner->term_type() == T_CONSTANT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d_inner->term_type == 1) 6 0 6
497 6 my $spec = $d_inner->special();
498 6 ▶ if ($spec eq 'zero') {
Branch: 2 branches
BranchTrueFalseTotal
if ($spec eq 'zero') { } 0 6 6
elsif ($spec eq 'one') { } 0 6 6
499 0 return $d_inner;
500 }
501 elsif ($spec eq 'one') {
502 0 return $trig;
503 }
504 }
505 6 return $op->new( '*', $d_inner, $trig );
506 }
507  
508 1 ▶sub _inverse_atan2 {
Subroutine _inverse_atan2: called CC=1 SCAR=0.0
509 1 my ( $tree, $var, $cloned, $d_sub ) = @_;
510 # d/df atan(y/x) = x^2/(x^2+y^2) * (d/df y/x)
511 1 my ($op1, $op2) = @{$tree->{operands}};
512  
513 1 my $inner = $d_sub->( $op1->new()/$op2->new(), $var, 0 );
514 # templates
515 1 my $two = Math::Symbolic::Constant->new(2);
516 1 my $op = Math::Symbolic::Operator->new('+', $two, $two);
517  
518 1 my $result = $op->new('*',
519 $op->new('/',
520 $op->new('^', $op2->new(), $two->new()),
521 $op->new(
522 '+', $op->new('^', $op2->new(), $two->new()),
523 $op->new('^', $op1->new(), $two->new())
524 )
525 ),
526 $inner
527 );
528 1 return $result;
529 }
530  
531 =head1 SUBROUTINES
532  
533 =cut
534  
535 =head2 partial_derivative
536  
537 Takes a Math::Symbolic tree and a Math::Symbolic::Variable as argument.
538 third argument is an optional boolean indicating whether or not the
539 tree has to be cloned before being derived. If it is true, the
540 subroutine happily stomps on any code that might rely on any components
541 of the Math::Symbolic tree that was passed to the sub as first argument.
542  
543 =cut
544  
545 449 ▶sub partial_derivative {
Subroutine partial_derivative: called CC=11 SCAR=24.3
Pod documented
546 449 my $tree = shift;
547 449 my $var = shift;
548 449 ▶ defined $var or die "Cannot derive using undefined variable.";
Branch: 1 branch
BranchTrueFalseTotal
defined $var or die "Cannot derive using undefined variable." 449 0 449
549 449 ▶ if ( ref($var) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var eq '') { } 10 439 449
550 10 $var = Math::Symbolic::parse_from_string($var);
551 10 ▶ croak "2nd argument to partial_derivative must be variable."
Branch: 1 branch
BranchTrueFalseTotal
if ref $var ne "Math::Symbolic::Variable" 0 10 10
552 if ( ref($var) ne 'Math::Symbolic::Variable' );
553 }
554 else {
555 439 ▶ croak "2nd argument to partial_derivative must be variable."
Branch: 1 branch
BranchTrueFalseTotal
if ref $var ne "Math::Symbolic::Variable" 0 439 439
556 if ( ref($var) ne 'Math::Symbolic::Variable' );
557 }
558  
559 449 my $cloned = shift;
560  
561 449 ▶ if ( not $cloned ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cloned) 383 66 449
562 383 $tree = $tree->new();
563 383 $cloned = 1;
564 }
565  
566 449 ▶ if ( $tree->term_type() == T_OPERATOR ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tree->term_type == 0) { } 276 173 449
elsif ($tree->term_type == 1) { } 59 114 173
elsif ($tree->term_type == 2) { } 114 0 114
567 my $rulename =
568 276 $Math::Symbolic::Operator::Op_Types[ $tree->type() ]->{derive};
569 276 my $subref = $Rules{$rulename};
570  
571 276 ▶ die "Cannot derive using rule '$rulename'."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subref 0 276 276
572 unless defined $subref;
573 276 $tree = $subref->( $tree, $var, $cloned, $Partial_Sub );
574 }
575 elsif ( $tree->term_type() == T_CONSTANT ) {
576 59 $tree = Math::Symbolic::Constant->zero();
577 }
578 elsif ( $tree->term_type() == T_VARIABLE ) {
579 114 ▶ if ( $tree->name() eq $var->name() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree->name eq $var->name) { } 96 18 114
580 96 $tree = Math::Symbolic::Constant->one;
581 }
582 else {
583 18 $tree = Math::Symbolic::Constant->zero;
584 }
585 }
586 else {
587 0 die "Cannot apply partial derivative to anything but a tree.";
588 }
589  
590 449 return $tree;
591 }
592  
593 =head2 total_derivative
594  
595 Takes a Math::Symbolic tree and a Math::Symbolic::Variable as argument.
596 third argument is an optional boolean indicating whether or not the
597 tree has to be cloned before being derived. If it is true, the
598 subroutine happily stomps on any code that might rely on any components
599 of the Math::Symbolic tree that was passed to the sub as first argument.
600  
601 =cut
602  
603 283 ▶sub total_derivative {
Subroutine total_derivative: called CC=15 SCAR=28.1
Pod documented
604 283 my $tree = shift;
605 283 my $var = shift;
606 283 ▶ defined $var or die "Cannot derive using undefined variable.";
Branch: 1 branch
BranchTrueFalseTotal
defined $var or die "Cannot derive using undefined variable." 283 0 283
607 283 ▶ if ( ref($var) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var eq '') { } 0 283 283
608 0 $var = Math::Symbolic::parse_from_string($var);
609 0 ▶ croak "Second argument to total_derivative must be variable."
Branch: 1 branch
BranchTrueFalseTotal
if ref $var ne "Math::Symbolic::Variable" 0 0 0
610 if ( ref($var) ne 'Math::Symbolic::Variable' );
611 }
612 else {
613 283 ▶ croak "Second argument to total_derivative must be variable."
Branch: 1 branch
BranchTrueFalseTotal
if ref $var ne "Math::Symbolic::Variable" 0 283 283
614 if ( ref($var) ne 'Math::Symbolic::Variable' );
615 }
616  
617 283 my $cloned = shift;
618  
619 283 ▶ if ( not $cloned ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cloned) 247 36 283
620 247 $tree = $tree->new();
621 247 $cloned = 1;
622 }
623  
624 283 ▶ if ( $tree->term_type() == T_OPERATOR ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tree->term_type == 0) { } 191 92 283
elsif ($tree->term_type == 1) { } 10 82 92
elsif ($tree->term_type == 2) { } 82 0 82
625 191 my $var_name = $var->name();
626 191 my @tree_sig = $tree->signature();
627 191 ▶ if ( ( grep { $_ eq $var_name } @tree_sig ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep({$_ eq $var_name;} @tree_sig) > 0) { } 186 5 191
628 my $rulename =
629 186 $Math::Symbolic::Operator::Op_Types[ $tree->type() ]->{derive};
630 186 my $subref = $Rules{$rulename};
631  
632 186 ▶ die "Cannot derive using rule '$rulename'."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subref 0 186 186
633 unless defined $subref;
634 186 $tree = $subref->( $tree, $var, $cloned, $Total_Sub );
635 }
636 else {
637 5 $tree = Math::Symbolic::Constant->zero();
638 }
639 }
640 elsif ( $tree->term_type() == T_CONSTANT ) {
641 10 $tree = Math::Symbolic::Constant->zero();
642 }
643 elsif ( $tree->term_type() == T_VARIABLE ) {
644 82 my $name = $tree->name();
645 82 my $var_name = $var->name();
646  
647 82 ▶ if ( $name eq $var_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $var_name) { } 79 3 82
648 79 $tree = Math::Symbolic::Constant->one;
649 }
650 else {
651 3 my @tree_sig = $tree->signature();
652 3 my $is_dependent;
653 3 foreach my $ident (@tree_sig) {
654 5 ▶ if ( $ident eq $var_name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ident eq $var_name) 3 2 5
655 3 $is_dependent = 1;
656 3 last;
657 }
658 }
659 3 ▶ if ( $is_dependent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dependent) { } 3 0 3
660 3 $tree =
661 Math::Symbolic::Operator->new( 'total_derivative', $tree,
662 $var );
663 }
664 else {
665 0 $tree = Math::Symbolic::Constant->zero;
666 }
667 }
668 }
669 else {
670 0 die "Cannot apply total derivative to anything but a tree.";
671 }
672  
673 283 return $tree;
674 }
675  
676 # Class data again.
677 23 $Partial_Sub = \&partial_derivative;
678 23 $Total_Sub = \&total_derivative;
679  
680 23 1;
681 __END__
« Symbolic/Base.pm ↑ Summary Symbolic/VectorCalculus.pm »