tidyfun / tf

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

Improve maths ops: use `vctrs` API, implement tfb-specific logic #69

Open m-muecke opened 5 months ago

m-muecke commented 5 months ago

Currently the math and ops methods are implemented in base R, but there is a interface from vctrs to do it directly: https://vctrs.r-lib.org/reference/vec_math.html, https://vctrs.r-lib.org/reference/vec_arith.html

Here is an example from the bignum package: https://github.com/davidchall/bignum/blob/master/R/vctrs-arith.R#L42 and from the tsibble package: https://github.com/tidyverts/tsibble/blob/main/R/yearmonth.R#L240

and from the docs: https://vctrs.r-lib.org/articles/s3-vector.html

vec_arith.vctrs_meter.vctrs_meter <- function(op, x, y, ...) {
  switch(op,
    "+" = ,
    "-" = new_meter(op, x, y)),
    "/" = vec_arith_base(op, x, y),
    stop_incompatible_op(op, x, y)
  )
}
fabian-s commented 5 months ago

.. yeah -- not going to tackle this rn, but seems like the way to go.

  1. implement vctrs-Ops for
  1. implement arithmetic on basis coefficients (multiplication) or basis functions (addition) for tfbs -- this should also fix stupid&sloppy behavior like:
> x 
tfb[3] on (0,1) in basis representation:
 using  2 FPCs 
1: (0.000,0.77);(0.005,0.79);(0.010,0.74); ...
> x - 2
tfb[3] on (0,1) in basis representation: #BASIS CHANGE SHOULD NOT HAPPEN
 using  s(arg, bs = "cr", k = 25) 
1: (0.000,-1.2);(0.005,-1.2);(0.010,-1.3); ...
> (x - 2) + 2
tfb[3] on (0,1) in basis representation:
 using  s(arg, bs = "cr", k = 25) 
1: (0.000,0.80);(0.005,0.77);(0.010,0.74); ... # NOT THE SAME VALUES AS BEFORE