RobinHankin / stokes

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

bug in pullback() #68

Closed RobinHankin closed 2 years ago

RobinHankin commented 2 years ago

Set the last line of the matrix M to zero gives an error:

 K <- as.kform(matrix(c(1,1,2,3),2,2),c(1,5))
 M <- matrix(1:9,3,3)
 pullback(K,M)
An alternating linear map from V^2 to R with V=R^3:
         val
 1 2  =  -33
 1 3  =  -66
 2 3  =  -33
 M[3,] <- 0
 pullback(K,M)
Error in seq_len(n) : argument must be coercible to non-negative integer
In addition: Warning message:
In seq_len(n) : first element used of 'length.out' argument
> 
RobinHankin commented 2 years ago

Observation: the too-clever-by-half form of pullback() makes it pretty much impossible to debug

RobinHankin commented 2 years ago

It is possible to do a bit of archaeology and trigger the bug more simply:

M <- matrix(1:9,3,3)
M[1,] <- 0
K <- as.kform(matrix(c(1,1,2,3),2,2),c(1,5))
i <- 1
do.call("wedge", apply(M[index(K)[i,,drop=FALSE],,drop=FALSE],1,as.1form))

Thence

> as.1form(c(0,0,0)) ^ as.1form(c(3,6,9))
Error in seq_len(n) : 
  (converted from warning) first element used of 'length.out' argument
> 

The problem is that the arity is not preserved by as.1form():

> as.1form(c(0,0,1))  # works fine 
An alternating linear map from V^1 to R with V=R^3:
       val
 3  =    1
> arity(as.1form(c(0,0,1)))
[1] 1
> as.1form(c(0,0,0))
The zero alternating linear map from V^ to R with V=R^n:
empty sparse array with  columns
> arity(as.1form(c(0,0,0)))
NULL
> 

Idiom as.1form(c(0,0,0)) returns an object with arity NULL not 1 as it should do.

RobinHankin commented 2 years ago

The problem is with spray::spray(). Currently we have

> v <- rep(0,6)
> dput(spray(cbind(seq_along(v)),v))
structure(list(index = NULL, value = NULL), class = "spray")

But what we want is

> dput(spray(matrix(0,0,6)))
structure(list(structure(numeric(0), dim = c(0L, 6L)), numeric(0)), class = "spray")
> 

There is an extended discussion of this issue in zero.Rd of the spray package. This issue inspired commit https://github.com/RobinHankin/spray/commit/def2c2624ea7190679c50658d6aaffdc8b3079fd.

RobinHankin commented 2 years ago

The commit that fixes the issue was https://github.com/RobinHankin/spray/commit/43b3f32b1863a4bf7b403993ebb69cc43ccfde54