-
-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Matrix 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 0 I 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.h
primarily 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.
The text was updated successfully, but these errors were encountered: