OpenWareLaboratory
FloatMatrix.h
Go to the documentation of this file.
1 #ifndef __FloatMatrix_h__
2 #define __FloatMatrix_h__
3 
4 #include <cstddef>
5 #include "basicmaths.h"
6 
10 class FloatMatrix {
11 private:
12 #ifdef ARM_CORTEX
13  arm_matrix_instance_f32 instance;
14 #else
15  float* data;
16  size_t rows;
17  size_t columns;
18 #endif
19 public:
20  FloatMatrix();
21  FloatMatrix(float* data, size_t rows, size_t columns);
22  FloatMatrix(const float* data, size_t rows, size_t columns);
23 
25  size_t getSize() const{
26  return getRows()*getColumns();
27  }
28 
29  size_t getRows() const{
30 #ifdef ARM_CORTEX
31  return instance.numRows;
32 #else
33  return rows;
34 #endif
35  }
36 
37  size_t getColumns() const{
38 #ifdef ARM_CORTEX
39  return instance.numCols;
40 #else
41  return columns;
42 #endif
43  }
44 
49  void clear(){
50  setAll(0);
51  }
52 
58  void add(FloatMatrix operand2, FloatMatrix destination);
59 
65  void add(FloatMatrix operand2); //in-place
66 
67  /* /\** */
68  /* * Element-wise sum between matrix and array. */
69  /* * Sets each element in **destination** to the sum of the corresponding element of the matrix and **operand2** */
70  /* * @param[in] operand2 second operand for the sum */
71  /* * @param[out] destination the destination array */
72  /* *\/ */
73  /* void add(FloatArray operand2, FloatMatrix destination); */
74 
75  /* /\** */
76  /* * Element-wise sum between matrix and array. */
77  /* * Adds each element of **operand2** to the corresponding element of the matrix. */
78  /* * @param operand2 second operand for the sum */
79  /* *\/ */
80  /* void add(FloatMatrix operand2); //in-place */
81 
87  void add(float scalar);
88 
96  void multiply(FloatMatrix operand2, FloatMatrix destination);
97 
103  void multiply(FloatMatrix operand2); //in-place
104 
110  void multiply(float scalar);
111 
117  void setAll(float value);
118 
123  operator float*(){
124  return getData();
125  }
126 
127  float* operator [](const int index){
128  return &getData()[index*getColumns()];
129  }
130 
135  float* getData(){
136 #ifdef ARM_CORTEX
137  return instance.pData;
138 #else
139  return data;
140 #endif
141  }
142 
147  float getElement(int row, int col){
148  return getData()[row*getColumns()+col];
149  }
150 
154  void setElement(int row, int col, float value){
155  getData()[row*getColumns()+col] = value;
156  }
157 
158  void softmax(FloatMatrix destination);
159 
160  void softmax(){
161  softmax(*this);
162  }
163 
164  void sigmoid(FloatMatrix destination);
165 
166  void sigmoid(){
167  sigmoid(*this);
168  }
169 
175  void copyTo(FloatMatrix destination);
176 
182  void copyFrom(FloatMatrix source);
183 
192  static FloatMatrix create(size_t rows, size_t columns);
193 
200  static void destroy(FloatMatrix array);
201 };
202 
203 #endif // __FloatMatrix_h__
This class contains useful methods for manipulating NxN dimensioned matrices of floats.
Definition: FloatMatrix.h:10
static FloatMatrix create(size_t rows, size_t columns)
Creates a new FloatMatrix.
float getElement(int row, int col)
Get a single float stored in the FloatMatrix.
Definition: FloatMatrix.h:147
void clear()
Clear the array.
Definition: FloatMatrix.h:49
float * operator[](const int index)
Definition: FloatMatrix.h:127
size_t getColumns() const
Definition: FloatMatrix.h:37
size_t getRows() const
Definition: FloatMatrix.h:29
void softmax()
Definition: FloatMatrix.h:160
static void destroy(FloatMatrix array)
Destroys a FloatMatrix created with the create() method.
void multiply(FloatMatrix operand2, FloatMatrix destination)
Element-wise multiplication between arrays.
Definition: FloatMatrix.cpp:54
void sigmoid()
Definition: FloatMatrix.h:166
void setAll(float value)
Set all the values in the array.
Definition: FloatMatrix.cpp:93
size_t getSize() const
Get the number of elements in this matrix.
Definition: FloatMatrix.h:25
void add(FloatMatrix operand2, FloatMatrix destination)
Element-wise sum between matrices.
Definition: FloatMatrix.cpp:27
void copyFrom(FloatMatrix source)
Copies the content of another matrix into this matrix.
void setElement(int row, int col, float value)
Set a single float in the FloatMatrix.
Definition: FloatMatrix.h:154
void copyTo(FloatMatrix destination)
Copies the content of this matrix to another matrix.
float * getData()
Get the data stored in the FloatMatrix.
Definition: FloatMatrix.h:135