Symbolic/Constant.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 70.8% 17 / 24 Conditioncond 66.6% 18 / 27 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 100% 17 / 17 Podpod 100% 11 / 11 totaltotal 80.7% 126 / 156 CC 22 SCAR 15.1
CC
22
Cov
85%
new
10.4
value
4.0
one
3.3
CRAP 4.5
« Symbolic/MiscCalculus.pm ↑ Summary Symbolic/Parser.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Constant - Constants in symbolic calculations
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic::Constant;
11 my $const = Math::Symbolic::Constant->new(25);
12 my $zero = Math::Symbolic::Constant->zero();
13 my $one = Math::Symbolic::Constant->one();
14 my $euler = Math::Symbolic::Constant->euler();
15 # e = 2.718281828...
16  
17 =head1 DESCRIPTION
18  
19 This module implements numeric constants for Math::Symbolic trees.
20  
21 =head2 EXPORT
22  
23 None by default.
24  
25 =cut
26  
27 package Math::Symbolic::Constant;
28  
29 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 23 ▶use base 'Math::Symbolic::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 23 our $VERSION = '0.613';
39  
40 =head1 METHODS
41  
42 =cut
43  
44 =head2 Constructor new
45  
46 Takes hash reference of key-value pairs as argument.
47 Special case: a value for the constant instead of the hash.
48 Returns a Math::Symbolic::Constant.
49  
50 =cut
51  
52 10708 ▶sub new {
Subroutine new: called CC=10 SCAR=23.4
Pod documented
53 10708 my $proto = shift;
54 10708 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
376969390
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
55  
56 10708 my %args;
57 0 ▶ %args = %{ shift() } if @_ && ref( $_[0] ) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if @_ and ref $_[0] eq "HASH" 0 10708 10708
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
373969690
MC/DC0%0/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
58  
59 10708 ▶ my $value = ( @_ && !%args ? shift : $args{value} );
Branch: 1 branch
BranchTrueFalseTotal
@_ && !%args ? : 6969 3739 10708
Condition66%2/3
@_ and not %args
!ll&&!rl&&r
373906969
MC/DC50%1/2
@_ and not %args
@_ not %args
Truth table66%2/3
A and not(B)
A= @_B= not %args
ABresult
0X0
100
111
60 10708 ▶ $value = $proto->value() if !defined($value) and ref($proto);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value and ref $proto 3738 6970 10708
Condition100%3/3
not defined $value and ref $proto
!ll&&!rl&&r
696913738
MC/DC100%2/2
not defined $value and ref $proto
not defined $value ref $proto
Truth table100%3/3
not(A) and B
A= not defined $valueB= ref $proto
ABresult
0X0
100
111
61  
62 10708 ▶ croak("Math::Symbolic::Constant created with undefined value!")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 10707 10708
63 if not defined($value);
64  
65 10707 ▶ my $self = {
Branch: 1 branch
BranchTrueFalseTotal
ref $proto ? : 3769 6938 10707
66 special => '',
67 ( ref($proto) ? %$proto : () ),
68 value => $value,
69 %args,
70 };
71  
72 10707 bless $self => $class;
73 }
74  
75 =head2 Constructor zero
76  
77 Arguments are treated as key-value pairs of object attributes.
78 Returns a Math::Symbolic::Constant with value of 0.
79  
80 =cut
81  
82 168 ▶sub zero {
Subroutine zero: called CC=3 SCAR=11.3
Pod documented
83 168 my $proto = shift;
84 168 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
461220
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
85  
86 168 ▶ croak("Uneven number of arguments to zero()") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 168 168
87  
88 return(
89 168 bless {@_, value => 0, special => 'zero' } => $class
90 );
91  
92 # return $class->new( { @_, value => 0, special => 'zero' } );
93 }
94  
95 =head2 Constructor one
96  
97 Arguments are treated as key-value pairs of object attributes.
98 Returns a Math::Symbolic::Constant with value of 1.
99  
100 =cut
101  
102 202 ▶sub one {
Subroutine one: called CC=3 SCAR=12.0
Pod documented
103 202 my $proto = shift;
104 202 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
02020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
105  
106 202 ▶ croak("Uneven number of arguments to one()") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 202 202
107  
108 return(
109 202 bless {@_, value => 1, special => 'one' } => $class
110 );
111
112 #return $class->new( { @_, value => 1 } );
113 }
114  
115 =head2 Constructor euler
116  
117 Arguments are treated as key-value pairs of object attributes.
118 Returns a Math::Symbolic::Constant with value of e, the Euler number.
119 The object has its 'special' attribute set to 'euler'.
120  
121 =cut
122  
123 41 ▶sub euler {
Subroutine euler: called CC=3 SCAR=11.3
Pod documented
124 41 my $proto = shift;
125 41 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
2390
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
126  
127 41 ▶ croak("Uneven number of arguments to euler()") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 41 41
128  
129 return(
130 41 bless {@_, value => EULER, special => 'euler' } => $class
131 );
132
133 #return $class->new( { @_, value => EULER, special => 'euler' } );
134 }
135  
136 =head2 Constructor pi
137  
138 Arguments are treated as key-value pairs of object attributes.
139 Returns a Math::Symbolic::Constant with value of pi.
140 The object has its 'special' attribute set to 'pi'.
141  
142 =cut
143  
144 1 ▶sub pi {
Subroutine pi: called CC=3 SCAR=12.0
Pod documented
145 1 my $proto = shift;
146 1 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
100
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
147  
148 1 ▶ croak("Uneven number of arguments to pi()") if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 1 1
149  
150 return(
151 1 bless {@_, value => PI, special => 'pi' } => $class
152 );
153
154 #return $class->new( { @_, value => PI, special => 'pi' } );
155 }
156  
157 =head2 Method value
158  
159 value() evaluates the Math::Symbolic tree to its numeric representation.
160  
161 value() without arguments requires that every variable in the tree contains
162 a defined value attribute. Please note that this refers to every variable
163 I<object>, not just every named variable.
164  
165 value() with one argument sets the object's value if you're dealing with
166 Variables or Constants. In case of operators, a call with one argument will
167 assume that the argument is a hash reference. (see next paragraph)
168  
169 value() with named arguments (key/value pairs) associates variables in the tree
170 with the value-arguments if the corresponging key matches the variable name.
171 (Can one say this any more complicated?) Since version 0.132, an
172 equivalent and valid syntax is to pass a single hash reference instead of a
173 list.
174  
175 Example: $tree->value(x => 1, y => 2, z => 3, t => 0) assigns the value 1 to
176 any occurrances of variables of the name "x", aso.
177  
178 If a variable in the tree has no value set (and no argument of value sets
179 it temporarily), the call to value() returns undef.
180  
181 =cut
182  
183 13021 ▶sub value {
Subroutine value: called CC=4 SCAR=13.9
Pod documented
184 13021 my $self = shift;
185 13021 ▶ if ( @_ == 1 and not ref( $_[0] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0] eq "HASH") 1 13020 13021
Condition100%3/3
@_ == 1 and not ref $_[0] eq "HASH"
!ll&&!rl&&r
539976211
MC/DC100%2/2
@_ == 1 and not ref $_[0] eq "HASH"
@_ == 1 not ref $_[0] eq "HASH"
Truth table100%3/3
A and not(B)
A= @_ == 1B= not ref $_[0] eq "HASH"
ABresult
0X0
100
111
186 1 ▶ croak "Constant assigned undefined value!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 1 1
187 if not defined $_[0];
188
189 1 $self->{value} = $_[0];
190 1 $self->{special} = undef; # !!!FIXME!!! one day, this
191 # needs better handling.
192 }
193 13021 return $self->{value};
194 }
195  
196 =head2 Method signature
197  
198 signature() returns a tree's signature.
199  
200 In the context of Math::Symbolic, signatures are the list of variables
201 any given tree depends on. That means the tree "v*t+x" depends on the
202 variables v, t, and x. Thus, applying signature() on the tree that would
203 be parsed from above example yields the sorted list ('t', 'v', 'x').
204  
205 Constants do not depend on any variables and therefore return the empty list.
206 Obviously, operators' dependencies vary.
207  
208 Math::Symbolic::Variable objects, however, may have a slightly more
209 involved signature. By convention, Math::Symbolic variables depend on
210 themselves. That means their signature contains their own name. But they
211 can also depend on various other variables because variables themselves
212 can be viewed as placeholders for more compicated terms. For example
213 in mechanics, the acceleration of a particle depends on its mass and
214 the sum of all forces acting on it. So the variable 'acceleration' would
215 have the signature ('acceleration', 'force1', 'force2',..., 'mass', 'time').
216  
217 If you're just looking for a list of the names of all variables in the tree,
218 you should use the explicit_signature() method instead.
219  
220 =cut
221  
222 288 ▶sub signature {
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
223 288 return ();
224 }
225  
226 =head2 Method explicit_signature
227  
228 explicit_signature() returns a lexicographically sorted list of
229 variable names in the tree.
230  
231 See also: signature().
232  
233 =cut
234  
235 83 ▶sub explicit_signature {
Subroutine explicit_signature: called CC=1 SCAR=0.0
Pod documented
236 83 return ();
237 }
238  
239 =head2 Method special
240  
241 Optional argument: sets the object's special attribute.
242 Returns the object's special attribute.
243  
244 =cut
245  
246 531 ▶sub special {
Subroutine special: called CC=2 SCAR=7.1
Pod documented
247 531 my $self = shift;
248 531 ▶ $self->{special} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 531 531
249 531 return $self->{special};
250 }
251  
252 =head2 Method to_string
253  
254 Returns a string representation of the constant.
255  
256 =cut
257  
258 312 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
259 312 my $self = shift;
260 312 return $self->value();
261 }
262  
263 =head2 Method term_type
264  
265 Returns the type of the term. (T_CONSTANT)
266  
267 =cut
268  
269 8304 ▶sub term_type { T_CONSTANT }
Subroutine term_type: called CC=1 SCAR=0.0
Pod documented
270  
271 23 1;
272 __END__
« Symbolic/MiscCalculus.pm ↑ Summary Symbolic/Parser.pm »