Skip to content

Commit

Permalink
OPENNLP-1285: Changing Math to StrictMath for Java 8 vs 11 test incon…
Browse files Browse the repository at this point in the history
…sistencies. (#377)
  • Loading branch information
jzonthemtn committed Jun 10, 2020
1 parent e4b331d commit 7c10174
Show file tree
Hide file tree
Showing 41 changed files with 114 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static void usage() {

System.out.print(" " + tool.getName());

for (int i = 0; i < Math.abs(tool.getName().length()
for (int i = 0; i < StrictMath.abs(tool.getName().length()
- numberOfSpaces); i++) {
System.out.print(" ");
}
Expand Down
2 changes: 1 addition & 1 deletion opennlp-tools/src/main/java/opennlp/tools/cmdline/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private static void usage() {

System.out.print(" " + tool.getName());

for (int i = 0; i < Math.abs(tool.getName().length() - numberOfSpaces); i++) {
for (int i = 0; i < StrictMath.abs(tool.getName().length() - numberOfSpaces); i++) {
System.out.print(" ");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ private void increment(String column) {
*/
public double getAccuracy() {
// we save the accuracy because it is frequently used by the comparator
if (Math.abs(acc - 1.0d) < 0.0000000001) {
if (StrictMath.abs(acc - 1.0d) < 0.0000000001) {
if (total == 0)
acc = 0.0d;
acc = (double) correct / (double) total;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public Dictionary(InputStream in) throws IOException {
*/
public void put(StringList tokens) {
entrySet.add(new StringListWrapper(tokens));
minTokenCount = Math.min(minTokenCount, tokens.size());
maxTokenCount = Math.max(maxTokenCount, tokens.size());
minTokenCount = StrictMath.min(minTokenCount, tokens.size());
maxTokenCount = StrictMath.max(maxTokenCount, tokens.size());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public double calculateProbability(StringList tokens) {
if (size() > 0) {
for (StringList ngram : NGramUtils.getNGrams(tokens, n)) {
double score = stupidBackoff(ngram);
probability += Math.log(score);
probability += StrictMath.log(score);
if (Double.isNaN(probability)) {
probability = 0d;
break;
}
}
probability = Math.exp(probability);
probability = StrictMath.exp(probability);
}
return probability;
}
Expand All @@ -79,13 +79,13 @@ public double calculateProbability(String... tokens) {
if (size() > 0) {
for (String[] ngram : NGramUtils.getNGrams(tokens, n)) {
double score = stupidBackoff(new StringList(ngram));
probability += Math.log(score);
probability += StrictMath.log(score);
if (Double.isNaN(probability)) {
probability = 0d;
break;
}
}
probability = Math.exp(probability);
probability = StrictMath.exp(probability);
}
return probability;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public DefaultLemmatizerContextGenerator() {
protected static String[] getPrefixes(String lex) {
String[] prefs = new String[PREFIX_LENGTH];
for (int li = 1; li < PREFIX_LENGTH; li++) {
prefs[li] = lex.substring(0, Math.min(li + 1, lex.length()));
prefs[li] = lex.substring(0, StrictMath.min(li + 1, lex.length()));
}
return prefs;
}

protected static String[] getSuffixes(String lex) {
String[] suffs = new String[SUFFIX_LENGTH];
for (int li = 1; li < SUFFIX_LENGTH; li++) {
suffs[li] = lex.substring(Math.max(lex.length() - li - 1, 0));
suffs[li] = lex.substring(StrictMath.max(lex.length() - li - 1, 0));
}
return suffs;
}
Expand Down
8 changes: 4 additions & 4 deletions opennlp-tools/src/main/java/opennlp/tools/ml/ArrayMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ public static double innerProduct(double[] vecA, double[] vecB) {
public static double l1norm(double[] v) {
double norm = 0;
for (int i = 0; i < v.length; i++)
norm += Math.abs(v[i]);
norm += StrictMath.abs(v[i]);
return norm;
}

/**
* L2-norm
*/
public static double l2norm(double[] v) {
return Math.sqrt(innerProduct(v, v));
return StrictMath.sqrt(innerProduct(v, v));
}

/**
Expand All @@ -73,9 +73,9 @@ public static double logSumOfExps(double[] x) {
double sum = 0.0;
for (int i = 0; i < x.length; i++) {
if (x[i] != Double.NEGATIVE_INFINITY)
sum += Math.exp(x[i] - max);
sum += StrictMath.exp(x[i] - max);
}
return max + Math.log(sum);
return max + StrictMath.log(sum);
}

public static double max(double[] x) {
Expand Down
6 changes: 3 additions & 3 deletions opennlp-tools/src/main/java/opennlp/tools/ml/BeamSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Sequence[] bestSequences(int numSequences, T[] sequence,
}

for (int i = 0; i < sequence.length; i++) {
int sz = Math.min(size, prev.size());
int sz = StrictMath.min(size, prev.size());

for (int sc = 0; prev.size() > 0 && sc < sz; sc++) {
Sequence top = prev.remove();
Expand All @@ -115,7 +115,7 @@ public Sequence[] bestSequences(int numSequences, T[] sequence,

Arrays.sort(temp_scores);

double min = temp_scores[Math.max(0,scores.length - size)];
double min = temp_scores[StrictMath.max(0,scores.length - size)];

for (int p = 0; p < scores.length; p++) {
if (scores[p] >= min) {
Expand Down Expand Up @@ -149,7 +149,7 @@ public Sequence[] bestSequences(int numSequences, T[] sequence,
next = tmp;
}

int numSeq = Math.min(numSequences, prev.size());
int numSeq = StrictMath.min(numSequences, prev.size());
Sequence[] topSequences = new Sequence[numSeq];

for (int seqIndex = 0; seqIndex < numSeq; seqIndex++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ static double[] eval(Context[] context, float[] values, double[] prior,

double normal = 0.0;
for (int oid = 0; oid < model.getNumOutcomes(); oid++) {
prior[oid] = Math.exp(prior[oid]);
prior[oid] = StrictMath.exp(prior[oid]);
normal += prior[oid];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,14 @@ private double gaussianUpdate(int predicate, int oid, double correctionConstant)
double modelValue = modelExpects[0][predicate].getParameters()[oid];
double observedValue = observedExpects[predicate].getParameters()[oid];
for (int i = 0; i < 50; i++) {
double tmp = modelValue * Math.exp(correctionConstant * x0);
double tmp = modelValue * StrictMath.exp(correctionConstant * x0);
double f = tmp + (param + x0) / sigma - observedValue;
double fp = tmp * correctionConstant + 1 / sigma;
if (fp == 0) {
break;
}
double x = x0 - f / fp;
if (Math.abs(x - x0) < 0.000001) {
if (StrictMath.abs(x - x0) < 0.000001) {
x0 = x;
break;
}
Expand Down Expand Up @@ -623,8 +623,8 @@ private double nextIteration(double correctionConstant,
if (model[aoi] == 0) {
System.err.println("Model expects == 0 for " + predLabels[pi] + " " + outcomeLabels[aoi]);
}
//params[pi].updateParameter(aoi,(Math.log(observed[aoi]) - Math.log(model[aoi])));
params[pi].updateParameter(aoi, ((Math.log(observed[aoi]) - Math.log(model[aoi]))
//params[pi].updateParameter(aoi,(StrictMath.log(observed[aoi]) - StrictMath.log(model[aoi])));
params[pi].updateParameter(aoi, ((StrictMath.log(observed[aoi]) - StrictMath.log(model[aoi]))
/ correctionConstant));
}

Expand Down Expand Up @@ -695,7 +695,7 @@ public ModelExpectationComputeTask call() {
}
}

loglikelihood += Math.log(modelDistribution[outcomeList[ei]]) * numTimesEventsSeen[ei];
loglikelihood += StrictMath.log(modelDistribution[outcomeList[ei]]) * numTimesEventsSeen[ei];

numEvents += numTimesEventsSeen[ei];
if (printMessages) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public double[] gradientAt(double[] x) {
logSumOfExps = ArrayMath.logSumOfExps(expectation);

for (oi = 0; oi < numOutcomes; oi++) {
expectation[oi] = Math.exp(expectation[oi] - logSumOfExps);
expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps);
}

for (oi = 0; oi < numOutcomes; oi++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ public GradientComputeTask call() {
logSumOfExps = ArrayMath.logSumOfExps(expectation);

for (oi = 0; oi < numOutcomes; oi++) {
expectation[oi] = Math.exp(expectation[oi] - logSumOfExps);
expectation[oi] = StrictMath.exp(expectation[oi] - logSumOfExps);
}

for (oi = 0; oi < numOutcomes; oi++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* {@literal @}Override
* public double valueAt(double[] x) {
* return Math.pow(x[0]-1, 2) + 10;
* return StrictMath.pow(x[0]-1, 2) + 10;
* }
*
* {@literal @}Override
Expand Down Expand Up @@ -279,7 +279,7 @@ else if (iter < 100)
if (l1Cost > 0 && l2Cost > 0) {
double[] x = lsr.getNextPoint();
for (int i = 0; i < dimension; i++) {
x[i] = Math.sqrt(1 + l2Cost) * x[i];
x[i] = StrictMath.sqrt(1 + l2Cost) * x[i];
}
}

Expand Down Expand Up @@ -375,7 +375,7 @@ private boolean isConverged(LineSearchResult lsr) {
}

// Check gradient's norm using the criteria: ||g(x)|| / max(1, ||x||) < threshold
double xNorm = Math.max(1, ArrayMath.l2norm(lsr.getNextPoint()));
double xNorm = StrictMath.max(1, ArrayMath.l2norm(lsr.getNextPoint()));
double gradNorm = l1Cost > 0 ?
ArrayMath.l2norm(lsr.getPseudoGradAtNext()) : ArrayMath.l2norm(lsr.getGradAtNext());
if (gradNorm / xNorm < REL_GRAD_NORM_TOL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private double[] eval(String[] context, float[] values, double[] probs) {

double logSumExp = ArrayMath.logSumOfExps(probs);
for (int oi = 0; oi < outcomeNames.length; oi++) {
probs[oi] = Math.exp(probs[oi] - logSumExp);
probs[oi] = StrictMath.exp(probs[oi] - logSumExp);
}
return probs;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ static double[] eval(int[] context, float[] values, double[] probs,
double logSumExp = ArrayMath.logSumOfExps(probs);

for (int oi = 0; oi < nOutcomes; oi++) {
probs[oi] = Math.exp(probs[oi] - logSumExp);
probs[oi] = StrictMath.exp(probs[oi] - logSumExp);
}

return probs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public int compareTo(ComparableEvent ce) {
return compareOutcome;
}

int smallerLength = Math.min(predIndexes.length, ce.predIndexes.length);
int smallerLength = StrictMath.min(predIndexes.length, ce.predIndexes.length);

for (int i = 0; i < smallerLength; i++) {
int comparePredIndexes = Integer.compare(predIndexes[i], ce.predIndexes[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ComparablePredicate(String n, int[] ocs, double[] ps) {
}

public int compareTo(ComparablePredicate cp) {
int smallerLength = Math.min(outcomes.length, cp.outcomes.length);
int smallerLength = StrictMath.min(outcomes.length, cp.outcomes.length);

for (int i = 0; i < smallerLength; i++) {
int compareOutcomes = Integer.compare(outcomes[i], cp.outcomes[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void logPrior(double[] dist, int[] context) {

public void setLabels(String[] outcomeLabels, String[] contextLabels) {
this.numOutcomes = outcomeLabels.length;
r = Math.log(1.0 / numOutcomes);
r = StrictMath.log(1.0 / numOutcomes);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private Map<T, Double> normalize() {
T t = entry.getKey();
Double p = entry.getValue();
if (p != null) {
double temp_p = Math.exp(p - highestLogProbability);
double temp_p = StrictMath.exp(p - highestLogProbability);
if (!Double.isNaN(temp_p)) {
sum += temp_p;
temp.put(t, temp_p);
Expand All @@ -133,7 +133,7 @@ private Map<T, Double> normalize() {
}

private double log(double prob) {
return Math.log(prob);
return StrictMath.log(prob);
}

/**
Expand Down Expand Up @@ -163,7 +163,7 @@ public Double getLog(T t) {
}

public void discardCountsBelow(double i) {
i = Math.log(i);
i = StrictMath.log(i);
ArrayList<T> labelsToRemove = new ArrayList<>();
for (Entry<T, Double> entry : map.entrySet()) {
final T label = entry.getKey();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public LogProbability(T label) {
* @param probability the probability to assign
*/
public void set(double probability) {
this.probability = Math.log(probability);
this.probability = StrictMath.log(probability);
}

/**
Expand All @@ -55,7 +55,7 @@ public void set(Probability probability) {
* @param probability the probability to assign
*/
public void setIfLarger(double probability) {
double logP = Math.log(probability);
double logP = StrictMath.log(probability);
if (this.probability < logP) {
this.probability = logP;
}
Expand Down Expand Up @@ -98,7 +98,7 @@ public void setLog(double probability) {
* @param probability the probability weight to add
*/
public void addIn(double probability) {
setLog(this.probability + Math.log(probability));
setLog(this.probability + StrictMath.log(probability));
}

/**
Expand All @@ -107,7 +107,7 @@ public void addIn(double probability) {
* @return the probability associated with the label
*/
public Double get() {
return Math.exp(probability);
return StrictMath.exp(probability);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void setIfLarger(T t, double probability) {
* @param probability the log probability to assign
*/
public void setLog(T t, double probability) {
set(t, Math.exp(probability));
set(t, StrictMath.exp(probability));
}

/**
Expand All @@ -97,7 +97,7 @@ public void addIn(T t, double probability, int count) {
Double p = map.get(t);
if (p == null)
p = 1.0;
probability = Math.pow(probability, count);
probability = StrictMath.pow(probability, count);
map.put(t, p * probability);
}

Expand All @@ -121,7 +121,7 @@ public Double get(T t) {
* @return the log probability associated with the label
*/
public Double getLog(T t) {
return Math.log(get(t));
return StrictMath.log(get(t));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public boolean isLarger(Probability probability) {
* @param probability the log probability to assign
*/
public void setLog(double probability) {
set(Math.exp(probability));
set(StrictMath.exp(probability));
}

/**
Expand All @@ -115,7 +115,7 @@ public Double get() {
* @return the log probability associated with the label
*/
public Double getLog() {
return Math.log(get());
return StrictMath.log(get());
}

/**
Expand Down
Loading

0 comments on commit 7c10174

Please sign in to comment.