lazy-graphs.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. const d3 = require('../d3.js');
  2. const cephes = require('../../');
  3. function linspace(start, stop, nsteps){
  4. const delta = (stop-start)/(nsteps-1);
  5. return d3.range(start, stop+delta, delta).slice(0, nsteps);
  6. }
  7. class LazyGraphs {
  8. constructor() {
  9. this._computed = new Map();
  10. }
  11. get(exampleNumber) {
  12. if (this._computed.has(exampleNumber)) {
  13. return this._computed.get(exampleNumber);
  14. }
  15. let data;
  16. switch (exampleNumber) {
  17. case 1:
  18. data = this._bessel();
  19. break;
  20. case 2:
  21. data = this._gamma();
  22. break;
  23. case 3:
  24. data = this._beta();
  25. break;
  26. case 4:
  27. data = this._ariy();
  28. break;
  29. default:
  30. throw new Error(`example ${exampleNumber} is not supported`);
  31. }
  32. this._computed.set(exampleNumber, data);
  33. return data;
  34. }
  35. _gamma() {
  36. const xInput = linspace(0.001, 5, 100);
  37. return {
  38. xDomain: [0.001, 5],
  39. yDomain: [-5, 5],
  40. lines: [{
  41. line: xInput.map((x) => [x, cephes.gamma(x)]),
  42. description: "Gamma(x)"
  43. }, {
  44. line: xInput.map((x) => [x, cephes.lgam(x)]),
  45. description: "ln(Gamma(x))"
  46. }, {
  47. line: xInput.map((x) => [x, cephes.psi(x)]),
  48. description: "DiGamma(x)"
  49. }]
  50. };
  51. }
  52. _beta() {
  53. const xInput = linspace(0.0001, 0.9999, 100);
  54. function betadist(alpha, beta, x) {
  55. return (Math.pow(x, alpha - 1) * Math.pow(1 - x, beta - 1)) / cephes.beta(alpha, beta);
  56. }
  57. return {
  58. xDomain: [0, 1],
  59. yDomain: [0, 2.5],
  60. lines: [{
  61. line: xInput.map((x) => [x, betadist(0.5, 0.5, x)]),
  62. description: "BetaDistribution(x; 0.5, 0.5)"
  63. }, {
  64. line: xInput.map((x) => [x, betadist(5, 1, x)]),
  65. description: "BetaDistribution(x; 5, 1)"
  66. }, {
  67. line: xInput.map((x) => [x, betadist(1, 3, x)]),
  68. description: "BetaDistribution(x; 1, 3)"
  69. }, {
  70. line: xInput.map((x) => [x, betadist(2, 2, x)]),
  71. description: "BetaDistribution(x; 2, 2)"
  72. }, {
  73. line: xInput.map((x) => [x, betadist(2, 5, x)]),
  74. description: "BetaDistribution(x; 2, 5)"
  75. }]
  76. };
  77. }
  78. _bessel() {
  79. const xInput = linspace(0, 20, 100);
  80. return {
  81. xDomain: [0, 20],
  82. yDomain: [-1, 1],
  83. lines: [{
  84. line: xInput.map((x) => [x, cephes.j0(x)]),
  85. description: "Bessel(0, x)"
  86. }, {
  87. line: xInput.map((x) => [x, cephes.jv(0.5, x)]),
  88. description: "Bessel(0.5, x)"
  89. }, {
  90. line: xInput.map((x) => [x, cephes.j1(x)]),
  91. description: "Bessel(1, x)"
  92. }, {
  93. line: xInput.map((x) => [x, cephes.jv(1.5, x)]),
  94. description: "Bessel(1.5, x)"
  95. }, {
  96. line: xInput.map((x) => [x, cephes.jv(2, x)]),
  97. description: "Bessel(2, x)"
  98. }]
  99. };
  100. }
  101. _ariy() {
  102. const xInput = linspace(-10, 5, 100);
  103. const yResult = xInput.map((x) => cephes.airy(x));
  104. return {
  105. xDomain: [-10, 5],
  106. yDomain: [-2, 2],
  107. lines: [{
  108. line: xInput.map(function (x, i) {
  109. const [ret, {ai, aip, bi, bip}] = yResult[i];
  110. return [x, ai];
  111. }),
  112. description: "Ai(x)"
  113. }, {
  114. line: xInput.map(function (x, i) {
  115. const [ret, {ai, aip, bi, bip}] = yResult[i];
  116. return [x, aip];
  117. }),
  118. description: "Ai'(x)"
  119. }, {
  120. line: xInput.map(function (x, i) {
  121. const [ret, {ai, aip, bi, bip}] = yResult[i];
  122. return [x, bi];
  123. }),
  124. description: "Bi(x)"
  125. }, {
  126. line: xInput.map(function (x, i) {
  127. const [ret, {ai, aip, bi, bip}] = yResult[i];
  128. return [x, bip];
  129. }),
  130. description: "Bi'(x)"
  131. }]
  132. };
  133. }
  134. }
  135. module.exports = LazyGraphs;