Skip to content

Instantly share code, notes, and snippets.

var defint = function(symbol, dx, from, to) {
var vars = core.Utils.variables(symbol),
integral = __.integrate(symbol, dx),
retval;
if(!integral.hasIntegral()) {
var upper = {},
lower = {};
upper[dx] = to;
lower[dx] = from;
retval = _.subtract(_.parse(integral, upper), _.parse(integral, lower));
@jiggzson
jiggzson / erf.js
Last active March 12, 2017 03:54
Erf function using Horner's method
//Ported from: http://stackoverflow.com/questions/457408/is-there-an-easily-available-implementation-of-erf-for-python
var erf = function(x) {
var t = 1/(1+0.5*Math.abs(x));
var result = 1-t*Math.exp( -x*x - 1.26551223 +
t * ( 1.00002368 +
t * ( 0.37409196 +
t * ( 0.09678418 +
t * (-0.18628806 +
t * ( 0.27886807 +
t * (-1.13520398 +
/**
* Gets the intersection of two arrays
* @param a An array
* @param b An array
* @returns {Array}
*/
function intersection(a, b) {
b = b.slice();
var c = [];
if(a.length > b.length) {
@jiggzson
jiggzson / min_array.js
Created August 17, 2016 22:30
Find smallest number in Javascript array. Much faster than Math.min.apply
function min(arr) {
var l, a, b;
while(true) {
l = arr.length;
if(l < 2) return arr[0];
a = arr.pop();
b = arr[l-2];
if(a < b) {
arr.pop();
arr.push(a);
/**
* Inserts an object into an array at a given index or recursively adds items if an array is given.
* When inserting another array, passing in false for unpackArray will result in the array being inserted
* rather than its items.
* @param {Array} arr - The target array
* @param {Array} item - The item being inserted
* @param {Number} index - Where to place the item
* @param {boolean} unpackArray - Will insert the array instead of adding its items
*/
insertArray = Utils.insertArray = function( arr, item, index, unpackArray ) {
var toNumberPlace = function(n) {
var number = String(n),
l = number.length,
zeroes = l-1,
places = [];
for(var i=0; i<l; i++) {
var digit = Number(number.charAt(i));
places.push(digit * Math.pow(10, zeroes--));
}
return places;
(function($) {
$.fn.circularMeter = function() {
var defaults = {
dimension: 25,
'border-width': 25,
color: '#ABABAB',
'text-color': '#000',
'z-index': 99
};
var userOptions = typeof arguments[0] === 'object' ? [].shift.apply(arguments) : defaults;
@jiggzson
jiggzson / factors.js
Last active August 29, 2015 14:09
Get all the factors of an integer
function factors(num, unique) {
var l = num, i=1, factors = [],
epsilon = 2.2204460492503130808472633361816E-16;
while(i<l) {
var quotient = num/i;
var whole = Math.floor(quotient);
var remainder = quotient-whole;
if(remainder <= epsilon) {
unique && i === whole ? factors.push(i) : factors.push(i, whole);
l = whole;
/**
* This function searches an array and its sub-arrays. A condition function can be used
* to limit the results
* @param {Array} arr
* @param {Function} condfn A function with extra conditions
* @param {String} prop A hashable property to identify the object when found
* @param {Object} c A collector object defined by the function
* @returns {Object} An object containing the found objects
*/
function countFrequency(arr, condfn, prop, c) {
@jiggzson
jiggzson / withsteps.js
Created November 8, 2014 21:05
This add-on function logs step using nerdamer
nerdamer.register({
parent: 'Helper',
name: 'withsteps',
visible: false,
build: function() {
var core = this;
var operationLog = [];
//write a function which would perform the log
var log = function(operator, symbol1, symbol2, result, is_first) {
if(!is_first) operationLog.pop();