Skip to content

Matrix Diagonal Fill Bugged for Nonsymmetric #619

@coatless

Description

@coatless

Consider:

Rcpp::cppFunction("NumericMatrix fdiag(NumericMatrix x) {
    x.fill_diag(0.0);
    return x;
}")

The issue arises with the .fill_diag()'s fill_diag__dispatch() method in Matrix.h primarily with a nonsymmetric matrix under either n > p or n < p.

Case 1: Nonsymmetric (n < p)

m1 <- matrix(1:10, nrow = 2, ncol = 5, byrow = TRUE)
fdiag(m1)
     [,1] [,2] [,3] [,4] [,5]
[1,]    0    2    3    0    5
[2,]    6    7    8    9   10

Expected:

     [,1] [,2] [,3] [,4] [,5]
[1,]    0    2    3    4    5
[2,]    6    0    8    9   10

Case 2: Nonsymmetric (n > p)

m3 <- matrix(1:10, nrow = 5, ncol = 2, byrow = TRUE)
fdiag(m3)
     [,1] [,2]
[1,]    0    2
[2,]    3    4
[3,]    5    6
[4,]    0    8
[5,]    9   10

Expected:

     [,1] [,2]
[1,]    0    2
[2,]    3    0
[3,]    5    6
[4,]    7    8
[5,]    9   10

Case 3: n = p

Though, the square matrix iteration works correctly.

m3 <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
fdiag(m3)
     [,1] [,2] [,3]
[1,]    0    2    3
[2,]    4    0    6
[3,]    7    8    0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions