|
|
|
/**
|
|
|
|
return max ;
|
|
|
|
/**
|
|
|
|
total = jx.math.sum(sqr);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This section implements a few handlers based on sets
|
|
|
|
*/
|
|
|
|
jx.math.sets = {} ;
|
|
|
|
/**
|
|
|
|
* This function will perform a unique operation of values/objects
|
|
|
|
* @param list list/vector of values or objects
|
|
|
|
* @param equals operator to be used, only provide this for complex objects
|
|
|
|
*/
|
|
|
|
jx.math.sets.unique = jx.utils.unique ;
|
|
|
|
/**
|
|
|
|
* This function will perform the union of 2 sets (objects, or values)
|
|
|
|
* @param list1 list/vector of values or objects
|
|
|
|
* @param list2 list/vector of values or objects
|
|
|
|
* @param equals operator to be used to evaluate equality (use this for complex objects)
|
|
|
|
*/
|
|
|
|
jx.math.sets.union = function(list1,list2,equals){
|
|
|
|
runion = [] ;
|
|
|
|
runion = list1.concat(list2) ;
|
|
|
|
runion = jx.math.sets.unique(runion,equals)
|
|
|
|
return runion;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will normalize values within a vector
|
|
|
|
* By definition normalization is (x - u) / sd (assuming population parameters are known)
|
|
|
|
*/
|
|
|
|
jx.math.normalize = function(lvalues){
|
|
|
|
mean = jx.math.mean(lvalues) ;
|
|
|
|
sd = jx.math.sd(lvalues) ;
|
|
|
|
return jx.utils.patterns.visitor(lvalues,function(x){
|
|
|
|
return ((x - mean) / sd)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function will scale a feature vector over it's range
|
|
|
|
*/
|
|
|
|
jx.math.scale = function(lvalues,percent){
|
|
|
|
max = jx.math.max(lvalues) ;
|
|
|
|
min = jx.math.min(lvalues) ;
|
|
|
|
return jx.utils.patterns.visitor(lvalues,function(x){
|
|
|
|
var value = (x - min ) / max ;
|
|
|
|
if(percent == true){
|
|
|
|
return (100*value).toFixed(2)
|
|
|
|
}else{
|
|
|
|
return value ;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* This is a lightweight map reduce infrastructure
|
|
|
|
*/
|
|
|
|
jx.mr = {} ;
|
|
|
|
/**
|
|
|
|
* This function will perform a map on a given id in rec, then will call emit with the
|
|
|
|
*/
|
|
|
|
jx.mr.map = null
|
|
|
|
/**
|
|
|
|
* @param keys
|
|
|
|
* @param values array of values that were mapped
|
|
|
|
*/
|
|
|
|
jx.mr.reduce = null;
|
|
|
|
jx.mr.mapreduce = function(data,fn_map,fn_reduce){
|
|
|
|
if (fn_map == null){
|
|
|
|
throw new "Map function is not defined"
|
|
|
|
}
|
|
|
|
map = {} ;
|
|
|
|
emit = function(id,values){
|
|
|
|
if(map[id] == null){
|
|
|
|
map[id] = []
|
|
|
|
}
|
|
|
|
map[id].push(values);
|
|
|
|
}
|
|
|
|
if(data.constructor != Array){
|
|
|
|
for (id in data){
|
|
|
|
//rec = data[id] ;
|
|
|
|
rec = {}
|
|
|
|
rec['__id'] = id;
|
|
|
|
rec['data'] = data[id] ;
|
|
|
|
fn_map(rec,emit)
|
|
|
|
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for (var i=0; i < data.length; i++){
|
|
|
|
rec = data[i];
|
|
|
|
fn_map(rec,emit);
|
|
|
|
//if(i == 2)break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(fn_reduce != null){
|
|
|
|
keys = jx.utils.keys(map) ;
|
|
|
|
m = {}
|
|
|
|
for(var i=0; i < keys.length; i++){
|
|
|
|
id = keys[i] ;
|
|
|
|
values = map[id] ;
|
|
|
|
value = fn_reduce(id,values) ;
|
|
|
|
id = keys[i] ;
|
|
|
|
m[id] = value;
|
|
|
|
|
|
|
|
}
|
|
|
|
map = m
|
|
|
|
|
|
|
|
}
|
|
|
|
return map ;
|
|
|
|
}
|