Skip to content

Instantly share code, notes, and snippets.

@amilajack
Created June 26, 2016 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amilajack/4ba897c357cfa067f606bedf8813caf2 to your computer and use it in GitHub Desktop.
Save amilajack/4ba897c357cfa067f606bedf8813caf2 to your computer and use it in GitHub Desktop.
esnextbin sketch
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>ESNextbin Sketch</title>
<!-- put additional styles and scripts here -->
</head>
<body>
<!-- put markup and other contents here -->
</body>
</html>
// write ES2015 code and import modules from npm
// and then press "Execute" to run your program
import { expect } from 'chai'
function integ(coefs) {
const newCoefs = coefs.map(
(coef, index) => coef / (coefs.length - index)
)
return [...newCoefs, 0]
}
function addExp(coefs, x) {
let sum = 0
const some = coefs.map((a, index) =>
Math.pow(x, coefs.length - index - 1) * a
)
for (let e of some) {
sum += e
}
return sum
}
function areaExact(coefs, a, b) {
console.time('areaExact')
const some = addExp(integ(coefs), b) - addExp(integ(coefs), a)
console.timeEnd('areaExact')
return some
}
function areaNumerical(coefs, delta = 1, a, b) {
console.time('areaNumerical')
let sum = 0
for (let i = a; i < b; i+=delta) {
const comp = addExp(coefs, i) * delta;
sum += comp
}
console.timeEnd('areaNumerical')
return sum;
}
// expect(integ([3, 4, 5])).to.eql([1, 2, 5, 0])
// expect(addExp([1, 2, 5, 0], 3)).to.eql(60)
expect(areaExact([3, 4, 5], 0, 3)).to.equal(60)
expect(Math.round(areaNumerical([3, 4, 5], 0.001, 0, 3))).to.equal(60)
{
"name": "esnextbin-sketch",
"version": "0.0.0",
"dependencies": {
"chai": "3.5.0",
"babel-runtime": "6.9.2"
}
}
'use strict';
var _getIterator2 = require('babel-runtime/core-js/get-iterator');
var _getIterator3 = _interopRequireDefault(_getIterator2);
var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
var _chai = require('chai');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function integ(coefs) {
var newCoefs = coefs.map(function (coef, index) {
return coef / (coefs.length - index);
});
return [].concat((0, _toConsumableArray3.default)(newCoefs), [0]);
} // write ES2015 code and import modules from npm
// and then press "Execute" to run your program
function addExp(coefs, x) {
var sum = 0;
var some = coefs.map(function (a, index) {
return Math.pow(x, coefs.length - index - 1) * a;
});
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = (0, _getIterator3.default)(some), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var e = _step.value;
sum += e;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return sum;
}
function areaExact(coefs, a, b) {
console.time('areaExact');
var some = addExp(integ(coefs), b) - addExp(integ(coefs), a);
console.timeEnd('areaExact');
return some;
}
function areaNumerical(coefs) {
var delta = arguments.length <= 1 || arguments[1] === undefined ? 1 : arguments[1];
var a = arguments[2];
var b = arguments[3];
console.time('areaNumerical');
var sum = 0;
for (var i = a; i < b; i += delta) {
var comp = addExp(coefs, i) * delta;
sum += comp;
}
console.timeEnd('areaNumerical');
return sum;
}
// expect(integ([3, 4, 5])).to.eql([1, 2, 5, 0])
// expect(addExp([1, 2, 5, 0], 3)).to.eql(60)
(0, _chai.expect)(areaExact([3, 4, 5], 0, 3)).to.equal(60);
(0, _chai.expect)(Math.round(areaNumerical([3, 4, 5], 0.001, 0, 3))).to.equal(60);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment