| 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<OVERLOADED OPERATORS>) 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 C<Math::Symbolic-E<gt>parse_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<SEE ALSO> | ||||||||||
| 55 | for an incomplete list of these. | ||||||||||
| 56 | |||||||||||
| 57 | For example, L<Math::Symbolic::MiscCalculus> come with C<Math::Symbolic> 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<Math::Symbolic::VectorCalculus> | ||||||||||
| 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<Math::Symbolic::MiscAlgebra>. This includes a routine to compute | ||||||||||
| 68 | the determinant of a matrix of C<Math::Symbolic> 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 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 122 | 23 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 123 | 23 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 124 | |||||||||||
| 125 | 23 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 126 | |||||||||||
| 127 | 23 | ▶ | use Math::Symbolic::ExportConstants qw/:all/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 128 | 23 | ▶ | use Math::Symbolic::AuxFunctions; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 129 | |||||||||||
| 130 | 23 | ▶ | use Math::Symbolic::Base; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 131 | 23 | ▶ | use Math::Symbolic::Operator; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 132 | 23 | ▶ | use Math::Symbolic::Variable; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 133 | 23 | ▶ | use Math::Symbolic::Constant; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 134 | |||||||||||
| 135 | 23 | ▶ | use Math::Symbolic::Derivative; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 136 | |||||||||||
| 137 | 23 | ▶ | use Math::Symbolic::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 138 | 23 | ▶ | use Math::Symbolic::Compiler; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 139 | |||||||||||
| 140 | 23 | ▶ | use Math::Symbolic::Custom; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 141 | |||||||||||
| 142 | 23 | require Exporter; | |||||||||
| 143 | |||||||||||
| 144 | 23 | our @ISA = qw(Exporter); | |||||||||
| 145 | |||||||||||
| 146 | our %EXPORT_TAGS = ( | ||||||||||
| 147 | all => [ | ||||||||||
| 148 | 23 | @{ $Math::Symbolic::ExportConstants::EXPORT_TAGS{all} }, | |||||||||
| 149 | qw{&parse_from_string}, | ||||||||||
| 150 | ], | ||||||||||
| 151 | 23 | constants => [ @{ $Math::Symbolic::ExportConstants::EXPORT_TAGS{all} }, ], | |||||||||
| 152 | ); | ||||||||||
| 153 | 23 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 154 | 23 | our @EXPORT = qw(); | |||||||||
| 155 | |||||||||||
| 156 | 23 | our $VERSION = '0.613'; | |||||||||
| 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 | 23 | 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<Math::Symbolic::Parser> for more information. | ||||||||||
| 180 | |||||||||||
| 181 | =cut | ||||||||||
| 182 | |||||||||||
| 183 | 358 | ▶ | sub parse_from_string { | ||||||||
|
Subroutine
parse_from_string: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 184 | 358 | my $string = shift; | |||||||||
| 185 | 358 | ▶ | croak "Missing string argument from parse_from_string() call" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | unless defined $string; | ||||||||||
| 187 | 357 | ▶ | if ($string eq 'Math::Symbolic') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | 93 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 189 | 92 | $string = shift; | |||||||||
| 190 | } | ||||||||||
| 191 | else { | ||||||||||
| 192 | 1 | croak("Missing string argument from Math::Symbolic->parse_from_string() call"); | |||||||||
| 193 | } | ||||||||||
| 194 | } | ||||||||||
| 195 | 356 | $string =~ s/\s+//gso; | |||||||||
| 196 | 356 | ▶ | if ( not defined $Parser ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 197 | 1 | $Parser = Math::Symbolic::Parser->new(); | |||||||||
| 198 | } | ||||||||||
| 199 | 356 | return $Parser->parse($string); | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | 23 | 1; | |||||||||
| 203 | __END__ | ||||||||||