Skip to content
Advanced Mathematics Library for PHP (port of Numbers.js)
PHP Shell
#9 Compare This branch is 303 commits ahead, 363 commits behind numbers:master.
Latest commit d4fd836 Mar 13, 2013 powder96 Removed useless function Matrix::deepCopy().


Numbers.php - an advanced mathematics toolkit for PHP >= 5.3. It is a port of Numbers.js - same toolkit for JavaScript.

There is a version of Numbers.php which supports PHP 5.2, but it is no longer developed:


Numbers.php provides a comprehensive set of mathematical tools that currently are not offered in PHP. These tools include:

  • Basic calculations
  • Calculus
  • Matrix Operations
  • Prime Numbers
  • Statistics
  • More...

A few things to note before using: PHP, like many languages, does not necessarily manage floating points as well as we'd all like it to. For example, if adding decimals, the addition tool won't return the exact value. This is an unfortunate error. Precautions have been made to account for this. After including numbers, you can set an error bound. Anything in this will be considered an "acceptable outcome."

The primary uses cases are calculations and data analysis on the server side. For client side operations, please use Numbers.js.

How to use

Numbers is pretty straightforward to use.

For example, if we wanted to estimate the integral of sin(x) from -2 to 4, we could:

Use riemann integrals (with 200 subdivisions)

use NumbersPHP\Calculus;
use NumbersPHP\Matrix;
use NumbersPHP\Statistic;
use NumbersPHP\Prime;

Calculus::riemann('sin', -2, 4, 200);

Or use adaptive simpson quadrature (with epsilon 0.0001)

Calculus::adaptiveSimpson('sin', -2, 4, 0.0001);

User-defined functions can be used too:

function myFunc($x) {
  return 2 * pow($x, 2) + 1;
Calculus::riemann('myFunc', -2, 4, 200);

Calculus::adaptiveSimpson(create_function('$x', 'return 2 * pow($x, 2) + 1;'), -2, 4, 0.0001);

Now say we wanted to run some matrix calculations:

We can add two matrices

$matrix1 = array(array(0, 1, 2),
                 array(3, 4, 5));
$matrix2 = array(array( 6,  7,  8),
                 array( 9, 10, 11));
Matrix::addition($matrix1, $matrix2);

We can transpose a matrix


Numbers also includes some basic prime number analysis. We can check if a number is prime:

//basic check

// Miller�Rabin primality test

The statistics tools include mean, median, mode, standard deviation, random sample generator, correlation, confidence intervals, t-test, chi-square, and more.

Statistic::randomSample($lower, $upper, $n);
Statistic::correlation($array1, $array2);


Download and install these things:

Run in the command prompt:

    php composer.phar install
    php phpunit.phar --configuration phpunit.xml.dist

If you are going to run tests multiple times and you are using Microsoft(R) Windows(TM), you can use the batch file /test.cmd. Do not forget to set the path to PHP, Composer, and PHPUnit in the beginnig of that file.




Something went wrong with that request. Please try again.