aboutsummaryrefslogtreecommitdiff
path: root/apps/px4/attitude_estimator_bm/matrix.h
blob: 613a2d0811a0372202d00458c945940b23ad54e7 (plain) (blame)
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
/*
 * matrix.h
 *
 *  Created on: 18.11.2010
 *      Author: Laurens Mackay
 */

#ifndef MATRIX_H_
#define MATRIX_H_

typedef float m_elem;

typedef struct {
	int rows;
	int cols;
	m_elem *a;
} matrix_t;

typedef struct {
	float x;
	float y;
	float z;
} float_vect3;

#define M(m,i,j) m.a[m.cols*i+j]

///*  This is the datatype used for the math and non-type specific ops.  */
//
//matrix_t matrix_create(const int rows, const int cols, m_elem * a);
///*  matrix C = matrix A + matrix B , both of size m x n   */
//void matrix_add(const matrix_t a, const matrix_t b, matrix_t c);
//
///*  matrix C = matrix A - matrix B , all of size m x n  */
//void matrix_sub(const matrix_t a, const matrix_t b, matrix_t c);
//
///*  matrix C = matrix A x matrix B , A(a_rows x a_cols), B(a_cols x b_cols) */
//void matrix_mult(const matrix_t a, const matrix_t b, matrix_t c);
//
//void matrix_mult_scalar(const float f, const matrix_t a, matrix_t c);
//
//void matrix_mult_element(const matrix_t a, const matrix_t b, matrix_t c);
//
///* matrix C = A*B'*/
//void matrix_mult_trans(const matrix_t a, const matrix_t b, matrix_t c);


static inline matrix_t matrix_create(const int rows, const int cols, m_elem *a)
{
	matrix_t ret;
	ret.rows = rows;
	ret.cols = cols;
	ret.a = a;
	return ret;
}

static inline void matrix_add(const matrix_t a, const matrix_t b, matrix_t c)
{
	if (a.rows != c.rows || a.cols != c.cols || b.rows != c.rows || b.cols
	    != c.cols) {
		//debug_message_buffer("matrix_add: Dimension mismatch");
	}

	for (int i = 0; i < c.rows; i++) {
		for (int j = 0; j < c.cols; j++) {
			M(c, i, j) = M(a, i, j) + M(b, i, j);
		}

	}
}

static inline void matrix_sub(const matrix_t a, const matrix_t b, matrix_t c)
{
	if (a.rows != c.rows || a.cols != c.cols || b.rows != c.rows || b.cols
	    != c.cols) {
		//debug_message_buffer("matrix_sub: Dimension mismatch");
	}

	for (int i = 0; i < c.rows; i++) {
		for (int j = 0; j < c.cols; j++) {
			M(c, i, j) = M(a, i, j) - M(b, i, j);
		}

	}
}

static inline void matrix_mult(const matrix_t a, const matrix_t b, matrix_t c)
{
	if (a.rows != c.rows || b.cols != c.cols || a.cols != b.rows) {
		//debug_message_buffer("matrix_mult: Dimension mismatch");
	}

	for (int i = 0; i < a.rows; i++) {
		for (int j = 0; j < b.cols; j++) {
			M(c, i, j) = 0;

			for (int k = 0; k < a.cols; k++) {
				M(c, i, j) += M(a, i, k) * M(b, k, j);
			}
		}

	}
}

static inline void matrix_mult_trans(const matrix_t a, const matrix_t b, matrix_t c)
{

	if (a.rows != c.rows || b.rows != c.cols || a.cols != b.cols) {
		//debug_message_buffer("matrix_mult: Dimension mismatch");
	}

	for (int i = 0; i < a.rows; i++) {
		for (int j = 0; j < b.cols; j++) {
			M(c, i, j) = 0;

			for (int k = 0; k < a.cols; k++) {
				M(c, i, j) += M(a, i, k) * M(b, j, k);
			}
		}

	}

}

static inline void matrix_mult_scalar(const float f, const matrix_t a, matrix_t c)
{
	if (a.rows != c.rows || a.cols != c.cols) {
		//debug_message_buffer("matrix_mult_scalar: Dimension mismatch");
	}

	for (int i = 0; i < c.rows; i++) {
		for (int j = 0; j < c.cols; j++) {
			M(c, i, j) = f * M(a, i, j);
		}

	}
}


static inline void matrix_mult_element(const matrix_t a, const matrix_t b, matrix_t c)
{
	if (a.rows != c.rows || a.cols != c.cols || b.rows != c.rows || b.cols
	    != c.cols) {
		//debug_message_buffer("matrix_mult_element: Dimension mismatch");
	}

	for (int i = 0; i < c.rows; i++) {
		for (int j = 0; j < c.cols; j++) {
			M(c, i, j) = M(a, i, j) * M(b, i, j);
		}

	}
}



#endif /* MATRIX_H_ */