File Coverage

blib/lib/Math/Symbolic/Constant.pm
Criterion Covered Total %
statement 57 58 98.2
branch 17 24 70.8
condition 18 27 66.6
subroutine 17 17 100.0
pod 11 11 100.0
total 120 137 87.5


line stmt bran cond sub pod time code
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     23   392 use 5.006;
  23         78  
  23         869  
30 23     23   116 use strict;
  23         41  
  23         717  
31 23     23   139 use warnings;
  23         57  
  23         634  
32 23     23   124 use Carp;
  23         40  
  23         1722  
33              
34 23     23   129 use Math::Symbolic::ExportConstants qw/:all/;
  23         43  
  23         5367  
35              
36 23     23   5924 use base 'Math::Symbolic::Base';
  23         47  
  23         16858  
37              
38             our $VERSION = '0.612';
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             sub new {
53 10508     10508 1 15371 my $proto = shift;
54 10508   66     33991 my $class = ref($proto) || $proto;
55              
56 10508         11600 my %args;
57 10508 50 66     39809 %args = %{ shift() } if @_ && ref( $_[0] ) eq 'HASH';
  0         0  
58              
59 10508 100 66     38961 my $value = ( @_ && !%args ? shift : $args{value} );
60 10508 100 100     42897 $value = $proto->value() if !defined($value) and ref($proto);
61              
62 10508 100       32840 croak("Math::Symbolic::Constant created with undefined value!")
63             if not defined($value);
64              
65 10507 100       49313 my $self = {
66             special => '',
67             ( ref($proto) ? %$proto : () ),
68             value => $value,
69             %args,
70             };
71              
72 10507         70584 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             sub zero {
83 168     168 1 889 my $proto = shift;
84 168   66     5626 my $class = ref($proto) || $proto;
85              
86 168 50       457 croak("Uneven number of arguments to zero()") if @_ % 2;
87              
88             return(
89 168         1122 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             sub one {
103 202     202 1 294 my $proto = shift;
104 202   33     802 my $class = ref($proto) || $proto;
105              
106 202 50       545 croak("Uneven number of arguments to one()") if @_ % 2;
107              
108             return(
109 202         1306 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             sub euler {
124 41     41 1 83 my $proto = shift;
125 41   66     214 my $class = ref($proto) || $proto;
126              
127 41 50       151 croak("Uneven number of arguments to euler()") if @_ % 2;
128              
129             return(
130 41         289 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             sub pi {
145 1     1 1 3 my $proto = shift;
146 1   33     4 my $class = ref($proto) || $proto;
147              
148 1 50       4 croak("Uneven number of arguments to pi()") if @_ % 2;
149              
150             return(
151 1         8 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, 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             sub value {
184 12745     12745 1 20043 my $self = shift;
185 12745 100 100     50200 if ( @_ == 1 and not ref( $_[0] ) eq 'HASH' ) {
186 1 50       6 croak "Constant assigned undefined value!"
187             if not defined $_[0];
188            
189 1         3 $self->{value} = $_[0];
190 1         2 $self->{special} = undef; # !!!FIXME!!! one day, this
191             # needs better handling.
192             }
193 12745         55763 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             sub signature {
223 283     283 1 1301 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             sub explicit_signature {
236 83     83 1 310 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             sub special {
247 531     531 1 831 my $self = shift;
248 531 50       1143 $self->{special} = shift if @_;
249 531         2154 return $self->{special};
250             }
251              
252             =head2 Method to_string
253              
254             Returns a string representation of the constant.
255              
256             =cut
257              
258             sub to_string {
259 312     312 1 445 my $self = shift;
260 312         714 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 8275     8275 1 18584 sub term_type { T_CONSTANT }
270              
271             1;
272             __END__