barty/D4J-Repair huggingface dataset:
File: src/main/java/org/apache/commons/math/distribution/PoissonDistributionImpl.java
Issue: Dangerous code in "PoissonDistributionImpl"
In the following excerpt from class "PoissonDistributionImpl":

{code:title=PoissonDistributionImpl.java|borderStyle=solid}
    public PoissonDistributionImpl(double p, NormalDistribution z) {
        super();
        setNormal(z);
        setMean(p);
    }
{code}

(1) Overridable methods are called within the constructor.
(2) The reference "z" is stored and modified within the class.

I've encountered problem (1) in several classes while working on issue 348. In those cases, in order to remove potential problems, I copied/pasted the body of the "setter" methods inside the constructor but I think that a more elegant solution would be to remove the "setters" altogether (i.e. make the classes immutable).
Problem (2) can also create unexpected behaviour. Is it really necessary to pass the "NormalDistribution" object; can't it be always created within the class?


Buggy code:
public PoissonDistributionImpl(double p, double epsilon, int maxIterations) {
        if (p <= 0) {
            throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_POSITIVE_POISSON_MEAN, p);
        }
        mean = p;
        normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
        this.epsilon = epsilon;
        this.maxIterations = maxIterations;
    }

Fixed code:
public PoissonDistributionImpl(double p, double epsilon, int maxIterations) {
        if (p <= 0) {
            throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p);
        }
        mean = p;
        normal = new NormalDistributionImpl(p, FastMath.sqrt(p));
        this.epsilon = epsilon;
        this.maxIterations = maxIterations;
    }
File: src/main/java/org/apache/commons/math/util/MathUtils.java
Issue: NaN in "equals" methods
In "MathUtils", some "equals" methods will return true if both argument are NaN.
Unless I'm mistaken, this contradicts the IEEE standard.
If nobody objects, I'm going to make the changes.

Buggy code:
public static boolean equals(double x, double y) {
    return (Double.isNaN(x) && Double.isNaN(y)) || x == y;
}

Fixed code:
public static boolean equals(double x, double y) {
    return equals(x, y, 1);
}
File: src/main/java/org/apache/commons/math/stat/correlation/PearsonsCorrelation.java
Issue: PearsonsCorrelation.getCorrelationPValues() precision limited by machine epsilon
Similar to the issue described in MATH-201, using PearsonsCorrelation.getCorrelationPValues() with many treatments results in p-values that are continuous down to 2.2e-16 but that drop to 0 after that.
In MATH-201, the problem was described as such:
> So in essence, the p-value returned by TTestImpl.tTest() is:
> 
> 1.0 - (cumulativeProbability(t) - cumulativeProbabily(-t))
> 
> For large-ish t-statistics, cumulativeProbabilty(-t) can get quite small, and cumulativeProbabilty(t) can get very close to 1.0. When 
> cumulativeProbability(-t) is less than the machine epsilon, we get p-values equal to zero because:
> 
> 1.0 - 1.0 + 0.0 = 0.0
The solution in MATH-201 was to modify the p-value calculation to this:
> p = 2.0 * cumulativeProbability(-t)
Here, the problem is similar.  From PearsonsCorrelation.getCorrelationPValues():
  p = 2 * (1 - tDistribution.cumulativeProbability(t));
Directly calculating the p-value using identical code as PearsonsCorrelation.getCorrelationPValues(), but with the following change seems to solve the problem:
  p = 2 * (tDistribution.cumulativeProbability(-t));

Buggy code:
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * (1 - tDistribution.cumulativeProbability(t));
            }
        }
    }
    return new BlockRealMatrix(out);
}

Fixed code:
public RealMatrix getCorrelationPValues() throws MathException {
    TDistribution tDistribution = new TDistributionImpl(nObs - 2);
    int nVars = correlationMatrix.getColumnDimension();
    double[][] out = new double[nVars][nVars];
    for (int i = 0; i < nVars; i++) {
        for (int j = 0; j < nVars; j++) {
            if (i == j) {
                out[i][j] = 0d;
            } else {
                double r = correlationMatrix.getEntry(i, j);
                double t = Math.abs(r * Math.sqrt((nObs - 2)/(1 - r * r)));
                out[i][j] = 2 * tDistribution.cumulativeProbability(-t);
            }
        }
    }
    return new BlockRealMatrix(out);
}
File: src/main/java/org/apache/commons/math/analysis/solvers/BisectionSolver.java
Issue: BisectionSolver.solve(final UnivariateRealFunction f, double min, double max, double initial) throws NullPointerException
Method 
    BisectionSolver.solve(final UnivariateRealFunction f, double min, double max, double initial)  
invokes 
    BisectionSolver.solve(double min, double max) 
which throws NullPointerException, as member variable
    UnivariateRealSolverImpl.f 
is null.
Instead the method:
    BisectionSolver.solve(final UnivariateRealFunction f, double min, double max)
should be called.
Steps to reproduce:
invoke:
     new BisectionSolver().solve(someUnivariateFunctionImpl, 0.0, 1.0, 0.5);
NullPointerException will be thrown.

Buggy code:
public double solve(final UnivariateRealFunction f, double min, double max, double initial)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(min, max);
}

Fixed code:
public double solve(final UnivariateRealFunction f, double min, double max, double initial)
    throws MaxIterationsExceededException, FunctionEvaluationException {
    return solve(f, min, max);
}
File: src/main/java/org/apache/commons/math/stat/Frequency.java
Issue: In stat.Frequency, getPct(Object) uses getCumPct(Comparable) instead of getPct(Comparable)
Drop in Replacement of 1.2 with 2.0 not possible because all getPct calls will be cummulative without code change
Frequency.java
   /**

Returns the percentage of values that are equal to v
@deprecated replaced by 
{@link #getPct(Comparable)}
 as of 2.0
     */
    @Deprecated
    public double getPct(Object v) 
{
        return getCumPct((Comparable<?>) v);
    }

Buggy code:
public double getPct(Object v) {
    return getCumPct((Comparable<?>) v);
}

Fixed code:
public double getPct(Object v) {
    return getPct((Comparable<?>) v);
}
File: src/main/java/org/apache/commons/math/util/MathUtils.java
Issue: NPE in  KMeansPlusPlusClusterer unittest
When running this unittest, I am facing this NPE:
java.lang.NullPointerException
	at org.apache.commons.math.stat.clustering.KMeansPlusPlusClusterer.assignPointsToClusters(KMeansPlusPlusClusterer.java:91)
This is the unittest:
package org.fao.fisheries.chronicles.calcuation.cluster;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import org.apache.commons.math.stat.clustering.Cluster;
import org.apache.commons.math.stat.clustering.EuclideanIntegerPoint;
import org.apache.commons.math.stat.clustering.KMeansPlusPlusClusterer;
import org.fao.fisheries.chronicles.input.CsvImportProcess;
import org.fao.fisheries.chronicles.input.Top200Csv;
import org.junit.Test;
public class ClusterAnalysisTest {
	@Test
	public void testPerformClusterAnalysis2() {
		KMeansPlusPlusClusterer<EuclideanIntegerPoint> transformer = new KMeansPlusPlusClusterer<EuclideanIntegerPoint>(
				new Random(1746432956321l));
		EuclideanIntegerPoint[] points = new EuclideanIntegerPoint[] {
				new EuclideanIntegerPoint(new int[] 
{ 1959, 325100 }
),
				new EuclideanIntegerPoint(new int[] 
{ 1960, 373200 }
), };
		List<Cluster<EuclideanIntegerPoint>> clusters = transformer.cluster(Arrays.asList(points), 1, 1);
		assertEquals(1, clusters.size());
	}
}

Buggy code:
public static double distance(int[] p1, int[] p2) {
  int sum = 0;
  for (int i = 0; i < p1.length; i++) {
      final int dp = p1[i] - p2[i];
      sum += dp * dp;
  }
  return Math.sqrt(sum);
}

Fixed code:
public static double distance(int[] p1, int[] p2) {
  double sum = 0;
  for (int i = 0; i < p1.length; i++) {
      final double dp = p1[i] - p2[i];
      sum += dp * dp;
  }
  return Math.sqrt(sum);
}
File: src/main/java/org/apache/commons/math3/distribution/DiscreteDistribution.java
Issue: DiscreteDistribution.sample(int) may throw an exception if first element of singletons of sub-class type
Creating an array with Array.newInstance(singletons.get(0).getClass(), sampleSize) in DiscreteDistribution.sample(int) is risky. An exception will be thrown if:

singleons.get(0) is of type T1, an sub-class of T, and
DiscreteDistribution.sample() returns an object which is of type T, but not of type T1.

To reproduce:

List<Pair<Object,Double>> list = new ArrayList<Pair<Object, Double>>();
list.add(new Pair<Object, Double>(new Object() {}, new Double(0)));
list.add(new Pair<Object, Double>(new Object() {}, new Double(1)));
new DiscreteDistribution<Object>(list).sample(1);

Attaching a patch.

Buggy code:
public T[] sample(int sampleSize) throws NotStrictlyPositiveException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                sampleSize);
    }

    final T[]out = (T[]) java.lang.reflect.Array.newInstance(singletons.get(0).getClass(), sampleSize);

    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }

    return out;

}

Fixed code:
public Object[] sample(int sampleSize) throws NotStrictlyPositiveException {
    if (sampleSize <= 0) {
        throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_SAMPLES,
                sampleSize);
    }

    final Object[] out = new Object[sampleSize];

    for (int i = 0; i < sampleSize; i++) {
        out[i] = sample();
    }

    return out;

}
File: src/main/java/org/apache/commons/math/optimization/linear/SimplexSolver.java
Issue: SimplexSolver not working as expected 2
SimplexSolver didn't find the optimal solution.
Program for Lpsolve:
=====================
/* Objective function */
max: 7 a 3 b;
/* Constraints */
R1: +3 a -5 c <= 0;
R2: +2 a -5 d <= 0;
R3: +2 b -5 c <= 0;
R4: +3 b -5 d <= 0;
R5: +3 a +2 b <= 5;
R6: +2 a +3 b <= 5;
/* Variable bounds */
a <= 1;
b <= 1;
=====================
Results(correct): a = 1, b = 1, value = 10
Program for SimplexSolve:
=====================
LinearObjectiveFunction kritFcia = new LinearObjectiveFunction(new double[]
{7, 3, 0, 0}
, 0);
Collection<LinearConstraint> podmienky = new ArrayList<LinearConstraint>();
podmienky.add(new LinearConstraint(new double[]
{1, 0, 0, 0}
, Relationship.LEQ, 1));
podmienky.add(new LinearConstraint(new double[]
{0, 1, 0, 0}
, Relationship.LEQ, 1));
podmienky.add(new LinearConstraint(new double[]
{3, 0, -5, 0}
, Relationship.LEQ, 0));
podmienky.add(new LinearConstraint(new double[]
{2, 0, 0, -5}
, Relationship.LEQ, 0));
podmienky.add(new LinearConstraint(new double[]
{0, 2, -5, 0}
, Relationship.LEQ, 0));
podmienky.add(new LinearConstraint(new double[]
{0, 3, 0, -5}
, Relationship.LEQ, 0));
podmienky.add(new LinearConstraint(new double[]
{3, 2, 0, 0}
, Relationship.LEQ, 5));
podmienky.add(new LinearConstraint(new double[]
{2, 3, 0, 0}
, Relationship.LEQ, 5));
SimplexSolver solver = new SimplexSolver();
RealPointValuePair result = solver.optimize(kritFcia, podmienky, GoalType.MAXIMIZE, true);
=====================
Results(incorrect): a = 1, b = 0.5, value = 8.5
P.S. I used the latest software from the repository (including MATH-286 fix).

Buggy code:
private Integer getPivotRow(final int col, final SimplexTableau tableau) {
    double minRatio = Double.MAX_VALUE;
    Integer minRatioPos = null;
    for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
        final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
        final double entry = tableau.getEntry(i, col);
        if (MathUtils.compareTo(entry, 0, epsilon) >= 0) {
            final double ratio = rhs / entry;
            if (ratio < minRatio) {
                minRatio = ratio;
                minRatioPos = i; 
            }
        }
    }
    return minRatioPos;
}

Fixed code:
private Integer getPivotRow(final int col, final SimplexTableau tableau) {
    double minRatio = Double.MAX_VALUE;
    Integer minRatioPos = null;
    for (int i = tableau.getNumObjectiveFunctions(); i < tableau.getHeight(); i++) {
        final double rhs = tableau.getEntry(i, tableau.getWidth() - 1);
        final double entry = tableau.getEntry(i, col);
        if (MathUtils.compareTo(entry, 0, epsilon) > 0) {
            final double ratio = rhs / entry;
            if (ratio < minRatio) {
                minRatio = ratio;
                minRatioPos = i; 
            }
        }
    }
    return minRatioPos;
}
File: src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
Issue: Basic variable is not found correctly in simplex tableau
The last patch to SimplexTableau caused an automated test suite I'm running at work to go down a new code path and uncover what is hopefully the last bug remaining in the Simplex code.
SimplexTableau was assuming an entry in the tableau had to be nonzero to indicate a basic variable, which is incorrect - the entry should have a value equal to 1.

Buggy code:
private Integer getBasicRow(final int col) {
    Integer row = null;
    for (int i = getNumObjectiveFunctions(); i < getHeight(); i++) {
        if (!MathUtils.equals(getEntry(i, col), 0.0, epsilon)) {
            if (row == null) {
            row = i;
            } else {
            return null;
            }
        }
    }
    return row;
}

Fixed code:
private Integer getBasicRow(final int col) {
    Integer row = null;
    for (int i = getNumObjectiveFunctions(); i < getHeight(); i++) {
        if (MathUtils.equals(getEntry(i, col), 1.0, epsilon) && (row == null)) {
            row = i;
        } else if (!MathUtils.equals(getEntry(i, col), 0.0, epsilon)) {
            return null;
        }
    }
    return row;
}
File: src/java/org/apache/commons/math/optimization/linear/SimplexTableau.java
Issue: Simplex Solver arrives at incorrect solution
I have reduced the problem reported to me down to a minimal test case which I will attach.

Buggy code:
protected RealPointValuePair getSolution() {
    double[] coefficients = new double[getOriginalNumDecisionVariables()];
    Integer basicRow =
        getBasicRow(getNumObjectiveFunctions() + getOriginalNumDecisionVariables());
    double mostNegative = basicRow == null ? 0 : getEntry(basicRow, getRhsOffset());
    for (int i = 0; i < coefficients.length; i++) {
        basicRow = getBasicRow(getNumObjectiveFunctions() + i);
            // if multiple variables can take a given value 
            // then we choose the first and set the rest equal to 0
            coefficients[i] =
                (basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) -
                (restrictToNonNegative ? 0 : mostNegative);
        if (basicRow != null) {
            for (int j = getNumObjectiveFunctions(); j < getNumObjectiveFunctions() + i; j++) {
                if (tableau.getEntry(basicRow, j) == 1) {
                     coefficients[i] = 0;
                }
            }
        }
    }
    return new RealPointValuePair(coefficients, f.getValue(coefficients));
}

Fixed code:
protected RealPointValuePair getSolution() {
    double[] coefficients = new double[getOriginalNumDecisionVariables()];
    Integer basicRow =
        getBasicRow(getNumObjectiveFunctions() + getOriginalNumDecisionVariables());
    double mostNegative = basicRow == null ? 0 : getEntry(basicRow, getRhsOffset());
    Set<Integer> basicRows = new HashSet<Integer>();
    for (int i = 0; i < coefficients.length; i++) {
        basicRow = getBasicRow(getNumObjectiveFunctions() + i);
        if (basicRows.contains(basicRow)) {
            // if multiple variables can take a given value 
            // then we choose the first and set the rest equal to 0
            coefficients[i] = 0;
        } else {
            basicRows.add(basicRow);
            coefficients[i] =
                (basicRow == null ? 0 : getEntry(basicRow, getRhsOffset())) -
                (restrictToNonNegative ? 0 : mostNegative);
        }
    }
    return new RealPointValuePair(coefficients, f.getValue(coefficients));
}
File: src/java/org/apache/commons/math/stat/Frequency.java
Issue: Bugs in Frequency API
I think the existing Frequency API has some bugs in it.
The addValue(Object v) method allows one to add a plain Object, but one cannot add anything further to the instance, as the second add fails with IllegalArgumentException.
In fact, the problem is with the first call to addValue(Object) which should not allow a plain Object to be added - it should only allow Comparable objects.
This could be fixed by checking that the object is Comparable.
Similar considerations apply to the getCumFreq(Object) and getCumPct(Object) methods - they will only work with objects that implement Comparable.
The getCount(Object) and getPct(Object) methods don't fail when given a non-Comparable object (because the class cast exception is caught), however they just return 0 as if the object was not present:

        final Object OBJ = new Object();
        f.addValue(OBJ); // This ought to fail, but doesn't, causing the unexpected behaviour below
        System.out.println(f.getCount(OBJ)); // 0
        System.out.println(f.getPct(OBJ)); // 0.0

Rather than adding extra checks for Comparable, it seems to me that the API would be much improved by using Comparable instead of Object.
Also, it should make it easier to implement generics.
However, this would cause compilation failures for some programs that pass Object rather than Comparable to the class.
These would need recoding, but I think they would continue to run OK against the new API.
It would also affect the run-time behaviour slightly, as the first attempt to add a non-Comparable object would fail, rather than the second add of a possibly valid object.
But is that a viable program? It can only add one object, and any attempt to get statistics will either return 0 or an Exception, and applying the instanceof fix would also cause it to fail.

Buggy code:
public void addValue(Object v) {
        addValue((Comparable<?>) v);            
}

Fixed code:
public void addValue(Object v) {
    if (v instanceof Comparable<?>){
        addValue((Comparable<?>) v);            
    } else {
        throw new IllegalArgumentException("Object must implement Comparable");
    }
}
File: src/main/java/org/apache/commons/math3/geometry/euclidean/threed/Line.java
Issue: Line.revert() is imprecise
Line.revert() only maintains ~10 digits for the direction. This becomes an issue when the line's position is evaluated far from the origin. A simple fix would be to use Vector3D.negate() for the direction.
Also, is there a reason why Line is not immutable? It is just comprised of two vectors.

Buggy code:
public Line revert() {
    final Line reverted = new Line(zero, zero.subtract(direction));
    return reverted;
}

Fixed code:
public Line revert() {
    final Line reverted = new Line(this);
    reverted.direction = reverted.direction.negate();
    return reverted;
}
File: src/java/org/apache/commons/math/stat/Frequency.java
Issue: Bugs in Frequency API
I think the existing Frequency API has some bugs in it.
The addValue(Object v) method allows one to add a plain Object, but one cannot add anything further to the instance, as the second add fails with IllegalArgumentException.
In fact, the problem is with the first call to addValue(Object) which should not allow a plain Object to be added - it should only allow Comparable objects.
This could be fixed by checking that the object is Comparable.
Similar considerations apply to the getCumFreq(Object) and getCumPct(Object) methods - they will only work with objects that implement Comparable.
The getCount(Object) and getPct(Object) methods don't fail when given a non-Comparable object (because the class cast exception is caught), however they just return 0 as if the object was not present:

        final Object OBJ = new Object();
        f.addValue(OBJ); // This ought to fail, but doesn't, causing the unexpected behaviour below
        System.out.println(f.getCount(OBJ)); // 0
        System.out.println(f.getPct(OBJ)); // 0.0

Rather than adding extra checks for Comparable, it seems to me that the API would be much improved by using Comparable instead of Object.
Also, it should make it easier to implement generics.
However, this would cause compilation failures for some programs that pass Object rather than Comparable to the class.
These would need recoding, but I think they would continue to run OK against the new API.
It would also affect the run-time behaviour slightly, as the first attempt to add a non-Comparable object would fail, rather than the second add of a possibly valid object.
But is that a viable program? It can only add one object, and any attempt to get statistics will either return 0 or an Exception, and applying the instanceof fix would also cause it to fail.

Buggy code:
public void addValue(Object v) {

/**
 * Adds 1 to the frequency count for v.
 * <p>
 * If other objects have already been added to this Frequency, v must
 * be comparable to those that have already been added.
 * </p>
 * 
 * @param v the value to add.
 * @throws IllegalArgumentException if <code>v</code> is not comparable with previous entries
 */
    Object obj = v;
    if (v instanceof Integer) {
       obj = Long.valueOf(((Integer) v).longValue());
    }
    try {
        Long count = (Long) freqTable.get(obj);
        if (count == null) {
            freqTable.put(obj, Long.valueOf(1));
        } else {
            freqTable.put(obj, Long.valueOf(count.longValue() + 1));
        }
    } catch (ClassCastException ex) {   
        //TreeMap will throw ClassCastException if v is not comparable
        throw new IllegalArgumentException("Value not comparable to existing values.");
    }
}

Fixed code:
public void addValue(Object v) {
    addValue((Comparable<?>) v);
}
File: src/java/org/apache/commons/math/fraction/Fraction.java
Issue: Fraction.comparTo returns 0 for some differente fractions
If two different fractions evaluate to the same double due to limited precision,
the compareTo methode returns 0 as if they were identical.

// value is roughly PI - 3.07e-18
Fraction pi1 = new Fraction(1068966896, 340262731);

// value is roughly PI + 1.936e-17
Fraction pi2 = new Fraction( 411557987, 131002976);

System.out.println(pi1.doubleValue() - pi2.doubleValue()); // exactly 0.0 due to limited IEEE754 precision
System.out.println(pi1.compareTo(pi2)); // display 0 instead of a negative value

Buggy code:
public int compareTo(Fraction object) {
    double nOd = doubleValue();
    double dOn = object.doubleValue();
    return (nOd < dOn) ? -1 : ((nOd > dOn) ? +1 : 0);
}

Fixed code:
public int compareTo(Fraction object) {
    long nOd = ((long) numerator) * object.denominator;
    long dOn = ((long) denominator) * object.numerator;
    return (nOd < dOn) ? -1 : ((nOd > dOn) ? +1 : 0);
}