Giter VIP home page Giter VIP logo

runexp's Introduction

RunExp v1.1

A lightweight runtime math expressions solver/compiler for JVM.

  • does not use any scripting engine
  • uses java reflection
  • writes JVM bytecode
  • aimed to reuse compiled expressions

Future:

  • integer expressions, bitwise operations
  • double precision expressions

Download built jar library file from GitHub

Dependecies:

RunExp requires one dependency since v1.1

version used: asm-all-5.2

ASM is used for runtime java-bytecode generation.

Usage:

with x:

Expression expression = RunExp.compile("sin(x) * 0.5 + (x * 0.1)");
expression.eval(1.25f); // same as sin(1.25f) * 0.5f + (1.25f * 0.1f)

// or

RunExpSolver solver = new RunExpSolver();
Expression expression = solver.compile("sin(x) * 0.5 + (x * 0.1)");
expression.eval(1.25f);

Solve constant expression:

float value = RunExp.eval("pi * 0.5");

if Expression wrapper needed (ConstantExpression used):

ConstantExpression expression = RunExp.compileConstant("pi ^ 2");

Solvers:

RunExp class uses RunExpSolver instance available as RunExp.solver, but it's preferred to create new one.

RunExpSolver solver = new RunExpSolver();
solver.addConstant("g", 9.8f);
...
float value = solver.eval(expressionString);

solver.allowJVM - allow compiling expressions directly into JVM bytecode (true by default)

Custom constants:

Method:

RunExpSolver.addConstant(String name, float value);

Example:

solver.addConstant("g", 9.8f);

Custom functions:

Methods:

RunExpSolver.addFunction(String name, Class<?> class, String methodName);
RunExpSolver.addFunction(String name, Class<?> class, String methodName, Class<?>... args);

Example:

try {
  // adding Noise.noise2d(float, float) static method as function 'noise'
  solver.addFunction("noise", Noise.class, "noise2d");
  // if Noise.noise is overloaded
  solver.addFunction("noise", Noise.class, "noise2d", class.float, class.float);
} catch (NoSuchMethodException e){
  ...
}
// see RunExpSolver.addFunction docs for more info

Built-in functions may be overriden.

Example:

// override built-in 'rand' with some MathUtils.random method
solver.addFunction("rand", MathUtils.class, "random");

Features:

  • unary operations: '-'
  • binary operations: '+', '-', '*', '/' and '^' (exponentation)
  • functions:
    • abs
    • sin, cos, tan
    • sqrt, exp, pow (same as '^' operator)
    • min(a, b), max(a, b)
    • round, floor, ceil
    • sign / signum
    • rand - random number in range [0.0, 1.0]
    • smoother (smoother step)
  • constants:
    • pi (Math.PI)
    • pi2 (Math.PI * 2)
    • e (Math.E)
    • raddeg (180.0 / Math.PI) usage: degrees = radians * raddeg
    • degrad (Math.PI / 180.0) usage: radians = degreen * degrad
  • custom constants
  • custom functions (directly calling static methods as functions)

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.