Program Listing for File DiagonalPreconditioner.hpp

Return to documentation for file (include/networkit/numerics/Preconditioner/DiagonalPreconditioner.hpp)

/*
 * DiagonalPreconditioner.hpp
 *
 *  Created on: Apr 23, 2016
 *      Author: Michael Wegner (michael.wegner@student.kit.edu)
 */

#ifndef NETWORKIT_NUMERICS_PRECONDITIONER_DIAGONAL_PRECONDITIONER_HPP_
#define NETWORKIT_NUMERICS_PRECONDITIONER_DIAGONAL_PRECONDITIONER_HPP_

#include <networkit/algebraic/CSRMatrix.hpp>

namespace NetworKit {

class DiagonalPreconditioner {
public:
    DiagonalPreconditioner() = default;

    DiagonalPreconditioner(const CSRMatrix &A) : inv_diag(A.numberOfRows()) {
        assert(A.numberOfColumns() == A.numberOfRows());

        // Diagonal preconditioner just needs to store the inverse diagonal of A
        inv_diag = A.diagonal();
#pragma omp parallel for
        for (omp_index i = 0; i < static_cast<omp_index>(inv_diag.getDimension()); ++i) {
            if (inv_diag[i] > 0)
                inv_diag[i] = 1.0 / inv_diag[i];
        }
    }

    virtual ~DiagonalPreconditioner() = default;

    Vector rhs(const Vector &b) const {
        assert(b.getDimension() == inv_diag.getDimension());
        Vector out(b.getDimension());
        for (index i = 0; i < b.getDimension(); ++i) {
            out[i] = inv_diag[i] * b[i];
        }
        return out;
    }

private:
    Vector inv_diag;
};

} /* namespace NetworKit */

#endif // NETWORKIT_NUMERICS_PRECONDITIONER_DIAGONAL_PRECONDITIONER_HPP_