Skip to content

Instantly share code, notes, and snippets.

View grossvogel's full-sized avatar
🐢

Jason Pollentier grossvogel

🐢
  • Revelry Labs
  • New Orleans, LA
View GitHub Profile
Verifying that +jasonpollentier is my openname (Bitcoin username). https://onename.io/jasonpollentier

Keybase proof

I hereby claim:

  • I am grossvogel on github.
  • I am grossvogel (https://keybase.io/grossvogel) on keybase.
  • I have a public key ASDhRR3NZWThtuOPAPEE8vxip4a_OV2JIWLAbtxWgLf4CAo

To claim this, I am signing this object:

describe('addition', () => {
describe('is commutative', () => {
it('gives the same result when adding numbers in either order', () => {
// presumably both equal 3, but that could be tested elsewhere
assert.equal(add(1, 2), add(2, 1));
});
});
});
const jsc = require('jsverify');
describe('addition', () => {
it('is commutative'() => {
const commutativeTest = (a, b) => add(a, b) === add(b, a);
const commutativeProperty = jsc.forall(jsc.number, jsc.number, commutativeTest);
jsc.assert(commutativeProperty);
});
});
class Polynomial {
/**
* @param {array} coefficients
* with coefficients[i] being the coefficient for X^i
*/
constructor(coefficients) {
// remove leading 0s, so 0x^2 + 2x + 1 becomes 2x + 1
this._coefficients = this._trimCoefficients(coefficients);
}
class Polynomial {
//...
get derivative() {
const newCoefficients = this.coefficients
.map((coefficient, degree) => coefficient * degree)
.slice(1);
return new Polynomial(newCoefficients);
}
//...
}
/**
* Find a zero of the given polynomial near the provided guess
* @param {Polynomial} polynomial
* @param {number} guessX - x value near the zero we're looking for
* @param {number} tolerance - how close we need to be to the desired Y
* @param {number} maxIterations - how many iterations before we fail
*/
const solve = (polynomial, guessX, tolerance = 0.000001, maxIterations = 1000) => {
const derivative = polynomial.derivative;
let iteration = 0;
/**
* Size in the case of polynomials will be the length of the coefficients array,
* which is one more than the degree of the polynomial
* For simplicity, use integer coefficients
* @param {Integer} size
*/
const generator = size => {
const coefficients = [];
for (i = 0; i < size; i++) {
coefficients.push(jsc.random(-MAX_COEFFICIENT, MAX_COEFFICIENT));
... snip: a bunch of passing inputs
[ 13, 10, 38, 33, 19 ]
[ 10, 38, 33, 19 ]
[ 6, 10, 38, 33, 19 ]
[ 9, 10, 38, 33, 19 ]
[ 10, 10, 38, 33, 19 ]
[ 10, 38, 33, 19 ]
[ 5, 10, 38, 33, 19 ]
[ 7, 10, 38, 33, 19 ]
[ 8, 10, 38, 33, 19 ]
const boundedInt = jsc.integer(-MAX_COEFFICIENT, MAX_COEFFICIENT);
const polynomialArbitrary = jsc.nearray(boundedInt).smap(
Polynomial.create,
Polynomial.getCoefficents
);