-
Notifications
You must be signed in to change notification settings - Fork 42
/
array_helper.cpp
88 lines (77 loc) · 1.7 KB
/
array_helper.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
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* array_helper.cpp
* peer
*
* Created by Oliver Stegle on 12/28/10.
* Copyright 2010 __MyCompanyName__. All rights reserved.
*
*/
#include "array_helper.h"
PMatrix array2matrix(const float32_t* matrix,int32_t rows,int32_t cols)
{
//create a matrix from a double array
PMatrix m = PMatrix(rows,cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
{
//m(i,j) = matrix[i*cols+j];
m(i,j) = matrix[j*rows+i];
}
return m;
}
PMatrix array2matrix(const float64_t* matrix,int32_t rows,int32_t cols)
{
//create a matrix from a double array
PMatrix m = PMatrix(rows,cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
{
//m(i,j) = matrix[i*cols+j];
m(i,j) = matrix[j*rows+i];
}
return m;
}
void matrix2array(const PMatrix m,float32_t** matrix, int32_t* rows, int32_t*cols)
{
int size = m.rows()*m.cols();
//allocate memory
(*matrix) = new float32_t[size];
//set dimensions
(*rows) = m.rows();
(*cols) = m.cols();
for (int i=0;i<m.rows();i++)
for(int j=0;j<m.cols();j++)
{
//(*matrix)[i*m.cols()+j] = m(i,j);
(*matrix)[j*m.rows()+i] = m(i,j);
}
}
void matrix2array(const PMatrix m,float64_t** matrix, int32_t* rows, int32_t*cols)
{
int size = m.rows()*m.cols();
//allocate memory
(*matrix) = new float64_t[size];
//set dimensions
(*rows) = m.rows();
(*cols) = m.cols();
for (int i=0;i<m.rows();i++)
for(int j=0;j<m.cols();j++)
{
//(*matrix)[i*m.cols()+j] = m(i,j);
(*matrix)[j*m.rows()+i] = m(i,j);
}
}
PMatrix randn(int n, int m)
/* create a randn matrix */
{
PMatrix rv(n,m);
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) {
rv(i,j) = randomreal();
}
return rv;
}
bool isnull(const PMatrix m)
{
return (m.cols()==0) & (m.rows()==0);
}