How to use the @mathigon/core.list 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 / fermat.js / src / expression.js View on Github external
function rewrite(expr, rule) {
  if (isString(expr) || isNumber(expr)) return expr;

  let [fn, ...args] = expr;
  args = args.map(a => rewrite(a, rule));

  // For addition and multiplications we need to match subsets of arguments, e.g.
  // `a + b + c` should match the rule `a + b`.
  let [ruleFn, ...ruleArgs] = rule[0];
  if ('+*'.includes(fn) && '+*'.includes(ruleFn) && args.length > ruleArgs.length) {
    for (let p of subsets(list(args.length), ruleArgs.length)) {
      let argsSubset = p.map(i => args[i]);  // The subset of args that match the rule.
      let argsOthers = args.filter((x, i) => !p.includes(i));  // All other args.

      let placeholders = match([fn, ...argsSubset], rule[0])[0];
      if (placeholders) {
        let argsReplaced = substitute(rule[1], placeholders);
        return flattenAssociative([fn, argsReplaced, ...argsOthers]);
      }
    }
    return [fn, ...args];
  }

  let placeholders = match([fn,...args], rule[0])[0];
  return placeholders ? substitute(rule[1], placeholders) : [fn, ...args];
}
github mathigon / textbooks / content / sequences / functions.ts View on Github external
export function quiz($step: Step) {
  const goals = list(14).map(x => 'blank-' + x).join(' ');
  $step.onScore(goals, () => {
    // Don't show confetti during page load.
    if ($step.isPageLoaded) $step.tools.confetti();
  });
}
github mathigon / fermat.js / src / regression.ts View on Github external
    let X = data.map(d => list(order + 1).map(p => Math.pow(d[0], p)));
    let XT = Matrix.transpose(X);
github mathigon / textbooks / content / graph-theory / components / geometry.ts View on Github external
export function travellingSalesman(dist: number[][]) {
  const n = dist.length;
  const cities = list(n);

  let minLength = Infinity;
  let minPath = undefined;

  loop1:
      for (let path of permutations(cities)) {
        let length = 0;
        for (let i = 0; i < n - 1; ++i) {
          length += dist[path[i]][path[i + 1]];
          if (length > minLength) continue loop1;
        }
        if (length < minLength) {
          minLength = length;
          minPath = path;
        }
      }
github mathigon / textbooks / content / graph-theory / functions.js View on Github external
$section.model.set('tsnPaths', function(x) {
    return list(x, 1).join(' × ') + ' = ' + numberFormat(factorial(x));
  });
}
github mathigon / textbooks / content / graph-theory / components / graph.js View on Github external
load(vertices, edges, posn) {
    this.repulsion = 50 / Math.sqrt(vertices);
    this.attraction = 0.1 * Math.sqrt(vertices) / edges.length * 200 / (this.width + this.height);
    this.gravity = vertices/4;
    this.damping = 0.9;

    this.$vertices.removeChildren();
    this.$edges.removeChildren();

    this.vertices = list(vertices).map((v) => {
      let x = posn ? (posn[v][0] || posn[v].x) : this.width  * (0.3 + 0.4 * Math.random());
      let y = posn ? (posn[v][1] || posn[v].y) : this.height * (0.3 + 0.4 * Math.random());

      let $el = this.options.icon ?
        $N('path', { 'class': 'node', d: this.options.icon }, this.$vertices) :
        $N('circle', { 'class': 'node', r: this.options.r || 5 }, this.$vertices);
      if (this.options.vertex) $el.css('fill', run(this.options.vertex, v));
      return { $el: $el, posn: { x: x, y: y }, neighbours: [], v: { x: 0, y: 0 } };
    });

    this.edges = edges.map((e) => {
      let v1 = this.vertices[e[0]];
      let v2 = this.vertices[e[1]];

      let type = (v1 === v2) || this.options.arc ? 'path' : 'line';
      let $el = $N(type, { 'class': 'link' }, this.$edges);
github mathigon / textbooks / content / graph-theory / functions.ts View on Github external
export function utilities1($section: Step) {
  let graphs = $section.$$('.graph') as SVGParentView[];

  let p3 = [{x: 100, y: 35}, {x: 170, y: 155}, {x: 30, y: 155}];
  new Graph(graphs[0], 3, subsets(list(3), 2), {r: 8, static: true, posn: p3});

  let p4 = [{x: 40, y: 40}, {x: 160, y: 40}, {x: 160, y: 160}, {x: 40, y: 160}];
  let p4x = [{x: 100, y: 110}, {x: 100, y: 25}, {x: 175, y: 160}, {x: 25, y: 160}];
  let k4 = new Graph(graphs[1], 4, subsets(list(4), 2),
      {r: 8, static: true, posn: p4});

  let p5 = [{x: 100, y: 30}, {x: 175, y: 85}, {x: 145, y: 170}, {x: 55, y: 170}, {x: 25, y: 85}];
  let p5x = [{x: 100, y: 30}, {x: 120, y: 110}, {x: 185, y: 170}, {x: 15, y: 170}, {x: 80, y: 110}];
  let k5 = new Graph(graphs[2], 5, subsets(list(5), 2),
      {r: 8, static: true, posn: p5});

  function transition(graph: Graph, to: SimplePoint[]) {
    let from = graph.vertices.map(v => ({x: v.posn.x, y: v.posn.y}));
    animate((q) => {
      graph.arrange(graph.vertices.map((v, i) => {
        return {
          x: lerp(from[i].x, to[i].x, q),
          y: lerp(from[i].y, to[i].y, q)
        };
      }));
github mathigon / textbooks / content / graph-theory / functions.js View on Github external
export function handshakes3($section) {
  let graphs = $$('.graph', $section);
  new Graph(graphs[0], 4, subsets(list(4), 2));
  new Graph(graphs[1], 5, subsets(list(5), 2));
  new Graph(graphs[2], 6, subsets(list(6), 2));
  new Graph(graphs[3], 7, subsets(list(7), 2));
}
github mathigon / textbooks / content / graph-theory / functions.ts View on Github external
$section.model.set('tsnPaths', (x: number) => {
    return list(x, 1).join(' × ') + ' = ' + numberFormat(factorial(x));
  });
}