use of org.hipparchus.exception.MathIllegalArgumentException in project Orekit by CS-SI.
the class AngularCoordinatesTest method checkInverse.
private void checkInverse(Vector3D omega, Vector3D v1, Vector3D c1, Vector3D v2, Vector3D c2) throws MathIllegalArgumentException {
try {
Method inverse;
inverse = AngularCoordinates.class.getDeclaredMethod("inverseCrossProducts", Vector3D.class, Vector3D.class, Vector3D.class, Vector3D.class, double.class);
inverse.setAccessible(true);
Vector3D rebuilt = (Vector3D) inverse.invoke(null, v1, c1, v2, c2, 1.0e-9);
Assert.assertEquals(0.0, Vector3D.distance(omega, rebuilt), 5.0e-12 * omega.getNorm());
} catch (NoSuchMethodException e) {
Assert.fail(e.getLocalizedMessage());
} catch (SecurityException e) {
Assert.fail(e.getLocalizedMessage());
} catch (IllegalAccessException e) {
Assert.fail(e.getLocalizedMessage());
} catch (IllegalArgumentException e) {
Assert.fail(e.getLocalizedMessage());
} catch (InvocationTargetException e) {
throw (MathIllegalArgumentException) e.getCause();
}
}
use of org.hipparchus.exception.MathIllegalArgumentException in project Orekit by CS-SI.
the class AngularCoordinates method inverseCrossProducts.
/**
* Find a vector from two known cross products.
* <p>
* We want to find Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
* </p>
* <p>
* The first equation (Ω ⨯ v₁ = c₁) will always be fulfilled exactly,
* and the second one will be fulfilled if possible.
* </p>
* @param v1 vector forming the first known cross product
* @param c1 know vector for cross product Ω ⨯ v₁
* @param v2 vector forming the second known cross product
* @param c2 know vector for cross product Ω ⨯ v₂
* @param tolerance relative tolerance factor used to check singularities
* @return vector Ω such that: Ω ⨯ v₁ = c₁ and Ω ⨯ v₂ = c₂
* @exception MathIllegalArgumentException if vectors are inconsistent and
* no solution can be found
*/
private static Vector3D inverseCrossProducts(final Vector3D v1, final Vector3D c1, final Vector3D v2, final Vector3D c2, final double tolerance) throws MathIllegalArgumentException {
final double v12 = v1.getNormSq();
final double v1n = FastMath.sqrt(v12);
final double v22 = v2.getNormSq();
final double v2n = FastMath.sqrt(v22);
final double threshold = tolerance * FastMath.max(v1n, v2n);
Vector3D omega;
try {
// create the over-determined linear system representing the two cross products
final RealMatrix m = MatrixUtils.createRealMatrix(6, 3);
m.setEntry(0, 1, v1.getZ());
m.setEntry(0, 2, -v1.getY());
m.setEntry(1, 0, -v1.getZ());
m.setEntry(1, 2, v1.getX());
m.setEntry(2, 0, v1.getY());
m.setEntry(2, 1, -v1.getX());
m.setEntry(3, 1, v2.getZ());
m.setEntry(3, 2, -v2.getY());
m.setEntry(4, 0, -v2.getZ());
m.setEntry(4, 2, v2.getX());
m.setEntry(5, 0, v2.getY());
m.setEntry(5, 1, -v2.getX());
final RealVector rhs = MatrixUtils.createRealVector(new double[] { c1.getX(), c1.getY(), c1.getZ(), c2.getX(), c2.getY(), c2.getZ() });
// find the best solution we can
final DecompositionSolver solver = new QRDecomposition(m, threshold).getSolver();
final RealVector v = solver.solve(rhs);
omega = new Vector3D(v.getEntry(0), v.getEntry(1), v.getEntry(2));
} catch (MathIllegalArgumentException miae) {
if (miae.getSpecifier() == LocalizedCoreFormats.SINGULAR_MATRIX) {
// handle some special cases for which we can compute a solution
final double c12 = c1.getNormSq();
final double c1n = FastMath.sqrt(c12);
final double c22 = c2.getNormSq();
final double c2n = FastMath.sqrt(c22);
if (c1n <= threshold && c2n <= threshold) {
// simple special case, velocities are cancelled
return Vector3D.ZERO;
} else if (v1n <= threshold && c1n >= threshold) {
// this is inconsistent, if v₁ is zero, c₁ must be 0 too
throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, c1n, 0, true);
} else if (v2n <= threshold && c2n >= threshold) {
// this is inconsistent, if v₂ is zero, c₂ must be 0 too
throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, c2n, 0, true);
} else if (Vector3D.crossProduct(v1, v2).getNorm() <= threshold && v12 > threshold) {
// simple special case, v₂ is redundant with v₁, we just ignore it
// use the simplest Ω: orthogonal to both v₁ and c₁
omega = new Vector3D(1.0 / v12, Vector3D.crossProduct(v1, c1));
} else {
throw miae;
}
} else {
throw miae;
}
}
// check results
final double d1 = Vector3D.distance(Vector3D.crossProduct(omega, v1), c1);
if (d1 > threshold) {
throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, d1, 0, true);
}
final double d2 = Vector3D.distance(Vector3D.crossProduct(omega, v2), c2);
if (d2 > threshold) {
throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_LARGE, d2, 0, true);
}
return omega;
}
use of org.hipparchus.exception.MathIllegalArgumentException in project symja_android_library by axkr.
the class LinearProgramming method numericEval.
@Override
public IExpr numericEval(final IAST ast, EvalEngine engine) {
try {
if (ast.arg1().isList() && ast.arg2().isList() && ast.arg3().isList()) {
double[] arg1D = ast.arg1().toDoubleVector();
if (arg1D != null) {
LinearObjectiveFunction f = new LinearObjectiveFunction(arg1D, 0);
Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
IAST arg2 = (IAST) ast.arg2();
IAST arg3 = (IAST) ast.arg3();
if (arg2.size() != arg3.size()) {
return F.NIL;
}
double[] arg2D;
double[] arg3D;
for (int i = 1; i < arg2.size(); i++) {
if (arg2.get(i).isList()) {
arg2D = arg2.get(i).toDoubleVector();
if (arg2D == null) {
return F.NIL;
}
if (arg3.get(i).isList()) {
arg3D = arg3.get(i).toDoubleVector();
if (arg3D == null) {
return F.NIL;
}
if (arg3D.length >= 2) {
double val = arg3D[1];
if (val == 0.0) {
constraints.add(new LinearConstraint(arg2D, Relationship.EQ, arg3D[0]));
} else if (val < 0.0) {
constraints.add(new LinearConstraint(arg2D, Relationship.LEQ, arg3D[0]));
} else if (val > 0.0) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
}
} else if (arg3D.length == 1) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, arg3D[0]));
}
} else {
ISignedNumber sn = arg3.get(i).evalReal();
if (sn != null) {
constraints.add(new LinearConstraint(arg2D, Relationship.GEQ, sn.doubleValue()));
} else {
LOGGER.log(engine.getLogLevel(), "Numeric vector or number expected!");
return F.NIL;
}
}
} else {
LOGGER.log(engine.getLogLevel(), "Numeric vector expected!");
return F.NIL;
}
}
SimplexSolver solver = new SimplexSolver();
// PointValuePair solution = solver.optimize(f, constraints, GoalType.MINIMIZE, true);
PointValuePair solution = solver.optimize(f, new LinearConstraintSet(constraints), GoalType.MINIMIZE, new NonNegativeConstraint(true), PivotSelectionRule.BLAND);
double[] values = solution.getPointRef();
return F.List(values);
}
}
} catch (MathIllegalArgumentException miae) {
// `1`.
return IOFunctions.printMessage(ast.topHead(), "error", F.list(F.$str(miae.getMessage())), engine);
} catch (MathRuntimeException mre) {
LOGGER.log(engine.getLogLevel(), ast.topHead(), mre);
}
return F.NIL;
}
use of org.hipparchus.exception.MathIllegalArgumentException in project symja_android_library by axkr.
the class AbstractFractionSym method valueOf.
/**
* Construct a rational from two BigIntegers. Use this method to create a
* rational number if the numerator or denominator may be to big to fit in
* an Java int. This method normalizes the rational number.
*
* @param num
* Numerator
* @param den
* Denominator
* @return
*/
public static IFraction valueOf(BigInteger num, BigInteger den) {
if (BigInteger.ZERO.equals(den)) {
throw new MathIllegalArgumentException(LocalizedCoreFormats.ZERO_DENOMINATOR);
}
int cp = den.signum();
if (cp < 0) {
num = num.negate();
den = den.negate();
}
if (!BigInteger.ONE.equals(den)) {
BigInteger norm = gcd(num, den).abs();
if (!norm.equals(BigInteger.ONE)) {
num = num.divide(norm);
den = den.divide(norm);
}
}
if (den.bitLength() <= 31 && num.bitLength() <= 31) {
return valueOf(num.intValue(), den.intValue());
} else {
return new BigFractionSym(num, den);
}
}
use of org.hipparchus.exception.MathIllegalArgumentException in project Orekit by CS-SI.
the class TabulatedEphemerisTest method checkInterpolation.
private void checkInterpolation(StateFilter f, double expectedDP, double expectedDV) throws OrekitException {
double mass = 2500;
double a = 7187990.1979844316;
double e = 0.5e-4;
double i = 1.7105407051081795;
double omega = 1.9674147913622104;
double OMEGA = FastMath.toRadians(261);
double lv = 0;
final AbsoluteDate initDate = new AbsoluteDate(new DateComponents(2004, 01, 01), TimeComponents.H00, TimeScalesFactory.getUTC());
final AbsoluteDate finalDate = new AbsoluteDate(new DateComponents(2004, 01, 02), TimeComponents.H00, TimeScalesFactory.getUTC());
double deltaT = finalDate.durationFrom(initDate);
Orbit transPar = new KeplerianOrbit(a, e, i, omega, OMEGA, lv, PositionAngle.TRUE, FramesFactory.getEME2000(), initDate, mu);
int nbIntervals = 720;
EcksteinHechlerPropagator eck = new EcksteinHechlerPropagator(transPar, mass, ae, mu, c20, c30, c40, c50, c60);
AdditionalStateProvider provider = new AdditionalStateProvider() {
public String getName() {
return "dt";
}
public double[] getAdditionalState(SpacecraftState state) {
return new double[] { state.getDate().durationFrom(initDate) };
}
};
eck.addAdditionalStateProvider(provider);
try {
eck.addAdditionalStateProvider(provider);
Assert.fail("an exception should have been thrown");
} catch (OrekitException oe) {
Assert.assertEquals(OrekitMessages.ADDITIONAL_STATE_NAME_ALREADY_IN_USE, oe.getSpecifier());
}
List<SpacecraftState> tab = new ArrayList<SpacecraftState>(nbIntervals + 1);
for (int j = 0; j <= nbIntervals; j++) {
AbsoluteDate current = initDate.shiftedBy((j * deltaT) / nbIntervals);
tab.add(f.filter(eck.propagate(current)));
}
try {
new Ephemeris(tab, nbIntervals + 2);
Assert.fail("an exception should have been thrown");
} catch (MathIllegalArgumentException miae) {
// expected
}
Ephemeris te = new Ephemeris(tab, 2);
Assert.assertEquals(0.0, te.getMaxDate().durationFrom(finalDate), 1.0e-9);
Assert.assertEquals(0.0, te.getMinDate().durationFrom(initDate), 1.0e-9);
double maxP = 0;
double maxV = 0;
for (double dt = 0; dt < 3600; dt += 1) {
AbsoluteDate date = initDate.shiftedBy(dt);
CartesianOrbit c1 = (CartesianOrbit) eck.propagate(date).getOrbit();
CartesianOrbit c2 = (CartesianOrbit) te.propagate(date).getOrbit();
maxP = FastMath.max(maxP, Vector3D.distance(c1.getPVCoordinates().getPosition(), c2.getPVCoordinates().getPosition()));
maxV = FastMath.max(maxV, Vector3D.distance(c1.getPVCoordinates().getVelocity(), c2.getPVCoordinates().getVelocity()));
}
Assert.assertEquals(expectedDP, maxP, 0.1 * expectedDP);
Assert.assertEquals(expectedDV, maxV, 0.1 * expectedDV);
}
Aggregations