Skip to content

Commit 1e8d981

Browse files
author
Martin D. Weinberg
committed
Fix tensor-4 Eigen->Python conversion typo
1 parent 065ec9e commit 1e8d981

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

pyEXP/TensorToArray.H

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ py::array_t<T> make_ndarray3(Eigen::Tensor<T, 3>& mat)
1111
// Check rank
1212
if (dims.size() != 3) {
1313
std::ostringstream sout;
14-
sout << "make_ndarray: tensor rank must be 3, found " << dims.size();
14+
sout << "make_ndarray3: tensor rank must be 3, found " << dims.size();
1515
throw std::runtime_error(sout.str());
1616
}
1717

@@ -37,10 +37,17 @@ py::array_t<T> make_ndarray4(Eigen::Tensor<T, 4>& mat)
3737
// Check rank
3838
if (dims.size() != 4) {
3939
std::ostringstream sout;
40-
sout << "make_ndarray: tensor rank must be 4, found " << dims.size();
40+
sout << "make_ndarray4: tensor rank must be 4, found " << dims.size();
4141
throw std::runtime_error(sout.str());
4242
}
4343

44+
// Sanity check
45+
for (int i=0; i<mat.size(); i++) {
46+
if (isnan(std::abs(mat.data()[i]))) {
47+
throw std::runtime_error("make_ndarray4: NaN encountered");
48+
}
49+
}
50+
4451
// Make the memory mapping
4552
return py::array_t<T>
4653
(
@@ -107,11 +114,11 @@ Eigen::Tensor<T, 4> make_tensor4(py::array_t<T> array)
107114

108115
// Build result tensor with col-major ordering
109116
Eigen::Tensor<T, 4> tensor(shape[0], shape[1], shape[2], shape[3]);
110-
for (int i=0, l=0; i < shape[0]; i++) {
117+
for (int i=0, c=0; i < shape[0]; i++) {
111118
for (int j=0; j < shape[1]; j++) {
112119
for (int k=0; k < shape[2]; k++) {
113-
for (int l=0; l < shape[3]; k++) {
114-
tensor(i, j, k, l) = data[l++];
120+
for (int l=0; l < shape[3]; l++, c++) {
121+
tensor(i, j, k, l) = data[c];
115122
}
116123
}
117124
}

0 commit comments

Comments
 (0)