Math::Eval -- Evaluate Mathematic terms
#!/usr/bin/perl use strict; use warnings; use Math::Eval; my $meval=Math::Eval->parse("7*x^9+5*x^3+11"); die("PARSE ERROR") unless($meval); print "f(x)=".$meval->get_string()."\n"; print "f($_)=".$meval->evaluate(x => $_)."\n" for(-10..10); my $meval2=$meval->derivative('x'); print "f'(x)=".$meval2->get_string()."\n"; print "f'($_)=".$meval2->evaluate(x => $_)."\n" for(-10..10);
parse Mathematical String and evaluate the results
ToPeG
libmatheval
Inline::C
Create Math::Eval object from string containing mathematical representation of function. Math::Eval object could be used later to evaluate function for specific variable values or to calculate function derivative over some variable.
String representation of function is allowed to consist of decimal numbers, constants, variables, elementary functions, unary and binary operations.
Supported constants are (names that should be used are given in parenthesis): e (e), log2(e) (log2e), log10(e) (log10e), ln(2) (ln2), ln(10) (ln10), pi (pi), pi / 2 (pi_2), pi / 4 (pi_4), 1 / pi (1_pi), 2 / pi (2_pi), 2 / sqrt(pi) (2_sqrtpi), sqrt(2) (sqrt) and sqrt(1 / 2) (sqrt1_2).
Variable name is any combination of alphanumericals and _ characters beginning with a non-digit that is not elementary function name.
Supported elementary functions are (names that should be used are given in parenthesis): exponential (exp), logarithmic (log), square root (sqrt), sine (sin), cosine (cos), tangent (tan), cotangent (cot), secant (sec), cosecant (csc), inverse sine (asin), inverse cosine (acos), inverse tangent (atan), inverse cotangent (acot), inverse secant (asec), inverse cosecant (acsc), hyperbolic sine (sinh), cosine (cosh), hyperbolic tangent (tanh), hyperbolic cotangent (coth), hyperbolic secant (sech), hyperbolic cosecant (csch), hyperbolic inverse sine (asinh), hyperbolic inverse cosine (acosh), hyperbolic inverse tangent (atanh), hyperbolic inverse cotangent (acoth), hyperbolic inverse secant (asech), hyperbolic inverse cosecant (acsch), absolute value (abs), Heaviside step function (step) with value 1 defined for x = 0, Dirac delta function with infinity (delta) and not-a-number (nandelta) values defined for x = 0, and error function (erf).
Supported unary operation is unary minus (’-’).
Supported binary operations are addition (’+’), subtraction (’+’), multiplication (’*’), division multiplication (’/’) and exponentiation (’^’).
Usual mathematical rules regarding operation precedence apply. Parenthesis (’(’ and ’)’) could be used to change priority order.
Return array of strings with names of variables appearing in function repre- sented by Math::Eval object
Return textual representation (i.e. mathematical function) of Math::Eval object
Calculate value of function represented by Math::Eval object for given variable values.
Create Math::Eval object for derivative of function represented by given Math::Eval object
see libmatheval
Errorhandling
Tobias Grönhagen <osm@topeg.de>
Version 1.0 17 Apr 2011
Copyright (c) Tobias Grönhagen. All rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms and conditions as Perl itself.
This means that you can, at your option, redistribute it and/or modify it under either the terms the GNU Public License (GPL) version 2 or later, or under the Perl Artistic License.
See http://dev.perl.org/licenses/
THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Use of this software in any way or in any form, source or binary, is not allowed in any country which prohibits disclaimers of any implied warranties of merchantability or fitness for a particular purpose or any disclaimers of a similar nature.
IN NO EVENT SHALL I BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION (INCLUDING, BUT NOT LIMITED TO, LOST PROFITS) EVEN IF I HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE