.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Math::Symbolic::Parser 3pm" .TH Math::Symbolic::Parser 3pm "2021-01-07" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Math::Symbolic::Parser \- Parse strings into Math::Symbolic trees .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 4 \& use Math::Symbolic::Parser; \& my $parser = Math::Symbolic::Parser\->new(); \& $string =~ s/\es+//g; \& my $tree = $parser\->parse($string); \& \& # or better: \& use Math::Symbolic; \& my $tree = Math::Symbolic\->parse_from_string($string); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module contains the parsing routines used by Math::Symbolic to parse strings into Math::Symbolic trees. Usually, you will want to simply use the Math::Symbolic\->\fBparse_from_string()\fR class method instead of this module directly. If you do use this module directly, however, make sure to remove any whitespace from your input string. .SS "\s-1NOTE\s0" .IX Subsection "NOTE" With version 0.501 of Math::Symbolic, an experimental, new parser is introduced, but it is not enabled by default. The new parser is based on Parse::Yapp instead of Parse::RecDescent and comes with an at least ten fold speed increase. However, it has not been available for a long time and is not as well tested. Since version 2.00 of the Math::SymbolicX::ParserExtensionFactory module, it's possible to extend Yapp parsers. .PP \&\fBAt some point in the future the Yapp-based parser will become the default!\fR It is suggested you test your code against it before that. Code that uses the RecDescent based parser's \f(CW\*(C`Extend\*(C'\fR method may fail! .PP Until then, you need to load it by hand as follows: .PP .Vb 3 \& $Math::Symbolic::Parser = Math::Symbolic::Parser\->new( \& implementation=>\*(AqYapp\*(Aq \& ); .Ve .PP This replaces the default Math::Symbolic parser with an instance of the new Yapp parser. .SS "\s-1STRING FORMAT\s0" .IX Subsection "STRING FORMAT" The parser has been designed to parse strings that are reminiscient of ordinary algebraic expressions including the standard arithmetic infix operators such as multiplication. Many functions such as a rather comprehensive set of trigonometric functions are parsed in prefix form like 'sin(expression)' or 'log(base, expression)'. Unknown identifiers starting with a letter and containing only letters, digits, and underscores are parsed as variables. If these identifiers are followed by parenthesis containing a list of identifiers, the list is parsed as the signature of the variable. Example: '5*x(t)' is parsed as the product of the constant five and the variable 'x' which depends on 't'. These dependencies are important for total derivatives. .PP The supported builtin-functions are listed in the documentation for Math::Symbolic::Operator in the section on the \fBnew()\fR constructor. .SS "\s-1EXTENSIONS\s0" .IX Subsection "EXTENSIONS" In version 0.503, a function named \f(CW\*(C`exp(...)\*(C'\fR is recognized and transformed into \f(CW\*(C`e^(...)\*(C'\fR internally. In version 0.506, a function named \f(CW\*(C`sqrt(...)\*(C'\fR was added which is transformed into \f(CW\*(C`(...)^0.5\*(C'\fR. Version 0.511 added support for the typical \f(CW\*(C`f\*(Aq(x)\*(C'\fR syntax for derivatives. For details, refer to the section on parsing derivatives below. .SS "\s-1EXAMPLES\s0" .IX Subsection "EXAMPLES" .Vb 5 \& # An example from analytical mechanics: \& my $hamilton_function = \& Math::Symbolic\->parse_from_string( \& \*(Aqp_q(q, dq_dt, t) * dq_dt(q, t) \- Lagrange(q, p_q, t)\*(Aq \& ); .Ve .PP This parses as \*(L"The product of the generalized impulse p_q (which is a function of the generalized coordinate q, its derivative, and the time) and the derivative of the generalized coordinate dq_dt (which depends on q itself and the time). This term minus the Lagrange Function (of q, the impulse, and the time) is the Hamilton Function.\*(R" .PP Well, that's how it parses in my head anyway. The parser will generate a tree like this: .PP .Vb 10 \& Operator { \& type => difference, \& operands => ( \& Operator { \& type => product, \& operands => ( \& Variable { \& name => p_q, \& dependencies => q, dq_dt, t \& }, \& Variable { \& name => dq_dt, \& dependencies => q, t \& } \& ) \& }, \& Variable { \& name => Lagrange, \& dependencies => q, p_q, t \& } \& ) \& } .Ve .PP Possibly a simpler example would be 'amplitude * sin(phi(t))' which descibes an oscillation. sin(...) is assumed to be the sine function, amplitude is assumed to be a symbol / variable that doesn't depend on any others. phi is recognized as a variable that changes over time (t). So phi(t) is actually a function of t that hasn't yet been specified. phi(t) could look like 'omega*t + theta' where strictly speaking, omega, t, and theta are all symbols without dependencies. So omega and theta would be treated as constants if you derived them in respect to t. Figuratively speaking, omega would be a frequency and theta would be a initial value. .SS "\s-1PARSING DERIVATIVES\s0" .IX Subsection "PARSING DERIVATIVES" The traditional way of specifying a derivative for parsing was \&\f(CW\*(C`partial_derivative(EXPRESSION, VARIABLE)\*(C'\fR where \f(CW\*(C`EXPRESSION\*(C'\fR can be any valid expression and \f(CW\*(C`VARIABLE\*(C'\fR is a variable name. The syntax denotes a partial derivative of the expression with respect to the variable. The same syntax is available for total derivatives. .PP With version 0.511, a new syntax for specifying partial derivatives was added to the parser(s). \f(CW\*(C`f\*(Aq(x)\*(C'\fR denotes the first partial derivative of \f(CW\*(C`f\*(C'\fR with respect to \f(CW\*(C`x\*(C'\fR. If \f(CW\*(C`(x)\*(C'\fR is omitted, \&\f(CW\*(C`f\*(Aq\*(C'\fR defaults to using \f(CW\*(C`x\*(C'\fR. \f(CW\*(C`f\*(Aq\*(Aq(a)\*(C'\fR is the second order partial derivative with respect to \f(CW\*(C`a\*(C'\fR. If there are multiple variables in the parenthesis, a la \f(CW\*(C`f\*(Aq(b, a)\*(C'\fR, the first variable is used for the derivatives. .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" None by default. .SH "CLASS DATA" .IX Header "CLASS DATA" While working with this module, you might get into the not-so-convient position of having to debug the parser and/or its grammar. In order to make this possible, there's the \f(CW$DEBUG\fR package variable which, when set to 1, makes the parser warn which grammar elements are being processed. Note, however, that their order is bottom-up, not top-down. .SS "Constructor new" .IX Subsection "Constructor new" This constructor does not expect any arguments and returns a Parse::RecDescent parser to parse algebraic expressions from a string into Math::Symbolic trees. .PP The constructor takes key/value pairs of options. .PP You can regenerate the parser from the grammar in the scalar \&\f(CW$Math::Symbolic::Parser::Grammar\fR instead of using the (slightly faster) precompiled grammar from Math::Symbolic::Parser::Precompiled. You can enable recompilation from the grammar with the option \&\f(CW\*(C`recompile => 1\*(C'\fR. This only has an effect if the implementation is the Parse::RecDescent based parser (which is the default). .PP If you care about parsing speed more than about being able to extend the parser at run-time, you can specify the \f(CW\*(C`implementation\*(C'\fR option. Currently recognized are \f(CW\*(C`RecDescent\*(C'\fR and \f(CW\*(C`Yapp\*(C'\fR implementations. \f(CW\*(C`RecDescent\*(C'\fR is the default and \f(CW\*(C`Yapp\*(C'\fR is significantly faster. The Parse::Yapp based implementation may not support all extension modules. It has been tested with Math::SymbolicX::ParserExtensionFactory and Math::SymbolicX::Complex. .SH "AUTHOR" .IX Header "AUTHOR" Please send feedback, bug reports, and support requests to the Math::Symbolic support mailing list: math-symbolic-support at lists dot sourceforge dot net. Please consider letting us know how you use Math::Symbolic. Thank you. .PP If you're interested in helping with the development or extending the module's functionality, please contact the developers' mailing list: math-symbolic-develop at lists dot sourceforge dot net. .PP List of contributors: .PP .Vb 3 \& Steffen MXller, symbolic\-module at steffen\-mueller dot net \& Stray Toaster, mwk at users dot sourceforge dot net \& Oliver EbenhXh .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" New versions of this module can be found on http://steffen\-mueller.net or \s-1CPAN.\s0 The module development takes place on Sourceforge at http://sourceforge.net/projects/math\-symbolic/ .PP Math::Symbolic .PP Math::Symbolic::Parser::Precompiled .SH "ADDITIONAL COPYRIGHT NOTICE" .IX Header "ADDITIONAL COPYRIGHT NOTICE" This package is distributed under the same license as the rest of the Math::Symbolic distribution (Artistic+GPL), but the author of Parse::Yapp has requested that his copyright and the licensing terms of Parse::Yapp derived works be reproduced. Note that the license is the same as Math::Symbolic's license. We're using the \*(L"standalone parser\*(R" option. .PP .Vb 3 \& The Parse::Yapp module and its related modules and shell scripts \& are copyright (c) 1998\-2001 Francois Desarmenien, France. All \& rights reserved. \& \& You may use and distribute them under the terms of either the GNU \& General Public License or the Artistic License, as specified in \& the Perl README file. \& \& If you use the "standalone parser" option so people don\*(Aqt need to \& install Parse::Yapp on their systems in order to run you software, \& this copyright notice should be included in your software \& copyright too, and the copyright notice in the embedded driver \& should be left untouched. .Ve