use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class BidiagonalTest method doTestCorrect.
private void doTestCorrect(final PhysicalStore<Double> aMatrix) {
final BidiagonalDecomposition<Double> tmpDecomposition = (BidiagonalDecomposition<Double>) Bidiagonal.PRIMITIVE.make();
tmpDecomposition.decompose(aMatrix);
if (!Bidiagonal.equals(aMatrix, tmpDecomposition, new NumberContext(7, 6))) {
this.doPrint(tmpDecomposition, aMatrix);
TestUtils.fail("Not equals, easy!");
}
if (!Bidiagonal.equals(aMatrix, tmpDecomposition, new NumberContext(7, 6))) {
this.doPrint(tmpDecomposition, aMatrix);
TestUtils.fail("Not equals, hard!");
}
final MatrixStore<Double> tmpReconstructed = Bidiagonal.reconstruct(tmpDecomposition);
if (!Access2D.equals(aMatrix, tmpReconstructed, new NumberContext(7, 6))) {
this.doPrint(tmpDecomposition, aMatrix);
TestUtils.fail("Failed to reconstruct!");
}
TestUtils.assertEquals(aMatrix, tmpDecomposition, new NumberContext(7, 6));
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class StandardType method percent.
public static NumberContext percent(final Locale locale) {
final NumberContext retVal = NumberContext.getPercent(locale);
retVal.format(new BigDecimal("0.5000"));
return retVal;
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class ApproximationCase method testFirstOrderApproximation.
@Test
public void testFirstOrderApproximation() {
final int tmpArity = 9;
final PhysicalStore<Double> tmpLinear = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-1, 2));
final PhysicalStore<Double> tmpPoint = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-10, 20));
final LinearFunction<Double> tmpOrgFunc = LinearFunction.makePrimitive(tmpLinear);
final FirstOrderApproximation<Double> tmpApprFunc = tmpOrgFunc.toFirstOrderApproximation(tmpPoint);
final PhysicalStore<Double> tmpX = PrimitiveDenseStore.FACTORY.makeFilled(tmpArity, 1, new Uniform(-10, 20));
TestUtils.assertEquals(tmpOrgFunc.invoke(tmpX), tmpApprFunc.invoke(tmpX), new NumberContext(7, 14));
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class ConjugateGradientSolver method resolve.
public double resolve(final List<Equation> equations, final PhysicalStore<Double> solution) {
final int tmpCountRows = equations.size();
double tmpNormErr = POSITIVE_INFINITY;
double tmpNormRHS = ONE;
final PrimitiveDenseStore tmpResidual = this.residual(solution);
final PrimitiveDenseStore tmpDirection = this.direction(solution);
final PrimitiveDenseStore tmpPreconditioned = this.preconditioned(solution);
final PrimitiveDenseStore tmpVector = this.vector(solution);
double tmpStepLength;
double tmpGradientCorrectionFactor;
double zr0 = 1;
double zr1 = 1;
double pAp0 = 0;
for (int r = 0; r < tmpCountRows; r++) {
final Equation tmpRow = equations.get(r);
double tmpVal = tmpRow.getRHS();
tmpNormRHS = PrimitiveFunction.HYPOT.invoke(tmpNormRHS, tmpVal);
tmpVal -= tmpRow.dot(solution);
tmpResidual.set(tmpRow.index, tmpVal);
// precondition
tmpPreconditioned.set(tmpRow.index, tmpVal / tmpRow.getPivot());
}
// tmpPreconditioned.supplyNonZerosTo(tmpDirection);
tmpDirection.fillMatching(tmpPreconditioned);
int tmpIterations = 0;
final int tmpLimit = this.getIterationsLimit();
final NumberContext tmpCntxt = this.getAccuracyContext();
// zr1 = tmpPreconditioned.transpose().multiply(tmpResidual).doubleValue(0L);
zr1 = tmpPreconditioned.dot(tmpResidual);
do {
zr0 = zr1;
for (int i = 0; i < tmpCountRows; i++) {
final Equation tmpRow = equations.get(i);
final double tmpVal = tmpRow.dot(tmpDirection);
tmpVector.set(tmpRow.index, tmpVal);
}
// pAp0 = tmpVector.multiplyLeft(tmpDirection.transpose()).get().doubleValue(0L);
pAp0 = tmpDirection.dot(tmpVector);
tmpStepLength = zr0 / pAp0;
if (!Double.isNaN(tmpStepLength)) {
// solution.maxpy(tmpStepLength, tmpDirection);
tmpDirection.axpy(tmpStepLength, solution);
// tmpResidual.maxpy(-tmpStepLength, tmpVector);
tmpVector.axpy(-tmpStepLength, tmpResidual);
}
tmpNormErr = ZERO;
for (int r = 0; r < tmpCountRows; r++) {
final Equation tmpRow = equations.get(r);
final double tmpValue = tmpResidual.doubleValue(tmpRow.index);
tmpNormErr = PrimitiveFunction.HYPOT.invoke(tmpNormErr, tmpValue);
tmpPreconditioned.set(tmpRow.index, tmpValue / tmpRow.getPivot());
}
zr1 = tmpPreconditioned.dot(tmpResidual);
tmpGradientCorrectionFactor = zr1 / zr0;
tmpDirection.modifyAll(PrimitiveFunction.MULTIPLY.second(tmpGradientCorrectionFactor));
tmpDirection.modifyMatching(PrimitiveFunction.ADD, tmpPreconditioned);
tmpIterations++;
if (this.isDebugPrinterSet()) {
this.debug(tmpIterations, solution);
}
} while ((tmpIterations < tmpLimit) && !Double.isNaN(tmpNormErr) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
return tmpNormErr / tmpNormRHS;
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class GaussSeidelSolver method resolve.
public double resolve(final List<Equation> equations, final PhysicalStore<Double> solution) {
double tmpNormErr = POSITIVE_INFINITY;
double tmpNormRHS = ZERO;
final int tmpCountRows = equations.size();
for (int r = 0; r < tmpCountRows; r++) {
tmpNormRHS = PrimitiveFunction.HYPOT.invoke(tmpNormRHS, equations.get(r).getRHS());
}
int tmpIterations = 0;
final int tmpLimit = this.getIterationsLimit();
final NumberContext tmpCntxt = this.getAccuracyContext();
final double tmpRelaxationFactor = this.getRelaxationFactor();
do {
tmpNormErr = ZERO;
for (int r = 0; r < tmpCountRows; r++) {
tmpNormErr = PrimitiveFunction.HYPOT.invoke(tmpNormErr, equations.get(r).adjust(solution, tmpRelaxationFactor));
}
tmpIterations++;
if (this.isDebugPrinterSet()) {
this.debug(tmpIterations, solution);
}
} while ((tmpIterations < tmpLimit) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
return tmpNormErr / tmpNormRHS;
}
Aggregations