Skip to content

Commit a1d6a9c

Browse files
committed
Use Commons Numbers Erf implementation
1 parent 69a55bf commit a1d6a9c

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

gdsc-smlm/src/main/java/uk/ac/sussex/gdsc/smlm/function/Erf.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static double erf0(double x1, double x2) {
108108
* @return the error function erf(x)
109109
*/
110110
public static double erf(double x) {
111-
// return org.apache.commons.math3.special.Erf.erf(x);
111+
// return org.apache.commons.numbers.gamma.Erf.value(x);
112112

113113
final boolean negative = (x < 0);
114114
if (negative) {

gdsc-smlm/src/main/java/uk/ac/sussex/gdsc/smlm/function/PoissonGammaGaussianFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public double logLikelihood(final double obs, final double exp) {
484484
* @return the cumulative density
485485
*/
486486
double gaussianCdf(final double x) {
487-
// return 0.5 * (1 + org.apache.commons.math3.special.Erf.erf(x / sqrt2sigma2));
487+
// return 0.5 * org.apache.commons.numbers.gamma.Erfc.value(-x / sqrt2sigma2);
488488
// This may not be precise enough.
489489
// Absolute error is <3e-7. Not sure what relative error is.
490490
// The standard Erf is much slower.
@@ -499,7 +499,7 @@ public double logLikelihood(final double obs, final double exp) {
499499
* @return the cumulative density
500500
*/
501501
double gaussianCdf(final double x, final double x2) {
502-
// return 0.5 * (org.apache.commons.math3.special.Erf.erf(x / sqrt2sigma2, x2 / sqrt2sigma2));
502+
// return 0.5 * org.apache.commons.numbers.gamma.ErfDifference.value(x / sqrt2sigma2, x2 / sqrt2sigma2);
503503
// This may not be precise enough.
504504
// Absolute error is <3e-7. Not sure what relative error is.
505505
// The standard Erf is much slower.
@@ -513,7 +513,7 @@ public double logLikelihood(final double obs, final double exp) {
513513
* @return the cumulative density
514514
*/
515515
double gaussianErf(final double x) {
516-
// return org.apache.commons.math3.special.Erf.erf(x / sqrt2sigma2);
516+
// return org.apache.commons.numbers.gamma.Erf.value(x / sqrt2sigma2);
517517
// This may not be precise enough.
518518
// Absolute error is <3e-7. Not sure what relative error is.
519519
// The standard Erf is much slower.

gdsc-smlm/src/main/java/uk/ac/sussex/gdsc/smlm/function/PoissonGaussianConvolutionFunction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ private double getX(final double D, int q) {
215215
* @return the cumulative density
216216
*/
217217
double gaussianCdf(final double x) {
218-
// return org.apache.commons.math3.special.Erf.erf(x / sqrt_var_by_2)
218+
// return org.apache.commons.numbers.gamma.Erf.value(x / sqrt_var_by_2)
219219
// This may not be precise enough.
220220
// Absolute error is <3e-7. Not sure what relative error is.
221221
// The standard CDF is much slower.

gdsc-smlm/src/main/java/uk/ac/sussex/gdsc/smlm/function/gaussian/erf/ErfGaussian2DFunction.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ public enum ErfFunction {
102102
/** Use a fast approximation for the Error function. */
103103
FAST,
104104

105-
/** Use the Apache commons math library Error function. */
105+
/** Use the Apache Commons library Error function.
106+
* <p>As of version 1.2 this uses the implementation in Commons Numbers
107+
* (replacing the Commons Math implementation). */
106108
COMMONS_MATH;
107109
}
108110

@@ -130,14 +132,14 @@ public double erf(double x) {
130132
}
131133

132134
/**
133-
* Compute the error function using the Commons Math implementation.
135+
* Compute the error function using the Commons Numbers implementation.
134136
*/
135-
private static class CommontsMathErrorFunction implements ErrorFunction {
136-
static final CommontsMathErrorFunction INSTANCE = new CommontsMathErrorFunction();
137+
private static class CommontsNumbersErrorFunction implements ErrorFunction {
138+
static final CommontsNumbersErrorFunction INSTANCE = new CommontsNumbersErrorFunction();
137139

138140
@Override
139141
public double erf(double x) {
140-
return org.apache.commons.math3.special.Erf.erf(x);
142+
return org.apache.commons.numbers.gamma.Erf.value(x);
141143
}
142144
}
143145

@@ -265,7 +267,7 @@ public ErfFunction getErfFunction() {
265267
public void setErfFunction(ErfFunction erfFunction) {
266268
switch (erfFunction) {
267269
case COMMONS_MATH:
268-
errorFunction = CommontsMathErrorFunction.INSTANCE;
270+
errorFunction = CommontsNumbersErrorFunction.INSTANCE;
269271
break;
270272
case FAST:
271273
errorFunction = FastErrorFunction.INSTANCE;

gdsc-smlm/src/main/java/uk/ac/sussex/gdsc/smlm/model/GaussianPsfModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
package uk.ac.sussex.gdsc.smlm.model;
2626

2727
import org.apache.commons.lang3.tuple.Pair;
28-
import org.apache.commons.math3.special.Erf;
28+
import org.apache.commons.numbers.gamma.Erf;
2929
import org.apache.commons.rng.UniformRandomProvider;
3030
import org.apache.commons.rng.sampling.distribution.NormalizedGaussianSampler;
3131
import uk.ac.sussex.gdsc.core.utils.MathUtils;
@@ -289,11 +289,11 @@ public double[] gaussian2D(int x0range, int x1range, double sum, double x0, doub
289289
// Note: The 0.5 factors are moved to reduce computations
290290
for (int x = 0; x <= x0range; x++) {
291291
// erf0[x] = 0.5 * Erf.erf((x - x0) * denom0);
292-
erf0[x] = Erf.erf((x - x0) * denom0);
292+
erf0[x] = Erf.value((x - x0) * denom0);
293293
}
294294
for (int y = 0; y <= x1range; y++) {
295295
// erf1[y] = 0.5 * Erf.erf((y - x1) * denom1);
296-
erf1[y] = Erf.erf((y - x1) * denom1);
296+
erf1[y] = Erf.value((y - x1) * denom1);
297297
}
298298
sum *= 0.5; // Incorporate the 0.5 factor for Y into the sum
299299

gdsc-smlm/src/main/java/uk/ac/sussex/gdsc/smlm/utils/GaussianKernel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ public static double[] makeErfGaussianKernel(double sigma, double range) {
413413
final double sqrtTwoVar = Math.sqrt(sigma * sigma * 2);
414414

415415
// Use erfc to reach into the long tail at sigma <= 38
416-
double lower = org.apache.commons.math3.special.Erf.erfc(-0.5 / sqrtTwoVar);
416+
double lower = org.apache.commons.numbers.gamma.Erfc.value(-0.5 / sqrtTwoVar);
417417
for (int i = 0; i < kradius; i++) {
418418
final double upper = lower;
419-
lower = org.apache.commons.math3.special.Erf.erfc((i + 0.5) / sqrtTwoVar);
419+
lower = org.apache.commons.numbers.gamma.Erfc.value((i + 0.5) / sqrtTwoVar);
420420
kernel[i] = (upper - lower) * 0.5;
421421
if (kernel[i] == 0) {
422422
break;

0 commit comments

Comments
 (0)