7

I did the following experiment, since there's no mention of complex data on the approx help page:

Rgames> zfoo
[1] 1+ 6i 2+ 7i 3+ 8i 4+ 9i 5+10i
Rgames> approx(zfoo,n=10)
$x
 [1] 1.000000 1.444444 1.888889 2.333333 2.777778 3.222222 3.666667 4.111111
 [9] 4.555556 5.000000

$y
 [1]  6.000000  6.444444  6.888889  7.333333  7.777778  8.222222  8.666667
 [8]  9.111111  9.555556 10.000000

Digging into the code for approx, I discovered that xy.coords (also apparently undocumented for complex data) treats the Real and Imag parts of complex data as the x and y parts of coordinate data. So my question is: is this intended behavior? I'm always a bit paranoid about depending on functionality that isn't explicitly documented.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73

1 Answers1

6

Looks intentional and reliable to me, as approx() calls regularize.values(), which calls xy.coords(), which includes this code block:

else if (is.complex(x)) {
    y <- Im(x)
    x <- Re(x)
    xlab <- paste0("Re(", ylab, ")")
    ylab <- paste0("Im(", ylab, ")")
}

following which approx() carries on just as it would had you passed it numeric (real/rational) vectors x and y.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • @CarlWitthoft -- FWIW, based on the comment introducing `regularize.values` in the R sources, I now lean towards the authors *not* having contemplated its application to complex numbers. Sort of immaterial, though, as long as the function performs reliably and your application of it makes sense! – Josh O'Brien Apr 17 '13 at 15:54