Advertisement
Guest User

Untitled

a guest
Feb 1st, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. var NumberTheory = {};
  2.  
  3. /**
  4. * @param {number} n
  5. * @param {number} k
  6. */
  7. NumberTheory.binomial = function(n, k) {
  8. if (n === k) return 1;
  9. if (k === 1) return n;
  10. return binomial(n - 1, k) + binomial(n - 1, k - 1);
  11. };
  12.  
  13. /**
  14. * Prototype that stores information about a given polynomial.
  15. * @constructor
  16. * @param {string|Polynomial} data The polynomial to parse.
  17. * @return {Polynomial}
  18. */
  19. function Polynomial(data) {
  20. // Support providing already existing Polynomials.
  21. this.polynomial = data instanceof Polynomial ? data.polynomial : data;
  22. }
  23.  
  24. /**
  25. * Rank all coefficients in the polynomial and return the largest.
  26. * @return {number}
  27. */
  28. Polynomial.prototype.getLargestCoefficient = function() {
  29. var re = /(\d+)/g, match, max = 0;
  30. while(match = re.exec(this.getPolynomial()))
  31. max = Math.max(max, match[1]);
  32. return max;
  33. };
  34.  
  35. /**
  36. * Returns the polynomial as plain text.
  37. * @return {string}
  38. */
  39. Polynomial.prototype.getPolynomial = function() {
  40. return this.polynomial;
  41. };
  42.  
  43. /**
  44. * Replaces x for a given value and evaluates the polynomial as a JavaScript expression.
  45. * @param {number} val The value for x.
  46. */
  47. Polynomial.prototype.insert = function(val) {
  48. return eval(this.getPolynomial().replace(/x/g, val.toString()));
  49. };
  50.  
  51. /**
  52. * Sigma sum of a polynomial from start to end.
  53. * @param {number} start The starting index.
  54. * @param {number} end The ending index.
  55. */
  56. Polynomial.prototype.sum = function(start, end) {
  57. var result = 0;
  58. var i = start;
  59.  
  60. while (i <= end) {
  61. result += this.insert(i);
  62. i += 1;
  63. }
  64.  
  65. return result;
  66. };
  67.  
  68. /**
  69. * Product of a polynomial from start to end.
  70. * @param {number} start The starting index.
  71. * @param {number} end The ending index.
  72. */
  73. Polynomial.prototype.product = function(start, end) {
  74. var result = start;
  75. var i = start;
  76.  
  77. while (i <= end) {
  78. result *= this.insert(i);
  79. i += 1;
  80. }
  81.  
  82. return result;
  83. };
  84.  
  85. Polynomial.prototype.limit = function(val) {
  86. // Might consider shrinking dx incrementally to get good approximation.
  87. // Might also consider limiting from top and bottom of x.
  88. // Using analysis of dy we should also be able to detect convergence to infinities.
  89. return this.insert(val + 10e-6);
  90. };
  91.  
  92. /**
  93. * To differentiate we just insert an arbitrary small value as the delta x and evaluate it.
  94. * @param {number} val The value to differentiate for.
  95. */
  96. Polynomial.prototype.differentiate = function(val) {
  97. return (this.insert(val + 10e-6) - this.insert(val)) / 10e-6;
  98. };
  99.  
  100.  
  101. /**
  102. * Prototype that stores information about a given equation.
  103. * @constructor
  104. * @return {Equation}
  105. */
  106. function Equation(data) {
  107. // Support providing already existing Equations.
  108. data = data instanceof Equation ? data.polynomial : data;
  109. // Move everything over to the left side of the equation and set equal to zero.
  110. data = data.replace(/(.*)=(.*)/, '$1-($2)');
  111. // We internally store equations as a polynomial set equal to zero.
  112. this.polynomial = new Polynomial(data);
  113. }
  114.  
  115. /**
  116. * Evaluates the left side polynomial of the equation.
  117. */
  118. Equation.prototype.insert = function(val) {
  119. return this.polynomial.insert(val);
  120. };
  121.  
  122. /**
  123. * Solves the equation using Newtons method.
  124. * @param {number=} errorTreshold The error treshold to stop at.
  125. * @param {number=} guess The initial guess.
  126. * @param {number=} maxIterations The max number of iterations before giving up.
  127. * @return {number}
  128. */
  129. Equation.prototype.solve = function(errorTreshold, guess, maxIterations) {
  130. // Set default error treshold value.
  131. errorTreshold = errorTreshold || 10e-6;
  132.  
  133. // Pick largest coefficient as initial guess. It gives some basic
  134. // understanding over what scale of numbers we are dealing with.
  135. guess = guess || this.polynomial.getLargestCoefficient();
  136.  
  137. // Set default value for maximum number of iterations before timeout.
  138. maxIterations = maxIterations || 1000;
  139.  
  140. // Loop requires number or it will stop prematurely.
  141. var error = errorTreshold + 1;
  142.  
  143. // Store iterations to detect timeouts.
  144. var iteration = 0;
  145.  
  146. // Run iterations of Newtons method.
  147. while (error >= errorTreshold && iteration <= maxIterations) {
  148. error = this.insert(guess) / this.polynomial.differentiate(guess);
  149. guess = guess - error;
  150. iteration += 1;
  151. }
  152.  
  153. // Return our final guess.
  154. return guess;
  155. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement