From 14b54aded0196a31d554d44610fe404b4d1a7fbb Mon Sep 17 00:00:00 2001 From: "Steve L. Nyemba" Date: Mon, 18 May 2015 11:28:16 -0500 Subject: [PATCH] Update on dom.js and math functions --- dom.js | 9 +++++---- ext/math.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/dom.js b/dom.js index 627bb4c..4c2530d 100644 --- a/dom.js +++ b/dom.js @@ -35,12 +35,13 @@ jx.dom.exists = function(id){ * @returns {undefined} */ jx.dom.remove = function(id){ + var _item = null; if (id.constructor == String){ - item = jx.dom.get.instance(id) + _item = jx.dom.get.instance(id) }else{ - item = id ; + _item = id ; } - item.parentNode.removeChild(item) ; + _item.parentNode.removeChild(_item) ; } @@ -51,7 +52,7 @@ jx.dom.remove = function(id){ * @param _child child dom object */ jx.dom.append = function(id,_child){ - _parent = jx.dom.get.instance(id) ; + var _parent = jx.dom.get.instance(id) ; _parent.appendChild(_child) ; } diff --git a/ext/math.js b/ext/math.js index 6b466db..949c0fb 100644 --- a/ext/math.js +++ b/ext/math.js @@ -95,6 +95,17 @@ jx.math.avg = function(lxi,lni){ } }; +/** +* This function will repete a value a given number of times +* @pre times > 0 +*/ +jx.math.rep = function(value,times){ + var lvalues = [] + for(var i=0; i < times; i++){ + lvalues.push(value) ; + } + return lvalues; +} jx.math.mean = jx.math.avg ; jx.math.pow = Math.pow jx.math.sd = function(lxi,lni){ @@ -103,7 +114,6 @@ jx.math.sd = function(lxi,lni){ sqr = [] ; for(var i=0; i < lxi.length ;i++){ - console.log(lxi[i]) sqr[i] = jx.math.pow((Number(lxi[i])-mean),2 ) ; } @@ -111,6 +121,27 @@ jx.math.sd = function(lxi,lni){ return jx.math.sqrt(total/(N-1)) ; } ; +/** +* This function computes the correlation between two vectors +* @pre x1.length == x2.length +*/ +jx.math.cor = function(x1,x2){ + return jx.math.cov(x1,x2) / (jx.math.sd(x1)*jx.math.sd(x2)) +} +/** +* This function will compute the covariance of 2 vectors +* @pre x1.length == x2.length +*/ +jx.math.cov = function(x1,x2){ + var u1 = jx.math.mean(x1) ; + var u2 = jx.math.mean(x2) ; + var N = x1.length ; + var value = 0 + for(var i in x1){ + value += (x1[i] - u1) * (x2[i] - u2) + } + return value / (N - 1) +} /** * Computes the factorial of a given value