Symbolic/Variable.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 100% 18 / 18 Conditioncond 86.6% 13 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 13 / 13 Podpod 100% 8 / 8 totaltotal 94.9% 112 / 118 CC 15 SCAR 15.8
CC
15
Cov
97%
new
9.0
value
6.0
name
2.0
CRAP 4.9
« Symbolic/MiscAlgebra.pm ↑ Summary Symbolic/MiscCalculus.pm »
1  
2 =encoding utf8
3  
4 =head1 NAME
5  
6 Math::Symbolic::Variable - Variable in symbolic calculations
7  
8 =head1 SYNOPSIS
9  
10 use Math::Symbolic::Variable;
11  
12 my $var1 = Math::Symbolic::Variable->new('name');
13 $var1->value(5);
14
15 my $var2 = Math::Symbolic::Variable->new('x', 2);
16  
17 my $var3 =
18 Math::Symbolic::Variable->new(
19 {
20 name => 'variable',
21 value => 1,
22 }
23 );
24  
25 =head1 DESCRIPTION
26  
27 This class implements variables for Math::Symbolic trees.
28 The objects are overloaded in stringification context to
29 return their names.
30  
31 =head2 EXPORT
32  
33 None by default.
34  
35 =cut
36  
37 package Math::Symbolic::Variable;
38  
39 23 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 23 ▶use Math::Symbolic::ExportConstants qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 23 ▶use base 'Math::Symbolic::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 23 our $VERSION = '0.613';
48  
49 =head1 METHODS
50  
51 =head2 Constructor new
52  
53 First argument is expected to be a hash reference of key-value
54 pairs which will be used as object attributes.
55  
56 In particular, a variable is required to have a 'name'. Optional
57 arguments include a 'value', and a 'signature'. The value expected
58 for the signature key is a reference to an array of identifiers.
59  
60 Special case: First argument is not a hash reference. In this
61 case, first argument is treated as variable name, second as value.
62 This special case disallows cloning of objects (when used as
63 object method).
64  
65 Returns a Math::Symbolic::Variable.
66  
67 =cut
68  
69 6750 ▶sub new {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
70 6750 my $proto = shift;
71 6750 ▶ my $class = ref($proto) || $proto;
Condition66%2/3
ref $proto || $proto
l!l&&r!l&&!r
341133390
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table66%2/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
72  
73 6750 ▶ if ( @_ == 1
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'Math::Symbolic::Variable') { } 17 6733 6750
elsif (@_ and not ref $_[0] eq 'HASH') { } 3164 6975 10139
Condition100%3/3
@_ == 1 and ref $_[0] eq "Math::Symbolic::Variable"
!ll&&!rl&&r
3425330817
MC/DC100%2/2
@_ == 1 and ref $_[0] eq "Math::Symbolic::Variable"
@_ == 1 ref $_[0] eq "Math::Symbolic::Variable"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] eq "Math::Symbolic::Variable"
ABresult
0X0
100
111
74 and ref( $_[0] ) eq 'Math::Symbolic::Variable' )
75 {
76 17 return $_[0]->new();
77 }
78 ▶ elsif ( @_ and not ref( $_[0] ) eq 'HASH' ) {
Condition100%3/3
@_ and not ref $_[0] eq "HASH"
!ll&&!rl&&r
34061633164
MC/DC100%2/2
@_ and not ref $_[0] eq "HASH"
@_ not ref $_[0] eq "HASH"
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0] eq "HASH"
ABresult
0X0
100
111
79 3164 my $name = shift;
80 3164 my $value = shift;
81 return
82 3164 bless { name => $name, value => $value, signature => [@_] } => $class;
83 }
84  
85  
86 my $self = {
87 value => undef,
88 name => undef,
89 signature => [],
90 ( ref($proto) ? %$proto : () ),
91 3569 ▶ ((@_ and ref($_[0]) eq 'HASH') ? %{$_[0]} : ()),
Branch: 2 branches
BranchTrueFalseTotal
ref $proto ? : 3395 174 3569
@_ && ref $_[0] eq 'HASH' ? : 163 3406 3569
Condition66%2/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
34060163
MC/DC50%1/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
92 };
93  
94 3569 bless $self => $class;
95 }
96  
97 =head2 Method value
98  
99 value() evaluates the Math::Symbolic tree to its numeric representation.
100  
101 value() without arguments requires that every variable in the tree contains
102 a defined value attribute. Please note that this refers to every variable
103 I<object>, not just every named variable.
104  
105 value() with one argument sets the object's value if you're dealing with
106 Variables or Constants. In case of operators, a call with one argument will
107 assume that the argument is a hash reference. (see next paragraph)
108  
109 value() with named arguments (key/value pairs) associates variables in the tree
110 with the value-arguments if the corresponging key matches the variable name.
111 (Can one say this any more complicated?) Since version 0.132, an
112 equivalent and valid syntax is to pass a single hash reference instead of a
113 list.
114  
115 Example: $tree->value(x => 1, y => 2, z => 3, t => 0) assigns the value 1 to
116 any occurrances of variables of the name "x", aso.
117  
118 If a variable in the tree has no value set (and no argument of value sets
119 it temporarily), the call to value() returns undef.
120  
121 =cut
122  
123 2376 ▶sub value {
Subroutine value: called CC=6 SCAR=17.9
Pod documented
124 2376 my $self = shift;
125 2376 ▶ if ( @_ == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 0) { } 17 2359 2376
elsif (@_ == 1 and not ref $_[0] eq 'HASH') { } 1 2358 2359
126 17 return $self->{value};
127 }
128 ▶ elsif ( @_ == 1 and not ref( $_[0] ) eq 'HASH' ) {
Condition100%3/3
@_ == 1 and not ref $_[0] eq "HASH"
!ll&&!rl&&r
4023181
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
129 1 $self->{value} = shift;
130 1 return $self->{value};
131 }
132 else {
133 2358 ▶ my $args = ( @_ == 1 ? $_[0] : +{@_} );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 2318 40 2358
134 2358 ▶ if ( exists $args->{ $self->{name} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$args{$self->{'name'}}) { } 2278 80 2358
135 2278 return $args->{ $self->{name} };
136 }
137 else {
138 80 return $self->{value};
139 }
140 }
141 0 die "Sanity check in Math::Symbolic::Variable::value()";
142 }
143  
144 =head2 Method name
145  
146 Optional argument: sets the object's name.
147 Returns the object's name.
148  
149 =cut
150  
151 1886 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
152 1886 my $self = shift;
153 1886 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1885 1886
154 1886 return $self->{name};
155 }
156  
157 =head2 Method signature
158  
159 signature() returns a tree's signature.
160  
161 In the context of Math::Symbolic, signatures are the list of variables
162 any given tree depends on. That means the tree "v*t+x" depends on the
163 variables v, t, and x. Thus, applying signature() on the tree that would
164 be parsed from above example yields the sorted list ('t', 'v', 'x').
165  
166 Constants do not depend on any variables and therefore return the empty list.
167 Obviously, operators' dependencies vary.
168  
169 Math::Symbolic::Variable objects, however, may have a slightly more
170 involved signature. By convention, Math::Symbolic variables depend on
171 themselves. That means their signature contains their own name. But they
172 can also depend on various other variables because variables themselves
173 can be viewed as placeholders for more compicated terms. For example
174 in mechanics, the acceleration of a particle depends on its mass and
175 the sum of all forces acting on it. So the variable 'acceleration' would
176 have the signature ('acceleration', 'force1', 'force2',..., 'mass', 'time').
177  
178 If you're just looking for a list of the names of all variables in the tree,
179 you should use the explicit_signature() method instead.
180  
181 =cut
182  
183 1112 ▶sub signature {
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
184 1112 my $self = shift;
185 1112 my $sig = [ @{ $self->{signature} } ]; # copying it
186 1112 push @$sig, $self->{name};
187  
188 # Make things unique, then sort and return.
189 1112 return sort keys %{ { map { ( $_, undef ) } @$sig } };
190 }
191  
192 =head2 Method explicit_signature
193  
194 explicit_signature() returns a lexicographically sorted list of
195 variable names in the tree.
196  
197 See also: signature().
198  
199 =cut
200  
201 97 ▶sub explicit_signature {
Subroutine explicit_signature: called CC=1 SCAR=0.0
Pod documented
202 97 return $_[0]->{name};
203 }
204  
205 =head2 Method set_signature
206  
207 set_signature expects any number of variable identifiers as arguments.
208 It sets a variable's signature to this list of identifiers.
209  
210 =cut
211  
212 1 ▶sub set_signature {
Subroutine set_signature: called CC=1 SCAR=0.0
Pod documented
213 1 my $self = shift;
214 1 @{ $self->{signature} } = @_;
215 1 return ();
216 }
217  
218 =head2 Method to_string
219  
220 Returns a string representation of the variable.
221  
222 =cut
223  
224 293 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
225 293 my $self = shift;
226 293 return $self->name();
227 }
228  
229 =head2 Method term_type
230  
231 Returns the type of the term. (T_VARIABLE)
232  
233 =cut
234  
235 8342 ▶sub term_type {
Subroutine term_type: called CC=1 SCAR=0.0
Pod documented
236 8342 return T_VARIABLE;
237 }
238  
239 23 1;
240 __END__
« Symbolic/MiscAlgebra.pm ↑ Summary Symbolic/MiscCalculus.pm »