Wiki

Clone wiki

dart_trotter / Home

Home | Permutations | Combinations | Amalgams | Compositions | Subsets | Compounds | Inverses | About

Welcome to trotter!

trotter is a Dart library for working with structures commonly encountered in combinatorics. It has class definitions for the following structures:

  • Permutations.
  • Combinations.
  • Selections (combinations with replacement).
  • Amalgams (permutations with replacement).
  • Subsets (combinations of unspecified length).
  • Compounds (permutations of unspecified length).

Use the menu above to explore this wiki or click here to see the repository.

Example

(This is the code that produced the illustrations in the rest of this documentation.)

import 'dart:html';
import 'package:trotter/trotter.dart';

void main() {
  var sprites = new ImageElement(src: "piggy_sprites.png");

  void draw(combinatoric, title) {
    BigInt n = combinatoric.length;

    CanvasElement illustrateFrom(BigInt from, {BigInt to}) {
      final int WIDTH = 275, HEIGHT = ((to - from).toInt() * 50);
      var pPic = new CanvasElement(width: WIDTH, height: HEIGHT);
      for (BigInt row = from; row < to; row += BigInt.one) {
        for (var col = 0; col < combinatoric[row].length; col++) {
          pPic.context2D.drawImageToRect(
              sprites,
              new Rectangle(
                  col * ((WIDTH - 25) ~/ 5) + 25,
                  (row - from).toInt() * (HEIGHT ~/ (to - from).toInt()),
                  WIDTH ~/ 5,
                  HEIGHT ~/ (to - from).toInt()),
              sourceRect: new Rectangle(combinatoric[row][col] * 100, 0, 100, 100));
        }

        pPic.context2D
            .fillText("[$row]", 5, (row - from).toInt() * (HEIGHT ~/ (to - from).toInt()) + 15);
      }
      return pPic;
    }

    document.body.children
        .add(new DivElement()..innerHtml = "<h1>${combinatoric.length} $title</h1>");
    if (n <= new BigInt.from(20)) {
      document.body.children.add(illustrateFrom(BigInt.zero, to: n));
    } else {
      document.body.children
        ..add(illustrateFrom(BigInt.zero, to: new BigInt.from(10)))
        ..add(new DivElement()..innerHtml = ".<br>.<br>.<br>")
        ..add(illustrateFrom(n - new BigInt.from(10), to: n));
    }
  }

  sprites.onLoad.listen((_) {
    var frames = [0, 1, 2, 3, 4];

    var perms = new Permutations(3, frames);
    draw(perms, "Permutations of 3 items from 5");

    var combos = new Combinations(3, frames);
    draw(combos, "Combinations of 3 items from 5");

    var amals = new Amalgams(3, frames);
    draw(amals, "Amalgams of 3 items from 5");

    var compos = new Compositions(3, frames);
    draw(compos, "Compositions of 3 items from 5");

    var subs = new Subsets(frames);
    draw(subs, "Subsets of 5 items");

    var comps = new Compounds(frames);
    draw(comps, "Compounds of 5 items");
  });
}

Where piggy_sprites.png is the image:

Updated