The only library you need to integrate mathematical functions into your applications. It is a self-contained library in pure PHP with no external dependencies.
- Algebra
- Arithmetic
- Expression
- Finance
- Functions
- Information Theory
- Linear Algebra
- Numbers
- Number Theory
- Numerical Analysis
- Probability
- Combinatorics
- Distributions
- Sample Data
- Search
- Sequences
- Set Theory
- Statistics
- Trigonometry
Add the library to your composer.json
file in your project:
{
"require": {
"markrogoyski/math-php": "2.*"
}
}
Use composer to install the library:
$ php composer.phar install
Composer will install MathPHP inside your vendor folder. Then you can add the following to your .php files to use the library with Autoloading.
require_once __DIR__ . '/vendor/autoload.php';
Alternatively, use composer on the command line to require and install MathPHP:
$ php composer.phar require markrogoyski/math-php:2.*
- PHP 7.2
Note: For PHP 7.0 and 7.1, use v1.0 (markrogoyski/math-php:1.*
)
use MathPHP\Algebra;
// Greatest common divisor (GCD)
$gcd = Algebra::gcd(8, 12);
// Extended greatest common divisor - gcd(a, b) = a*a' + b*b'
$gcd = Algebra::extendedGcd(12, 8); // returns array [gcd, a', b']
// Least common multiple (LCM)
$lcm = Algebra::lcm(5, 2);
// Factors of an integer
$factors = Algebra::factors(12); // returns [1, 2, 3, 4, 6, 12]
// Linear equation of one variable: ax + b = 0
[$a, $b] = [2, 4]; // 2x + 4 = 0
$x = Algebra::linear($a, $b);
// Quadratic equation: ax² + bx + c = 0
[$a, $b, $c] = [1, 2, -8]; // x² + 2x - 8
[$x₁, $x₂] = Algebra::quadratic($a, $b, $c);
// Discriminant: Δ = b² - 4ac
[$a, $b, $c] = [2, 3, 4]; // 3² - 4(2)(4)
$Δ = Algebra::discriminant($a, $b, $c);
// Cubic equation: z³ + a₂z² + a₁z + a₀ = 0
[$a₃, $a₂, $a₁, $a₀] = [2, 9, 3, -4]; // 2x³ + 9x² + 3x -4
[$x₁, $x₂, $x₃] = Algebra::cubic($a₃, $a₂, $a₁, $a₀);
// Quartic equation: a₄z⁴ + a₃z³ + a₂z² + a₁z + a₀ = 0
[$a₄, $a₃, $a₂, $a₁, $a₀] = [1, -10, 35, -50, 24]; // z⁴ - 10z³ + 35z² - 50z + 24 = 0
[$z₁, $z₂, $z₃, $z₄] = Algebra::quartic($a₄, $a₃, $a₂, $a₁, $a₀);
use MathPHP\Arithmetic;
$√x = Arithmetic::isqrt(8); // 2 Integer square root
$³√x = Arithmetic::cubeRoot(-8); // -2
$ⁿ√x = Arithmetic::root(81, 4); // nᵗʰ root (4ᵗʰ): 3
// Sum of digits
$digit_sum = Arithmetic::digitSum(99); // 18
$digital_root = Arithmetic::digitalRoot(99); // 9
// Equality of numbers within a tolerance
$x = 0.00000003458;
$y = 0.00000003455;
$ε = 0.0000000001;
$almostEqual = Arithmetic::almostEqual($x, $y, $ε); // true
// Copy sign
$magnitude = 5;
$sign = -3;
$signed_magnitude = Arithmetic::copySign($magnitude, $sign); // -5
// Modulo (Differs from PHP remainder (%) operator for negative numbers)
$dividend = 12;
$divisor = 5;
$modulo = Arithmetic::modulo($dividend, $divisor); // 2
$modulo = Arithmetic::modulo(-$dividend, $divisor); // 3
use MathPHP\Expression\Polynomial;
// Polynomial x² + 2x + 3
$coefficients = [1, 2, 3]
$polynomial = new Polynomial($coefficients);
// Evaluate for x = 3
$x = 3;
$y = $polynomial($x); // 18: 3² + 2*3 + 3
// Calculus
$derivative = $polynomial->differentiate(); // Polynomial 2x + 2
$integral = $polynomial->integrate(); // Polynomial ⅓x³ + x² + 3x
// Arithmetic
$sum = $polynomial->add($polynomial); // Polynomial 2x² + 4x + 6
$sum = $polynomial->add(2); // Polynomial x² + 2x + 5
$difference = $polynomial->subtract($polynomial); // Polynomial 0
$difference = $polynomial->subtract(2); // Polynomial x² + 2x + 1
$product = $polynomial->multiply($polynomial); // Polynomial x⁴ + 4x³ + 10x² + 12x + 9
$product = $polynomial->multiply(2); // Polynomial 2x² + 4x + 6
$negated = $polynomial->negate(); // Polynomial -x² - 2x - 3
// Data
$degree = $polynomial->getDegree(); // 2
$coefficients = $polynomial->getCoefficients(); // [1, 2, 3]
// String representation
print($polynomial); // x² + 2x + 3
// Roots
$polynomial = new Polynomial([1, -3, -4]);
$roots = $polynomial->roots(); // [-1, 4]
// Companion matrix
$companion = $polynomial->companionMatrix();
use MathPHP\Finance;
// Financial payment for a loan or annuity with compound interest
$rate = 0.035 / 12; // 3.5% interest paid at the end of every month
$periods = 30 * 12; // 30-year mortgage
$present_value = 265000; // Mortgage note of $265,000.00
$future_value = 0;
$beginning = false; // Adjust the payment to the beginning or end of the period
$pmt = Finance::pmt($rate, $periods, $present_value, $future_value, $beginning);
// Interest on a financial payment for a loan or annuity with compound interest.
$period = 1; // First payment period
$ipmt = Finance::ipmt($rate, $period, $periods, $present_value, $future_value, $beginning);
// Principle on a financial payment for a loan or annuity with compound interest
$ppmt = Finance::ppmt($rate, $period, $periods, $present_value, $future_value = 0, $beginning);
// Number of payment periods of an annuity.
$periods = Finance::periods($rate, $payment, $present_value, $future_value, $beginning);
// Annual Equivalent Rate (AER) of an annual percentage rate (APR)
$nominal = 0.035; // APR 3.5% interest
$periods = 12; // Compounded monthly
$aer = Finance::aer($nominal, $periods);
// Annual nominal rate of an annual effective rate (AER)
$nomial = Finance::nominal($aer, $periods);
// Future value for a loan or annuity with compound interest
$payment = 1189.97;
$fv = Finance::fv($rate, $periods, $payment, $present_value, $beginning)
// Present value for a loan or annuity with compound interest
$pv = Finance::pv($rate, $periods, $payment, $future_value, $beginning)
// Net present value of cash flows
$values = [-1000, 100, 200, 300, 400];
$npv = Finance::npv($rate, $values);
// Interest rate per period of an annuity
$beginning = false; // Adjust the payment to the beginning or end of the period
$rate = Finance::rate($periods, $payment, $present_value, $future_value, $beginning);
// Internal rate of return
$values = [-100, 50, 40, 30];
$irr = Finance::irr($values); // Rate of return of an initial investment of $100 with returns of $50, $40, and $30
// Modified internal rate of return
$finance_rate = 0.05; // 5% financing
$reinvestment_rate = 0.10; // reinvested at 10%
$mirr = Finance::mirr($values, $finance_rate); // rate of return of an initial investment of $100 at 5% financing with returns of $50, $40, and $30 reinvested at 10%
// Discounted payback of an investment
$values = [-1000, 100, 200, 300, 400, 500];
$rate = 0.1;
$payback = Finance::payback($values, $rate); // The payback period of an investment with a $1,000 investment and future returns of $100, $200, $300, $400, $500 and a discount rate of 0.10
// Profitability index
$values = [-100, 50, 50, 50];
$profitability_index = Finance::profitabilityIndex($values, $rate); // The profitability index of an initial $100 investment with future returns of $50, $50, $50 with a 10% discount rate
use MathPHP\Functions\Map;
$x = [1, 2, 3, 4];
$sums = Map\Single::add($x, 2); // [3, 4, 5, 6]
$differences = Map\Single::subtract($x, 1); // [0, 1, 2, 3]
$products = Map\Single::multiply($x, 5); // [5, 10, 15, 20]
$quotients = Map\Single::divide($x, 2); // [0.5, 1, 1.5, 2]
$x² = Map\Single::square($x); // [1, 4, 9, 16]
$x³ = Map\Single::cube($x); // [1, 8, 27, 64]
$x⁴ = Map\Single::pow($x, 4); // [1, 16, 81, 256]
$√x = Map\Single::sqrt($x); // [1, 1.414, 1.732, 2]
$∣x∣ = Map\Single::abs($x); // [1, 2, 3, 4]
$maxes = Map\Single::max($x, 3); // [3, 3, 3, 4]
$mins = Map\Single::min($x, 3); // [1, 2, 3, 3]
$reciprocals = Map\Single::reciprocal($x); // [1, 1/2, 1/3, 1/4]
use MathPHP\Functions\Map;
$x = [10, 10, 10, 10];
$y = [1, 2, 5, 10];
// Map function against elements of two or more arrays, item by item (by item ...)
$sums = Map\Multi::add($x, $y); // [11, 12, 15, 20]
$differences = Map\Multi::subtract($x, $y); // [9, 8, 5, 0]
$products = Map\Multi::multiply($x, $y); // [10, 20, 50, 100]
$quotients = Map\Multi::divide($x, $y); // [10, 5, 2, 1]
$maxes = Map\Multi::max($x, $y); // [10