Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upMatrix Diagonal Fill Bugged for Nonsymmetric #619
Comments
|
Hm. Shall we warn, or even stop if we have a non-symmetric matrix? Maybe require an override var? What is Maybe still easiest to just refuse... Dunno. Edit OTOH it is clearly named |
|
I think it would be pretty safe to go with the main diagonal definition, i.e. diagonal elements are where `diag<-`(matrix(1, 3, 4), 0)
# [,1] [,2] [,3] [,4]
# [1,] 0 1 1 1
# [2,] 1 0 1 1
# [3,] 1 1 0 1
`diag<-`(matrix(1, 4, 3), 0)
# [,1] [,2] [,3]
# [1,] 0 1 1
# [2,] 1 0 1
# [3,] 1 1 0
# [4,] 1 1 1
`diag<-`(matrix(1, 3, 3), 0)
# [,1] [,2] [,3]
# [1,] 0 1 1
# [2,] 1 0 1
# [3,] 1 1 0I don't know why an iterator is used in the current implementation but this would be trivial with an ordinary loop: #include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix FillDiag(NumericMatrix x) {
R_xlen_t i = 0, n = x.nrow() < x.ncol() ? x.nrow() : x.ncol();
for (; i < n; i++) {
x(i, i) = 0.0;
}
return x;
}
/*** R
FillDiag(matrix(1, 3, 4))
# [,1] [,2] [,3] [,4]
# [1,] 0 1 1 1
# [2,] 1 0 1 1
# [3,] 1 1 0 1
FillDiag(matrix(1, 4, 3))
# [,1] [,2] [,3]
# [1,] 0 1 1
# [2,] 1 0 1
# [3,] 1 1 0
# [4,] 1 1 1
FillDiag(matrix(1, 3, 3))
# [,1] [,2] [,3]
# [1,] 0 1 1
# [2,] 1 0 1
# [3,] 1 1 0
*/ |
|
Apologies for the lack of context regarding To quickly address the points:
Fun trivia factoid, the bug has been present since at least 0.11.0. |
Fix for non-symmetric matrix diagonal fill (Closes #619)
Consider:
The issue arises with the
.fill_diag()'sfill_diag__dispatch()method inMatrix.hprimarily with a nonsymmetric matrix under either n > p or n < p.Case 1: Nonsymmetric (n < p)
Expected:
Case 2: Nonsymmetric (n > p)
Expected:
Case 3: n = p
Though, the square matrix iteration works correctly.