MatrixGroup.h 2 KB
Newer Older
1 2 3 4 5 6 7 8
#ifndef OERSTED_MATRIXGROUP_H
#define OERSTED_MATRIXGROUP_H

#include "Eigen"
#include "Eigen/Sparse"

class SparseMatrixGroup {
public:
9 10 11
    SparseMatrixGroup(size_t const Q, size_t const rows, size_t const cols, size_t const nnz) {
        Matrices.resize(Q);
        for (size_t i = 0; i != Matrices.size(); ++i) {
12 13 14 15 16 17 18 19 20 21 22
            Matrices[i].resize(rows, cols);
            Matrices[i].reserve(Eigen::VectorXi::Constant(cols, nnz));
        }
    };

    double &operator()(size_t const q, size_t const i, size_t const j) { return Matrices[q].coeffRef(i, j); };

    auto &operator[](size_t const q) { return Matrices[q]; };

    auto &operator[](size_t const q) const { return Matrices[q]; };

JasonPries's avatar
JasonPries committed
23
    void transform(Eigen::SparseMatrix<double> &A) {
24
        for (size_t i = 0; i != Matrices.size(); ++i) {
JasonPries's avatar
JasonPries committed
25 26 27 28
            Matrices[i] = A * Matrices[i];
        }
    }

29
protected:
30
    std::vector<Eigen::SparseMatrix<double_t>> Matrices;
31 32 33 34
};

class DiagonalMatrixGroup {
public:
35 36 37
    DiagonalMatrixGroup(size_t Q, size_t const dim) {
        Matrices.resize(Q);
        for (size_t i = 0; i != Matrices.size(); ++i) {
38 39 40 41
            Matrices[i].resize(dim);
        }
    }

JasonPries's avatar
JasonPries committed
42 43
    auto &operator()(size_t const q) const { return Matrices[q]; };
    auto &operator()(size_t const q) { return Matrices[q]; };
44 45

protected:
46
    std::vector<Eigen::ArrayXd> Matrices;
47 48 49 50
};

class DerivativeMatrixGroup {
public:
51
    DerivativeMatrixGroup(size_t const Q, size_t const rows, size_t const cols, size_t const nnz) : Dx{Q, rows, cols, nnz}, Dy{Q, rows, cols, nnz} {};
52

JasonPries's avatar
JasonPries committed
53
    auto const &dx(size_t const q) const { return Dx[q]; };
54

JasonPries's avatar
JasonPries committed
55
    auto const &dy(size_t const q) const { return Dy[q]; };
56 57 58 59 60

    auto &dx(size_t const q, size_t const i, size_t const j) { return Dx(q, i, j); };

    auto &dy(size_t const q, size_t const i, size_t const j) { return Dy(q, i, j); };

JasonPries's avatar
JasonPries committed
61 62 63 64 65
    void transform(Eigen::SparseMatrix<double> &A) {
        Dx.transform(A);
        Dy.transform(A);
    }

66
protected:
67 68
    SparseMatrixGroup Dx;
    SparseMatrixGroup Dy;
69 70 71
};

#endif //OERSTED_MATRIXGROUP_H