How to use the @mathigon/core.tabulate2D 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 / chaos / components / water-ripples.ts View on Github external
createDropPattern(size: number) {
    const $canvas = $N('canvas', {width: size, height: size}) as CanvasView;

    const grad = $canvas.ctx.createRadialGradient(size / 2, size / 2, 0, size / 2, size / 2, size / 2);
    grad.addColorStop(0, '#fff');
    grad.addColorStop(1, '#000');

    $canvas.ctx.fillStyle = grad;
    $canvas.ctx.fillRect(0, 0, size, size);
    const pixels = $canvas.ctx.getImageData(0, 0, size, size).data;

    // Only use the red component when extracting pixel values.
    return tabulate2D((x, y) => pixels[(x * size + y) * 4] / 255, size, size);
  }
github mathigon / fermat.js / src / matrix.ts View on Github external
export function inverse(M: Matrix) {
    // Perform Gaussian elimination:
    // (1) Apply the same operations to both I and C.
    // (2) Turn C into the identity, thereby turning I into the inverse of C.

    let n = M.length;
    if (n !== M[0].length) throw new Error('Not a square matrix.');

    let I = identity(n);
    let C = tabulate2D((x, y) => M[x][y], n, n);  // Copy of original matrix

    for (let i = 0; i < n; ++i) {
      // Loop over the elements e in along the diagonal of C.
      let e = C[i][i];

      // If e is 0, we need to swap this row with a lower row.
      if (!e) {
        for (let ii = i + 1; ii < n; ++ii) {
          if (C[ii][i] !== 0) {
            for (let j = 0; j < n; ++j) {
              [C[ii][j], C[i][j]] = [C[i][j], C[ii][j]];
              [I[ii][j], I[i][j]] = [I[i][j], I[ii][j]];
            }
            break;
          }
        }
github mathigon / textbooks / content / transformations / components / wallpaper.ts View on Github external
function grid(points: Point[], x: number, y: number) {
  return flatten(tabulate2D((i, j) =>
      points.map((p: Point) => p.shift(i * x, j * y)), width / x, height / y));
}
github mathigon / textbooks / content / probability / functions.ts View on Github external
export function radioactive($step: Step) {
  let $box = $step.$('.radioactive')!;

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

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

  $step.$('.btn')!.one('click', decay);
}
github mathigon / textbooks / content / graph-theory / functions.ts View on Github external
$step.model.set('handshakeTable', (n: number) => {
    let colours = Color.rainbow($step.model.n);

    function makePerson(x: number, y: number) {
      let newX = (x >= y ? x + 1 : x);
      return ['<span style="background: ',
        colours[y], '" class="person">', y + 1,
        '</span> + <span style="background: ', colours[newX],
        '" class="person">', newX + 1, '</span>'].join('');
    }

    return '' +
           tabulate2D(makePerson, n - 1, n).map(x =&gt; `${x.join('')}`)
               .join('') +
           '<table class="handshakes grid"><tbody><tr></tr></tbody></table>';
  });
}