All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ojalgo.matrix.task.iterative.JacobiSolver Maven / Gradle / Ivy

Go to download

oj! Algorithms - ojAlgo - is Open Source Java code that has to do with mathematics, linear algebra and optimisation.

There is a newer version: 55.0.1
Show newest version
/*
 * Copyright 1997-2021 Optimatika
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.ojalgo.matrix.task.iterative;

import static org.ojalgo.function.constant.PrimitiveMath.*;

import org.ojalgo.RecoverableCondition;
import org.ojalgo.function.aggregator.Aggregator;
import org.ojalgo.matrix.store.MatrixStore;
import org.ojalgo.matrix.store.PhysicalStore;
import org.ojalgo.matrix.store.Primitive64Store;
import org.ojalgo.matrix.store.TransformableRegion;
import org.ojalgo.structure.Access2D;
import org.ojalgo.type.context.NumberContext;

public final class JacobiSolver extends StationaryIterativeSolver {

    public JacobiSolver() {
        super();
    }

    @SuppressWarnings("unchecked")
    public MatrixStore solve(final Access2D body, final Access2D rhs, final PhysicalStore current) throws RecoverableCondition {

        MatrixStore tmpBody = null;
        if ((body instanceof MatrixStore) && (body.get(0L) instanceof Double)) {
            tmpBody = (MatrixStore) body;
        } else {
            tmpBody = MatrixStore.PRIMITIVE64.makeWrapper(body).get();
        }
        final MatrixStore tmpBodyDiagonal = Primitive64Store.FACTORY.columns(tmpBody.sliceDiagonal());

        MatrixStore tmpRHS = null;
        if ((rhs instanceof MatrixStore) && (rhs.get(0L) instanceof Double)) {
            tmpRHS = (MatrixStore) rhs;
        } else {
            tmpRHS = MatrixStore.PRIMITIVE64.makeWrapper(rhs).get();
        }

        final PhysicalStore tmpIncrement = this.preallocate(body, rhs);
        TransformableRegion incremetReceiver = body.isFat() ? tmpIncrement.regionByLimits((int) body.countRows(), 1) : tmpIncrement;

        double tmpNormErr = POSITIVE_INFINITY;
        final double tmpNormRHS = tmpRHS.aggregateAll(Aggregator.NORM2);

        int tmpIterations = 0;
        final int tmpLimit = this.getIterationsLimit();
        final NumberContext tmpCntxt = this.getAccuracyContext();
        final double tmpRelaxation = this.getRelaxationFactor();
        do {

            current.premultiply(tmpBody).onMatching(tmpRHS, SUBTRACT).supplyTo(incremetReceiver);
            tmpNormErr = tmpIncrement.aggregateAll(Aggregator.NORM2);
            tmpIncrement.modifyMatching(DIVIDE, tmpBodyDiagonal);

            if (this.getAccuracyContext().isDifferent(ONE, tmpRelaxation)) {
                tmpIncrement.multiply(tmpRelaxation);
            }

            current.modifyMatching(ADD, tmpIncrement);

            tmpIterations++;

            if (this.isDebugPrinterSet()) {
                this.debug(tmpIterations, tmpNormErr / tmpNormRHS, current);
            }

        } while ((tmpIterations < tmpLimit) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));

        return current;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy