Skip to content

Commit

Permalink
fix weird diag
Browse files Browse the repository at this point in the history
  • Loading branch information
alkino committed Aug 29, 2023
1 parent 45109a3 commit 5e72ba0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
22 changes: 14 additions & 8 deletions src/ivoc/ocmatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ void OcFullMatrix::getdiag(int k, Vect* out) {
#endif
}
} else {
// Yes for negative diagonal we set the vector from the middle
// The output vector should ALWAYS be the size of biggest diagonal
for (i = -k, j = 0; i < row && j < col; ++i, ++j) {
#ifdef WIN32
v_elem(out, j) = m_entry(m_, i, j);
v_elem(out, i) = m_entry(m_, i, j);
#else
out->elem(j) = m_entry(m_, i, j);
out->elem(i) = m_entry(m_, i, j);
#endif
}
}
Expand Down Expand Up @@ -228,11 +230,13 @@ void OcFullMatrix::setdiag(int k, Vect* in) {
#endif
}
} else {
// Yes for negative diagonal we set the vector from the middle
// The input vector should ALWAYS be the size of biggest diagonal
for (i = -k, j = 0; i < row && j < col; ++i, ++j) {
#ifdef WIN32
m_set_val(m_, i, j, v_elem(in, j));
m_set_val(m_, i, j, v_elem(in, i));
#else
m_set_val(m_, i, j, in->elem(j));
m_set_val(m_, i, j, in->elem(i));
#endif
}
}
Expand Down Expand Up @@ -491,15 +495,17 @@ void OcSparseMatrix::setdiag(int k, Vect* in) {
}
} else {
for (i = -k, j = 0; i < row && j < col; ++i, ++j) {
// Yes for negative diagonal we set the vector from the middle
// The input vector should ALWAYS be the size of biggest diagonal
if ((p = pelm(i, j)) != nullptr) {
#ifdef WIN32
*p = v_elem(in, j);
*p = v_elem(in, i);
} else if (v_elem(in, i)) {
sp_set_val(m_, i, j, v_elem(in, j));
sp_set_val(m_, i, j, v_elem(in, i));
#else
*p = in->elem(j);
*p = in->elem(i);
} else if (in->elem(i)) {
sp_set_val(m_, i, j, in->elem(j));
sp_set_val(m_, i, j, in->elem(i));
#endif
}
}
Expand Down
14 changes: 8 additions & 6 deletions test/unit_tests/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,18 @@ SCENARIO("A Matrix", "[neuron_ivoc][OcMatrix]") {
REQUIRE(compareMatrix(n, {{0., 0., 0.}, {0., 0., 0.}, {0., 0., 0.}}));
}
{
IvocVect v(2);
IvocVect v(3);
m.getdiag(1, &v);
REQUIRE_THAT(v.vec(), Catch::Matchers::Approx(std::vector<double>({72., 114.})));
REQUIRE_THAT(v.vec(), Catch::Matchers::Approx(std::vector<double>({72., 114., 0.})));
v.vec() = {0., 72., 114.};
m.setdiag(-1, &v);
REQUIRE(compareMatrix(m, {{42., 72., 72.}, {72., 114., 114.}, {1., 114., 2.}}));
}
{
IvocVect v(1);
IvocVect v(3);
m.getdiag(-2, &v);
REQUIRE_THAT(v.vec(), Catch::Matchers::Approx(std::vector<double>({1.})));
REQUIRE(v.vec()[2] == Catch::Detail::Approx({1.}));

Check warning on line 205 in test/unit_tests/matrix.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04 - cmake (-DNRN_ENABLE_CORENEURON=ON -DNRN_ENABLE_INTERVIEWS=OFF -DNMODL_SANITIZERS=undefinedundefined)

braces around scalar initializer [-Wbraced-scalar-init]

Check warning on line 205 in test/unit_tests/matrix.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04 - cmake (-DNRN_ENABLE_CORENEURON=ON -DNRN_ENABLE_INTERVIEWS=OFF -DNMODL_SANITIZERS=undefinedundefined)

braces around scalar initializer [-Wbraced-scalar-init]

Check warning on line 205 in test/unit_tests/matrix.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04 - cmake (-DNRN_ENABLE_CORENEURON=ON -DNRN_ENABLE_INTERVIEWS=OFF -DNMODL_SANITIZERS=undefinedundefined)

braces around scalar initializer [-Wbraced-scalar-init]
v.vec() = {1., 0., 0.};
m.setdiag(2, &v);
REQUIRE(compareMatrix(m, {{42., 72., 1.}, {72., 114., 114.}, {1., 114., 2.}}));
}
Expand Down Expand Up @@ -377,8 +379,8 @@ SCENARIO("A Matrix", "[neuron_ivoc][OcMatrix]") {
REQUIRE(compareMatrix(m, {{1., 2., 3.}, {2., 2., 2.}, {3., 4., 3.}}));
}
{
IvocVect v(2);
v.vec() = {1, 2};
IvocVect v(3);
v.vec() = {0., 1., 2.};
m.setdiag(-1, &v);
REQUIRE(compareMatrix(m, {{1., 2., 3.}, {1., 2., 2.}, {3., 2., 3.}}));
}
Expand Down

0 comments on commit 5e72ba0

Please sign in to comment.