Just Checkstyle and Javadoc corrections

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140991 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2003-08-09 04:03:41 +00:00
parent 88b4e3dbc9
commit b3b89139bb
29 changed files with 565 additions and 386 deletions

View File

@ -55,8 +55,7 @@ package org.apache.commons.math;
/**
* Signals a configuration problem with any of the factory methods.
*
* @version $Revision: 1.5 $ $Date: 2003/07/30 21:58:10 $
* @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
*/
public class MathConfigurationException extends MathException {
@ -71,7 +70,7 @@ public class MathConfigurationException extends MathException {
* Construct an exception with the given message.
* @param message message describing the problem
*/
public MathConfigurationException(String message) {
public MathConfigurationException(final String message) {
super(message);
}
@ -80,7 +79,9 @@ public class MathConfigurationException extends MathException {
* @param message message describing the problem
* @param throwable caught exception causing this problem
*/
public MathConfigurationException(String message, Throwable throwable) {
public MathConfigurationException(
final String message,
final Throwable throwable) {
super(message, throwable);
}
@ -88,7 +89,7 @@ public class MathConfigurationException extends MathException {
* Construct an exception with the given root cause.
* @param throwable caught exception causing this problem
*/
public MathConfigurationException(Throwable throwable) {
public MathConfigurationException(final Throwable throwable) {
super(throwable);
}
}

View File

@ -55,13 +55,12 @@ package org.apache.commons.math;
/**
* A generic exception indicating problems in the math package.
*
* @version $Revision: 1.4 $ $Date: 2003/07/09 20:02:44 $
* @version $Revision: 1.5 $ $Date: 2003/08/09 04:03:41 $
*/
public class MathException extends Exception {
/**
*
* Constructs a MathException
*/
public MathException() {
super();
@ -70,7 +69,7 @@ public class MathException extends Exception {
/**
* @param message message describing the problem
*/
public MathException(String message) {
public MathException(final String message) {
super(message);
}
@ -78,14 +77,14 @@ public class MathException extends Exception {
* @param message message describing the problem
* @param throwable caught exception causing this problem
*/
public MathException(String message, Throwable throwable) {
public MathException(final String message, final Throwable throwable) {
super(message, throwable);
}
/**
* @param throwable caught exception causing this problem
*/
public MathException(Throwable throwable) {
public MathException(final Throwable throwable) {
super(throwable);
}

View File

@ -59,8 +59,7 @@ import org.apache.commons.math.MathException;
* Provide the bisection algorithm for solving for zeros of real univariate
* functions. It will only search for one zero in the given interval. The
* function is supposed to be continuous but not necessarily smooth.
*
* @version $Revision: 1.2 $ $Date: 2003/07/09 20:02:43 $
* @version $Revision: 1.3 $ $Date: 2003/08/09 04:03:41 $
*/
public class BisectionSolver extends UnivariateRealSolverImpl {
/**

View File

@ -62,7 +62,7 @@ import org.apache.commons.math.util.BeanTransformer;
* univariate statistics for a List of Java Beans by property. This
* implementation uses beanutils' PropertyUtils to get a simple, nested,
* indexed, mapped, or combined property from an element of a List.
* @version $Revision: 1.3 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
*/
public class BeanListUnivariateImpl extends ListUnivariateImpl {

View File

@ -57,16 +57,22 @@ package org.apache.commons.math.stat;
* StatUtils provides easy static implementations of common double[] based
* statistical methods. These return a single result value or in some cases, as
* identified in the javadoc for each method, Double.NaN.
* @version $Revision: 1.14 $ $Date: 2003/07/09 21:45:23 $
* @version $Revision: 1.15 $ $Date: 2003/08/09 04:03:41 $
*/
public class StatUtils {
public final class StatUtils {
/**
* Private Constructor
*/
private StatUtils() {
}
/**
* The sum of the values that have been added to Univariate.
* @param values Is a double[] containing the values
* @return the sum of the values or Double.NaN if the array is empty
*/
public static double sum(double[] values) {
public static double sum(final double[] values) {
return sum(values, 0, values.length);
}
@ -77,7 +83,10 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the sum of the values or Double.NaN if the array is empty
*/
public static double sum(double[] values, int begin, int length) {
public static double sum(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double accum = 0.0;
for (int i = begin; i < begin + length; i++) {
@ -91,7 +100,7 @@ public class StatUtils {
* @param values Is a double[] containing the values
* @return the sum of the squared values or Double.NaN if the array is empty
*/
public static double sumSq(double[] values) {
public static double sumSq(final double[] values) {
return sumSq(values, 0, values.length);
}
@ -102,7 +111,10 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the sum of the squared values or Double.NaN if the array is empty
*/
public static double sumSq(double[] values, int begin, int length) {
public static double sumSq(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double accum = 0.0;
for (int i = begin; i < begin + length; i++) {
@ -116,7 +128,7 @@ public class StatUtils {
* @param values Is a double[] containing the values
* @return the product values or Double.NaN if the array is empty
*/
public static double product(double[] values) {
public static double product(final double[] values) {
return product(values, 0, values.length);
}
@ -127,7 +139,10 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the product values or Double.NaN if the array is empty
*/
public static double product(double[] values, int begin, int length) {
public static double product(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double product = 1.0;
for (int i = begin; i < begin + length; i++) {
@ -141,7 +156,7 @@ public class StatUtils {
* @param values Is a double[] containing the values
* @return the sumLog value or Double.NaN if the array is empty
*/
public static double sumLog(double[] values) {
public static double sumLog(final double[] values) {
return sumLog(values, 0, values.length);
}
@ -152,7 +167,10 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the sumLog value or Double.NaN if the array is empty
*/
public static double sumLog(double[] values, int begin, int length) {
public static double sumLog(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double sumLog = 0.0;
for (int i = begin; i < begin + length; i++) {
@ -167,7 +185,7 @@ public class StatUtils {
* @param values Is a double[] containing the values
* @return the mean of the values or Double.NaN if the array is empty
*/
public static double mean(double[] values) {
public static double mean(final double[] values) {
return sum(values) / (double) values.length;
}
@ -179,7 +197,10 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the mean of the values or Double.NaN if the array is empty
*/
public static double mean(double[] values, int begin, int length) {
public static double mean(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
return sum(values, begin, length) / ((double) length);
}
@ -197,7 +218,7 @@ public class StatUtils {
* @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set.
*/
public static double variance(double[] values) {
public static double variance(final double[] values) {
return variance(values, 0, values.length);
}
@ -216,7 +237,10 @@ public class StatUtils {
* @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set.
*/
public static double variance(double[] values, int begin, int length) {
public static double variance(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double variance = Double.NaN;
@ -231,7 +255,7 @@ public class StatUtils {
accum2 += (values[i] - mean);
}
variance =
(accum - (Math.pow(accum2, 2) / ((double)length)))
(accum - (Math.pow(accum2, 2) / ((double) length)))
/ (double) (length - 1);
}
return variance;
@ -242,7 +266,7 @@ public class StatUtils {
* @param values Is a double[] containing the values
* @return the maximum of the values or Double.NaN if the array is empty
*/
public static double max(double[] values) {
public static double max(final double[] values) {
return max(values, 0, values.length);
}
@ -253,14 +277,19 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the maximum of the values or Double.NaN if the array is empty
*/
public static double max(double[] values, int begin, int length) {
public static double max(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double max = Double.NaN;
for (int i = begin; i < begin + length; i++) {
if (i == 0) {
max = values[i];
} else {
max = (max > values[i]) ? max : values[i];
if (max < values[i]) {
max = values[i];
}
}
}
return max;
@ -271,7 +300,7 @@ public class StatUtils {
* @param values Is a double[] containing the values
* @return the minimum of the values or Double.NaN if the array is empty
*/
public static double min(double[] values) {
public static double min(final double[] values) {
return min(values, 0, values.length);
}
@ -282,7 +311,11 @@ public class StatUtils {
* @param length processing at this point in the array
* @return the minimum of the values or Double.NaN if the array is empty
*/
public static double min(double[] values, int begin, int length) {
public static double min(
final double[] values,
final int begin,
final int length) {
testInput(values, begin, length);
double min = Double.NaN;
@ -290,7 +323,9 @@ public class StatUtils {
if (i == 0) {
min = values[i];
} else {
min = (min < values[i]) ? min : values[i];
if (min > values[i]) {
min = values[i];
}
}
}
return min;
@ -303,16 +338,22 @@ public class StatUtils {
* @param begin processing at this point in the array
* @param length processing at this point in the array
*/
private static void testInput(double[] values, int begin, int length) {
private static void testInput(
final double[] values,
final int begin,
final int length) {
if (length > values.length)
if (length > values.length) {
throw new IllegalArgumentException("length > values.length");
}
if (begin + length > values.length)
throw new IllegalArgumentException("begin + length > values.length");
if (begin + length > values.length) {
throw new IllegalArgumentException(
"begin + length > values.length");
}
if (values == null)
if (values == null) {
throw new IllegalArgumentException("input value array is null");
}
}
}

View File

@ -59,7 +59,7 @@ package org.apache.commons.math.stat.univariate;
* Provides the ability to extend polymophically so that
* indiviual statistics do not need to implement these methods unless
* there are better algorithms for handling the calculation.
* @version $Revision: 1.6 $ $Date: 2003/07/30 21:58:11 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public abstract class AbstractStorelessUnivariateStatistic
extends AbstractUnivariateStatistic
@ -70,10 +70,12 @@ public abstract class AbstractStorelessUnivariateStatistic
* calculation off to the instantanious increment method. In most cases of
* StorelessUnivariateStatistic this is never really used because more
* efficient algorithms are available for that statistic.
* @see org.apache.commons.math.stat.univariate.
* UnivariateStatistic#evaluate(double[], int, int)
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
if (this.test(values, begin, length)) {
this.clear();
int l = begin + length;
@ -84,4 +86,19 @@ public abstract class AbstractStorelessUnivariateStatistic
return getResult();
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public abstract void clear();
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public abstract double getResult();
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public abstract void increment(final double d);
}

View File

@ -57,7 +57,7 @@ package org.apache.commons.math.stat.univariate;
* Abstract Implementation for UnivariateStatistics.
* Provides the ability to extend polymophically so that
* indiviual statistics do not need to implement these methods.
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:37:10 $
* @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
*/
public abstract class AbstractUnivariateStatistic
implements UnivariateStatistic {
@ -65,20 +65,20 @@ public abstract class AbstractUnivariateStatistic
/**
* This implementation provides a simple wrapper around the double[]
* and passes the request onto the evaluate(DoubleArray da) method.
*
* @see org.apache.commons.math.stat.univariate.
* UnivariateStatistic#evaluate(double[])
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[])
*/
public double evaluate(double[] values) {
public double evaluate(final double[] values) {
return evaluate(values, 0, values.length);
}
/**
* Subclasses of AbstractUnivariateStatistc need to implement this method.
* @see org.apache.commons.math.stat.univariate.
* UnivariateStatistic#evaluate(double[], int, int)
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public abstract double evaluate(double[] values, int begin, int length);
public abstract double evaluate(
final double[] values,
final int begin,
final int length);
/**
* this protected test method used by all methods to verify the content
@ -86,15 +86,22 @@ public abstract class AbstractUnivariateStatistic
* @param values Is a double[] containing the values
* @param begin processing at this point in the array
* @param length processing at this point in the array
* @return this is used to determine if the array is of 0 length or not,
* it is used by an individual statistic to determine if continuation
* of a statistical calculation should continue or return NaN.
*/
protected boolean test(double[] values, int begin, int length) {
protected boolean test(
final double[] values,
final int begin,
final int length) {
if (length > values.length) {
throw new IllegalArgumentException("length > values.length");
}
if (begin + length > values.length) {
throw new IllegalArgumentException("begin + length > values.length");
throw new IllegalArgumentException(
"begin + length > values.length");
}
if (values == null) {

View File

@ -62,7 +62,7 @@ package org.apache.commons.math.stat.univariate;
* implemented using it. If a Statistic cannot be implemented in a Storeless
* approach it should implement the UnivariateStatistic interface directly
* instead.
* @version $Revision: 1.6 $ $Date: 2003/07/15 03:37:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public interface StorelessUnivariateStatistic extends UnivariateStatistic {

View File

@ -58,7 +58,7 @@ package org.apache.commons.math.stat.univariate;
* double[] based content using an implemented statistical approach.
* The interface provides two "stateless" simple methods to calculate
* a statistic from a double[] based parameter.
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:37:10 $
* @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:41 $
*/
public interface UnivariateStatistic {

View File

@ -62,7 +62,7 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.5 $ $Date: 2003/07/15 03:36:36 $
* @version $Revision: 1.6 $ $Date: 2003/08/09 04:03:40 $
*/
public class FirstMoment extends AbstractStorelessUnivariateStatistic {
@ -91,10 +91,9 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
protected double n0 = 0.0;
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#increment(double)
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (n < 1) {
m1 = 0.0;
}
@ -108,8 +107,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#clear()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
m1 = Double.NaN;
@ -120,8 +118,7 @@ public class FirstMoment extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return m1;

View File

@ -58,7 +58,7 @@ package org.apache.commons.math.stat.univariate.moment;
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/
public class FourthMoment extends ThirdMoment {
@ -75,9 +75,12 @@ public class FourthMoment extends ThirdMoment {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (n < 1) {
m4 = m3 = m2 = m1 = 0.0;
m4 = 0.0;
m3 = 0.0;
m2 = 0.0;
m1 = 0.0;
}
/* retain previous m3 */
@ -96,7 +99,7 @@ public class FourthMoment extends ThirdMoment {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return m4;

View File

@ -58,25 +58,29 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
/**
* Returns the <a href="http://www.xycoon.com/geometric_mean.htm">
* geometric mean </a> of the available values
* @version $Revision: 1.8 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.9 $ $Date: 2003/08/09 04:03:40 $
*/
public class GeometricMean extends SumOfLogs {
/** */
protected int n = 0;
/** */
private double geoMean = Double.NaN;
/** */
private double lastSum = 0.0;
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
n++;
super.increment(d);
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
if (lastSum != super.getResult() || n == 1) {
@ -105,7 +109,10 @@ public class GeometricMean extends SumOfLogs {
* any of the values are &lt;= 0.
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
return Math.exp(
super.evaluate(values, begin, length) / (double) length);
}

View File

@ -62,23 +62,34 @@ import org
.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/
public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/** */
protected FourthMoment moment = null;
/** */
protected boolean incMoment = true;
/** */
private double kurtosis = Double.NaN;
/** */
private int n = 0;
/**
* Construct a Kurtosis
*/
public Kurtosis() {
moment = new FourthMoment();
}
public Kurtosis(FourthMoment m4) {
/**
* Construct a Kurtosis with an external moment
* @param m4 external Moment
*/
public Kurtosis(final FourthMoment m4) {
incMoment = false;
this.moment = m4;
}
@ -86,14 +97,14 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (incMoment) {
moment.increment(d);
}
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
if (n < moment.n) {
@ -135,6 +146,7 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
/*UnvariateStatistic Approach */
/** */
Mean mean = new Mean();
/**
@ -153,8 +165,10 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
* @param length processing at this point in the array
* @return the kurtosis of the values or Double.NaN if the array is empty
*/
public double evaluate(double[] values, int begin, int length) {
;
public double evaluate(
final double[] values,
final int begin,
final int length) {
// Initialize the kurtosis
double kurt = Double.NaN;
@ -167,8 +181,9 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
// Get the mean and the standard deviation
double m = mean.evaluate(values, begin, length);
// Calc the std, this is implemented here instead of using the
// standardDeviation method eliminate a duplicate pass to get the mean
// Calc the std, this is implemented here instead
// of using the standardDeviation method eliminate
// a duplicate pass to get the mean
double accum = 0.0;
double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) {
@ -189,12 +204,12 @@ public class Kurtosis extends AbstractStorelessUnivariateStatistic {
}
// Get N
double n = length;
double n0 = length;
double coefficientOne =
(n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
(n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
double termTwo =
((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3)));
((3 * Math.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3)));
// Calculate kurtosis
kurt = (coefficientOne * accum3) - termTwo;

View File

@ -53,26 +53,38 @@
*/
package org.apache.commons.math.stat.univariate.moment;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
import org
.apache
.commons
.math
.stat
.univariate
.AbstractStorelessUnivariateStatistic;
import org.apache.commons.math.stat.univariate.summary.Sum;
/**
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
* arithmetic mean </a> of the available values.
* @version $Revision: 1.7 $ $Date: 2003/07/15 03:36:36 $
* @version $Revision: 1.8 $ $Date: 2003/08/09 04:03:40 $
*/
public class Mean extends AbstractStorelessUnivariateStatistic {
/** first moment of values that have been added */
protected FirstMoment moment = null;
/** */
protected boolean incMoment = true;
/** */
public Mean() {
moment = new FirstMoment();
}
public Mean(FirstMoment m1) {
/**
* Constructs a Mean with an External Moment.
* @param m1 the moment
*/
public Mean(final FirstMoment m1) {
this.moment = m1;
incMoment = false;
}
@ -80,7 +92,7 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (incMoment) {
moment.increment(d);
}
@ -96,14 +108,16 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return moment.m1;
}
/*UnvariateStatistic Approach */
Sum sum = new Sum();
/** */
protected Sum sum = new Sum();
/**
* Returns the <a href="http://www.xycoon.com/arithmetic_mean.htm">
@ -114,7 +128,10 @@ public class Mean extends AbstractStorelessUnivariateStatistic {
* @return the mean of the values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
if (test(values, begin, length)) {
return sum.evaluate(values) / ((double) length);
}

View File

@ -58,7 +58,7 @@ package org.apache.commons.math.stat.univariate.moment;
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/
public class SecondMoment extends FirstMoment {
@ -71,7 +71,7 @@ public class SecondMoment extends FirstMoment {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (n < 1) {
m1 = m2 = 0.0;
}
@ -96,7 +96,7 @@ public class SecondMoment extends FirstMoment {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return m2;

View File

@ -63,23 +63,34 @@ import org
/**
*
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/
public class Skewness extends AbstractStorelessUnivariateStatistic {
/** */
protected ThirdMoment moment = null;
/** */
protected boolean incMoment = true;
/** */
protected double skewness = Double.NaN;
/** */
private int n = 0;
/**
* Constructs a Skewness
*/
public Skewness() {
moment = new ThirdMoment();
}
public Skewness(ThirdMoment m3) {
/**
* Constructs a Skewness with an external moment
* @param m3 external moment
*/
public Skewness(final ThirdMoment m3) {
incMoment = false;
this.moment = m3;
}
@ -87,14 +98,14 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (incMoment) {
moment.increment(d);
}
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
if (n < moment.n) {
@ -133,12 +144,13 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
/*UnvariateStatistic Approach */
/** */
Mean mean = new Mean();
/**
* This algorithm uses a corrected two pass algorithm of the following
* <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
* corrected two pass formula (14.1.8)</a>, and also referenced in:
* corrected two pass formula (14.1.8)</a>, and also referenced in
* <p>
* "Algorithms for Computing the Sample Variance: Analysis and
* Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J.
@ -152,7 +164,10 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
* @return the skewness of the values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
// Initialize the skewness
double skew = Double.NaN;
@ -165,8 +180,9 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
// Get the mean and the standard deviation
double m = mean.evaluate(values, begin, length);
// Calc the std, this is implemented here instead of using the
// standardDeviation method eliminate a duplicate pass to get the mean
// Calc the std, this is implemented here instead
// of using the standardDeviation method eliminate
// a duplicate pass to get the mean
double accum = 0.0;
double accum2 = 0.0;
for (int i = begin; i < begin + length; i++) {
@ -186,10 +202,10 @@ public class Skewness extends AbstractStorelessUnivariateStatistic {
}
// Get N
double n = length;
double n0 = length;
// Calculate skewness
skew = (n / ((n - 1) * (n - 2))) * accum3;
skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
}
}

View File

@ -55,31 +55,40 @@ package org.apache.commons.math.stat.univariate.moment;
/**
*
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/
public class StandardDeviation extends Variance {
/** */
protected double std = Double.NaN;
/** */
private double lastVar = 0.0;
/**
* Constructs a StandardDeviation
*/
public StandardDeviation() {
super();
}
public StandardDeviation(SecondMoment m2) {
/**
* Constructs a StandardDeviation with an external moment
* @param m2 the external moment
*/
public StandardDeviation(final SecondMoment m2) {
super(m2);
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
super.increment(d);
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
if (lastVar != super.getResult()) {
@ -112,7 +121,11 @@ public class StandardDeviation extends Variance {
* or 0.0 for a single value set.
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double var = super.evaluate(values, begin, length);
if (Double.isNaN(var)) {

View File

@ -58,9 +58,9 @@ package org.apache.commons.math.stat.univariate.moment;
* <a href="http://www.spss.com/tech/stat/Algorithms/11.5/descriptives.pdf">
* recursive strategy
* </a>. Both incremental and evaluation strategies currently use this approach.
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:10 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:40 $
*/
public class ThirdMoment extends SecondMoment{
public class ThirdMoment extends SecondMoment {
/** third moment of values that have been added */
protected double m3 = Double.NaN;
@ -77,7 +77,7 @@ public class ThirdMoment extends SecondMoment{
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (n < 1) {
m3 = m2 = m1 = 0.0;
}
@ -96,7 +96,7 @@ public class ThirdMoment extends SecondMoment{
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return m3;

View File

@ -57,39 +57,61 @@ import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatis
/**
*
* @version $Revision: 1.7 $ $Date: 2003/07/15 03:36:36 $
* @version $Revision: 1.8 $ $Date: 2003/08/09 04:03:40 $
*/
public class Variance extends AbstractStorelessUnivariateStatistic {
/** SecondMoment is used in incremental calculation of Variance*/
protected SecondMoment moment = null;
/**
* Boolean test to determine if this Variance should also increment
* the second moment, this evaluates to false when this Variance is
* constructed with an external SecondMoment as a parameter.
*/
protected boolean incMoment = true;
/**
* This property maintains the latest calculated
* variance for efficiency when getResult() is called
* many times between increments.
*/
protected double variance = Double.NaN;
/**
* Maintains the current count of inrementations that have occured.
* If the external SecondMoment is used, the this is updated from
* that moments counter
*/
protected int n = 0;
/**
* Constructs a Variance.
*/
public Variance() {
moment = new SecondMoment();
}
public Variance(SecondMoment m2) {
/**
* Constructs a Variance based on an externalized second moment.
* @param m2 the SecondMoment (Thrid or Fourth moments work
* here as well.)
*/
public Variance(final SecondMoment m2) {
incMoment = false;
this.moment = m2;
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#increment(double)
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (incMoment) {
moment.increment(d);
}
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
if (n < moment.n) {
@ -107,8 +129,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#clear()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
if (incMoment) {
@ -118,8 +139,8 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
n = 0;
}
/*UnvariateStatistic Approach */
Mean mean = new Mean();
/** Mean to be used in UnvariateStatistic evaluation approach. */
protected Mean mean = new Mean();
/**
* Returns the variance of the available values. This uses a corrected
@ -136,10 +157,12 @@ public class Variance extends AbstractStorelessUnivariateStatistic {
* @param length processing at this point in the array
* @return the result, Double.NaN if no values for an empty array
* or 0.0 for a single value set.
* @see org.apache.commons.math.stat.univariate.
* UnivariateStatistic#evaluate(double[], int, int)
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double var = Double.NaN;

View File

@ -62,16 +62,17 @@ import org
.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:12 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public class Max extends AbstractStorelessUnivariateStatistic {
/** */
private double value = Double.NaN;
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
value = Double.isNaN(value) ? d : Math.max(value, d);
}
@ -83,16 +84,19 @@ public class Max extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return value;
}
/* (non-Javadoc)
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double max = Double.NaN;
if (test(values, begin, length)) {
max = values[begin];

View File

@ -55,10 +55,13 @@ package org.apache.commons.math.stat.univariate.rank;
/**
* @version $Revision: 1.3 $ $Date: 2003/07/09 20:04:12 $
* @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
*/
public class Median extends Percentile {
/**
*
*/
public Median() {
super(50.0);
}

View File

@ -62,16 +62,17 @@ import org
.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:12 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public class Min extends AbstractStorelessUnivariateStatistic {
/** */
private double value = Double.NaN;
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
value = Double.isNaN(value) ? d : Math.min(value, d);
}
@ -83,7 +84,7 @@ public class Min extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return value;
@ -92,7 +93,10 @@ public class Min extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double min = Double.NaN;
if (test(values, begin, length)) {
min = values[begin];

View File

@ -57,10 +57,11 @@ import java.util.Arrays;
import org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic;
/**
* @version $Revision: 1.4 $ $Date: 2003/07/09 20:04:12 $
* @version $Revision: 1.5 $ $Date: 2003/08/09 04:03:41 $
*/
public class Percentile extends AbstractUnivariateStatistic {
/** */
private double percentile = 0.0;
/**
@ -74,10 +75,10 @@ public class Percentile extends AbstractUnivariateStatistic {
/**
* Constructs a Percentile with the specific percentile value.
* @param percentile
* @param p the percentile
*/
public Percentile(double percentile) {
this.percentile = percentile;
public Percentile(final double p) {
this.percentile = p;
}
/**
@ -89,14 +90,18 @@ public class Percentile extends AbstractUnivariateStatistic {
* @return the result of the evaluation or Double.NaN
* if the array is empty
*/
public double evaluate(double[] values, double p) {
return evaluate(values, 0,values.length, p);
public double evaluate(final double[] values, final double p) {
return evaluate(values, 0, values.length, p);
}
/**
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int start, int length) {
public double evaluate(
final double[] values,
final int start,
final int length) {
return evaluate(values, start, length, percentile);
}
@ -111,9 +116,13 @@ public class Percentile extends AbstractUnivariateStatistic {
* @return the result of the evaluation or Double.NaN
* if the array is empty
*/
public double evaluate(double[] values, int begin, int length, double p) {
public double evaluate(
final double[] values,
final int begin,
final int length,
final double p) {
test(values,begin,length);
test(values, begin, length);
if ((p > 100) || (p <= 0)) {
throw new IllegalArgumentException("invalid percentile value");
@ -130,7 +139,7 @@ public class Percentile extends AbstractUnivariateStatistic {
int intPos = (int) fpos;
double dif = pos - fpos;
double[] sorted = new double[length];
System.arraycopy(values, begin,sorted, 0, length);
System.arraycopy(values, begin, sorted, 0, length);
Arrays.sort(sorted);
if (pos < 1) {
@ -158,7 +167,7 @@ public class Percentile extends AbstractUnivariateStatistic {
* This will setthat value.
* @param p a value between 0 <= p <= 100
*/
public void setPercentile(double p) {
public void setPercentile(final double p) {
percentile = p;
}

View File

@ -62,7 +62,7 @@ import org
.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:13 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public class Product extends AbstractStorelessUnivariateStatistic {
@ -74,7 +74,7 @@ public class Product extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (Double.isNaN(value)) {
value = d;
} else {
@ -83,7 +83,7 @@ public class Product extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return value;
@ -104,7 +104,10 @@ public class Product extends AbstractStorelessUnivariateStatistic {
* @return the product values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double product = Double.NaN;
if (test(values, begin, length)) {
product = 1.0;

View File

@ -56,7 +56,7 @@ package org.apache.commons.math.stat.univariate.summary;
import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.8 $ $Date: 2003/07/15 03:38:50 $
* @version $Revision: 1.9 $ $Date: 2003/08/09 04:03:41 $
*/
public class Sum extends AbstractStorelessUnivariateStatistic {
@ -66,10 +66,9 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
private double value = Double.NaN;
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#increment(double)
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (Double.isNaN(value)) {
value = d;
} else {
@ -78,16 +77,14 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return value;
}
/**
* @see org.apache.commons.math.stat.univariate.
* StorelessUnivariateStatistic#clear()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
*/
public void clear() {
value = Double.NaN;
@ -99,10 +96,12 @@ public class Sum extends AbstractStorelessUnivariateStatistic {
* @param begin processing at this point in the array
* @param length processing at this point in the array
* @return the sum of the values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.
* UnivariateStatistic#evaluate(double[], int, int)
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double sum = Double.NaN;
if (test(values, begin, length)) {
sum = 0.0;

View File

@ -62,7 +62,7 @@ import org
.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:13 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
@ -71,11 +71,13 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
*/
private double value = Double.NaN;
/** */
private boolean init = true;
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
public void increment(final double d) {
if (init) {
value = Math.log(d);
init = false;
@ -85,7 +87,7 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return value;
@ -107,7 +109,10 @@ public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
* @return the sumLog value or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double sumLog = Double.NaN;
if (test(values, begin, length)) {
sumLog = 0.0;

View File

@ -62,7 +62,7 @@ import org
.AbstractStorelessUnivariateStatistic;
/**
* @version $Revision: 1.6 $ $Date: 2003/07/09 20:04:13 $
* @version $Revision: 1.7 $ $Date: 2003/08/09 04:03:41 $
*/
public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
@ -74,8 +74,8 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
*/
public void increment(double d) {
if (Double.isNaN(value )) {
public void increment(final double d) {
if (Double.isNaN(value)) {
value = d * d;
} else {
value += d * d;
@ -83,7 +83,7 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
}
/**
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
* @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
*/
public double getResult() {
return value;
@ -104,7 +104,10 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
* @return the sum of the squared values or Double.NaN if the array is empty
* @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int)
*/
public double evaluate(double[] values, int begin, int length) {
public double evaluate(
final double[] values,
final int begin,
final int length) {
double sumSq = Double.NaN;
if (test(values, begin, length)) {
sumSq = 0.0;
@ -115,5 +118,4 @@ public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
return sumSq;
}
}

View File

@ -58,7 +58,7 @@ import org.apache.commons.beanutils.PropertyUtils;
/**
* Uses PropertyUtils to map a Bean getter to a double value.
* @version $Revision: 1.3 $ $Date: 2003/07/09 20:04:12 $
* @version $Revision: 1.4 $ $Date: 2003/08/09 04:03:41 $
*/
public class BeanTransformer implements NumberTransformer {
@ -76,16 +76,16 @@ public class BeanTransformer implements NumberTransformer {
/**
* Create a BeanTransformer with a specific PropertyName.
* @param propertyName The property.
* @param property The property.
*/
public BeanTransformer(String propertyName) {
this.propertyName = propertyName;
public BeanTransformer(final String property) {
this.propertyName = property;
}
/**
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
*/
public double transform(Object o) {
public double transform(final Object o) {
double d = Double.NaN;
try {
d =
@ -113,7 +113,7 @@ public class BeanTransformer implements NumberTransformer {
* Set the propertyString
* @param string The string to set the property to.
*/
public void setPropertyName(String string) {
public void setPropertyName(final String string) {
propertyName = string;
}

View File

@ -57,55 +57,55 @@ package org.apache.commons.math.util;
/**
* Some useful additions to the built-in functions in lang.Math<p>
*
* @version $Revision: 1.2 $ $Date: 2003/07/07 23:19:22 $
* @version $Revision: 1.3 $ $Date: 2003/08/09 04:03:41 $
*/
public class MathUtils {
public final class MathUtils {
/**
* Private Constructor
*/
private MathUtils() {
}
/**
* For a double precision value x, this method returns +1.0 if x >= 0
* and -1.0 if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a double
* @return +1.0 or -1.0, depending on the the sign of x
*/
public static double sign( double x ) {
if ( x >= 0.0 ) {
return 1.0 ;
public static double sign(final double x) {
if (x >= 0.0) {
return 1.0;
} else {
return -1.0 ;
return -1.0;
}
}
/**
* For a float value x, this method returns +1.0F if x >= 0
* and -1.0F if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a float
* @return +1.0F or -1.0F, depending on the the sign of x
*/
public static float sign( float x ) {
if ( x >= 0.0F ) {
return 1.0F ;
public static float sign(final float x) {
if (x >= 0.0F) {
return 1.0F;
} else {
return -1.0F ;
return -1.0F;
}
}
/**
* For a byte value x, this method returns (byte)(+1) if x >= 0
* and (byte)(-1) if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a byte
* @return (byte)(+1) or (byte)(-1), depending on the the sign of x
*/
public static byte sign( byte x ) {
if ( x >= (byte)0 ) {
return (byte)1 ;
public static byte sign(final byte x) {
if (x >= (byte) 0) {
return (byte) 1;
} else {
return (byte)(-1) ;
return (byte) (-1);
}
}
@ -113,15 +113,14 @@ public class MathUtils {
* For a short value x, this method returns (short)(+1) if x >= 0
* and (short)(-1) if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a short
* @return (short)(+1) or (short)(-1), depending on the the sign of x
*/
public static short sign( short x ) {
if ( x >= (short)0 ) {
return (short)1 ;
public static short sign(final short x) {
if (x >= (short) 0) {
return (short) 1;
} else {
return (short)(-1) ;
return (short) (-1);
}
}
@ -129,15 +128,14 @@ public class MathUtils {
* For an int value x, this method returns +1 if x >= 0
* and -1 if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, an int
* @return +1 or -1, depending on the the sign of x
*/
public static int sign( int x ) {
if ( x >= 0 ) {
return 1 ;
public static int sign(final int x) {
if (x >= 0) {
return 1;
} else {
return -1 ;
return -1;
}
}
@ -145,15 +143,14 @@ public class MathUtils {
* For a long value x, this method returns +1L if x >= 0
* and -1L if x < 0.
*
* @author Albert Davidson Chou
* @param x the value, a long
* @return +1L or -1L, depending on the the sign of x
*/
public static long sign( long x ) {
if ( x >= 0L ) {
return 1L ;
public static long sign(final long x) {
if (x >= 0L) {
return 1L;
} else {
return -1L ;
return -1L;
}
}
/**
@ -182,14 +179,14 @@ public class MathUtils {
* @param k the size of the subsets to be counted
* @return <code>n choose k</code>
*/
public static long binomialCoefficient(int n, int k) {
public static long binomialCoefficient(final int n, final int k) {
if (n < k) {
throw new IllegalArgumentException
("must have n >= k for binomial coefficient (n,k)");
throw new IllegalArgumentException(
"must have n >= k for binomial coefficient (n,k)");
}
if (n <= 0) {
throw new IllegalArgumentException
("must have n > 0 for binomial coefficient (n,k)");
throw new IllegalArgumentException(
"must have n > 0 for binomial coefficient (n,k)");
}
if ((n == k) || (k == 0)) {
return 1;
@ -200,8 +197,8 @@ public class MathUtils {
long result = Math.round(binomialCoefficientDouble(n, k));
if (result == Long.MAX_VALUE) {
throw new ArithmeticException
("result too large to represent in a long integer");
throw new ArithmeticException(
"result too large to represent in a long integer");
}
return result;
}
@ -226,8 +223,8 @@ public class MathUtils {
* @param k the size of the subsets to be counted
* @return <code>n choose k</code>
*/
public static double binomialCoefficientDouble(int n, int k) {
return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + .5);
public static double binomialCoefficientDouble(final int n, final int k) {
return Math.floor(Math.exp(binomialCoefficientLog(n, k)) + 0.5);
}
/**
@ -246,14 +243,14 @@ public class MathUtils {
* @param k the size of the subsets to be counted
* @return <code>n choose k</code>
*/
public static double binomialCoefficientLog(int n, int k) {
public static double binomialCoefficientLog(final int n, final int k) {
if (n < k) {
throw new IllegalArgumentException
("must have n >= k for binomial coefficient (n,k)");
throw new IllegalArgumentException(
"must have n >= k for binomial coefficient (n,k)");
}
if (n <= 0) {
throw new IllegalArgumentException
("must have n > 0 for binomial coefficient (n,k)");
throw new IllegalArgumentException(
"must have n > 0 for binomial coefficient (n,k)");
}
if ((n == k) || (k == 0)) {
return 0;
@ -295,11 +292,11 @@ public class MathUtils {
* @param n argument
* @return <code>n!</code>
*/
public static long factorial(int n) {
public static long factorial(final int n) {
long result = Math.round(factorialDouble(n));
if (result == Long.MAX_VALUE) {
throw new ArithmeticException
("result too large to represent in a long integer");
throw new ArithmeticException(
"result too large to represent in a long integer");
}
return result;
}
@ -323,10 +320,9 @@ public class MathUtils {
* @param n argument
* @return <code>n!</code>
*/
public static double factorialDouble(int n) {
public static double factorialDouble(final int n) {
if (n <= 0) {
throw new IllegalArgumentException
("must have n > 0 for n!");
throw new IllegalArgumentException("must have n > 0 for n!");
}
return Math.floor(Math.exp(factorialLog(n)) + 0.5);
}
@ -346,10 +342,9 @@ public class MathUtils {
* @param n argument
* @return <code>n!</code>
*/
public static double factorialLog(int n) {
public static double factorialLog(final int n) {
if (n <= 0) {
throw new IllegalArgumentException
("must have n > 0 for n!");
throw new IllegalArgumentException("must have n > 0 for n!");
}
double logSum = 0;
for (int i = 2; i <= n; i++) {