RobinHankin / spray

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

spray::index() breaks disordR discipline #38

Open RobinHankin opened 1 year ago

RobinHankin commented 1 year ago

And I am not sure what to do about it.

library("spray")

A <- spray(matrix(c(1:7,1:5),4,3),1:4)
coeffs(A)
#> A disord object with hash dbda492120778a871dab03aa6642cdd1771eab5b and elements
#> [1] 4 3 2 1
#> (in some order)
coeffs(A)[1]
#> Error in .local(x, i, j = j, ..., drop): if using a regular index to extract, must extract each element once and once only (or none of them)
index(A)
#>      [,1] [,2] [,3]
#> [1,]    4    1    5
#> [2,]    3    7    4
#> [3,]    2    6    3
#> [4,]    1    5    2
index(A)[1,]
#> [1] 4 1 5

Created on 2023-07-01 with reprex v2.0.2

Above, trying to extract the first element of coeffs(A) correctly throws an error. But trying to find the first row of index(A) returns a result which is technically implementation-specific.

RobinHankin commented 1 year ago

Might be better to enforce disordR discipline from the dependent packages, see:

https://github.com/RobinHankin/stokes/commit/76d9d20fb3ed54d9ad77fcac724d3a2a112814c6

RobinHankin commented 1 year ago

I have started work on the dismat package which might resolve this.

RobinHankin commented 1 month ago

Obervation: a note at spray.Rd states that index() breaks disordR discipline.

RobinHankin commented 1 month ago

I have been reminding myself about the dismat package, https://github.com/RobinHankin/dismat, which emphasises rownames and colnames of matrices when multiplying them. But index() returns a matrix with no dimnames attribute. It might be better to create a new class of matrices that does everything regular R matrices do, but forbids idiom such as M[1,].

Note that the columns have no such problem (not being affected by disordR discipline), and idiom such as M[,1] is fine.

RobinHankin commented 3 weeks ago

If we wanted some disord-type functionality for index() then the following should work:

library("spray")
#> 
#> Attaching package: 'spray'
#> The following objects are masked from 'package:base':
#> 
#>     pmax, pmin
(a <- rspray())
#>            val
#>  0 1 1  =    9
#>  2 0 1  =    6
#>  0 1 0  =    7
#>  0 0 1  =    5
#>  2 2 2  =    4
#>  0 1 2  =    3
#>  1 1 1  =   10
#>  2 2 0  =    1
cbind(index(a),coeffs(a))
#> rbind() and cbind() not currently implemented for disord objects (even if the hash codes match)
#> 
#> disordR discipline error in:
#> NULL
#> Error in check_matching_hash(x, y): 
#> cannot combine disord object with hash code 86a85578733844f47c46e1f2623ebaef0cefdec0 with a vector

[note the sub-optimal error message]. We would need index() to return an object with a hash code that would have to match that of coeffs(a) for the cbind() to work. Also note that the columns are not subject to any disord-type discipline.