utils.ListMaths Maven / Gradle / Ivy
package utils;
import parallel.tasks.SumSqrDiffTask;
import parallel.tasks.SumSqrTask;
import parallel.tasks.SumTask;
import tech.tablesaw.api.DoubleColumn;
import java.util.List;
import java.util.concurrent.CyclicBarrier;
/**
* Utilities for all classes that implement the List interface
*/
public class ListMaths {
/**
* Compute the max of the List elements
*/
public static double max(final List data){
double rslt = data.get(0);
for(int i=1; i data){
double rslt = data.get(0);
for(int i=1; i data.get(i)){
rslt = data.get(i);
}
}
return rslt;
}
/**
* Compute the sum of the List elements
*/
public static double sum(final List data){
double rslt = 0.0;
for(int i=0; i data){
double rslt = 0.0;
for(int i=0; i data){
CyclicBarrier barrier = new CyclicBarrier(2);
SumTask> sumTask = new SumTask<>(data, barrier);
Thread sum = new Thread(sumTask);
sum.start();
SumSqrTask> sumSqrTask = new SumSqrTask(data, barrier);
Thread sumSqr = new Thread(sumSqrTask);
sumSqr.start();
while(!sumTask.isFinished() || !sumSqrTask.isFinished()){
//spin the main thread
}
return sumSqrTask.getResult() - (1./data.size())*(sumTask.getResult()*sumTask.getResult());
}
/**
* Sum Squared Regression or SSR = \Sum (data[i] - \bar(data))^2
*
* if data is an approximation coming from a model then this is the
* SSR or Sum Squared Regression. If data is an observation then this
* will be the SST or Sum Squared Total
*/
public static double sse(final List data){
double mean = ListMaths.sum(data)/data.size();
SumSqrDiffTask> task = new SumSqrDiffTask<>(data, mean);
task.run();
return task.getResult();
}
/**
* Compute the sum of the absolute values of the List elements
*/
public static double absSum(final List data){
double rslt = 0.0;
for (int i=0; i data){
double min = ListMaths.min(data);
double max = ListMaths.max(data);
for(int i=0; i