RobinHankin / spray

sparse arrays and fast multivariate polynomials
https://robinhankin.github.io/spray/
2 stars 2 forks source link

as.character.spray #40

Open stla opened 1 year ago

stla commented 1 year ago

Hello,

I have something strange. I have a list of four sprays:

> groups
[[1]]
         val
 0 2  =    1
 2 2  =    1
 1 2  =   -2

[[2]]
         val
 2 1  =   -2
 1 1  =    2

[[3]]
         val
 2 0  =    1

[[4]]
         val
 1 2  =   -2
 0 2  =   -2

I want to have them as character. Since there's not as.character.spray function - and I think it would be nice to have one -, I did my own function:

asCharacter <- function(poly) {
  op <- options(polyform = TRUE, sprayvars = letters)
  x <- capture.output(print(poly))
  options(op)
  x
}

I apply my function to the list:

> lapply(groups, asCharacter)
[[1]]
[1] "+b^2 +a^2*b^2 -2*a*b^2" "+b^2 +a^2*b^2 -2*a*b^2"

[[2]]
[1] "-2*a^2*b +2*a*b" "-2*a^2*b +2*a*b"

[[3]]
[1] "+a^2" "+a^2"

[[4]]
[1] "-2*a*b^2 -2*b^2" "-2*a*b^2 -2*b^2"

Every spray is duplicated! Do you have an idea way?

Another strange thing:

> str(groups)
List of 4
 $ :Class 'spray'  hidden list of 2
  ..$ index: int [1:3, 1:2] 0 2 1 2 2 2
  ..$ value: num [1:3] 1 1 -2
 $ :List of 2
  ..$ index: int [1:2, 1:2] 2 1 1 1
  ..$ value: num [1:2] -2 2
  ..- attr(*, "class")= chr "spray"
 $ :Class 'spray'  hidden list of 2
  ..$ index: int [1, 1:2] 2 0
  ..$ value: num 1
 $ :List of 2
  ..$ index: int [1:2, 1:2] 1 0 2 2
  ..$ value: num [1:2] -2 -2
  ..- attr(*, "class")= chr "spray"

Sometimes this print "Class spray hidden list of 2", but not always.

stla commented 1 year ago

That works with capture.output(print_spray_polyform(poly)).

RobinHankin commented 1 year ago

Hi there, thanks for this, great to hear from you! I've been working on spray recently and I think that this is a bug in print():

library("spray")
#> 
#> Attaching package: 'spray'
#> The following objects are masked from 'package:base':
#> 
#>     pmax, pmin
a <- spray(diag(3))
print(a)
#>            val
#>  0 0 1  =    1
#>  0 1 0  =    1
#>  1 0 0  =    1
#>            val
#>  0 0 1  =    1
#>  0 1 0  =    1
#>  1 0 0  =    1

Created on 2023-07-03 with reprex v2.0.2 I remember being a bit confused about best practice for print methods and I think I forgot an invisible() somewhere. I will raise an issue for the print() problem separately.

Not 100% sure about the str() issue. Can you give me a dput() for the object that gave the inconsistent str() output please?

stla commented 1 year ago
> dput(groups)
list(structure(list(index = structure(c(0L, 2L, 1L, 2L, 2L, 2L
), dim = 3:2), value = c(1, 1, -2)), class = "spray"), structure(list(
    index = structure(c(2L, 1L, 1L, 1L), dim = c(2L, 2L)), value = c(-2, 
    2)), class = "spray"), structure(list(index = structure(c(2L, 
0L), dim = 1:2), value = 1), class = "spray"), structure(list(
    index = structure(c(1L, 0L, 2L, 2L), dim = c(2L, 2L)), value = c(-2, 
    -2)), class = "spray"))
RobinHankin commented 1 year ago

OK thanks for this, will raise a separate issue for the str() behaviour.

stla commented 1 year ago

I didn't show you why I needed as.character.spray: https://stackoverflow.com/q/76593269/1100107. That might interest you.

stla commented 1 year ago

blogged :-) https://laustep.github.io/stlahblog/posts/expandPolynomialWithSpray.html