_base.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. if (!dojo._hasResource["dojox.math._base"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dojox.math._base"] = true;
  6. dojo.provide("dojox.math._base");
  7. dojo.mixin(dojox.math, {
  8. degreesToRadians : function(/* Number */n) {
  9. // summary
  10. // Convert the passed number to radians.
  11. return (n * Math.PI) / 180; // Number
  12. },
  13. radiansToDegrees : function(/* Number */n) {
  14. // summary
  15. // Convert the passed number to degrees.
  16. return (n * 180) / Math.PI; // Number
  17. },
  18. factoral : function(/* Number */n) {
  19. // summary
  20. // Return the factoral of n.
  21. if (n < 1) {
  22. return 0; // Number
  23. }
  24. var ret = 1;
  25. for (var i = 1; i <= n; i++) {
  26. ret *= i;
  27. }
  28. return ret; // Number
  29. },
  30. permutations : function(/* Number */n, /* Number */k) {
  31. // summary
  32. // TODO
  33. if (n == 0 || k == 0) {
  34. return 1; // Number
  35. }
  36. return (this.factoral(n) / this.factoral(n - k));
  37. },
  38. combinations : function(/* Number */n, /* Number */r) {
  39. // summary
  40. // TODO
  41. if (n == 0 || r == 0) {
  42. return 1; // Number
  43. }
  44. return (this.factoral(n) / (this.factoral(n - r) * this.factoral(r))); // Number
  45. },
  46. bernstein : function(/* Number */t, /* Number */n, /* Number */i) {
  47. // summary
  48. // TODO
  49. return (this.combinations(n, i) * Math.pow(t, i) * Math.pow(1 - t,
  50. n - i)); // Number
  51. },
  52. gaussian : function() {
  53. // summary
  54. // Return a random number based on the Gaussian algo.
  55. var k = 2;
  56. do {
  57. var i = 2 * Math.random() - 1;
  58. var j = 2 * Math.random() - 1;
  59. k = i * i + j * j;
  60. } while (k >= 1);
  61. return (i * Math.sqrt((-2 * Math.log(k)) / k)); // Number
  62. },
  63. // basic statistics
  64. sd : function(/* Array */a) {
  65. // summary
  66. // Returns the standard deviation of the passed arguments.
  67. return Math.sqrt(this.variance(a)); // Number
  68. },
  69. variance : function(/* Array */a) {
  70. // summary
  71. // Find the variance in the passed array of numbers.
  72. var mean = 0, squares = 0;
  73. dojo.forEach(a, function(item) {
  74. mean += item;
  75. squares += Math.pow(item, 2);
  76. });
  77. return (squares / a.length) - Math.pow(mean / a.length, 2); // Number
  78. },
  79. // create a range of numbers
  80. range : function(/* Number */a, /* Number? */b, /* Number? */step) {
  81. // summary
  82. // Create a range of numbers based on the parameters.
  83. if (arguments.length < 2) {
  84. b = a, a = 0;
  85. }
  86. var s = step || 1;
  87. var range = [];
  88. if (s > 0) {
  89. for (var i = a; i < b; i += s) {
  90. range.push(i);
  91. }
  92. } else {
  93. if (s < 0) {
  94. for (var i = a; i > b; i += s) {
  95. range.push(i);
  96. }
  97. } else {
  98. throw new Error("dojox.math.range: step must not be zero.");
  99. }
  100. }
  101. return range; // Array
  102. },
  103. distance : function(/* Array */a, /* Array */b) {
  104. // summary
  105. // Calculate the distance between point A and point B
  106. return Math.sqrt(Math.pow(b[0] - a[0], 2)
  107. + Math.pow(b[1] - a[1], 2)); // Number
  108. },
  109. midpoint : function(/* Array */a, /* Array */b) {
  110. // summary
  111. // Calculate the midpoint between points A and B. A and B may be
  112. // multidimensional.
  113. if (a.length != b.length) {
  114. console
  115. .error(
  116. "dojox.math.midpoint: Points A and B are not the same dimensionally.",
  117. a, b);
  118. }
  119. var m = [];
  120. for (var i = 0; i < a.length; i++) {
  121. m[i] = (a[i] + b[i]) / 2;
  122. }
  123. return m; // Array
  124. }
  125. });
  126. }