diff --git a/src/ivoc/ocmatrix.cpp b/src/ivoc/ocmatrix.cpp index b92cf39a78..123de08771 100644 --- a/src/ivoc/ocmatrix.cpp +++ b/src/ivoc/ocmatrix.cpp @@ -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 } } @@ -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 } } @@ -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 } } diff --git a/test/unit_tests/matrix.cpp b/test/unit_tests/matrix.cpp index ebd487c391..fc91b654ca 100644 --- a/test/unit_tests/matrix.cpp +++ b/test/unit_tests/matrix.cpp @@ -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({72., 114.}))); + REQUIRE_THAT(v.vec(), Catch::Matchers::Approx(std::vector({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({1.}))); + REQUIRE(v.vec()[2] == Catch::Detail::Approx({1.})); + v.vec() = {1., 0., 0.}; m.setdiag(2, &v); REQUIRE(compareMatrix(m, {{42., 72., 1.}, {72., 114., 114.}, {1., 114., 2.}})); } @@ -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.}})); }