Skip to content

R-devel change in complex variables and NA assignment #1397

@eddelbuettel

Description

@eddelbuettel

While trying to re-jig coverage (which I got working again for RcppArmadillo and RcppEigen) I noticed two failures on sugar tests for complex variables. It reproduces locally:

  ----- FAILED[data]: test_sugar.R<1272--1272>
   call| expect_equal(cx_row_means(x, TRUE), rowMeans(x, TRUE), info = "complex / rowMeans / rm NA / mixed input")
   diff| Mean relative Mod difference: 0.7597472                                                                                                                                                                   
   info| complex / rowMeans / rm NA / mixed input
  ----- FAILED[data]: test_sugar.R<1275--1275>
   call| expect_equal(cx_col_means(x, TRUE), colMeans(x, TRUE), info = "complex / colMeans / rm NA / mixed input")
   diff| Mean relative Mod difference: 0.1915024
   info| complex / colMeans / rm NA / mixed input
  Error: 2 out of 1622 tests failed
  Execution halted

This is from the part where we take a 3 x 3 complex matrix and inject NA values. This now differs:

> x <- matrix(rnorm(9) + 2i, 3)
> x
             [,1]         [,2]         [,3]
[1,] -1.862645+2i -2.059943+2i  0.694593+2i
[2,]  1.469971+2i -0.722717+2i -0.367111+2i
[3,] -0.946859+2i  0.468356+2i -0.486049+2i
> x[sample(1:9, 4)] <- NA
> x
             [,1]        [,2]         [,3]
[1,] -1.862645+2i -2.05994+2i           NA
[2,]           NA          NA -0.367111+2i
[3,] -0.946859+2i          NA -0.486049+2i
> rowMeans(x, TRUE)
[1] -1.961294+1.333333i -0.367111+0.666667i -0.716454+1.333333i

We get

> cppFunction("Rcpp::ComplexVector cx_row_means(Rcpp::ComplexMatrix x, bool na_rm = false) { return rowMeans(x, na_rm); }")
> cx_row_means(x, TRUE)
[1] -1.961294+2i -0.367111+2i -0.716454+2i
> 

So a disagreement over the complex part. R scales by 'n_rows' (NA or not), we scale by number of non-NA. One of the two is wrong.

I see

r88444 | maechler | 2025-07-22 08:43:24 -0500 (Tue, 22 Jul 2025) | 1 line

subassignment <complex>[i] <- NA should only touch the real part

which could be related. We may have to mirror that.

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