<?php
include_once("class.Numerical.php") ;
/*
* Solve the following linear equations:
*
* 3x + 2y - z = 1
* 2x - 2y + 4z = -2
* -x + y/2 - z = 0
*/
$a =
array(
array(2, -2, 4, -2),
array(3, 2, -1, 1),
array(-1, .5, -1, 0)) ;
$f = Numerical::gaussianElimination(3, $a) ;
var_dump($f) ;
$a =
array(
array(-3, 2),
array(5, -3)) ;
$f = Numerical::gaussianInversion($a) ;
var_dump($f) ;
/*
* Find the factors of a number.
*/
$f = Numerical::factor(400) ;
var_export($f) ;
$g = Numerical::factor(423) ;
var_export($g) ;
/*
* Find the maximum or minimum of an interval.
*/
$f = create_function('$x', 'return ( - ($x * $x) + 2 ) ;') ;
$c = Numerical::critical($criticalPoint, $f, -0.5, 2, 1e-03, 1e-03, 1e-03) ;
echo "Critical point of - x**2 + 2\n" ;
echo "Critical Point($c) = $criticalPoint\n\n" ;
$f = create_function('$x', 'return ( ($x * $x) + 2 ) ;') ;
$c = Numerical::critical($criticalPoint, $f, -0.5, 2, 1e-03, 1e-03, 1e-03) ;
echo "Critical point of x**2 + 2\n" ;
echo "Critical Point($c) = $criticalPoint\n\n" ;
/*
* 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" ;
/*
* 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" ;
$a = array(1, 2, 3, 4, 100 => 5) ;
echo "\nAdd array and constant\n" ;
var_dump($xxx = Numerical::add($a, 4)) ;
echo "\nAdd constant and array\n" ;
var_dump($yyy = Numerical::add(6, $a)) ;
echo "\nAdd array and array\n" ;
var_dump(Numerical::add($xxx, $yyy)) ;
$yyy = array(10, 100 => 20) ;
echo "\nAdd array and array of varying sizes\n" ;
var_dump(Numerical::add($xxx, $yyy)) ;
echo "\nDivide array by constant\n" ;
var_dump(Numerical::divide($xxx, 3)) ;
echo "\nDivide constant by array\n" ;
var_dump(Numerical::divide(3, $xxx)) ;
echo "\ln of array\n" ;
var_dump(Numerical::ln($xxx)) ;
echo "\nmininum of array\n" ;
var_dump(Numerical::minimum(array(3, 4, 2, -6, 10))) ;
echo "\nmaximum of array\n" ;
var_dump(Numerical::maximum(array(3, 4, 2, -6, 10))) ;
?>
|