RobinHankin / stokes

methods for exterior calculus
https://robinhankin.github.io/stokes/
3 stars 0 forks source link

zero/empty tensors lose S3 classes. #41

Closed RobinHankin closed 3 years ago

RobinHankin commented 3 years ago
> A <- as.ktensor(rbind(1:3))
> B <- A %X% A
> B
A linear map from V^6 to R with V=R^3:
                 val
 1 2 3 1 2 3  =    1
> Alt(B)
empty sparse array with 6 columns

so far so good. But as.function() behaves differently when given B and Alt(B), which I don't think is correct:

> as.function(B)
function (E) 
{
    sum(sapply(p, function(i) {
        v[i] * prod(E[cbind(M[i, ], k)])
    }))
}
[snip]

> as.function(Alt(B))
function (X) 
{
    if (!is.matrix(X)) {
        X <- rbind(X)
    }
    stopifnot(ncol(X) == arity(x))
[snip] 

What I think is happening is that B has class ktensor but Alt(B) reverts to a spray object:

> dput(B)
structure(list(index = structure(c(1L, 2L, 3L, 1L, 2L, 3L), .Dim = c(1L, 
6L)), value = 1), class = c("ktensor", "spray"))
> dput(Alt(B))
structure(list(structure(numeric(0), .Dim = c(0L, 6L)), numeric(0)), class = "spray")

This is a problem because of inconsistent behaviour:

> V <- matrix(rnorm(18),3,6)
> as.function(B)(V)
[1] 0.001408884
> as.function(Alt(B))(V)
[1] 0 0 0