GithubHelp home page GithubHelp logo

roelroel / fparse Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bylexus/fparse

0.0 0.0 0.0 78 KB

A JavaScript Formula Parser

Home Page: http://fparse.alexi.ch/

JavaScript 98.34% HTML 1.66%

fparse's Introduction

fparser Build Status

A JavaScript Formula Parser

fparser provides a Formula class that parses strings containing mathematical formulas (e.g. x*sin(PI*x/2)) into an evaluationable object. One can then provide values for all unknown variables / functions and evaluate a numeric value from the formula.

For an example application, see http://fparse.alexi.ch/.

Features

Parses a mathematical formula from a string. Known expressions:

  • Numbers in the form [-]digits[.digits], e.g. "-133.2945"
  • simple operators: '+','-','*','/', '^' expanded in correct order
  • parentheses '(', ')' for grouping (e.g. "5*(3+2)")
  • all JavaScript Math object functions (e.g. "sin(3.14)")
  • all JavaScript Math constants like PI, E
  • the use of own functions
  • the use of single-char variables (like '2x')
  • the use of named variables (like '2*[myVar]')
  • use it in Web pages, as ES6 module or as NodeJS module
  • Example:
    -1*(sin(2^x)/(PI*x))*cos(x))

Usage

<!-- Within a web page: Load the fparser library: -->
<script src="dist/fparser.js"></script>
// As node module:
Install:
$ npm install --save fparser

Use:
var Formula = require('./fparser');

or:
import Formula from 'fparser';
// 1. Create a Formula object instance by passing a formula string:
var fObj = new Formula('2^x');

// 2. evaluate the formula, delivering a value object for each unknown entity:
var result = fObj.evaluate({x: 3}); // result = 8

// or deliver multiple value objects to return multiple results:
var results = fObj.evaluate([{x: 2},{x: 4},{x: 8}]); // results = [4,16,256]

// You can also directly evaluate a value if you only need a one-shot result:
var result = Formula.calc('2^x',{x: 3}); // result = 8
var results = fObj.calc('2^x',[{x: 2},{x: 4},{x: 8}]); // results = [4,16,256]

// Usage in NodeJS:
var Formula = require('./fparser');
var fObj = new Formula('2^x)');
// .... vice versa

Advanced Usage

Using multiple variables

var fObj = new Formula('a*x^2 + b*x + c');

// Just pass a value object containing a value for each unknown variable:
var result = fObj.evaluate({a:2,b:-1,c:3,x:3}); // result = 18

Using named variables

Instead of single-char variables (like 2x+y), you can also use named variables in brackets:

var fObj = new Formula('2*[var1] + sin([var2]+PI)');

// Just pass a value object containing a value for each named variable:
var result = fObj.evaluate({var1: 5, var2: 0.7});

Using user-defined functions

var fObj = new Formula('sin(inverse(x))');

//Define the function(s) on the Formula object, then use it multiple times:
fObj.inverse = function(value){
    return 1/value;
};
var results = fObj.evaluate({x: 1,x:2,x:3});

// Or pass it in the value object, and OVERRIDE an existing function:
var result = fObj.evaluate({
	x: 2/Math.PI,
	inverse: function(value){
		return -1*value;
	}
});

If defined in the value object AND on the formula object, the Value object has the precedence

Get all used variables

// Get all used variables in the order of their appereance:
var f4 = new Formula('x*sin(PI*y) + y / (2-x*[var1]) + [var2]');
console.log(f4.getVariables()); // ['x','y','var1','var2']

Changelog

1.4.0

  • Adding support for named variables (2x + [var1])
  • switched testing to chromium runner instead of PhantomJS

1.3.0

  • modernized library: The source is now ES6 code, and transpiled in a dist ES5+ library.
  • Make sure you include dist/fparser.js if you are using it as a browser library.
  • Drop support for Bower, as there are more modern approaches (npm) for package dependency nowadays

fparse's People

Contributors

bylexus avatar

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.