SquareMatrix: add functions to copy upper and lower triangle

This commit is contained in:
bresch
2023-08-07 14:15:55 +02:00
committed by Daniel Agar
parent 56dd1dc930
commit 01fc4c3cf1
2 changed files with 45 additions and 0 deletions
+21
View File
@@ -280,6 +280,27 @@ public:
return self.isBlockSymmetric<Width>(first, eps);
}
void copyLowerToUpperTriangle()
{
SquareMatrix<Type, M> &self = *this;
for (size_t row_idx = 1; row_idx < M; row_idx++) {
for (size_t col_idx = 0 ; col_idx < row_idx; col_idx++) {
self(col_idx, row_idx) = self(row_idx, col_idx);
}
}
}
void copyUpperToLowerTriangle()
{
SquareMatrix<Type, M> &self = *this;
for (size_t row_idx = 1; row_idx < M; row_idx++) {
for (size_t col_idx = 0 ; col_idx < row_idx; col_idx++) {
self(row_idx, col_idx) = self(col_idx, row_idx);
}
}
}
};
using SquareMatrix3f = SquareMatrix<float, 3>;
+24
View File
@@ -174,4 +174,28 @@ TEST(MatrixSquareTest, Square)
};
SquareMatrix<float, 4> K(data_K);
EXPECT_FALSE(K.isRowColSymmetric<1>(2));
float data_L[16] = {1, 0, 0, 0,
2, 3, 0, 0,
3, 4, 11, 0,
4, 11, 15, 16
};
float data_L_check[16] = {1, 2, 3, 4,
2, 3, 4, 11,
3, 4, 11, 15,
4, 11, 15, 16
};
SquareMatrix<float, 4> L(data_L);
L.copyLowerToUpperTriangle();
SquareMatrix<float, 4> L_check(data_L_check);
EXPECT_EQ(L, L_check);
float data_M[16] = {1, 2, 3, 4,
0, 3, 4, 11,
0, 0, 11, 15,
0, 0, 0, 16
};
SquareMatrix<float, 4> M(data_M);
M.copyUpperToLowerTriangle();
EXPECT_EQ(M, L_check);
}