Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is approx() designed to be used with complex numbers?

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.

like image 957
Carl Witthoft Avatar asked Apr 17 '13 12:04

Carl Witthoft


1 Answers

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.

like image 118
Josh O'Brien Avatar answered Nov 02 '22 09:11

Josh O'Brien