-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.h
110 lines (92 loc) · 2.34 KB
/
matrix.h
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
#ifndef _MATRIX_H_
#define _MATRIX_H_
#include <cstdlib>
#include <iostream>
// Matrix Structure
class Matrix {
public:
Matrix() {
//constructor
min = 9999999999;
max = 0;
}
void setDimension(int w, int h) {
width = w;
height = h;
}
void copyFrom(Matrix const & orig, float padVal = 0.0f) {
for (unsigned int i = 0; i < height; ++i) {
for (unsigned int j = 0; j < width; ++j) {
float val = padVal;
if (i < orig.height && j < orig.width) {
val = orig.get(j, i);
}
get(j,i) = val;
}
}
}
void set(unsigned int i, unsigned int j, float store) {
elements[((i * width) + j)] = store; //i is row, j is column
}
float get(unsigned int i, unsigned int j) const {
return elements[(i * width) + j]; //i is row, j is column
}
float & get(unsigned int i, unsigned int j) {
return elements[(i * width) + j]; //i is row, j is column
}
void updateMinMax()
{
//goes through the elements and determines the smallest and largest value
for (unsigned int i = 0; i < width; ++i)
{
for (unsigned int j = 0; j < height; ++j)
{
if (get(j,i) < min)
min = get(j,i);
if (get(j,i) > max)
max = get(j,i);
}
}
}
// Prints the matrix to the screen
void printMatrix()
{
for (unsigned int i = 0; i < width; ++i)
{
for (unsigned int j = 0; j < height; ++j)
{
std::cout << get(j,i) << ",";
}
std::cout << std::endl;
}
}
//quantization class values (if applicable)
unsigned int quantization;
unsigned int width;
unsigned int height;
//min/max values, these get updated via the updateMinMax() method
float min;
float max;
//number of elements between the beginnings of adjacent
// rows in the memory layout (useful for representing sub-matrices)
unsigned int pitch;
//Pointer to the first element of the matrix represented
float* elements;
};
// Allocate a random matrix of dimensions height*width
inline Matrix AllocateMatrix(int height, int width, int q)
{
Matrix M;
M.width = M.pitch = width;
M.height = height;
int size = M.width * M.height;
M.quantization = q;
M.elements = NULL;
M.elements = (float*) std::malloc(size*sizeof(float));
for (unsigned int i = 0; i < M.height * M.width; ++i)
{
M.elements[i] = 0;
}
return M;
}
#endif // _MATRIX_H_