How to use the @mathigon/core.tabulate function in @mathigon/core

To help you get started, we’ve selected a few @mathigon/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mathigon / textbooks / content / circles / functions.ts View on Github external
export function kepler($step: Step) {
  const N = 100;  // number of points
  const a = 120;  // semi-major axis (ry)
  const e = 0.5;  // eccentricity

  // BesselJ function - Table[BesselJ[i, i*0.5], {i, 0, 10}]
  const besselJ = [1, 0.242268, 0.114903, 0.060964, 0.0339957, 0.0195016,
    0.0113939, 0.006743, 0.00402867, 0.00242466, 0.0014678];

  const points = tabulate((n) => {
    const M = 2 * Math.PI * n / N;  // mean anomaly

    let E = M;  // eccentric anomaly
    for (let i = 1; i < 10; ++i) E += 2 / i * besselJ[i] * Math.sin(i * M);

    const th = 2 * Math.atan(Math.sqrt((1 + e) / (1 - e)) * Math.tan(E / 2));
    const r = a * (1 - e * e) / (1 + e * Math.cos(th));
    return Point.fromPolar(th, r).shift(220, 120);
  }, N + 1);

  const $orbit = $step.$('.orbit') as SVGView;
  const $earth = $step.$('.earth') as SVGView;
  const $sweep = $step.$('.sweep')!;
  const $play = $step.$('x-play-btn') as PlayBtn;

  $orbit.points = points;
github mathigon / textbooks / content / graph-theory / functions.ts View on Github external
function shuffle(n: number) {
    return tabulate(() =>
        new Point(Math.random() * graph.width, Math.random() * graph.height), n);
  }
github mathigon / boost.js / src / colour.js View on Github external
static solar(steps) {
    return tabulate(x => getColourAt(solar, x/(steps-1)), steps);
  }
github mathigon / textbooks / probability / functions.js View on Github external
export function radioactive($section) {
  let $box = $section.$('.radioactive');

  let $atoms = tabulate((x, y) => $N('circle', { cx: x * 20 + 10, cy: y * 20 + 10, r: 6 }, $box), 15, 10);
  $atoms = random.shuffle(flatten($atoms));

  function decay() {
    $section.score('decay');
    let $atom = $atoms.pop();
    $atom.addClass('off');
    if ($atoms.length) setTimeout(decay, random.exponential($atoms.length / 20000));
  }

  $section.$('.btn').one('click', decay);
}
github mathigon / fermat.js / src / random.js View on Github external
export function smart(n, id) {
  if (!id) id = uid();
  if (!SMART_RANDOM_CACHE.has(id)) SMART_RANDOM_CACHE.set(id, tabulate(1, n));

  let cache = SMART_RANDOM_CACHE.get(id);
  let x = weighted(cache.map(x => x * x));

  cache[x] -= 1;
  if (cache[x] <= 0) SMART_RANDOM_CACHE.set(id, cache.map(x => x + 1));

  return x;
}
github mathigon / textbooks / content / graph-theory / functions.js View on Github external
$section.model.watch(function(state) {
    let m = state.m;
    let f = state.f;

    let mPoints = tabulate(x => ({ x: (x+1) / (m+1) * 300, y:  30 }), m);
    let fPoints = tabulate(x => ({ x: (x+1) / (f+1) * 300, y: 110 }), f);

    let edges = [];
    for (let i=0; i
github mathigon / boost.js / src / colour.js View on Github external
static rainbow(steps) {
    return tabulate(x => getColourAt(rainbow, x/(steps-1)), steps);
  }
github mathigon / textbooks / content / sequences / functions.ts View on Github external
function arithmetic(a: number, d: number, n: number) {
  return tabulate((i) => a + i * d, n);
}