OpenWareLaboratory
FloatMatrix.cpp
Go to the documentation of this file.
1 #include "FloatMatrix.h"
2 #include "basicmaths.h"
3 #include "message.h"
4 #include <cstring>
5 
6 #ifdef ARM_CORTEX
8  instance.numRows = 0;
9  instance.numCols = 0;
10  instance.pData = NULL;
11 }
12 FloatMatrix::FloatMatrix(float* dt, size_t rs, size_t cs){
13  arm_mat_init_f32(&instance, rs, cs, dt);
14 }
15 FloatMatrix::FloatMatrix(const float* dt, size_t rs, size_t cs){
16  arm_mat_init_f32(&instance, rs, cs, (float*)dt);
17 }
18 #else
20  : data(NULL), rows(0), columns(0) {}
21 FloatMatrix::FloatMatrix(float* dt, size_t rs, size_t cs)
22  : data(dt), rows(rs), columns(cs) {}
23 FloatMatrix::FloatMatrix(const float* dt, size_t rs, size_t cs)
24  : data((float*)dt), rows(rs), columns(cs) {}
25 #endif
26 
27 void FloatMatrix::add(FloatMatrix operand2, FloatMatrix destination){
28 #ifdef ARM_CORTEX
29  arm_mat_add_f32(&instance, &operand2.instance, &destination.instance);
30 #else
31  // ASSERT(getColumns() == operand2.getColumns(), "Incompatible column size");
32  // ASSERT(getRows() == operand2.getRows(), "Incompatible row size");
33  // for(size_t i=0; i<getRows(); i++)
34  // for(size_t j=0; j<operand2.getColumns(); j++)
35  // destination.setElement(i, j, getElement(i, j)+operand2.getElement(i, j));
36  // element wise addition
37  ASSERT(getSize() == operand2.getSize(), "Incompatible matrix sizes");
38  for(size_t i=0; i<getSize(); i++)
39  destination.data[i] = data[i] + operand2.data[i];;
40 #endif
41 }
42 
44  add(operand2, *this);
45 }
46 
47 void FloatMatrix::add(float scalar){
48  size_t size = getSize();
49  float* data = getData();
50  for(size_t i=0; i<size; i++)
51  data[i] += scalar;
52 }
53 
54 void FloatMatrix::multiply(FloatMatrix operand2, FloatMatrix destination){
55 #ifdef ARM_CORTEX
56  arm_mat_mult_f32(&instance, &operand2.instance, &destination.instance);
57 #else
58  ASSERT(getColumns() == operand2.getRows(), "Incompatible matrix sizes");
59  ASSERT(destination.rows >= rows, "Insufficient rows in destination");
60  ASSERT(destination.columns >= operand2.columns, "Insufficient columns in destination");
61 
62  // FloatMatrix operand1 = *this;
63  // for(size_t i=0; i<rows; i++){
64  // for(size_t j=0; j<operand2.columns; j++){
65  // destination[i][j] = 0;
66  // for(size_t k=0; k<operand2.rows; k++){
67  // destination[i][j] += operand1[i][k] * operand2[k][j];
68  // }
69  // }
70  // }
71  for(size_t i=0; i<getRows(); i++){
72  for(size_t j=0; j<operand2.getColumns(); j++){
73  destination.setElement(i, j, 0);
74  for(size_t k=0; k<operand2.getRows(); k++){
75  destination.setElement(i, j, destination.getElement(i, j) + getElement(i, k) * operand2.getElement(k, j));
76  }
77  }
78  }
79 #endif
80 }
81 
83  multiply(operand2, *this);
84 }
85 
86 void FloatMatrix::multiply(float scalar){
87  size_t size = getSize();
88  float* data = getData();
89  for(size_t i=0; i<size; i++)
90  data[i] *= scalar;
91 }
92 
93 void FloatMatrix::setAll(float value){
94  size_t size = getSize();
95  float* data = getData();
96  for(size_t i=0; i<size; i++)
97  data[i] = value;
98 }
99 
101  ASSERT(destination.getSize() >= getSize(), "Destination size too small");
102  float* src = getData();
103  float* dest = destination.getData();
104  size_t size = getSize();
105  float m = -INFINITY;
106  for(size_t i = 0; i < size; i++) {
107  if(src[i] > m)
108  m = src[i];
109  }
110  float sum = 0.0;
111  for(size_t i = 0; i < size; i++)
112  sum += expf(src[i] - m);
113  float offset = m + logf(sum);
114  for(size_t i = 0; i < size; i++)
115  dest[i] = expf(src[i] - offset);
116 }
117 
118 
120  ASSERT(destination.getSize() >= getSize(), "Destination size too small");
121  float* src = getData();
122  float* dest = destination.getData();
123  size_t size = getSize();
124  for(size_t i = 0; i < size; i++)
125  dest[i] = 1.0f / (1 + expf(-src[i]));
126 }
127 
128 void FloatMatrix::copyTo(FloatMatrix destination) {
129  ASSERT(destination.getSize() >= getSize(), "Destination size too small");
130  memcpy((void*)destination.getData(), (void*)getData(), getSize() * sizeof(float));
131 }
132 
134  ASSERT(getSize() >= source.getSize(), "Destination size too small");
135  memcpy((void*)getData(), (void*)source.getData(), source.getSize() * sizeof(float));
136 }
137 
138 FloatMatrix FloatMatrix::create(size_t m, size_t n){
139  FloatMatrix fm(new float[m*n], m, n);
140  fm.clear();
141  return fm;
142 }
143 
145 #ifdef ARM_CORTEX
146  delete[] array.instance.pData;
147 #else
148  delete[] array.data;
149 #endif
150 };
151 
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
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
#define ASSERT(cond, msg)
Definition: message.h:16