Statistics
| Branch: | Revision:

adafruit_bno055 / utility / matrix.h @ fd9de024

History | View | Annotate | Download (4.889 KB)

1
/*
2
    Inertial Measurement Unit Maths Library
3
    Copyright (C) 2013-2014  Samuel Cowen
4
    www.camelsoftware.com
5

6
    This program is free software: you can redistribute it and/or modify
7
    it under the terms of the GNU General Public License as published by
8
    the Free Software Foundation, either version 3 of the License, or
9
    (at your option) any later version.
10

11
    This program is distributed in the hope that it will be useful,
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU General Public License for more details.
15

16
    You should have received a copy of the GNU General Public License
17
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19

    
20
#ifndef IMUMATH_MATRIX_HPP
21
#define IMUMATH_MATRIX_HPP
22

    
23
#include <string.h>
24
#include <stdint.h>
25

    
26
#include "vector.h"
27

    
28
namespace imu
29
{
30

    
31

    
32
template <uint8_t N> class Matrix
33
{
34
public:
35
    Matrix()
36
    {
37
        memset(_cell_data, 0, N*N*sizeof(double));
38
    }
39

    
40
    Matrix(const Matrix &m)
41
    {
42
        for (int ij = 0; ij < N*N; ++ij)
43
        {
44
            _cell_data[ij] = m._cell_data[ij];
45
        }
46
    }
47

    
48
    ~Matrix()
49
    {
50
    }
51

    
52
    Matrix& operator=(const Matrix& m)
53
    {
54
        for (int ij = 0; ij < N*N; ++ij)
55
        {
56
            _cell_data[ij] = m._cell_data[ij];
57
        }
58
        return *this;
59
    }
60

    
61
    Vector<N> row_to_vector(int i) const
62
    {
63
        Vector<N> ret;
64
        for (int j = 0; j < N; j++)
65
        {
66
            ret[j] = cell(i, j);
67
        }
68
        return ret;
69
    }
70

    
71
    Vector<N> col_to_vector(int j) const
72
    {
73
        Vector<N> ret;
74
        for (int i = 0; i < N; i++)
75
        {
76
            ret[i] = cell(i, j);
77
        }
78
        return ret;
79
    }
80

    
81
    void vector_to_row(const Vector<N>& v, int i)
82
    {
83
        for (int j = 0; j < N; j++)
84
        {
85
            cell(i, j) = v[j];
86
        }
87
    }
88

    
89
    void vector_to_col(const Vector<N>& v, int j)
90
    {
91
        for (int i = 0; i < N; i++)
92
        {
93
            cell(i, j) = v[i];
94
        }
95
    }
96

    
97
    double operator()(int i, int j) const
98
    {
99
        return cell(i, j);
100
    }
101
    double& operator()(int i, int j)
102
    {
103
        return cell(i, j);
104
    }
105

    
106
    double cell(int i, int j) const
107
    {
108
        return _cell_data[i*N+j];
109
    }
110
    double& cell(int i, int j)
111
    {
112
        return _cell_data[i*N+j];
113
    }
114

    
115

    
116
    Matrix operator+(const Matrix& m) const
117
    {
118
        Matrix ret;
119
        for (int ij = 0; ij < N*N; ++ij)
120
        {
121
            ret._cell_data[ij] = _cell_data[ij] + m._cell_data[ij];
122
        }
123
        return ret;
124
    }
125

    
126
    Matrix operator-(const Matrix& m) const
127
    {
128
        Matrix ret;
129
        for (int ij = 0; ij < N*N; ++ij)
130
        {
131
            ret._cell_data[ij] = _cell_data[ij] - m._cell_data[ij];
132
        }
133
        return ret;
134
    }
135

    
136
    Matrix operator*(double scalar) const
137
    {
138
        Matrix ret;
139
        for (int ij = 0; ij < N*N; ++ij)
140
        {
141
            ret._cell_data[ij] = _cell_data[ij] * scalar;
142
        }
143
        return ret;
144
    }
145

    
146
    Matrix operator*(const Matrix& m) const
147
    {
148
        Matrix ret;
149
        for (int i = 0; i < N; i++)
150
        {
151
            Vector<N> row = row_to_vector(i);
152
            for (int j = 0; j < N; j++)
153
            {
154
                ret(i, j) = row.dot(m.col_to_vector(j));
155
            }
156
        }
157
        return ret;
158
    }
159

    
160
    Matrix transpose() const
161
    {
162
        Matrix ret;
163
        for (int i = 0; i < N; i++)
164
        {
165
            for (int j = 0; j < N; j++)
166
            {
167
                ret(j, i) = cell(i, j);
168
            }
169
        }
170
        return ret;
171
    }
172

    
173
    Matrix<N-1> minor_matrix(int row, int col) const
174
    {
175
        Matrix<N-1> ret;
176
        for (int i = 0, im = 0; i < N; i++)
177
        {
178
            if (i == row)
179
                continue;
180

    
181
            for (int j = 0, jm = 0; j < N; j++)
182
            {
183
                if (j != col)
184
                {
185
                    ret(im, jm++) = cell(i, j);
186
                }
187
            }
188
            im++;
189
        }
190
        return ret;
191
    }
192

    
193
    double determinant() const
194
    {
195
        // specialization for N == 1 given below this class
196
        double det = 0.0, sign = 1.0;
197
        for (int i = 0; i < N; ++i, sign = -sign)
198
            det += sign * cell(0, i) * minor_matrix(0, i).determinant();
199
        return det;
200
    }
201

    
202
    Matrix invert() const
203
    {
204
        Matrix ret;
205
        double det = determinant();
206

    
207
        for (int i = 0; i < N; i++)
208
        {
209
            for (int j = 0; j < N; j++)
210
            {
211
                ret(i, j) = minor_matrix(j, i).determinant() / det;
212
                if ((i+j)%2 == 1)
213
                    ret(i, j) = -ret(i, j);
214
            }
215
        }
216
        return ret;
217
    }
218

    
219
    double trace() const
220
    {
221
        double tr = 0.0;
222
        for (int i = 0; i < N; ++i)
223
            tr += cell(i, i);
224
        return tr;
225
    }
226

    
227
private:
228
    double _cell_data[N*N];
229
};
230

    
231

    
232
template<>
233
inline double Matrix<1>::determinant() const
234
{
235
    return cell(0, 0);
236
}
237

    
238
};
239

    
240
#endif
241