File Coverage

blib/lib/Math/Symbolic.pm
Criterion Covered Total %
statement 52 52 100.0
branch 8 8 100.0
condition n/a
subroutine 15 15 100.0
pod 1 1 100.0
total 76 76 100.0


line stmt bran cond sub pod time code
1             =encoding utf8
2              
3             =head1 NAME
4              
5             Math::Symbolic - Symbolic calculations
6              
7             =head1 SYNOPSIS
8              
9             use Math::Symbolic;
10            
11             my $tree = Math::Symbolic->parse_from_string('1/2 * m * v^2');
12             # Now do symbolic calculations with $tree.
13             # ... like deriving it...
14            
15             my ($sub) = Math::Symbolic::Compiler->compile_to_sub($tree);
16              
17             my $kinetic_energy = $sub->($mass, $velocity);
18              
19             =head1 DESCRIPTION
20              
21             Math::Symbolic is intended to offer symbolic calculation capabilities
22             to the Perl programmer without using external (and commercial) libraries
23             and/or applications.
24              
25             Unless, however, some interested and knowledgable developers turn up to
26             participate in the development, the library will be severely limited by
27             my experience in the area. Symbolic calculations are an active field of
28             research in CS.
29              
30             There are several ways to construct Math::Symbolic trees. There are no
31             actual Math::Symbolic objects, but rather trees of objects of subclasses of
32             Math::Symbolic. The most general but unfortunately also the least intuitive
33             way of constructing trees is to use the constructors of
34             the Math::Symbolic::Operator, Math::Symbolic::Variable, and
35             Math::Symbolic::Constant classes to create (nested) objects of the
36             corresponding types.
37              
38             Furthermore, you may use the overloaded interface to apply the standard
39             Perl operators (and functions, see L) to existing
40             Math::Symbolic trees and standard Perl expressions.
41              
42             Possibly the most convenient way of constructing Math::Symbolic trees is
43             using the builtin parser to generate trees from expressions such as C<2 * x^5>.
44             You may use the Cparse_from_string()> class method for this.
45              
46             Of course, you may combine the overloaded interface with the parser to
47             generate trees with Perl code such as C<$term * 5 * 'sin(omega*t+phi)'> which
48             will create a tree of the existing tree $term times 5 times the sine of
49             the vars omega times t plus phi.
50              
51             There are several modules in the distribution that contain subroutines
52             related to calculus. These are not loaded by Math::Symbolic by default.
53             Furthermore, there are several extensions to Math::Symbolic available
54             from CPAN as separate distributions. Please refer to L
55             for an incomplete list of these.
56              
57             For example, L come with C and
58             contains routines to compute Taylor Polynomials and the associated errors.
59              
60             Routines related to vector calculus such as grad, div, rot, and Jacobi- and
61             Hesse matrices are available through the L
62             module. This module is also able to compute Taylor Polynomials of
63             functions of two variables, directional derivatives, total differentials,
64             and Wronskian Determinants.
65              
66             Some basic support for linear algebra can be found in
67             L. This includes a routine to compute
68             the determinant of a matrix of C trees.
69              
70             =head2 EXPORT
71              
72             None by default, but you may choose to have the following constants
73             exported to your namespace using the standard Exporter semantics.
74             There are two export tags: :all and :constants. :all will export
75             all constants and the parse_from_string subroutine.
76              
77             Constants for transcendetal numbers:
78             EULER (2.7182...)
79             PI (3.14159...)
80            
81             Constants representing operator types: (First letter indicates arity)
82             (These evaluate to the same numbers that are returned by the type()
83             method of Math::Symbolic::Operator objects.)
84             B_SUM
85             B_DIFFERENCE
86             B_PRODUCT
87             B_DIVISION
88             B_LOG
89             B_EXP
90             U_MINUS
91             U_P_DERIVATIVE (partial derivative)
92             U_T_DERIVATIVE (total derivative)
93             U_SINE
94             U_COSINE
95             U_TANGENT
96             U_COTANGENT
97             U_ARCSINE
98             U_ARCCOSINE
99             U_ARCTANGENT
100             U_ARCCOTANGENT
101             U_SINE_H
102             U_COSINE_H
103             U_AREASINE_H
104             U_AREACOSINE_H
105             B_ARCTANGENT_TWO
106            
107             Constants representing Math::Symbolic term types:
108             (These evaluate to the same numbers that are returned by the term_type()
109             methods.)
110             T_OPERATOR
111             T_CONSTANT
112             T_VARIABLE
113            
114             Subroutines:
115             parse_from_string (returns Math::Symbolic tree)
116              
117             =cut
118              
119             package Math::Symbolic;
120              
121 23     23   738683 use 5.006;
  23         93  
  23         989  
122 23     23   164 use strict;
  23         47  
  23         955  
123 23     23   132 use warnings;
  23         76  
  23         844  
124              
125 23     23   148 use Carp;
  23         44  
  23         2596  
126              
127 23     23   13728 use Math::Symbolic::ExportConstants qw/:all/;
  23         60  
  23         6855  
128 23     23   30040 use Math::Symbolic::AuxFunctions;
  23         80  
  23         884  
129              
130 23     23   19621 use Math::Symbolic::Base;
  23         81  
  23         794  
131 23     23   42663 use Math::Symbolic::Operator;
  23         89  
  23         908  
132 23     23   17838 use Math::Symbolic::Variable;
  23         66  
  23         705  
133 23     23   13866 use Math::Symbolic::Constant;
  23         66  
  23         674  
134              
135 23     23   143 use Math::Symbolic::Derivative;
  23         44  
  23         1000  
136              
137 23     23   16184 use Math::Symbolic::Parser;
  23         68  
  23         765  
138 23     23   16450 use Math::Symbolic::Compiler;
  23         75  
  23         1041  
139              
140 23     23   13849 use Math::Symbolic::Custom;
  23         87  
  23         7431  
141              
142             require Exporter;
143              
144             our @ISA = qw(Exporter);
145              
146             our %EXPORT_TAGS = (
147             all => [
148             @{ $Math::Symbolic::ExportConstants::EXPORT_TAGS{all} },
149             qw{&parse_from_string},
150             ],
151             constants => [ @{ $Math::Symbolic::ExportConstants::EXPORT_TAGS{all} }, ],
152             );
153             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
154             our @EXPORT = qw();
155              
156             our $VERSION = '0.612';
157              
158             =head1 CLASS DATA
159              
160             The package variable $Parser will contain a Parse::RecDescent
161             object that is used to parse strings at runtime.
162              
163             =cut
164              
165             our $Parser = Math::Symbolic::Parser->new();
166              
167             =head1 SUBROUTINES
168              
169             =head2 parse_from_string
170              
171             This subroutine takes a string as argument and parses it using
172             a Parse::RecDescent parser taken from the package variable
173             $Math::Symbolic::Parser. It generates a Math::Symbolic tree
174             from the string and returns that tree.
175              
176             The string may contain any identifiers matching /[a-zA-Z][a-zA-Z0-9_]*/ which
177             will be parsed as variables of the corresponding name.
178              
179             Please refer to L for more information.
180              
181             =cut
182              
183             sub parse_from_string {
184 357     357 1 52802 my $string = shift;
185 357 100       1561 croak "Missing string argument from parse_from_string() call"
186             unless defined $string;
187 356 100       1331 if ($string eq 'Math::Symbolic') {
188 93 100       323 if (@_) {
189 92         221 $string = shift;
190             }
191             else {
192 1         109 croak("Missing string argument from Math::Symbolic->parse_from_string() call");
193             }
194             }
195 355         1426 $string =~ s/\s+//gso;
196 355 100       1319 if ( not defined $Parser ) {
197 1         10 $Parser = Math::Symbolic::Parser->new();
198             }
199 355         3385 return $Parser->parse($string);
200             }
201              
202             1;
203             __END__