All Projects → madorin → Matex

madorin / Matex

Licence: mit
PHP Mathematical expression parser and evaluator

Projects that are alternatives of or similar to Matex

Dentaku
math and logic formula parser and evaluator
Stars: ✭ 636 (+1056.36%)
Mutual labels:  calculator, formula
node calculator
Create Maya node-network by entering a math-formula.
Stars: ✭ 56 (+1.82%)
Mutual labels:  calculator, formula
keisan
A Ruby-based expression parser, evaluator, and programming language
Stars: ✭ 48 (-12.73%)
Mutual labels:  calculator, formula
Formula Parser
Javascript Library parsing Excel Formulas and more
Stars: ✭ 544 (+889.09%)
Mutual labels:  formula
Readme2tex
Renders TeXy Math for Github Readmes
Stars: ✭ 826 (+1401.82%)
Mutual labels:  formula
Derivative Calculator
calculates the derivative of the given function
Stars: ✭ 21 (-61.82%)
Mutual labels:  calculator
Adder
Executing untrusted code with ease.
Stars: ✭ 45 (-18.18%)
Mutual labels:  eval
Ncalc
Power calculator for Android. Solve some problem algebra and calculus.
Stars: ✭ 512 (+830.91%)
Mutual labels:  calculator
Luckysheet
Luckysheet is an online spreadsheet like excel that is powerful, simple to configure, and completely open source.
Stars: ✭ 9,772 (+17667.27%)
Mutual labels:  formula
P5js
Simplex Noise & WebGL with p5js from Processing.Org
Stars: ✭ 12 (-78.18%)
Mutual labels:  formula
Clock Tuner
Tweaks clock frequency of HP 39gs to play retro System RPL games
Stars: ✭ 9 (-83.64%)
Mutual labels:  calculator
Nsdl
SDL for TI-Nspire calculators
Stars: ✭ 27 (-50.91%)
Mutual labels:  calculator
Js calculator
JavaScript模仿Windows10计算器
Stars: ✭ 34 (-38.18%)
Mutual labels:  calculator
Php Math Parser
Simple mathematical expression parser and calculator.
Stars: ✭ 24 (-56.36%)
Mutual labels:  calculator
Cemu
Third-party TI-84 Plus CE / TI-83 Premium CE emulator, focused on developer features
Stars: ✭ 593 (+978.18%)
Mutual labels:  calculator
Sharpmath
A small .NET math library.
Stars: ✭ 36 (-34.55%)
Mutual labels:  calculator
Reogrid
Fast and powerful .NET spreadsheet component, support data format, freeze, outline, formula calculation, chart, script execution and etc. Compatible with Excel 2007 (.xlsx) format and working on .NET 3.5 (or client profile), WPF and Android platform.
Stars: ✭ 532 (+867.27%)
Mutual labels:  formula
Ccalc
Scientific calculator in which you can define new constants and functions
Stars: ✭ 19 (-65.45%)
Mutual labels:  calculator
Notecalc3
NoteCalc is a handy calculator trying to bring the advantages of Soulver to the web.
Stars: ✭ 879 (+1498.18%)
Mutual labels:  calculator
Flutter Calculator Demo
Example project - how to build a simple calculator in Flutter
Stars: ✭ 49 (-10.91%)
Mutual labels:  calculator

License

Matex

PHP Mathematical expression parser and evaluator

Features

  • Fast evaluation
  • Compact codebase
  • Operators: + - * / ^ %
  • Brackets, nested, unlimited levels
  • Variables: predefined or estimated dynamically
  • Functions: predefined or connected dynamically
  • String arguments in functions, like field("name")
  • String operations, currently concatenation is supported

Installation

Using Composer run

$ composer require madorin/matex

See manual for more details and options.

Usage

Basic:

$evaluator = new \Matex\Evaluator();
echo $evaluator->execute('1 + 2');

String concatenation:

$evaluator = new \Matex\Evaluator();
echo $evaluator->execute('"String" + " " + "concatenation"');

Variables:

$evaluator = new \Matex\Evaluator();
$evaluator->variables = [
	'a' => 1,
	'b' => 2
	];
echo $evaluator->execute('a + b');

Dynamic variables:

public function doVariable($name, &$value) {
	switch ($name) {
		case 'b':
			$value = 2;
			break;
	}
}

$evaluator = new \Matex\Evaluator();
$evaluator->variables = [
	'a' => 1
	];
$evaluator->onVariable = [$this, 'doVariable'];
echo $evaluator->execute('a + b');

Functions:

static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}

$evaluator = new \Matex\Evaluator();
$evaluator->functions = [
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
];
echo $evaluator->execute('sum(1, 2, 3)');

Extravaganza:

/*
Dynamic variable resolver
Invoked when the variable is not found in the cache
Returns the value by name
*/
public function doVariable($name, &$value) {
	switch ($name) {
		case 'zen':
			// Here may be a database request, or a function call
			$value = 999;
			break;
		case 'hit':
			$value = 666;
			break;
	}
}

/*
Dynamic function resolver
Invoked when the function is not found in the cache
Returns an associative array array with:
	ref - Function reference
	arc - Expected argument count
*/
public function doFunction($name, &$value) {
	switch ($name) {
		case 'cos':
			// Map to a system function
			$value = ['ref' => 'cos', 'arc' => 1];
			break;
		case 'minadd':
			// Map to a public object instance function
			$value = ['ref' => [$this, 'minAdd'], 'arc' => 2];
			break;
	}
}

/*
Custom functions, may be a
	- Built-in function
	- Global defined function
	- Static class function
	- Object instance function
*/
static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}
// Just a sample custom function
function minAdd($a, $b) {
	$r = $a < 2 ? 2 : $a;
	return $r + $b;
}

// Let's do some calculations
$evaluator = new \Matex\Evaluator();
$evaluator->variables = [
	'a' => 1,
	'bet' => -10.59,
	'pi' => 3.141592653589
	];
$evaluator->onVariable = [$this, 'doVariable'];
$evaluator->functions = [
	'sin' => ['ref' => 'sin', 'arc' => 1],
	'max' => ['ref' => 'max', 'arc' => null],
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
	];
$evaluator->onFunction = [$this, 'doFunction'];
echo $evaluator->execute('a + MinAdd(PI * sin(zen), cos(-1.7 / pi)) / bet ^ ((A + 2) * 2) + sum(5, 4, max(6, hit))');

See examples for code samples.

Author

Dorin Marcoci - [email protected] - https://www.marcodor.com

License

Matex is distributed under MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].