BaH.muParser - fast math parser

BlitzMax Forums/Brucey's Modules/BaH.muParser - fast math parser

Brucey(Posted 2008) [#1]
Something new for the weekend ;-)
(well, I had a long weekend, so Monday counts!)

BaH.muParser is an extensible high performance math parser library. The speed comes with the conversion of the expression into bytecode.

You can define your own variables, constants, operators and functions.


Currently available via the maxmods SVN repository. Still working on the documentation, and will be released proper once that's done.


A small example of usage :
SuperStrict

Framework BaH.muParser
Import BRL.StandardIO

Try

	Local parser:TmuParser = New TmuParser

	Local fVal:Double = 1
	Local expr:String = "MyFunc(a) * sin(a)"

	parser.DefineVar("a", fVal)
	parser.DefineFun1("MyFunc", MyFunction)

	parser.SetExpr(expr)

	Print parser.Eval()
	
	' change the value of the variable and re-evaluate
	fVal = 15

	Print parser.Eval()

Catch e:TmuParserException

	Print e.message

End Try


Function MyFunction:Double(fVal:Double) 
	Return fVal * 10
End Function

'
' Output :
' 0.17452406437283513
' 38.822856765378113


Enjoy

:o)


xlsior(Posted 2008) [#2]
How does this compare speed-wise to native BlitzMax math operations? Is there any speedup there, or is it 'just' intended to make math operations scriptable?


Floyd(Posted 2008) [#3]
Since this will be used with BlitzMax the various trigonometric functions should probably use degrees.


Brucey(Posted 2008) [#4]
Is there any speedup there, or is it 'just' intended to make math operations scriptable?

No, it calls the same functions as BlitzMax does, and yes, it's just a parser. :-)

...trigonometric functions should probably use degrees.

Good point. Done.


plash(Posted 2008) [#5]
Excellent! Now I can allow scripts to do mathematical equations without doing any kind of parsing on my end. Thanks again Brucey.


Brucey(Posted 2008) [#6]
Released.

Please let me know if you have any problems with it. Thanks.


Floyd(Posted 2008) [#7]
It works. I ran the two examples with no problem.

I'm a little surprised that ATan2 is not one of the built-in functions. Of course it can be added easily enough.


Azathoth(Posted 2008) [#8]
How are constants used? Specifically string constants.