use of org.hipparchus.exception.MathIllegalArgumentException in project symja_android_library by axkr.
the class InterpolatingPolynomial method checkPartialRealOrder.
/**
* Check that the given array is sorted for real numbers. If symbolic expressions occur we don't
* compare.
*
* @param val Values.
* @param dir Ordering direction.
* @param strict Whether the order should be strict.
* @param abort Whether to throw an exception if the check fails.
* @return {@code true} if the array is sorted.
* @throws MathIllegalArgumentException if the array is not sorted and {@code abort} is {@code
* true}.
*/
public static boolean checkPartialRealOrder(IExpr[] val, OrderDirection dir, boolean strict, boolean abort) throws MathIllegalArgumentException {
ISignedNumber previous = F.C0;
final int max = val.length;
int start = max;
for (int i = 0; i < val.length; i++) {
if (val[i] instanceof ISignedNumber) {
previous = (ISignedNumber) val[i];
start = i + 1;
break;
}
}
int index;
ITEM: for (index = start; index < max; index++) {
if (val[index] instanceof ISignedNumber) {
switch(dir) {
case INCREASING:
if (strict) {
if (((ISignedNumber) val[index]).isLE(previous)) {
break ITEM;
}
} else {
if (((ISignedNumber) val[index]).isLT(previous)) {
break ITEM;
}
}
break;
case DECREASING:
if (strict) {
if (((ISignedNumber) val[index]).isGE(previous)) {
break ITEM;
}
} else {
if (((ISignedNumber) val[index]).isGT(previous)) {
break ITEM;
}
}
break;
default:
// Should never happen.
throw MathRuntimeException.createInternalError();
}
previous = (ISignedNumber) val[index];
}
}
if (index == max) {
// Loop completed.
return true;
}
// Loop early exit means wrong ordering.
if (abort) {
throw new MathIllegalArgumentException(dir == MathArrays.OrderDirection.INCREASING ? (strict ? LocalizedCoreFormats.NOT_STRICTLY_INCREASING_SEQUENCE : LocalizedCoreFormats.NOT_INCREASING_SEQUENCE) : (strict ? LocalizedCoreFormats.NOT_STRICTLY_DECREASING_SEQUENCE : LocalizedCoreFormats.NOT_DECREASING_SEQUENCE), val[index], previous, index, index - 1);
} else {
return false;
}
}
Aggregations