forked from spotify/echoprint-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixUtility.cxx
More file actions
52 lines (40 loc) · 1.29 KB
/
MatrixUtility.cxx
File metadata and controls
52 lines (40 loc) · 1.29 KB
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
//
// echoprint-codegen
// Copyright 2011 The Echo Nest Corporation. All rights reserved.
//
#include "MatrixUtility.h"
// http://www.boost.org/doc/libs/1_35_0/libs/numeric/ublas/doc/matrix.htm
namespace MatrixUtility {
bool TextFileOutput(const matrix_f& A, const char* filename) {
FILE *matrix_file = fopen(filename, "w");
bool success = (matrix_file != NULL);
if (success) {
const float *d = &A.data()[0];
for (uint i = 0; i < A.size1(); i++) {
for (uint j = 0; j < A.size2(); j++)
fprintf(matrix_file, "%2.3f ", d[i*A.size2() + j]);
fprintf(matrix_file, "\n");
}
fclose(matrix_file);
}
return success;
}
bool FileOutput(const matrix_f& A, const char* filename) {
FILE *matrix_file = fopen(filename, "wb");
bool success = (matrix_file != NULL);
if (success) {
uint mm = A.size1();
uint mn = A.size2();
fwrite(&mm, sizeof(int), 1, matrix_file);
fwrite(&mn, sizeof(int), 1, matrix_file);
for (uint i = 0; i< A.size1(); i++) {
for(uint j=0;j<A.size2(); j++) {
const float d = A(i, j);
fwrite(&d, sizeof(float), 1, matrix_file);
}
}
fclose(matrix_file);
}
return success;
}
} // namespace