66# ' Copyright (c) Adrian Baddeley, Ege Rubak and Rolf Turner 2016-2020
77# ' GNU Public Licence >= 2.0
88# '
9- # ' $Revision: 1.38 $ $Date: 2022/05/23 02:33:06 $
9+ # ' $Revision: 1.39 $ $Date: 2026/04/24 05:57:32 $
1010# '
1111
1212sumouter <- function (x , w = NULL , y = x ) {
@@ -239,8 +239,8 @@ bilinearform <- function(x, v, y) {
239239}
240240
241241sumsymouter <- function (x , w = NULL , distinct = TRUE ) {
242- # # x is a 3D array
243- # # w is a matrix
242+ # # x is a 3D array or sparse 3D array
243+ # # w is a matrix or sparse matrix
244244 # # Computes the sum of outer(x[,i,j], x[,j,i]) * w[i,j] over all pairs i != j
245245 # # handle complex values
246246 if (is.complex(w )) {
@@ -256,20 +256,25 @@ sumsymouter <- function(x, w=NULL, distinct=TRUE) {
256256 result <- a - b + (d - a - b ) * 1i
257257 return (result )
258258 }
259- # # handle sparse arrays
260- if (inherits(x , c(" sparseSlab" , " sparse3Darray" )) &&
261- (is.null(w ) || inherits(w , " sparseMatrix" )))
262- return (sumsymouterSparse(x , w , distinct = distinct ))
263259 # # arguments are numeric
264- x <- as.array( x )
260+ # # validate dimensions
265261 stopifnot(length(dim(x )) == 3 )
266262 if (dim(x )[2L ] != dim(x )[3L ])
267263 stop(" The second and third dimensions of x should be equal" )
268264 if (! is.null(w )) {
269- w <- as.matrix( w )
265+ stopifnot(length(dim( w )) == 2 )
270266 if (! all(dim(w ) == dim(x )[- 1L ]))
271267 stop(" Dimensions of w should match the second and third dimensions of x" )
272268 }
269+ # # handle sparse arrays
270+ if (inherits(x , c(" sparseSlab" , " sparse3Darray" ))) {
271+ if (! is.null(w ))
272+ w <- as(w , " sparseMatrix" )
273+ return (sumsymouterSparse(x , w , distinct = distinct ))
274+ }
275+ x <- as.array(x )
276+ if (! is.null(w ))
277+ w <- as.matrix(w )
273278 p <- dim(x )[1L ]
274279 n <- dim(x )[2L ]
275280 if (! distinct ) {
0 commit comments