Location: PHPKode > scripts > Numerical > numerical/example.php
```<?php

include_once("class.Numerical.php") ;

/*
* Get a the value of the Gaussian normal distribution for 0 standard
* deviations.
*/

echo "Value of Gaussian standard normal distribution at 0 sigma:\n" ;
echo Numerical::gaussian(0), "\n\n" ;

/*
* Integrate a standard normal distribution from -3 to +3 sigmas which
* should return a value of approximately .997.
*/

echo "Value of integral from -3 to 3 of the Gaussian standard normal distribution:\n" ;
\$f = create_function('\$x', 'return Numerical::gaussian(\$x) ;') ;
echo Numerical::integrate(\$f, -3, 3), "\n\n"  ;

/*
* Integrate the value of the sin function over the range of 0 to 2*pi
* which should returne a value of approximately 0.
*/

echo "Value of integral from 0 to 2 * pi of sin\n" ;
echo Numerical::integrate("sin", 0, M_PI * 2), "\n\n" ;

/*
* Solve some equations using the steepest descent algorithm.
*/

echo "Testing Steepest Descent equation solving:\n\n" ;

echo "Solution to y = x - 2:\n" ;
\$f = create_function('\$x', 'return (\$x - 2.0) ;') ;
echo Numerical::solveSteepestDescent(\$f, 7.3), "\n\n"  ;

echo "Solution to y = (x - 2)*(x + 3):\n" ;
\$f = create_function('\$x', 'return (\$x - 2.0)*(\$x + 3.0) ;') ;
echo "Initial guess is 1: ",Numerical::solveSteepestDescent(\$f, 1), "\n"  ;
echo "Initial guess is -6: ",Numerical::solveSteepestDescent(\$f, -6), "\n"  ;

/*
* Force not a number through iteration limiting.
*/

echo "Initial guess is -6: ",Numerical::solveSteepestDescent(\$f, -6, 1.0e-6, .001, 2), "\n\n"  ;

echo "Solution to y = (x - 2)*(x + 3)*(x + 10):\n" ;
\$f = create_function('\$x', 'return (\$x - 2.0)*(\$x + 3.0)*(\$x + 10) ;') ;
echo "Initial guess is 1: ",Numerical::solveSteepestDescent(\$f, 1), "\n"  ;
echo "Initial guess is -6: ",Numerical::solveSteepestDescent(\$f, -6), "\n"  ;
echo "Initial guess is -1000: ",Numerical::solveSteepestDescent(\$f, -1000), "\n\n"  ;

echo "Testing Steepest Descent equation solving for \"negative\" functions:\n\n" ;

echo "Solution to y = -(x - 2):\n" ;
\$f = create_function('\$x', 'return -(\$x - 2.0) ;') ;
echo Numerical::solveSteepestDescent(\$f, 7.3), "\n\n"  ;

echo "Solution to y = -((x - 2)*(x + 3)):\n" ;
\$f = create_function('\$x', 'return -((\$x - 2.0)*(\$x + 3.0)) ;') ;
echo "Initial guess is 1: ",Numerical::solveSteepestDescent(\$f, 1), "\n"  ;
echo "Initial guess is -6: ",Numerical::solveSteepestDescent(\$f, -6), "\n\n"  ;

echo "Solution to y = -((x - 2)*(x + 3)*(x + 10)):\n" ;
\$f = create_function('\$x', 'return -((\$x - 2.0)*(\$x + 3.0)*(\$x + 10)) ;') ;
echo "Initial guess is 1: ",Numerical::solveSteepestDescent(\$f, 1), "\n"  ;
echo "Initial guess is -6: ",Numerical::solveSteepestDescent(\$f, -6), "\n"  ;
echo "Initial guess is -1000: ",Numerical::solveSteepestDescent(\$f, -1000), "\n\n"  ;

/*
* Solve some equations using the bisection algorithm.
*/

echo "Testing Bisection equation solving:\n\n" ;

echo "Solution to y = x - 2:\n" ;
\$f = create_function('\$x', 'return (\$x - 2.0) ;') ;
echo Numerical::solveBisection(\$f, -3.7, 7.3), "\n"  ;
echo Numerical::solveBisection(\$f, 3, 4, 1.0e-2, 1.0e-3), "\n\n" ;

/*
* Generate the primes <= 20
*/

echo "Primes <= 20:\n\n" ;

var_dump(Numerical::sieve(20)) ;

/*
* Factor 20031
*/

echo "Prime Factors\n\n" ;

var_dump(Numerical::factor(20031)) ;
echo "\n";
var_dump(Numerical::factor(16)) ;
echo "\n\n" ;

/*
* Check some simple rational numbers.
*/

echo "Floating to rational conversion:\n\n" ;

\$a = Numerical::rational(.1) ;
printf(".1 = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;
\$a = Numerical::rational(M_PI, 1.0E-09) ;
printf("PI = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;
\$a = Numerical::rational(2.0/3.0) ;
printf(".66666... = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;
\$a = Numerical::rational(1.0/3.0) ;
printf(".33333... = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;
\$a = Numerical::rational(3.0/4.0) ;
printf(".75 = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;
\$a = Numerical::rational(-3.0/4.0) ;
printf("-.75 = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;
\$a = Numerical::rational(-.3) ;
printf("-.3 = %d/%d = %.15e\n", \$a[0], \$a[1], \$a[0]/\$a[1]) ;

/*
* Generate 10 random numbers using the Box-Mueller rectangular transform.
*/

echo "\nBox-Mueller Basic Transform:\n" ;

for (\$i = 0; \$i < 10; \$i++)
{
echo Numerical::randomGaussianBoxMuellerBasic(), "\n" ;
}

/*
* Generate 10 random numbers using the Box-Mueller Polar transform.
*/

echo "\nBox-Mueller Rectangular Polar Transform:\n" ;

for (\$i = 0; \$i < 10; \$i++)
{
echo Numerical::randomGaussianBoxMuellerPolar(), "\n" ;
}

\$theSample = array(1, 2, 3, 3, 3, 1, 1.3, 1.3, 1.4, 1) ;

echo "\nMean:\n" ;
echo Numerical::mean(\$theSample), "\n" ;

echo "\nMedian:\n" ;
echo Numerical::median(\$theSample), "\n" ;

echo "\nMode:\n" ;
echo var_export(Numerical::mode(\$theSample), TRUE), "\n" ;

echo "\nStandard Deviation:\n" ;
echo Numerical::standardDeviation(\$theSample),"\n" ;

echo "\nVariance:\n" ;
echo Numerical::variance(\$theSample),"\n" ;
?>
```