Math::...Parser(3pm)
π Math::Symbolic::Parser β the manual page, but make it make sense π
okay bestie, this is Math::Symbolic::Parser, a Perl module (section 3pm, "User Contributed Perl Documentation") that literally turns your strings into Math::Symbolic trees. parse strings β Math::Symbolic trees. that's the whole job. let's get into it π
β¨ SYNOPSIS β¨
the classic way:
use Math::Symbolic::Parser;
my $parser = Math::Symbolic::Parser->new();
$string =~ s/\s+//g;
my $tree = $parser->parse($string);
# or better:
use Math::Symbolic;
my $tree = Math::Symbolic->parse_from_string($string);
no cap, the second one is the upgrade. Math::Symbolic->parse_from_string($string) is giving convenience π
πΈ DESCRIPTION πΈ
so this module is where the parsing routines live β the ones Math::Symbolic uses to turn strings into Math::Symbolic trees. normally you're not going to touch this module directly at all; you'll just use the Math::Symbolic->parse_from_string() class method instead. icon behavior honestly.
but β and this is the important part β if you do use this module directly, you gotta make sure you remove any whitespace from your input string first. like. all of it. the synopsis shows the s/\s+//g move for a reason π«Ά
π NOTE π
okay so as of version 0.501 of Math::Symbolic, there's an experimental new parser. it's not enabled by default, so don't panic. it's based on Parse::Yapp instead of Parse::RecDescent, and it comes with an at least ten fold speed increase π₯. however β plot twist β it hasn't been available for a long time and it is not as well tested. since version 2.00 of the Math::SymbolicX::ParserExtensionFactory module, it's been possible to extend Yapp parsers.
at some point in the future the Yapp-based parser will become the default!! so the suggestion is: test your code against it before that happens. code that uses the RecDescent based parser's Extend method may fail! π
until then, you gotta load it by hand, like this:
$Math::Symbolic::Parser = Math::Symbolic::Parser->new(
implementation=>'Yapp'
);
this replaces the default Math::Symbolic parser with an instance of the new Yapp parser. slay.
π¦ STRING FORMAT π¦
the parser was designed to parse strings that are reminiscient of ordinary algebraic expressions, including the standard arithmetic infix operators such as multiplication. many functions β a rather comprehensive set of trigonometric functions, for example β get parsed in prefix form like 'sin(expression)' or 'log(base, expression)'.
unknown identifiers that start with a letter and contain only letters, digits, and underscores get parsed as variables. and if those identifiers are followed by parenthesis containing a list of identifiers, that list is parsed as the signature of the variable. example: '5*x(t)' parses as the product of the constant five and the variable 'x' which depends on 't'. these dependencies are important for total derivatives. periodt.
the supported builtin-functions are listed in the documentation for Math::Symbolic::Operator, in the section on the new() constructor. iykyk π
π§ EXTENSIONS π§
- in version 0.503, a function named
exp(...)is recognized and transformed into"e^(...)"internally. - in version 0.506, a function named
sqrt(...)was added, which is transformed into"(...)^0.5". - version 0.511 added support for the typical
"f'(x)"syntax for derivatives.
for details on that last one, refer to the section on parsing derivatives below π
π EXAMPLES π
an example from analytical mechanics, because we're fancy:
# An example from analytical mechanics:
my $hamilton_function =
Math::Symbolic->parse_from_string(
'p_q(q, dq_dt, t) * dq_dt(q, t) - Lagrange(q, p_q, t)'
);
this parses as "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."
well, that's how it parses in my head anyway π the parser will generate a tree like this:
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
}
)
}
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. π«
π PARSING DERIVATIVES π
the traditional way of specifying a derivative for parsing was "partial_derivative(EXPRESSION, VARIABLE)" where "EXPRESSION" can be any valid expression and "VARIABLE" is a variable name. that syntax denotes a partial derivative of the expression with respect to the variable. the same syntax is available for total derivatives.
with version 0.511, a new syntax for specifying partial derivatives was added to the parser(s). "f'(x)" denotes the first partial derivative of "f" with respect to "x". if "(x)" is omitted, "f'" defaults to using "x". "f''(a)" is the second order partial derivative with respect to "a". and if there are multiple variables in the parenthesis, a la "f'(b, a)", the first variable is used for the derivatives. got it? got it π
β EXPORT β
None by default.
π 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. to make that possible, there's the $DEBUG package variable β and when you set it to 1, the parser warns which grammar elements are being processed. ngl that's pretty useful.
but note: their order is bottom-up, not top-down. okay? okay.
π Constructor new π
this constructor does not expect any arguments, and it returns a Parse::RecDescent parser to parse algebraic expressions from a string into Math::Symbolic trees.
the constructor takes key/value pairs of options.
you can regenerate the parser from the grammar in the scalar $Math::Symbolic::Parser::Grammar instead of using the (slightly faster) precompiled grammar from Math::Symbolic::Parser::Precompiled. you can enable recompilation from the grammar with the option recompile => 1. this only has an effect if the implementation is the Parse::RecDescent based parser (which is the default).
if you care about parsing speed more than about being able to extend the parser at run-time, you can specify the implementation option. currently recognized are RecDescent and Yapp implementations. RecDescent is the default and Yapp 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.
πΈ 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 π«Ά
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.
list of contributors:
Steffen MΓΆller, symbolic-module at steffen-mueller dot net
Stray Toaster, mwk at users dot sourceforge dot net
Oliver EbenhΓΆh
π SEE ALSO π
new versions of this module can be found on http://steffen-mueller.net or CPAN. the module development takes place on Sourceforge at http://sourceforge.net/projects/math-symbolic/
Math::SymbolicMath::Symbolic::Parser::Precompiled
π 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 "standalone parser" option.
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't 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.
license drama aside, please keep that notice intact bestie, the copyright gods are watching π
aaaand that's the page!! π this one's from perl v5.40.0, dated 2024-09-16 β so you know it's fresh. go parse something iconic πβ¨π¦
π¦ where this page comes from
- ubuntu resolute: libmath-symbolic-perl 0.613-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libmath-symbolic-perl 0.613-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms