tidyfun / tf

S3 classes and methods for tidy functional data
https://tidyfun.github.io/tf/
GNU Affero General Public License v3.0
5 stars 2 forks source link

brackets [,] don't interpret array-valued arguments as expected #58

Open fabian-s opened 8 months ago

fabian-s commented 8 months ago

default expected behavior for arrays (i.e., array-args to [ return a vector of entries in the array) does not carry over to tf-vectors:

library(tf)

x <- tf_rgp(5, arg = 1:5)

# to extract function values at specific args for each function, we need:
as.matrix(x)[cbind(1:5, 1:5)]
#> [1] -0.3721783  0.6235588  1.3563847 -1.4085395  1.1629906
# .. and more complicated code to match arg-values to array-column-indices in general...

# default behavior for arrays does not carry over to tf-vectors:

x[cbind(1:5, 1:5)] # treats array arg as a vector :(
#> tfd[10] on (1,5) based on 5 evaluations each
#> interpolation by tf_approx_linear 
#> [1]: (1, -0.4);(2, -1.0);(3,  0.8); ...
#> [2]: (1, -0.8);(2,  0.6);(3,  0.3); ...
#> [3]: (1,  1.9);(2,  0.6);(3,  1.4); ...
#> [4]: (1, -2.5);(2, -1.0);(3, -0.6); ...
#> [5]: (1,-0.07);(2, 0.91);(3,-0.63); ...
#> [6]: (1, -0.4);(2, -1.0);(3,  0.8); ...
#> [7]: (1, -0.8);(2,  0.6);(3,  0.3); ...
#> [8]: (1,  1.9);(2,  0.6);(3,  1.4); ...
#> [9]: (1, -2.5);(2, -1.0);(3, -0.6); ...
#> [10]: (1,-0.07);(2, 0.91);(3,-0.63); ...

x <- tf_rgp(5)
x[cbind(1:5, runif(5))] # fails because i-arg does not check for array-type :(
#> Error in `[.tf`(x, cbind(1:5, runif(5))): Assertion on 'i' failed: Must be of type 'integerish', but element 6 is not close to an integer.
fabian-s commented 8 months ago

also this:

x[1:3, , matrix = FALSE] # or TRUE
tfd[3] on (0,1) based on 51 evaluations each
interpolation by tf_approx_linear 
1: (0.00,-0.30);(0.02,-0.25);(0.04,-0.14); ...

should be the same as x[1:3, tf_arg(x), matrix = ?] but it's not....