Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ALL ^ E_NOTICE);
- class Polynomial {
- private $coefficients = array();
- private $debug = false;
- function __construct($inputArr) {
- $loop = 0;
- foreach($inputArr AS $thisPart) {
- // It was ignoring 0 values, hense using a loop counter to insert values
- $this->coefficients[$loop] = $thisPart;
- $loop++;
- }
- print_r($this->coefficients);
- }
- function getVal($integer) {
- # Run it, return a value
- for($i = 0; $i < count($this->coefficients); $i++) {
- if($i == 0) {
- // x^0
- $outVal[] = $this->coefficients[$i];
- } elseif ($i == 1) {
- // x^1
- $outVal[] = ($this->coefficients[$i] * $integer);
- } else {
- //x^2 x^3 x^4
- $outVal[] = ($this->coefficients[$i] * $this->raisePower($integer, $i));
- }
- }
- if($this->debug) {
- print_r($outVal);
- }
- foreach($outVal AS $val) {
- $sum = $sum + $val;
- }
- return $sum;
- }
- // take a value, raise it to a power
- function raisePower($baseNumber, $power) {
- $loop = 1;
- if($power == 0) {
- return 1;
- } else {
- while($loop != $power) {
- if($loop == 1) {
- $integer = $baseNumber * $baseNumber;
- } else {
- $integer = $baseNumber * $integer;
- }
- $loop++;
- }
- }
- if($this->debug) {
- echo "Raising $baseNumber to the power of $power and got a result of $integer <br />";
- }
- return $integer;
- }
- // debug
- function debug($bool) {
- $this->debug = $bool;
- }
- function gdebug() {
- return $this->debug;
- }
- }
- set_time_limit(0);
- /* Input function values */
- //$input = array(1,2,3,4);
- $input = array(2,-7,8,0,-1);
- //$input = array(-2,2,-3,3);
- /* Initialise a Poly object with $input */
- $thisPoly = new Polynomial($input);
- /*
- * Find two values that lie on polynomial
- * Probably a better way to do this
- */
- # Generate < 0
- $attempts = 0;
- do {
- $thisNo = rand(-5000, 5000);
- if($attempts > 20000) {
- break;
- }
- # Feed into function
- $val[0] = $thisPoly->getVal($thisNo);
- $took[0]++;
- $attempts++;
- if($val[0] == 0) {
- continue;
- }
- } while ($val[0] > 0);
- # Generate > 0
- $attempts = 0;
- do {
- # Just a theory ... Will only work for a small amount of polynomials
- $thisNo = rand(-5000, 5000);
- if($attempts > 20000) {
- break;
- }
- # Feed into function
- $val[1] = $thisPoly->getVal($thisNo);
- $took[1]++;
- $attempts++;
- } while ($val[1] < 0);
- $a = $val[0];
- $b = $val[1];
- $tol = 0.000000000000001;
- $limitLoop = 10000;
- echo "Starting Values <br><br>";
- echo "A is $a<br>";
- echo "B is $b<br>";
- $n = 1;
- while($n < $limitLoop) {
- $c = (($a + $b) / 2);
- if($thisPoly->gdebug()) {
- //echo "Poly of c = " . $thisPoly->getVal($c) . " and tol setting is " . (($b - $a) / 2) . "<br />";
- }
- if(($thisPoly->getVal($c) == 0) || (($b - $a) / 2) < $tol) {
- // sol found
- echo "Solution: " . $c;
- break;
- } else {
- $n++;
- if((($thisPoly->getVal($c) < 0) && ($thisPoly->getVal($a) < 0)) || (($thisPoly->getVal($c) > 0) && ($thisPoly->getVal($a) > 0))) {
- $a = $c;
- } else {
- $b = $c;
- }
- }
- }
- ?>
Add Comment
Please, Sign In to add comment