use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.
the class ConvexSolver method validate.
/**
* Should validate the solver data/input/structue. Even "expensive" validation can be performed as the
* method should only be called if {@linkplain Optimisation.Options#validate} is set to true. In addition
* to returning true or false the implementation should set the state to either
* {@linkplain Optimisation.State#VALID} or {@linkplain Optimisation.State#INVALID} (or possibly
* {@linkplain Optimisation.State#FAILED}). Typically the method should be called at the very beginning of
* the solve-method.
*
* @return Is the solver instance valid?
*/
protected boolean validate() {
final MatrixStore<Double> tmpQ = this.getMatrixQ();
final MatrixStore<Double> tmpC = this.getMatrixC();
if ((tmpQ == null) || (tmpC == null)) {
throw new IllegalArgumentException("Neither Q nor C may be null!");
}
if (!MatrixUtils.isHermitian(tmpQ)) {
if (this.isDebug()) {
this.log("Q not symmetric!", tmpQ);
}
throw new IllegalArgumentException("Q must be symmetric!");
}
if (!mySolverQ.isSPD()) {
// Not symmetric positive definite. Check if at least positive semidefinite.
final Eigenvalue<Double> tmpEvD = Eigenvalue.PRIMITIVE.make(true);
tmpEvD.computeValuesOnly(tmpQ);
final Array1D<ComplexNumber> tmpEigenvalues = tmpEvD.getEigenvalues();
tmpEvD.reset();
for (final ComplexNumber tmpValue : tmpEigenvalues) {
if ((tmpValue.doubleValue() < ZERO) || !tmpValue.isReal()) {
if (this.isDebug()) {
this.log("Q not positive semidefinite!");
this.log("The eigenvalues are: {}", tmpEigenvalues);
}
throw new IllegalArgumentException("Q must be positive semidefinite!");
}
}
}
this.setState(State.VALID);
return true;
}
use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.
the class DecompositionProblems method testP20111213square.
/**
* A user reported problems solving complex valued (overdetermined) equation systemes.
*/
@Test
@Tag("unstable")
public void testP20111213square() {
final int tmpDim = Uniform.randomInteger(2, 6);
final PhysicalStore<ComplexNumber> tmpSquare = MatrixUtils.makeRandomComplexStore(tmpDim, tmpDim);
final MatrixStore<ComplexNumber> tmpHermitian = tmpSquare.conjugate().multiply(tmpSquare);
final PhysicalStore<ComplexNumber> tmpExpected = ComplexDenseStore.FACTORY.makeEye(tmpDim, tmpDim);
MatrixStore<ComplexNumber> tmpActual;
@SuppressWarnings("unchecked") final MatrixDecomposition<ComplexNumber>[] tmpCmplxDecomps = new MatrixDecomposition[] { Bidiagonal.COMPLEX.make(), Cholesky.COMPLEX.make(), Eigenvalue.COMPLEX.make(MatrixDecomposition.TYPICAL, true), /*
* , HessenbergDecomposition. makeComplex()
*/
LU.COMPLEX.make(), QR.COMPLEX.make(), SingularValue.COMPLEX.make() /*
* , TridiagonalDecomposition . makeComplex ( )
*/
};
for (final MatrixDecomposition<ComplexNumber> tmpDecomposition : tmpCmplxDecomps) {
tmpDecomposition.decompose(tmpHermitian);
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug(tmpDecomposition.toString());
BasicLogger.debug("Original", tmpHermitian);
BasicLogger.debug("Recretaed", tmpDecomposition.reconstruct());
}
TestUtils.assertEquals("Recreation: " + tmpDecomposition.toString(), tmpHermitian, tmpDecomposition.reconstruct(), new NumberContext(8, 5));
if ((tmpDecomposition instanceof MatrixDecomposition.Solver<?>) && ((Solver) tmpDecomposition).isSolvable()) {
tmpActual = ((Solver) tmpDecomposition).getSolution(tmpHermitian);
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug("Actual", tmpActual);
}
TestUtils.assertEquals("Solving: " + tmpDecomposition.toString(), tmpExpected, tmpActual, new NumberContext(7, 6));
}
}
}
use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.
the class DecompositionProblems method testP20111213tall.
/**
* A user reported problems solving complex valued (overdetermined) equation systemes.
*/
@Test
public void testP20111213tall() {
final int tmpDim = Uniform.randomInteger(2, 6);
final PhysicalStore<ComplexNumber> original = MatrixUtils.makeRandomComplexStore(tmpDim + tmpDim, tmpDim);
final PhysicalStore<ComplexNumber> identity = ComplexDenseStore.FACTORY.makeEye(tmpDim, tmpDim);
MatrixStore<ComplexNumber> solution;
@SuppressWarnings("unchecked") final MatrixDecomposition<ComplexNumber>[] tmpCmplxDecomps = new MatrixDecomposition[] { QR.COMPLEX.make(), SingularValue.COMPLEX.make(), Bidiagonal.COMPLEX.make() };
for (final MatrixDecomposition<ComplexNumber> decomp : tmpCmplxDecomps) {
decomp.decompose(original);
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug(decomp.toString());
BasicLogger.debug("Original", original);
BasicLogger.debug("Recretaed", decomp.reconstruct());
}
TestUtils.assertEquals(decomp.toString(), original, decomp.reconstruct(), new NumberContext(7, 5));
if ((decomp instanceof MatrixDecomposition.Solver<?>) && ((Solver<ComplexNumber>) decomp).isSolvable()) {
solution = ((Solver<ComplexNumber>) decomp).getSolution(original);
if (MatrixDecompositionTests.DEBUG) {
BasicLogger.debug("Actual", solution);
}
TestUtils.assertEquals(decomp.toString(), identity, solution, new NumberContext(7, 6));
}
}
}
use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.
the class DegenerateLUCase method testComplex.
@Test
public void testComplex() {
final NumberContext tmpEvalContext = new NumberContext(7, 4);
final BasicMatrix tmpMtrxA = RationalMatrix.FACTORY.makeZero(SimpleEquationCase.getBody().countRows(), (int) SimpleEquationCase.getBody().countColumns()).mergeColumns(SimpleEquationCase.getBody()).mergeColumns(SimpleEquationCase.getBody());
final LU<ComplexNumber> tmpComplexDecomp = LU.COMPLEX.make();
tmpComplexDecomp.decompose(ComplexDenseStore.FACTORY.copy(tmpMtrxA));
// System.out.println("A: " + tmpMtrxA.enforce(tmpEvalContext));
// System.out.println("P: " + new ComplexMatrix(tmpComplexDecomp.getP()).enforce(tmpEvalContext));
// System.out.println("L: " + new ComplexMatrix(tmpComplexDecomp.getL()).enforce(tmpEvalContext));
// System.out.println("PL: " + new ComplexMatrix(tmpComplexDecomp.getP().multiplyRight(tmpComplexDecomp.getL())).enforce(tmpEvalContext));
// System.out.println("D: " + new ComplexMatrix(tmpComplexDecomp.getD()).enforce(tmpEvalContext));
// System.out.println("U: " + new ComplexMatrix(tmpComplexDecomp.getU()).enforce(tmpEvalContext));
// System.out.println("DU: " + new ComplexMatrix(tmpComplexDecomp.getD().multiplyRight(tmpComplexDecomp.getU())).enforce(tmpEvalContext));
TestUtils.assertEquals(ComplexDenseStore.FACTORY.copy(tmpMtrxA), tmpComplexDecomp, tmpEvalContext);
}
use of org.ojalgo.scalar.ComplexNumber in project ojAlgo by optimatika.
the class EigenvalueTest method testRandomSymmetricValuesOnly.
@Test
public void testRandomSymmetricValuesOnly() {
final NumberContext evaluationContext = NumberContext.getGeneral(MathContext.DECIMAL32);
for (int dim = 1; dim < 10; dim++) {
final PrimitiveDenseStore matrix = MatrixUtils.makeSPD(dim);
for (final Eigenvalue<Double> decomp : MatrixDecompositionTests.getEigenvaluePrimitiveSymmetric()) {
decomp.decompose(matrix);
TestUtils.assertEquals(matrix, decomp, evaluationContext);
final Array1D<ComplexNumber> expected = decomp.getEigenvalues();
decomp.computeValuesOnly(matrix);
final Array1D<ComplexNumber> actual = decomp.getEigenvalues();
TestUtils.assertEquals(expected, actual, evaluationContext);
}
}
}
Aggregations