mckay.utilities.staticlibraries
Class MathAndStatsMethods

java.lang.Object
  extended by mckay.utilities.staticlibraries.MathAndStatsMethods

public class MathAndStatsMethods
extends java.lang.Object

A holder class for static methods relating to statistical and mathematical analysis.


Constructor Summary
MathAndStatsMethods()
           
 
Method Summary
static double calculateEuclideanDistance(double[] x, double[] y)
          Returns the Euclidian distance between x and y.
static int ensureIsPowerOfN(int x, int n)
          If the given x is a power of the given n, then x is returned.
static int generateRandomNumber(int max)
          Returns a random integer from 0 to max - 1, based on the uniform distribution.
static double getArraySum(double[] to_sum)
          Returns the sum of the contents of all of the entries of the given array.
static double getAverage(double[] data)
          Returns the average of a set of doubles.
static double getAverage(int[] data)
          Returns the average of a set of ints.
static long getFactorial(long n)
          Calculates the factorial of the given value.
static int getIndexOfLargest(double[] values)
          Returns the index of the entry of an array of doubles with the largest value.
static int getIndexOfLargest(float[] values)
          Returns the index of the entry of an array of floats with the largest value.
static int getIndexOfLargest(int[] values)
          Returns the index of the entry of an array of itegers with the largest value.
static int getIndexOfMedian(double[] values)
          Returns the index of the entry of an array of doubles that corresponds.
static int getIndexOfSmallest(double[] values)
          Returns the index of the entry of an array of doubles with the smallest value.
static int getIndexOfSmallest(int[] values)
          Returns the index of the entry of an array of doubles with the smallest value.
static int getNumberPerumutations(int set_size, int permutation_size)
          Calculates the number of permutations of the given parameters without replacement.
static double getPercentage(int value, int total)
          Returns the percentage of the given total that the given value represents.
static int[] getRandomOrdering(int number_entries)
          Returns an array number_entries arrays.
static double getStandardDeviation(double[] data)
          Returns the standard deviation of a set of doubles.
static double getStandardDeviation(int[] data)
          Returns the standard deviation of a set of ints.
static boolean isFactorOrMultiple(int x, int y, int[] z)
          Returns whether or not x is either a factor or a multiple of y.
static double logBaseN(double x, double n)
          Returns the logarithm of the specified base of the given number.
static double[] normalize(double[] to_normalize)
          Return a normalized copy of the the given array.
static double[][] normalize(double[][] to_normalize)
          Return a normalized copy of the the given array.
static double[][] normalizeEntirely(double[][] to_normalize)
          Return a normalized copy of the the given array.
static int pow(int a, int b)
          Returns the given a raised to the power of the given b.
static int[] removeRedundantEntries(int[] to_check)
          Returns a copy of the given array, but with any duplicate entries removed (e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MathAndStatsMethods

public MathAndStatsMethods()
Method Detail

getPercentage

public static double getPercentage(int value,
                                   int total)
Returns the percentage of the given total that the given value represents.

Parameters:
value - The numerator in the percentage calculation.
total - The denominator in the percentage calculation.
Returns:
The percentage.

getAverage

public static double getAverage(double[] data)
Returns the average of a set of doubles. Returns 0 if the length of the data is 0.

Parameters:
data - The data to be averaged.
Returns:
The mean of the given data.

getAverage

public static double getAverage(int[] data)
Returns the average of a set of ints. Returns 0 if the length of the data is 0.

Parameters:
data - The data to be averaged.
Returns:
The mean of the given data.

getStandardDeviation

public static double getStandardDeviation(double[] data)
Returns the standard deviation of a set of doubles. Returns 0 if there is only one piece of data.

Parameters:
data - The data for which the standard deviation is to be found.
Returns:
The standard deviation of the given data.

getStandardDeviation

public static double getStandardDeviation(int[] data)
Returns the standard deviation of a set of ints. Returns 0 if there is only one piece of data.

Parameters:
data - The data for which the standard deviation is to be found.
Returns:
The standard deviation of the given data.

getFactorial

public static long getFactorial(long n)
                         throws java.lang.Exception
Calculates the factorial of the given value.

Parameters:
n - The number to find the factorial of.
Returns:
The factorial.
Throws:
java.lang.Exception - Throws an informative exception if n is below 0 or greater than 20.

getIndexOfSmallest

public static int getIndexOfSmallest(double[] values)
Returns the index of the entry of an array of doubles with the smallest value. The first occurence is returned in the case of a tie.


getIndexOfSmallest

public static int getIndexOfSmallest(int[] values)
Returns the index of the entry of an array of doubles with the smallest value. The first occurence is returned in the case of a tie.


getIndexOfMedian

public static int getIndexOfMedian(double[] values)
Returns the index of the entry of an array of doubles that corresponds. to the median entry. Returns -1 if a problem occurs.


getIndexOfLargest

public static int getIndexOfLargest(float[] values)
Returns the index of the entry of an array of floats with the largest value. The first occurence is returned in the case of a tie.


getIndexOfLargest

public static int getIndexOfLargest(double[] values)
Returns the index of the entry of an array of doubles with the largest value. The first occurence is returned in the case of a tie.


getIndexOfLargest

public static int getIndexOfLargest(int[] values)
Returns the index of the entry of an array of itegers with the largest value. The first occurence is returned in the case of a tie.


removeRedundantEntries

public static int[] removeRedundantEntries(int[] to_check)
Returns a copy of the given array, but with any duplicate entries removed (e.g. {1, 3, 5, 3, 4} would return {1, 3, 5, 4}).

Parameters:
to_check - The array to remove double entries from.
Returns:
Returns the array with double entries removed.

calculateEuclideanDistance

public static double calculateEuclideanDistance(double[] x,
                                                double[] y)
                                         throws java.lang.Exception
Returns the Euclidian distance between x and y. Throws an exception if x and y have different sizes.

Throws:
java.lang.Exception

generateRandomNumber

public static int generateRandomNumber(int max)
Returns a random integer from 0 to max - 1, based on the uniform distribution.


getRandomOrdering

public static int[] getRandomOrdering(int number_entries)
Returns an array number_entries arrays. Each entry has a value between 0 and number_entries - 1, and no numbers are repeated. Ordering of numbers is random.


getArraySum

public static double getArraySum(double[] to_sum)
Returns the sum of the contents of all of the entries of the given array.


normalize

public static double[] normalize(double[] to_normalize)
Return a normalized copy of the the given array. The original array is not altered.


normalize

public static double[][] normalize(double[][] to_normalize)
Return a normalized copy of the the given array. Normalization is performed by row (i.e. the sum of each row (first indice) is one after normalization). Each row is independant. The original array is not altered.


normalizeEntirely

public static double[][] normalizeEntirely(double[][] to_normalize)
Return a normalized copy of the the given array. Normalization is performed overall so that the sum of all entries is 1.0. The original array is not altered.


pow

public static int pow(int a,
                      int b)
Returns the given a raised to the power of the given b.

IMPORTANT: b must be greater than zero.

Parameters:
a - The base.
b - The exponent.

logBaseN

public static double logBaseN(double x,
                              double n)
Returns the logarithm of the specified base of the given number.

IMPORTANT: Both x and n must be greater than zero.

Parameters:
x - The value to find the log of.
n - The base of the logarithm.

ensureIsPowerOfN

public static int ensureIsPowerOfN(int x,
                                   int n)
If the given x is a power of the given n, then x is returned. If not, then the next value above the given x that is a power of n is returned.

IMPORTANT: Both x and n must be greater than zero.

Parameters:
x - The value to ensure is a power of n.
n - The power to base x's validation on.

getNumberPerumutations

public static int getNumberPerumutations(int set_size,
                                         int permutation_size)
                                  throws java.lang.Exception
Calculates the number of permutations of the given parameters without replacement.

For example, if one wishes to find the number of ordered ways that the letters C, A and T can be combined into a set of size 2, then the set_size paramter would be 3 (because there are three letters in the alphabet being used) and the permutation_size parameter would be 2.

Parameters:
set_size - The number of entries in the alphabet that can be used to construct permutations.
permutation_size - The size of the permutation sets to considered.
Returns:
The number of possible permutations
Throws:
java.lang.Exception - An informative Exception is thrown if set_size is greater than 20, as this would necessitate the calculation of an overly large factorial. An Exception is also thrown if permutation_size is greater than set_size.

isFactorOrMultiple

public static boolean isFactorOrMultiple(int x,
                                         int y,
                                         int[] z)
Returns whether or not x is either a factor or a multiple of y. z denotes the possible multipliers to check for. True is returned if x is either a factor of a multiple of y (and vice versa), and false otherwise.