-
Notifications
You must be signed in to change notification settings - Fork 17
/
Matrix.cpp
75 lines (66 loc) · 2.08 KB
/
Matrix.cpp
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
/* Copyright (C) 2018-2019 Thomas Jespersen, TKJ Electronics. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the MIT License
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the MIT License for further details.
*
* Contact information
* ------------------------------------------
* Thomas Jespersen, TKJ Electronics
* Web : http://www.tkjelectronics.dk
* e-mail : [email protected]
* ------------------------------------------
*/
#include "Matrix.h"
#include <math.h>
#include <stdlib.h>
#include "Debug.h"
Matrix::Matrix()
{
}
Matrix::~Matrix()
{
}
/* Matrices are stored in memory in Row-major format
* This means that elements next to each other in memory corresponds to elements next to each other of the same row
* Assuming MATLAB syntax: mat(i,j) where i denotes a row index and j and column index of a matrix of size m x n (m rows, n columns)
* Then the corresponding memory entry is given as: mat[n*i + j]
*/
void Matrix_Extract(const float * in, const int in_rows, const int in_cols, const int in_row, const int in_col, const int out_rows, const int out_cols, float * out)
{
/*assert(out_rows <= in_rows);
assert(out_cols <= in_cols);
assert((in_row + out_rows) < in_rows);
assert((in_col + out_cols) < in_cols);*/
for (int m = 0; m < out_rows; m++)
{
for (int n = 0; n < out_cols; n++) {
out[m*out_cols + n] = in[(in_row+m)*in_cols + (in_col+n)];
}
}
}
void Matrix_Round(float * matrix, int rows, int cols)
{
for (int m = 0; m < rows; m++) {
for (int n = 0; n < cols; n++) {
matrix[cols*m + n] = roundf(matrix[cols*m + n] * 10) / 10;
if (matrix[cols*m + n] == -0.f) {
matrix[cols*m + n] = 0.f;
}
}
}
}
void Matrix_Print(float * matrix, int rows, int cols)
{
for (int m = 0; m < rows; m++) {
Debug::print(" ");
for (int n = 0; n < cols; n++) {
Debug::printf("%8.4f ", matrix[cols*m + n]);
}
Debug::print("\n");
}
}