ParserFunction-0.1.0: Parse and evaluate mathematical expressions.

Safe HaskellSafe-Inferred

Text.Parsec.Expr.Math

Contents

Description

Parse and evaluate mathematical expressions.

Synopsis

Expressions

data Expr a Source

Mathematical expressions.

Constructors

Num a 
Var String 
Neg (Expr a) 
Add (Expr a) (Expr a) 
Sub (Expr a) (Expr a) 
Mul (Expr a) (Expr a) 
Div (Expr a) (Expr a) 
Pow (Expr a) (Expr a) 
Sqrt (Expr a) 
Exp (Expr a) 
Log (Expr a) 
Abs (Expr a) 
Sin (Expr a) 
Cos (Expr a) 
Tan (Expr a) 
Sec (Expr a) 
Csc (Expr a) 
Cot (Expr a) 
Sinh (Expr a) 
Cosh (Expr a) 
Tanh (Expr a) 
Sech (Expr a) 
Csch (Expr a) 
Coth (Expr a) 
ArcSin (Expr a) 
ArcCos (Expr a) 
ArcTan (Expr a) 
ArcSec (Expr a) 
ArcCsc (Expr a) 
ArcCot (Expr a) 
ArcSinh (Expr a) 
ArcCosh (Expr a) 
ArcTanh (Expr a) 
ArcSech (Expr a) 
ArcCsch (Expr a) 
ArcCoth (Expr a) 

Instances

Show a => Show (Expr a) 

Parsing

parse :: Floating a => String -> Either ParseError (Expr a)Source

Parse a mathematical expression.

 >>> parse "exp(-pi*i)+1"
 Right (Add (Exp (Mul (Neg (Var "pi")) (Var "i"))) (Num 1.0))

data ParseError

The abstract data type ParseError represents parse errors. It provides the source position (SourcePos) of the error and a list of error messages (Message). A ParseError can be returned by the function parse. ParseError is an instance of the Show class.

Instances

Evaluation

evaluateSource

Arguments

:: Floating a 
=> Map String a

Variable definitions.

-> Maybe (Expr a)

Mathematical expression.

-> Maybe a 

Evaluate a mathematical expression using the supplied variable definitions.

 >>> :m + Data.Complex Data.Map
 >>> let def = fromList [("pi", pi), ("i", 0 :+ 1)]
 >>> evaluate def . Just $ Add (Exp (Mul (Neg (Var "pi")) (Var "i"))) (Num 1.0)
 Just (0.0 :+ (-1.2246467991473532e-16))