na-error-propagation
Version:
Calculates the error propagation
8 lines • 2.68 kB
JavaScript
/*!
* na-error-propagation
* @see https://github.com/tfoxy/na-error-propagation
* @version 0.1.3
* @author Tomás Fox <tomas.c.fox@gmail.com>
* @license MIT
*/
!function(t,r){"use strict";"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?module.exports=r():t.ErrorPropagation=r()}(this,function(){"use strict";function t(t){this.name="InputError",this.message=t}function r(r){switch(r=r||{},r.correlation){case void 0:case"correlated":this._calculateError=u;break;case"uncorrelated":this._calculateError=f;break;case"both":this._calculateError=s;break;default:throw new t("Wrong value for correlation: "+r.correlation+". It must be one of correlated, uncorrelated and both")}}function e(){throw new Error("ErrorPropagation.nerdamer must be set")}function o(t){r.prototype=Object.create(t.prototype),r.prototype.constructor=r,r.prototype.calculate=n,r.prototype._calculate=a}function n(t,r){try{return this._calculate(t,r)}catch(e){this.emit("error",e)}}function a(r,e){if(this.emit("input",r,e),"string"!=typeof r)throw new t("expression must be a string. Got "+typeof r);e=e||{};var o=l(e),n=i(r,o),a=this._calculateError(r,e,o,this),c={value:n,error:a};return this.emit("result",c),c}function i(t,e,o){var n=r.nerdamer(t);return r.nerdamer.clear("last"),o&&(o.e=n),n.evaluate(e).valueOf()}function c(t,r,e,o){var n=[];for(var a in r){var c=r[a];if(c.error){var u={},f="diff("+t+","+a+")",s=i(f,e,u),l=s*c.error;o.emit("differential",{value:s,variableName:a,variable:c,valueWithError:l,expression:u.e}),n.push(l)}}return n}function u(t,r,e,o){var n=0,a=c(t,r,e,o);return a.forEach(function(t){n+=t}),n}function f(t,r,e,o){var n=0,a=c(t,r,e,o);return a.forEach(function(t){n+=Math.pow(t,2)}),Math.sqrt(n)}function s(t,r,e,o){var n=0,a=0,i=c(t,r,e,o);return i.forEach(function(t){n+=t,a+=Math.pow(t,2)}),{correlated:n,uncorrelated:Math.sqrt(a)}}function l(t){var r={};for(var e in t){var o=t[e];p(o,e),r[e]=o.value}return r}function p(r,e){if("object"!=typeof r)throw new t("Variable "+e+" is not an object. Got "+typeof r);if(null===r)throw new t("Variable "+e+" must not be null");if(!("value"in r))throw new t("No value for variable "+e)}function h(){var t={prototype:{on:function(){throw new Error("An EventEmitter library needs to be set using HermiteInterpolation.setEventEmitter(EventEmitter) method. The /on/ function cannot be used otherwise")},emit:function(t,r){if("error"===t)throw r}}};if("object"==typeof module&&module.exports)try{t=require("events").EventEmitter}catch(r){}return t}return t.prototype=Object.create(Error.prototype),t.prototype.constructor=t,r.InputError=t,r.nerdamer=e,r.setEventEmitter=o,r.setEventEmitter(h()),r});