powder96/numbers.php

Advanced Mathematics Library for PHP (port of Numbers.js)

dev-master 2013-03-13 20:38 UTC

This package is not auto-updated.

Last update: 2024-12-15 03:26:11 UTC


README

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: https://github.com/powder96/numbers.php/archive/fd946ea8742ba46789dc2a38cc6c1f93a7512e6d.zip

Description

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

Matrix::transpose($array);

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

//basic check
Prime::simple($number);

// MillerRabin primality test
Prime::millerRabin($number);

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

Statistic::mean($array);
Statistic::median($array);
Statistic::mode($array);
Statistic::standardDev($array);
Statistic::randomSample($lower, $upper, $n);
Statistic::correlation($array1, $array2);

Test

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.

Authors

Numbers.js

Numbers.php