Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aresnick/d8c7818de536503cad7d731cf0a45651 to your computer and use it in GitHub Desktop.
Save aresnick/d8c7818de536503cad7d731cf0a45651 to your computer and use it in GitHub Desktop.
/*
# A quick sketch of a few ways that variables in computer programming relate to algebraic variables
A common (a la Bednarz, Kieran, & Lee, 1996; Küchemann, 1981;
MacGregor & Stacey, 1993; Malle, 1993; Usiskin, 1988) breakdown
of the functions of variable are into:
πŸ“­ β€” placeholder for number
❔ β€”Β an unknown number
πŸ“ˆ β€”Β a varying quantity
🎁 β€”Β a generalized number
πŸ“¨ β€”Β and a parameter
I've annotated the code below with emoji indicating which of the uses above are entailed.
*/
var l; // πŸ“­
l = 5;
//////////
// Find the intersection of two functions discretely
var y1 = (x) => x * x; // πŸ“­, 🎁, πŸ“¨
var y2 = (x) => x; // πŸ“­, 🎁, πŸ“¨
var find_intersections = function(f1, f2, domain_start, domain_end, epsilon = 0.0001, step_size = 0.01) { // πŸ“­, 🎁, πŸ“¨
intersections = []; // ❔
x = domain_start; // πŸ“ˆ
while (x <= domain_end) { // πŸ“ˆ, 🎁
if (Math.abs(f1(x) - f2(x)) <= epsilon) { // 🎁
intersections.push(x); // ❔
}
x += step_size; // πŸ“ˆ
}
return intersections; // πŸ“­
};
console.log(find_intersections(y1, y2, 0, 100));
//////////
// Represent the addition of complex numbers
var Complex = function(real, imaginary) { // 🎁, πŸ“¨
this.real = (typeof real === 'undefined') ? this.real : parseFloat(real); // πŸ“­, ❔
this.imaginary = (typeof imaginary === 'undefined') ? this.imaginary : parseFloat(imaginary); // πŸ“­, ❔
};
Complex.add = function(c1, c2) { // 🎁, πŸ“¨
return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary); // πŸ“ˆ, 🎁, πŸ“¨
};
var x = new Complex(1, 0); // πŸ“­
var y = new Complex(0, 1); // πŸ“­
console.log(Complex.add(x, y));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment