Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MATH-1572 - Simplify Conditions: #176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -175,14 +175,10 @@ public double value(double x, double y)
* @return {@code true} if (x, y) is a valid point.
*/
public boolean isValidPoint(double x, double y) {
if (x < xval[0] ||
x > xval[xval.length - 1] ||
y < yval[0] ||
y > yval[yval.length - 1]) {
return false;
} else {
return true;
}
return !(x < xval[0]) &&
!(x > xval[xval.length - 1]) &&
!(y < yval[0]) &&
!(y > yval[yval.length - 1]);
}

/**
Expand Down
Expand Up @@ -100,14 +100,10 @@ public BicubicInterpolatingFunction interpolate(final double[] xval,
/** {@inheritDoc} */
@Override
public boolean isValidPoint(double x, double y) {
if (x < xval[1] ||
x > xval[xval.length - 2] ||
y < yval[1] ||
y > yval[yval.length - 2]) {
return false;
} else {
return true;
}
return !(x < xval[1]) &&
!(x > xval[xval.length - 2]) &&
!(y < yval[1]) &&
!(y > yval[yval.length - 2]);
}
};
}
Expand Down
Expand Up @@ -157,14 +157,10 @@ public double value(double x,
*/
public boolean isValidPoint(double x,
double y) {
if (x < xval[0] ||
x > xval[xval.length - 1] ||
y < yval[0] ||
y > yval[yval.length - 1]) {
return false;
} else {
return true;
}
return !(x < xval[0]) &&
!(x > xval[xval.length - 1]) &&
!(y < yval[0]) &&
!(y > yval[yval.length - 1]);
}

/**
Expand Down
Expand Up @@ -341,16 +341,12 @@ public double value(double x, double y, double z)
* @return {@code true} if (x, y, z) is a valid point.
*/
public boolean isValidPoint(double x, double y, double z) {
if (x < xval[0] ||
x > xval[xval.length - 1] ||
y < yval[0] ||
y > yval[yval.length - 1] ||
z < zval[0] ||
z > zval[zval.length - 1]) {
return false;
} else {
return true;
}
return !(x < xval[0]) &&
!(x > xval[xval.length - 1]) &&
!(y < yval[0]) &&
!(y > yval[yval.length - 1]) &&
!(z < zval[0]) &&
!(z > zval[zval.length - 1]);
}

/**
Expand Down
Expand Up @@ -128,16 +128,12 @@ public TricubicInterpolatingFunction interpolate(final double[] xval,
/** {@inheritDoc} */
@Override
public boolean isValidPoint(double x, double y, double z) {
if (x < xval[1] ||
x > xval[xval.length - 2] ||
y < yval[1] ||
y > yval[yval.length - 2] ||
z < zval[1] ||
z > zval[zval.length - 2]) {
return false;
} else {
return true;
}
return !(x < xval[1]) &&
!(x > xval[xval.length - 2]) &&
!(y < yval[1]) &&
!(y > yval[yval.length - 2]) &&
!(z < zval[1]) &&
!(z > zval[zval.length - 2]);
}
};
}
Expand Down
Expand Up @@ -372,10 +372,7 @@ public boolean equals(Object obj) {
return false;
}
PolynomialFunction other = (PolynomialFunction) obj;
if (!Arrays.equals(coefficients, other.coefficients)) {
return false;
}
return true;
return Arrays.equals(coefficients, other.coefficients);
}

/**
Expand Down
Expand Up @@ -227,11 +227,7 @@ public double[] getKnots() {
* @return {@code true} if {@code x} is a valid point.
*/
public boolean isValidPoint(double x) {
if (x < knots[0] ||
x > knots[n]) {
return false;
} else {
return true;
}
return !(x < knots[0]) &&
!(x > knots[n]);
}
}
5 changes: 1 addition & 4 deletions src/main/java/org/apache/commons/math4/dfp/Dfp.java
Expand Up @@ -2365,10 +2365,7 @@ public Dfp nextAfter(final Dfp x) {
}

// if this is greater than x
boolean up = false;
if (this.lessThan(x)) {
up = true;
}
boolean up = this.lessThan(x);

if (compare(this, x) == 0) {
return newInstance(x);
Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/apache/commons/math4/dfp/DfpMath.java
Expand Up @@ -945,11 +945,7 @@ public static Dfp asin(final Dfp a) {
*/
public static Dfp acos(Dfp a) {
Dfp result;
boolean negative = false;

if (a.lessThan(a.getZero())) {
negative = true;
}
boolean negative = a.lessThan(a.getZero());

a = Dfp.copysign(a, a.getOne()); // absolute value

Expand Down
Expand Up @@ -273,10 +273,7 @@ public int compare(WeightedObservedPoint p1,
return comp;
}
comp = Double.compare(p1.getWeight(), p2.getWeight());
if (comp != 0) {
return comp;
}
return 0;
return comp;
}
};

Expand Down
Expand Up @@ -365,7 +365,7 @@ public void stepAccepted(final double t, final double[] y) {
// force the sign to its value "just after the event"
previousEventTime = t;
g0Positive = increasing;
nextAction = handler.eventOccurred(t, y, !(increasing ^ forward));
nextAction = handler.eventOccurred(t, y, increasing == forward);
} else {
g0Positive = g0 >= 0;
nextAction = EventHandler.Action.CONTINUE;
Expand Down
Expand Up @@ -301,7 +301,7 @@ public void stepAccepted(final FieldODEStateAndDerivative<T> state) {
// force the sign to its value "just after the event"
previousEventTime = state.getTime();
g0Positive = increasing;
nextAction = handler.eventOccurred(state, !(increasing ^ forward));
nextAction = handler.eventOccurred(state, increasing == forward);
} else {
g0Positive = g0.getReal() >= 0;
nextAction = Action.CONTINUE;
Expand Down
Expand Up @@ -57,6 +57,6 @@ public PointValuePair getSolution() {
* @return {@code true} if the solution is optimal, {@code false} otherwise
*/
public boolean isSolutionOptimal() {
return tableau != null ? tableau.isOptimal() : false;
return tableau != null && tableau.isOptimal();
}
}
9 changes: 3 additions & 6 deletions src/main/java/org/apache/commons/math4/stat/Frequency.java
Expand Up @@ -378,13 +378,10 @@ public boolean equals(Object obj) {
}
Frequency<?> other = (Frequency<?>) obj;
if (freqTable == null) {
if (other.freqTable != null) {
return false;
}
} else if (!freqTable.equals(other.freqTable)) {
return false;
return other.freqTable == null;
} else {
return freqTable.equals(other.freqTable);
}
return true;
}

}
Expand Up @@ -148,10 +148,7 @@ public boolean equals(Object obj) {
if (!Arrays.equals(productsSums, other.productsSums)) {
return false;
}
if (!Arrays.equals(sums, other.sums)) {
return false;
}
return true;
return Arrays.equals(sums, other.sums);
}

}
Expand Up @@ -96,10 +96,7 @@ public boolean equals(Object obj) {
return false;
}
VectorialMean other = (VectorialMean) obj;
if (!Arrays.equals(means, other.means)) {
return false;
}
return true;
return Arrays.equals(means, other.means);
}

}
Expand Up @@ -908,7 +908,7 @@ private static class FixedCapacityList<E> extends ArrayList<E> implements Serial
*/
@Override
public boolean add(final E e) {
return size() < capacity ? super.add(e) : false;
return size() < capacity && super.add(e);
}

/**
Expand All @@ -924,7 +924,7 @@ public boolean addAll(Collection<? extends E> collection) {
boolean isCollectionLess =
collection != null &&
collection.size() + size() <= capacity;
return isCollectionLess ? super.addAll(collection) : false;
return isCollectionLess && super.addAll(collection);
}
}

Expand Down
Expand Up @@ -1133,11 +1133,7 @@ private static boolean hasTies(double[] x, double[] y) {
throw new InsufficientDataException();
}

if (values.length == x.length + y.length) {
return false; // There are no ties.
}

return true;
return values.length != x.length + y.length; // There are no ties.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is then wrong, isn't it? Before it checked if there are no ties (values.length == x.length + y.length) and the comment was related to the false return value. Now it directly checks if there are ties which is basically the name of the method so that the comment could be simply removed, right?

}

/**
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/apache/commons/math4/util/MathArrays.java
Expand Up @@ -777,7 +777,7 @@ public int compare(PairDoubleInteger o1,
*/
public static boolean equals(float[] x, float[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand All @@ -803,7 +803,7 @@ public static boolean equals(float[] x, float[] y) {
*/
public static boolean equalsIncludingNaN(float[] x, float[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand All @@ -828,7 +828,7 @@ public static boolean equalsIncludingNaN(float[] x, float[] y) {
*/
public static boolean equals(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand All @@ -854,7 +854,7 @@ public static boolean equals(double[] x, double[] y) {
*/
public static boolean equalsIncludingNaN(double[] x, double[] y) {
if ((x == null) || (y == null)) {
return !((x == null) ^ (y == null));
return (x == null) == (y == null);
}
if (x.length != y.length) {
return false;
Expand Down Expand Up @@ -1117,11 +1117,7 @@ public static boolean verifyValues(final double[] values, final int begin,
Integer.valueOf(begin + length), Integer.valueOf(values.length), true);
}

if (length == 0 && !allowEmpty) {
return false;
}

return true;
return length != 0 || allowEmpty;

}

Expand Down