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
#ifndef __FloatMatrix_h__
#define __FloatMatrix_h__
#include <cstddef>
#include "basicmaths.h"
/**
* This class contains useful methods for manipulating NxN dimensioned matrices of floats.
*/
class FloatMatrix {
private:
#ifdef ARM_CORTEX
arm_matrix_instance_f32 instance;
#else
float* data;
size_t rows;
size_t columns;
#endif
public:
FloatMatrix();
FloatMatrix(float* data, size_t rows, size_t columns);
FloatMatrix(const float* data, size_t rows, size_t columns);
/** Get the number of elements in this matrix */
size_t getSize() const{
return getRows()*getColumns();
}
size_t getRows() const{
#ifdef ARM_CORTEX
return instance.numRows;
#else
return rows;
#endif
}
size_t getColumns() const{
#ifdef ARM_CORTEX
return instance.numCols;
#else
return columns;
#endif
}
/**
* Clear the array.
* Set all the values in the array to 0.
*/
void clear(){
setAll(0);
}
/**
* Element-wise sum between matrices.
* @param[in] operand2 second operand for the sum
* @param[out] destination the destination array
*/
void add(FloatMatrix operand2, FloatMatrix destination);
/**
* Element-wise sum between arrays.
* Adds each element of **operand2** to the corresponding element in the array.
* @param operand2 second operand for the sum
*/
void add(FloatMatrix operand2); //in-place
/* /\** */
/* * Element-wise sum between matrix and array. */
/* * Sets each element in **destination** to the sum of the corresponding element of the matrix and **operand2** */
/* * @param[in] operand2 second operand for the sum */
/* * @param[out] destination the destination array */
/* *\/ */
/* void add(FloatArray operand2, FloatMatrix destination); */
/* /\** */
/* * Element-wise sum between matrix and array. */
/* * Adds each element of **operand2** to the corresponding element of the matrix. */
/* * @param operand2 second operand for the sum */
/* *\/ */
/* void add(FloatMatrix operand2); //in-place */
/**
* Matrix-scalar sum.
* Adds **scalar** to the values in the array.
* @param scalar value to be added to the array
*/
void add(float scalar);
/**
* Element-wise multiplication between arrays.
* Sets each element in **destination** to the product of the corresponding element of the array and **operand2**
* Multiplying an M x N matrix with an N x P matrix results in an M x P matrix.
* @param[in] operand2 second operand for the product
* @param[out] destination the destination array
*/
void multiply(FloatMatrix operand2, FloatMatrix destination);
/**
* Element-wise multiplication between arrays.
* Multiplies each element in the array by the corresponding element in **operand2**.
* @param operand2 second operand for the sum
*/
void multiply(FloatMatrix operand2); //in-place
/**
* Matrix scalar multiplication.
* Multiplies each value in the matrix by **scalar**.
* @param scalar to be subtracted from the array
*/
void multiply(float scalar);
/**
* Set all the values in the array.
* Sets all the elements of the array to **value**.
* @param[in] value all the elements are set to this value.
*/
void setAll(float value);
/**
* Casting operator to float*
* @return a float* pointer to the data stored in the FloatMatrix
*/
operator float*(){
return getData();
}
float* operator [](const int index){
return &getData()[index*getColumns()];
}
/**
* Get the data stored in the FloatMatrix.
* @return a float* pointer to the data stored in the FloatMatrix
*/
float* getData(){
#ifdef ARM_CORTEX
return instance.pData;
#else
return data;
#endif
}
/**
* Get a single float stored in the FloatMatrix.
* @return the float stored at index **row** and **col**
*/
float getElement(int row, int col){
return getData()[row*getColumns()+col];
}
/**
* Set a single float in the FloatMatrix.
*/
void setElement(int row, int col, float value){
getData()[row*getColumns()+col] = value;
}
void softmax(FloatMatrix destination);
void softmax(){
softmax(*this);
}
void sigmoid(FloatMatrix destination);
void sigmoid(){
sigmoid(*this);
}
/**
* Copies the content of this matrix to another matrix.
* The other matrix needs to be at least as big as this matrix.
* @param[out] destination the destination matrix
*/
void copyTo(FloatMatrix destination);
/**
* Copies the content of another matrix into this matrix.
* This matrix needs to be at least as big as the other matrix.
* @param[in] source the source matrix
*/
void copyFrom(FloatMatrix source);
/**
* Creates a new FloatMatrix.
* Allocates rows*columns*sizeof(float) bytes of memory and returns a FloatMatrix that points to it.
* @param rows the number of rows of the new FloatMatrix.
* @param columns the number of columns of the new FloatMatrix.
* @return a FloatMatrix which **data** point to the newly allocated memory and **rows** and **columns** initialized to the proper values.
* @remarks a FloatMatrix created with this method should be destroyed invoking the FloatMatrix::destroy() method.
*/
static FloatMatrix create(size_t rows, size_t columns);
/**
* Destroys a FloatMatrix created with the create() method.
* @param array the FloatMatrix to be destroyed.
* @remarks the FloatMatrix object passed as an argument should not be used again after invoking this method.
* @remarks a FloatMatrix object that has not been created by the FloatMatrix::create() method might cause an exception if passed as an argument to this method.
*/
static void destroy(FloatMatrix array);
};
#endif // __FloatMatrix_h__