summaryrefslogtreecommitdiffstats
path: root/include/mm/experiments/mmsubdiag.h
blob: 2f6c93c543a65adfa2050eadad530534855a904e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#pragma once

#ifndef __TRIDIAG_H__
#define __TRIDIAG_H__

template<class T>
class tridiag
{
    std::vector<T> diag, lower, upper;

    struct row
    {
        std::size_t index;
        tridiag& ref;

        T shared_zero = 0;

        row(std::size_t i, tridiag& _ref) : index(i), ref(_ref) {}

        T& operator[](std::size_t j)
        {
            switch(static_cast<long int>(j) - index)
            {
            case -1:
                return ref.lower[j];
            case 0:
                return ref.diag[j];
            case 1:
                return ref.upper[index];
            default:
                shared_zero = 0; // assure zero
                return shared_zero;
            }
        }
    };

    struct const_row
    {
        std::size_t index;
        const tridiag& ref;

        const T shared_zero = 0;

        const_row(std::size_t i, const tridiag& _ref) : index(i), ref(_ref) {}

        const T& operator[](std::size_t j) const
        {
            switch(static_cast<long int>(j) - index)
            {
            case -1:
                return ref.lower[j];
            case 0:
                return ref.diag[j];
            case 1:
                return ref.upper[index];
            default:
                return shared_zero;
            }
        }
    };

public:

    tridiag(std::size_t N = 1) : diag(N), lower(N-1), upper(N-1)
    {
    } 

    void resize(std::size_t N)
    {
        diag.reserve(N);
        lower.reserve(N-1);
        upper.reserve(N-1);
    }

    void zeros()
    {
        for (std::size_t i = 0; i < size()-1; ++i)
                diag[i] = lower[i] = upper[i] = 0;

        diag[size()-1] = 0;
    }

    // set diagonal to zero
    void zeros_diag()
    {
        for (std::size_t i = 0; i < size(); ++i)
            diag[i] = 0;
    }

    row operator[](std::size_t i)
    {
        return row(i, *this);
    }

    const const_row operator[](std::size_t i) const
    {
        return const_row(i, *this);
    }

    std::size_t size() const
    {
        return diag.size();
    }

    template<template <typename> class V>
    V<T> solve(const V<T>& rhs)
    {
        V<T> solution(diag.size());
        V<T> new_diag(diag);
        V<T> new_rhs(rhs);

        for(std::size_t i(1); i < size(); ++i)
        {
            T pivot = lower[i-1]/new_diag[i-1];
            new_diag[i] -= pivot * upper[i-1];
            new_rhs[i] -= pivot * new_rhs[i-1];
        }

        solution[size()-1] = new_rhs[size()-1] / new_diag[size()-1];

        for(int i(static_cast<int>(size()-2)); i>=0; --i)
            solution[i] = (new_rhs[i] - upper[i]*solution[i+1]) / new_diag[i];

        return solution;
    }

    static constexpr tridiag id(size_t N)
    {
        tridiag<T> diag;

        for (size_t k = 0; k < N; ++k)
            diag[k][k] = 1;

        return diag;
    }

    const std::vector<T>& d() const
    {
        return diag;
    }

    const std::vector<T>& down() const
    {
        return lower;
    }

    const std::vector<T>& up() const
    {
        return upper;
    }
};

// multiplication operator overloading
/*template<class T, template <class> class V>
V<T> operator*(const tridiag<T>& M, const V<T>& v)
{
    const std::size_t N = v.size();

    V<T> u(N);

    u[0] = M[0][0] * v[0] + M[0][1] * v[1];

    for (std::size_t k = 1; k < N-1; ++k)
        u[k] = M[k][k-1] * v[k-1] + M[k][k] * v[k] + M[k][k+1] * v[k+1];

    u[N-1] = M[N-1][N-2] * v[N-2] + M[N-1][N-1] * v[N-1];

    return u;
}*/

template<class T, template <class> class V>
V<T> operator*(const tridiag<T>& M, const V<T>& v)
{
    const std::size_t N = v.size();

    V<T> u(N);

    u[0] = M.d()[0] * v[0] + M.up()[0] * v[1];

    for (std::size_t k = 1; k < N-1; ++k)
        u[k] = M.down()[k] * v[k-1] + M.d()[k] * v[k] + M.up()[k] * v[k+1];

    u[N-1] = M.down()[N-1] * v[N-2] + M.d()[N-1] * v[N-1];

    return u;
}

template<class T>
tridiag<T> operator+(tridiag<T> M, const tridiag<T>& A)
{
    if (M.size() != A.size())
        return M;

    const std::size_t N = M.size();

    M[0][0] += A[0][0];
    M[0][1] += A[0][1];

    for (std::size_t k = 1; k < N-1; ++k)
        for (std::size_t j = k-1; j <= k+1; ++j)
            M[k][j] += A[k][j];

    M[N-1][N-2] += A[N-1][N-2];
    M[N-1][N-1] += A[N-1][N-1];

    return M;
}

template<class T>
tridiag<T> operator*(tridiag<T> M, const T& value)
{
    const std::size_t N = M.size();

    M[0][0] *= value;
    M[0][1] *= value;

    for (std::size_t k = 1; k < N-1; ++k)
        for (std::size_t j = k-1; j <= k+1; ++j)
            M[k][j] *= value;

    M[N-1][N-2] *= value;
    M[N-1][N-1] *= value;

    return M;
}

// add value * Id
template<class T>
tridiag<T> operator+(tridiag<T> M, const T& value)
{
    const std::size_t N = M.size();

    for (std::size_t k = 0; k < N; ++k)
        M[k][k] += value;

    return M;
}

template<class T>
tridiag<T> operator*(const T& value, const tridiag<T>& M)
{
    return M * value;
}

// add value * Id
template<class T>
tridiag<T> operator+(const T& value, const tridiag<T>& M)
{    
    return M + value;
}

#include <ostream>

template<class T>
std::ostream& operator<<(std::ostream& os, const tridiag<T>& M)
{
    const std::size_t N = M.size();


    os << M[0][0] << " ";
    os << M[0][1] << " ";
    os << std::endl;

    for (std::size_t k = 1; k < N-1; ++k) {
        for (std::size_t j = k-1; j <= k+1; ++j)
            os << M[k][j] << " ";
        os << std::endl;
    }


    os << M[N-1][N-2] << " ";
    os << M[N-1][N-1] << std::endl;

    return os;
}

#endif