1Math::Symbolic::Base(3)User Contributed Perl DocumentatioMnath::Symbolic::Base(3)
2
3
4
6 Math::Symbolic::Base - Base class for symbols in symbolic calculations
7
9 use Math::Symbolic::Base;
10
12 This is a base class for all Math::Symbolic::* terms such as Math::Sym‐
13 bolic::Operator, Math::Symbolic::Variable and Math::Symbolic::Constant
14 objects.
15
16 EXPORT
17
18 None by default.
19
21 Method to_string
22
23 Default method for stringification just returns the object's value.
24
25 Method value
26
27 value() evaluates the Math::Symbolic tree to its numeric representa‐
28 tion.
29
30 value() without arguments requires that every variable in the tree con‐
31 tains a defined value attribute. Please note that this refers to every
32 variable object, not just every named variable.
33
34 value() with one argument sets the object's value (in case of a vari‐
35 able or constant).
36
37 value() with named arguments (key/value pairs) associates variables in
38 the tree with the value-arguments if the corresponging key matches the
39 variable name. (Can one say this any more complicated?) Since version
40 0.132, an alternative syntax is to pass a single hash reference.
41
42 Example: $tree->value(x => 1, y => 2, z => 3, t => 0) assigns the value
43 1 to any occurrances of variables of the name "x", aso.
44
45 If a variable in the tree has no value set (and no argument of value
46 sets it temporarily), the call to value() returns undef.
47
48 Method signature
49
50 signature() returns a tree's signature.
51
52 In the context of Math::Symbolic, signatures are the list of variables
53 any given tree depends on. That means the tree "v*t+x" depends on the
54 variables v, t, and x. Thus, applying signature() on the tree that
55 would be parsed from above example yields the sorted list ('t', 'v',
56 'x').
57
58 Constants do not depend on any variables and therefore return the empty
59 list. Obviously, operators' dependencies vary.
60
61 Math::Symbolic::Variable objects, however, may have a slightly more
62 involved signature. By convention, Math::Symbolic variables depend on
63 themselves. That means their signature contains their own name. But
64 they can also depend on various other variables because variables them‐
65 selves can be viewed as placeholders for more compicated terms. For
66 example in mechanics, the acceleration of a particle depends on its
67 mass and the sum of all forces acting on it. So the variable 'accelera‐
68 tion' would have the signature ('acceleration', 'force1', 'force2',...,
69 'mass', 'time').
70
71 If you're just looking for a list of the names of all variables in the
72 tree, you should use the explicit_signature() method instead.
73
74 Method explicit_signature
75
76 explicit_signature() returns a lexicographically sorted list of vari‐
77 able names in the tree.
78
79 See also: signature().
80
81 Method set_signature
82
83 set_signature expects any number of variable identifiers as arguments.
84 It sets a variable's signature to this list of identifiers.
85
86 Method implement
87
88 implement() works in-place!
89
90 Takes key/value pairs as arguments. The keys are to be variable names
91 and the values must be valid Math::Symbolic trees. All occurrances of
92 the variables will be replaced with their implementation.
93
94 Method replace
95
96 First argument must be a valid Math::Symbolic tree.
97
98 replace() modifies the object it is called on in-place in that it
99 replaces it with its first argument. Doing that, it retains the origi‐
100 nal object reference. This destroys the object it is called on.
101
102 However, this also means that you can create recursive trees of objects
103 if the new tree is to contain the old tree. So make sure you clone the
104 old tree using the new() method before using it in the replacement tree
105 or you will end up with a program that eats your memory fast.
106
107 fill_in_vars
108
109 This method returns a modified copy of the tree it was called on.
110
111 It walks the tree and replaces all variables whose value attribute is
112 defined (either done at the time of object creation or using
113 set_value()) with the corresponding constant objects. Variables whose
114 value is not defined are unaffected. Take, for example, the following
115 code:
116
117 $tree = parse_from_string('a*b+a*c');
118 $tree->set_value(a => 4, c => 10); # value of b still not defined.
119 print $tree->fill_in_vars();
120 # prints "(4 * b) + (4 * 10)"
121
122 Method simplify
123
124 Minimum method for term simpilification just clones.
125
126 Method descending_operands
127
128 When called on an operator, descending_operands tries hard to determine
129 which operands to descend into. (Which usually means all operands.) A
130 list of these is returned.
131
132 When called on a constant or a variable, it returns the empty list.
133
134 Of course, some routines may have to descend into different branches of
135 the Math::Symbolic tree, but this routine returns the default operands.
136
137 The first argument to this method may control its behaviour. If it is
138 any of the following key-words, behaviour is modified accordingly:
139
140 default -- obvious. Use default heuristics.
141
142 These are all supersets of 'default':
143 all -- returns ALL operands. Use with caution.
144 all_vars -- returns all operands that may contain vars.
145
146 Method descend
147
148 The method takes named arguments (key/value pairs). descend() descends
149 (Who would have guessed?) into the Math::Symbolic tree recursively and
150 for each node, it calls code references with a copy of the current node
151 as argument. The copy may be modified and will be used for construction
152 of the returned tree. The automatic copying behaviour may be turned
153 off.
154
155 Returns a (modified) copy of the original tree. If in-place modifica‐
156 tion is turned on, the returned tree will not be a copy.
157
158 Availlable parameters are:
159
160 before
161 A code reference to be used as a callback that will be invoked before
162 descent. Depending on whether or not the "in_place" option is set,
163 the callback will be passed a copy of the current node (default) or
164 the original node itself.
165
166 The callback may modify the tree node and the modified node will be
167 used to construct descend()'s return value.
168
169 The return value of this callback describes the way descend() handles
170 the descent into the current node's operands.
171
172 If it returns the empty list, the (possibly modified) copy of the
173 current that was passed to the callback is used as the return value
174 of descend(), but the recursive descent is continued for all of the
175 current node's operands which may or may not be modified by the call‐
176 back. The "after" callback will be called on the node after descent
177 into the operands. (This is the normal behavior.)
178
179 If the callback returns undef, the descent is stopped for the current
180 branch and an exact copy of the current branch's children will be
181 used for descend()'s return value. The "after" callback will be
182 called immediately.
183
184 If the callback returns a list of integers, these numbers are assumed
185 to be the indexes of the current node's operands that are to be
186 descended into. That means if the callback returns (1), descend will
187 be called for the second operand and only the second. All other chil‐
188 dren/operands will be cloned. As usual, the "after" callback will be
189 called after descent.
190
191 Any other return lists will lead to hard-to-debug errors. Tough luck.
192
193 Returning a hash reference from the callback allows for complete con‐
194 trol over the descend() routine. The hash may contain the following
195 elements:
196
197 operands
198 This is a referenced array that will be put in place of the previ‐
199 ous operands. It is the callback's job to make sure the number of
200 operands stays correct. The "operands" entry is evaluated before
201 the "descend_into" entry.
202
203 descend_into
204 This is a referenced array of integers and references. The integers
205 are assumed to be indices of the array of operands. Returning (1)
206 results in descent into the second operand and only the second.
207
208 References are assumed to be operands to descend into. descend()
209 will be directly called on them.
210
211 If the array is empty, descend() will act just as if an empty list
212 had been returned.
213
214 in_place
215 Boolean indicating whether or not to modify the operands in-place
216 or not. If this is true, descend() will be called with the
217 "in_place => 1" parameter. If false, it will be called with
218 "in_place => 0" instead. Defaults to false. (Cloning)
219
220 This does not affect the call to the "after" callback but only the
221 descent into operands.
222
223 skip_after
224 If this option exists and is set to true, the "after" callback will
225 not be invoked. This only applies to the current node, not to its
226 children/operands.
227
228 The list of options may grow in future versions.
229
230 after
231 This is a code reference which will be invoked as a callback after
232 the descent into the operands.
233
234 in_place
235 Controls whether or not to modify the current tree node in-place.
236 Defaults to false - cloning.
237
238 operand_finder
239 This option controls how the descend routine chooses which operands
240 to recurse into by default. That means it controls which operands
241 descend() recurses into if the 'before' routine returned the empty
242 list or if no 'before' routine was specified.
243
244 The option may either be a code reference or a string. If it is a
245 code reference, this code reference will be called with the current
246 node as argument. If it is a string, the method with that name will
247 be called on the current node object.
248
249 By default, descend() calls the 'descending_operands()' method on the
250 current node to determine the operands to descend into.
251
252 Method term_type
253
254 Returns the type of the term. This is a stub to be overridden.
255
256 Method set_value
257
258 set_value() returns the tree it modifies, but acts in-place on the
259 Math::Symbolic tree it was called on.
260
261 set_value() requires named arguments (key/value pairs) that associate
262 variable names of variables in the tree with the value-arguments if the
263 corresponging key matches the variable name. (Can one say this any
264 more complicated?) Since version 0.132, an alternative syntax is to
265 pass a single hash reference to the method.
266
267 Example: $tree->set_value(x => 1, y => 2, z => 3, t => 0) assigns the
268 value 1 to any occurrances of variables of the name "x", aso.
269
270 As opposed to value(), set_value() assigns to the variables permanently
271 and does not evaluate the tree.
272
273 When called on constants, set_value() sets their value to its first
274 argument, but only if there is only one argument.
275
277 Please send feedback, bug reports, and support requests to the
278 Math::Symbolic support mailing list: math-symbolic-support at lists dot
279 sourceforge dot net. Please consider letting us know how you use
280 Math::Symbolic. Thank you.
281
282 If you're interested in helping with the development or extending the
283 module's functionality, please contact the developers' mailing list:
284 math-symbolic-develop at lists dot sourceforge dot net.
285
286 List of contributors:
287
288 Steffen Müller, symbolic-module at steffen-mueller dot net
289 Stray Toaster, mwk at users dot sourceforge dot net
290 Oliver Ebenhöh
291
293 New versions of this module can be found on http://steffen-mueller.net
294 or CPAN. The module development takes place on Sourceforge at
295 http://sourceforge.net/projects/math-symbolic/
296
297 Math::Symbolic
298
299
300
301perl v5.8.8 2008-02-22 Math::Symbolic::Base(3)