File Coverage

blib/lib/Math/Symbolic/Constant.pm
Criterion Covered Total %
statement 56 57 98.2
branch 17 24 70.8
condition 18 27 66.6
subroutine 17 17 100.0
pod 11 11 100.0
total 119 136 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   377 use 5.006;
  23         81  
30 23     23   174 use strict;
  23         39  
  23         552  
31 23     23   98 use warnings;
  23         38  
  23         1145  
32 23     23   114 use Carp;
  23         39  
  23         1889  
33              
34 23     23   129 use Math::Symbolic::ExportConstants qw/:all/;
  23         70  
  23         4854  
35              
36 23     23   162 use base 'Math::Symbolic::Base';
  23         56  
  23         25407  
37              
38             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             sub new {
53 10711     10711 1 21942 my $proto = shift;
54 10711   66     47481 my $class = ref($proto) || $proto;
55              
56 10711         16851 my %args;
57 10711 50 66     35139 %args = %{ shift() } if @_ && ref( $_[0] ) eq 'HASH';
  0         0  
58              
59 10711 100 66     35618 my $value = ( @_ && !%args ? shift : $args{value} );
60 10711 100 100     32841 $value = $proto->value() if !defined($value) and ref($proto);
61              
62 10711 100       22699 croak("Math::Symbolic::Constant created with undefined value!")
63             if not defined($value);
64              
65 10710 100       46743 my $self = {
66             special => '',
67             ( ref($proto) ? %$proto : () ),
68             value => $value,
69             %args,
70             };
71              
72 10710         54912 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 590 my $proto = shift;
84 168   66     617 my $class = ref($proto) || $proto;
85              
86 168 50       459 croak("Uneven number of arguments to zero()") if @_ % 2;
87              
88             return(
89 168         972 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 362 my $proto = shift;
104 202   33     787 my $class = ref($proto) || $proto;
105              
106 202 50       589 croak("Uneven number of arguments to one()") if @_ % 2;
107              
108             return(
109 202         1253 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 88 my $proto = shift;
125 41   66     237 my $class = ref($proto) || $proto;
126              
127 41 50       161 croak("Uneven number of arguments to euler()") if @_ % 2;
128              
129             return(
130 41         251 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     8 my $class = ref($proto) || $proto;
147              
148 1 50       5 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 13044     13044 1 21333 my $self = shift;
185 13044 100 100     46075 if ( @_ == 1 and not ref( $_[0] ) eq 'HASH' ) {
186 1 50       4 croak "Constant assigned undefined value!"
187             if not defined $_[0];
188            
189 1         2 $self->{value} = $_[0];
190 1         1 $self->{special} = undef; # !!!FIXME!!! one day, this
191             # needs better handling.
192             }
193 13044         46357 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 288     288 1 716 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 230 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 890 my $self = shift;
248 531 50       1198 $self->{special} = shift if @_;
249 531         1926 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 509 my $self = shift;
260 312         751 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     8304 1 15452 sub term_type { T_CONSTANT }
270              
271             1;
272             __END__