Giter Club home page Giter Club logo

mathpresso's Introduction

MathPresso - Mathematical Expression Evaluator And Jit Compiler for C++ Language

This is fork of MathPresso originally written by Petr Kobalicek. Main project is located at http://code.google.com/p/mathpresso/

MathPresso is C++ library that is able to parse and evaluate mathematical expressions. Because evaluating expressions can be slow MathPresso contains jit compiler that can compile expressions into machine code. Jit compiled expressions are many times faster than built-in evaluator. Thanks to AsmJit library MathPresso is able to compile functions for 32-bit (x86) and 64-bit (x64) processors.

MathPresso is also an example how is possible to do with AsmJit library.

Dependencies

AsmJit - 1.0 or later

Example

Example how to compile and evaluate expression:

#include <cstdlib>
#include <iostream>
#include <MathPresso/MathPresso.h>

using namespace std;

int main(void)
{
	MathPresso::Context ctx;
	MathPresso::Expression e;

	// values of variables used in expression
	MathPresso::mreal_t variables[] = { 1, 2 };

	// initialize the context with math environment (add math functions like sin, cos ..)
	ctx.addEnvironment(MathPresso::MENVIRONMENT_MATH);

	// add variables (name, offset)
	ctx.addVariable("x", 0 * sizeof(MathPresso::mreal_t));
	ctx.addVariable("y", 1 * sizeof(MathPresso::mreal_t));

	// compile an expression in specific context
	if (e.create(ctx, "pow(sin(x), 2) + pow(cos(x), 2)", MathPresso::MOPTION_NONE) != MathPresso::MRESULT_OK)
	{
		// handle compilation errr
		cerr << "Error compiling expression" << endl;
		return EXIT_FAILURE;
	}

	// if success then evaluate an expression 
	cout << e.evaluate(variables) << endl;

	return EXIT_SUCCESS;
}

JIT compilation

Currently MathPresso is able to generate SSE or SSE2 instructions. The code in default branch generates SSE instructions and operates with single precision floating point numbers (SP-FP). If you need to perform an operations with double precision floating point (DP-FP, 64 bit) then you need to check out DoublePresso branch.

Embedded functions

MathPresso supports following embedded functions:

  • min(x, y), max(x, y), avg(x, y)
  • ceil(x), floor(x), round(x)
  • abs(x)
  • reciprocal(x)
  • sqrt(x), pow(x, y)
  • log(x), log10(x)
  • sin(x), cos(x), tan(x), sinh(x), cosh(x), tanh(x), asin(x), acos(x), atan(x), atan2(x)

If it's not enough you can add your function to the context:

// implement your function
static mreal_t sum(mreal_t x, mreal_t y) { return x + y; }
...

// add your function to the context
ctx.addFunction("sum", (void *) sum, MathPresso::MFUNC_F_ARG2);
...

// and use it on expression
if (e.create(ctx, "1 + sum(x, y)", MathPresso::MOPTION_NONE) != MathPresso::MRESULT_OK)
{
...

mathpresso's People

Contributors

tartakynov avatar zueuk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mathpresso's Issues

Extend the input and output type?

Hi,

I was thinking about registering custom input/return functions like:

static std::vector<float> aggregate(MathPresso::mreal_t x, MathPresso::mreal_t y) {
    std::vector<float> ret;
        ret.push_back(x);
        ret.push_back(y);
        ret.push_back(x+y);
        return ret;
}

// and then
 ctx.addFunction("agg", (void *) aggregate, MathPresso::MFUNC_F_ARG2);

Compiles OK, but as evaluated, the expression returns real type, which crashes of course.

Is there a way that I can define functions working on vector type?

Doublepresso with current ASMJit compile problems

It looks like asmjit has evolved quite a bit since the doublepresso fork... for example, the asmjit namespace seems to have changed, Emittable isn't a part of the asmjit repository, and more.

Is there any hope of getting doublepresso updated to the current asmjit?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.