Guest User

Untitled

a guest
May 26th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ALL ^ E_NOTICE);
  3.  
  4. class Polynomial {
  5. private $coefficients = array();
  6. private $debug = false;
  7.  
  8. function __construct($inputArr) {
  9. $loop = 0;
  10. foreach($inputArr AS $thisPart) {
  11. // It was ignoring 0 values, hense using a loop counter to insert values
  12. $this->coefficients[$loop] = $thisPart;
  13. $loop++;
  14. }
  15.  
  16. print_r($this->coefficients);
  17. }
  18.  
  19. function getVal($integer) {
  20. # Run it, return a value
  21.  
  22. for($i = 0; $i < count($this->coefficients); $i++) {
  23. if($i == 0) {
  24. // x^0
  25. $outVal[] = $this->coefficients[$i];
  26. } elseif ($i == 1) {
  27. // x^1
  28. $outVal[] = ($this->coefficients[$i] * $integer);
  29. } else {
  30. //x^2 x^3 x^4
  31. $outVal[] = ($this->coefficients[$i] * $this->raisePower($integer, $i));
  32. }
  33. }
  34.  
  35. if($this->debug) {
  36. print_r($outVal);
  37. }
  38.  
  39. foreach($outVal AS $val) {
  40. $sum = $sum + $val;
  41. }
  42.  
  43. return $sum;
  44. }
  45.  
  46. // take a value, raise it to a power
  47. function raisePower($baseNumber, $power) {
  48. $loop = 1;
  49.  
  50. if($power == 0) {
  51. return 1;
  52. } else {
  53. while($loop != $power) {
  54. if($loop == 1) {
  55. $integer = $baseNumber * $baseNumber;
  56. } else {
  57. $integer = $baseNumber * $integer;
  58. }
  59.  
  60. $loop++;
  61. }
  62. }
  63.  
  64. if($this->debug) {
  65. echo "Raising $baseNumber to the power of $power and got a result of $integer <br />";
  66. }
  67. return $integer;
  68. }
  69.  
  70. // debug
  71. function debug($bool) {
  72. $this->debug = $bool;
  73. }
  74.  
  75. function gdebug() {
  76. return $this->debug;
  77. }
  78.  
  79. }
  80.  
  81. set_time_limit(0);
  82.  
  83. /* Input function values */
  84. //$input = array(1,2,3,4);
  85. $input = array(2,-7,8,0,-1);
  86. //$input = array(-2,2,-3,3);
  87.  
  88. /* Initialise a Poly object with $input */
  89. $thisPoly = new Polynomial($input);
  90.  
  91. /*
  92. * Find two values that lie on polynomial
  93. * Probably a better way to do this
  94. */
  95.  
  96. # Generate < 0
  97. $attempts = 0;
  98. do {
  99. $thisNo = rand(-5000, 5000);
  100.  
  101. if($attempts > 20000) {
  102. break;
  103. }
  104.  
  105. # Feed into function
  106. $val[0] = $thisPoly->getVal($thisNo);
  107. $took[0]++;
  108.  
  109. $attempts++;
  110.  
  111. if($val[0] == 0) {
  112. continue;
  113. }
  114. } while ($val[0] > 0);
  115.  
  116. # Generate > 0
  117.  
  118. $attempts = 0;
  119. do {
  120. # Just a theory ... Will only work for a small amount of polynomials
  121. $thisNo = rand(-5000, 5000);
  122.  
  123. if($attempts > 20000) {
  124. break;
  125. }
  126.  
  127. # Feed into function
  128. $val[1] = $thisPoly->getVal($thisNo);
  129. $took[1]++;
  130.  
  131. $attempts++;
  132. } while ($val[1] < 0);
  133.  
  134. $a = $val[0];
  135. $b = $val[1];
  136. $tol = 0.000000000000001;
  137. $limitLoop = 10000;
  138.  
  139. echo "Starting Values <br><br>";
  140. echo "A is $a<br>";
  141. echo "B is $b<br>";
  142.  
  143. $n = 1;
  144. while($n < $limitLoop) {
  145. $c = (($a + $b) / 2);
  146.  
  147. if($thisPoly->gdebug()) {
  148. //echo "Poly of c = " . $thisPoly->getVal($c) . " and tol setting is " . (($b - $a) / 2) . "<br />";
  149. }
  150.  
  151. if(($thisPoly->getVal($c) == 0) || (($b - $a) / 2) < $tol) {
  152. // sol found
  153. echo "Solution: " . $c;
  154. break;
  155. } else {
  156. $n++;
  157.  
  158. if((($thisPoly->getVal($c) < 0) && ($thisPoly->getVal($a) < 0)) || (($thisPoly->getVal($c) > 0) && ($thisPoly->getVal($a) > 0))) {
  159. $a = $c;
  160. } else {
  161. $b = $c;
  162. }
  163. }
  164. }
  165.  
  166. ?>
Add Comment
Please, Sign In to add comment