Skip to content

nickmcintyre/numero

Repository files navigation

número

número

A friendly and intuitive math library for p5.js

All Contributors

Build Status

This addon library for p5.js turns the "software sketchbook" into a beginner-friendly environment for technical computing. It provides the following features:

  • A tensor object similar to NumPy arrays
  • A grammar of data manipulation similar to dplyr
  • A drawing turtle 🐢

The library is written in TypeScript and uses TensorFlow.js and tidy.js under the hood. It bundles p5.ten, p5.tidy, and TurtleGFX.

Usage

Turtle Geometry

View the Polygon example.

let numSides;

function setup() {
  createCanvas(400, 400);
  angleMode(DEGREES);

  numSides = createSlider(3, 50, 3, 1);
  numSides.position(100, 300);
  numSides.style('width', '200px');
}

function draw() {
  background('darkorchid');
  polygon(100, numSides.value());
  fill('darkturquoise');
  noStroke();
  text('3', 100, 330);
  text('50', 290, 330);
}

function polygon(diameter, n) {
  const interiorAngle = 180 * (n - 2) / n;
  const turnAngle = 180 - interiorAngle;
  const sideLength = 0.5 * diameter * sin(interiorAngle);
  const x = 0.5 * (width - diameter * sin(0.5 * interiorAngle));
  const y = 0.5 * height;
  setposition(x, y);
  pencolor('darkturquoise');
  for (let i = 0; i < n; i += 1) {
    forward(sideLength);
    right(turnAngle);
  }
}

Data Wrangling

View the Mauna Loa example.

let data;

function preload() {
  data = loadTable('co2.csv', 'csv', 'header');
}

function setup() {
  noCanvas();
  tidy(
    data,
    filter((d) => d.mean > 400),
    debug('Observations greater than 400ppm CO2'),
  );
}

/**
[tidy.debug] Observations greater than 400ppm CO2 -----------------------------
console.table()
(index) date        mean    unc
0       2013-05-01  400.02  0.13
1       2014-04-01  401.51  0.19
2       2014-05-01  401.96  0.21
**/

Tensors

View the matrix-vector example.

function setup() {
  noCanvas();
  print("---\n A \n---\n");
  const a = createTensor([[1, 0], [0, 2]]);
  a.print();
  print("---\n x \n---\n");
  const x = createTensor([3, 4]);
  x.print();
  print("--------\n Ax = b \n--------\n");
  const b = a.dot(x);
  b.print();
}

/**
---
 A 
---
Tensor
    [[1, 0],
     [0, 2]]
---
 x 
---
Tensor
    [3, 4]
--------
 Ax = b 
--------
Tensor
    [3, 8]
**/

Demo

The fluid simulation below was created using a 2-dimensional lattice Boltzmann method.

A fluid simulation

Contributing

See CONTRIBUTING.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Ashneel Das
Ashneel Das

💻 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!