package model.compute; /** *

Title:Compute.java

*

Description: Does Basic computations to server other * analysis objects

*

Copyright: Copyright (c) 2009

*

Company: Hanover College

* @author John H. Krantz, Ph.D. * @version 0.1 */ public class Compute { // empty constructor public Compute(){} /** * does a correlation of two two dimensional arrays * @param x first array * @param y second array * @return r the pearson's correlation value */ public static double Correl2D(double [][] x, double [][] y){ double r = -2; if (x.length == y.length & x[0].length == y[0].length){ r = 0; // find the number of pairs double n_p = x.length*x[0].length; double numerator = (n_p*sumXY2D(x,y)-(sum2D(x)*sum2D(y))); double xDenom = n_p*sumSq2D(x)-square(sum2D(x)); double yDenom = n_p*sumSq2D(y)-square(sum2D(y)); r = numerator/ Math.sqrt(xDenom*yDenom); } return r; } /** * Returns the square of a double values * @param val * @return double */ public static double square(double val){ return val*val; } /** * Returns the sum of a 2 dimensional array * @param double [][] vals the array to be summed * @return double the sum */ public static double sum2D(double [][] vals){ double sum = 0; for (int i = 0; i < vals.length; i ++){ for (int j = 0; j < vals[0].length; j ++){ sum += vals[i][j]; } } return sum; } /** * Returns the sum of squares of a two dimensional array * of values * @param double [][] vals the array to be manipulated * @return double the sum of squares of the array */ public static double sumSq2D(double [][] vals){ double sumSq = 0; for (int i = 0; i < vals.length; i ++){ for (int j = 0; j < vals[0].length; j ++){ sumSq += square(vals[i][j]); } } return sumSq; } /** * Gets the sum of x*y in 2d arrays, * multiplies the values in the same positions in the * two arrays and then sums. * @param x * @param y * @return */ public static double sumXY2D(double [][] x, double [][] y){ double sumXY = 0; if (x.length == y.length & x[0].length == y[0].length){ for (int i = 0; i < x.length; i ++){ for (int j = 0; j < y[0].length; j ++){ sumXY += x[i][j]*y[i][j]; } } } return sumXY; } }