Skip to content

Commit e6c0c70

Browse files
authored
Add AmiVector / AmiVectorArray formatters (#2878)
Add formatting functions for AmiVector / AmiVectorArray for debugging purposes.
1 parent aac0e1f commit e6c0c70

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

include/amici/vector.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef AMICI_VECTOR_H
22
#define AMICI_VECTOR_H
33

4+
#include <ostream>
45
#include <type_traits>
56
#include <vector>
67

@@ -281,6 +282,23 @@ class AmiVector {
281282
void synchroniseNVector(SUNContext sunctx);
282283
};
283284

285+
/**
286+
* @brief Output formatter for AmiVector.
287+
* @param os output stream
288+
* @param v AmiVector to output
289+
* @return os
290+
*/
291+
inline std::ostream& operator<<(std::ostream& os, AmiVector const& v) {
292+
os << "[";
293+
for (int i = 0; i < v.getLength(); ++i) {
294+
if (i > 0)
295+
os << ", ";
296+
os << v.at(i);
297+
}
298+
os << "]";
299+
return os;
300+
}
301+
284302
/**
285303
* @brief AmiVectorArray class.
286304
*
@@ -436,6 +454,23 @@ class AmiVectorArray {
436454
std::vector<N_Vector> nvec_array_;
437455
};
438456

457+
/**
458+
* @brief Output formatter for AmiVectorArray.
459+
* @param os output stream
460+
* @param arr AmiVectorArray to output
461+
* @return os
462+
*/
463+
inline std::ostream& operator<<(std::ostream& os, AmiVectorArray const& arr) {
464+
os << "[";
465+
for (int i = 0; i < arr.getLength(); ++i) {
466+
if (i > 0)
467+
os << ", ";
468+
os << arr[i];
469+
}
470+
os << "]";
471+
return os;
472+
}
473+
439474
/**
440475
* @brief Computes z = a*x + b*y
441476
* @param a coefficient for x

tests/cpp/unittests/testMisc.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ TEST_F(AmiVectorTest, Vector)
526526
N_Vector nvec = av.getNVector();
527527
for (int i = 0; i < av.getLength(); ++i)
528528
ASSERT_EQ(av.at(i), NV_Ith_S(nvec, i));
529+
530+
std::stringstream ss;
531+
ss << av;
532+
ASSERT_EQ(ss.str(), "[1, 2, 4, 3]");
529533
}
530534

531535
TEST_F(AmiVectorTest, VectorArray)
@@ -547,6 +551,10 @@ TEST_F(AmiVectorTest, VectorArray)
547551
for (int j = 0; j < av.getLength(); ++j)
548552
ASSERT_EQ(flattened.at(i * av.getLength() + j), av.at(j));
549553
}
554+
555+
std::stringstream ss;
556+
ss << ava;
557+
ASSERT_EQ(ss.str(), "[[1, 2, 4, 3], [4, 1, 2, 3], [4, 4, 2, 1]]");
550558
}
551559

552560
class SunMatrixWrapperTest : public ::testing::Test {

0 commit comments

Comments
 (0)