Skip to content

fabiogribeiro/symphp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SymPHP

SymPHP is a mathematical library that parses arithmetic expressions and can do some symbolic simplifications.

Example usage

<?php

use SymPHP\Parser\Parser;

$parser = new Parser();

$expr = $parser->parse('3 - 2^2 + x');
echo $expr, "\n";
// Add(Sub(3, Exp(2, 2)), x) - Representation of the expression

$expr = $expr->simplify();
echo $expr, "\n";
// Add(-1, x) - -1 + x

$expr2 = $parser->parse('-1 + x')->simplify();
echo $expr1->equals($expr2);
// 1 - true - Compares the two expressions

Available functions

<?php

// Basic operations
$expr->add($other);
$expr->sub($other);
$expr->mul($other);
$expr->div($other);
$expr->exp($other);

$expr->flatten();                       // Add(Add(2, 3), 4) -> Add(2, 3, 4)
$expr->evaluate(array $symbols=null);   // Evaluate $expr numerically with symbols replaced
$expr->simplify();                      // Simplify expression
$expr->equals($other);                  // Compare two expressions
$expr->distribute()                     // If a * (b + c) -> a * b + a * c

Running tests

$ composer install
$ ./vendor/bin/phpunit tests

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages