PHP package to simplify mathematical expressions and solve equations.
use MathSolver\Math;
Math::from('3x^2 + 2x * 4x')->simplify()->string(); // 11x^2
Math::from('2x + 3')->substitute(['x' => 5])->simplify()->string(); // 13
Not yet installable.
First, load an expression with the from
method.
use MathSolver\Math;
Math::from('2x + 3x');
Then, you can chain one of these methods:
Simplify the expression.
use MathSolver\Math;
Math::from('2x + 3x')->simplify()->string(); // 5x
Substitute a value into the expression.
use MathSolver\Math;
Math::from('2x + 3x')->substitute(['x' => 4])->string(); // 2(4) + 3(4)
The substitute
methods pairs nicely with the simplify
method.
use MathSolver\Math;
Math::from('2x + 3x')->substitute(['x' => 4])->simplify()->string(); // 20
You can call the config
method for any configuration options.
use MathSolver\Math;
Math::from('2x + 3 = 11')->config(['mathjax' => true, 'steps' => false])->...
These are all available options in short:
Name | Type | Default | Description |
---|---|---|---|
mathjax |
bool |
false |
Whether to return Mathjax output |
steps |
bool |
false |
Whether to record and return steps |
Mathjax is a tool to display mathematical expressions in your browser. Set this option to true so Math Solver will return responses in Mathjax syntax instead of the default one.
use MathSolver\Math;
Math::from('sqrt[18]')->config(['mathjax' => true])->simplify()->string(); // 3\sqrt{2}
Settings this value to true will Math Solver make record every step.
use MathSolver\Math;
Math::from('sqrt[18] + sqrt[32]')->config(['steps' => true])->simplify()->string();
// [
// 'result' => '7sqrt[2]',
// 'steps' => [
// ['type' => 'simplify', 'name' => 'Simplify roots', 'result' => '3sqrt[2]+4sqrt[2]'],
// ['type' => 'simplify', 'name' => 'Add like terms', 'result' => '7sqrt[2]']
// ]
// ]
Here are some functions that require another input syntax than you may be used to:
- Powers: five to the power of three is
5^3
- Roots: the cube root of 15 is
cbrt[15]
- Fractions: two-thirds is
frac[2, 3]