forked from ykai16/diHMM
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArma_Numpy_Conversions.h
71 lines (56 loc) · 1.69 KB
/
Arma_Numpy_Conversions.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
//
// Created by Stephanos Tsoucas on 6/14/17.
//
#ifndef BOOSTPYTHONHELLOWORLD_ARMA_NUMPY_CONVERSIONS_H
#define BOOSTPYTHONHELLOWORLD_ARMA_NUMPY_CONVERSIONS_H
#include <armadillo>
#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace p = boost::python;
namespace np = boost::python::numpy;
namespace a = arma;
np::ndarray from_vec(const a::vec &v) {
p::tuple shape = p::make_tuple(v.size());
np::dtype dtype = np::dtype::get_builtin<double>();
np::ndarray n = np::zeros(shape, dtype);
for (int i = 0; i < v.size(); i++) {
n[i] = v(i);
}
return n;
}
np::ndarray from_mat(const a::mat &m) {
p::tuple shape = p::make_tuple(m.n_rows, m.n_cols);
np::dtype dtype = np::dtype::get_builtin<double>();
np::ndarray n = np::zeros(shape, dtype);
for (int i = 0; i < m.n_rows; i++) {
for (int j = 0; j < m.n_cols; j++) {
n[i][j] = m(i, j);
}
}
return n;
}
np::ndarray from_cube(const a::cube &c) {
p::tuple shape = p::make_tuple(c.n_slices, c.n_rows, c.n_cols);
np::dtype dtype = np::dtype::get_builtin<double>();
np::ndarray n = np::zeros(shape, dtype);
for (int slice = 0; slice < c.n_slices; slice++) {
for (int i = 0; i < c.n_rows; i++) {
for (int j = 0; j < c.n_cols; j++) {
n[slice][i][j] = c(i, j, slice);
}
}
}
return n;
}
np::ndarray
from_vec_of_pairs(const std::vector<std::pair<a::uword, a::uword>> &v) {
p::tuple shape = p::make_tuple(v.size(), 2);
np::dtype dtype = np::dtype::get_builtin<int>();
np::ndarray n = np::zeros(shape, dtype);
for (int i = 0; i < v.size(); i++) {
n[i][0] = v.at(i).first;
n[i][1] = v.at(i).second;
}
return n;
}
#endif // BOOSTPYTHONHELLOWORLD_ARMA_NUMPY_CONVERSIONS_H