in Education by
I have a large banded sparse matrix S with few non-zero elements in its rows and columns. And I have vector b, I want to calculate sum(S*tcrossprod(b)). But because the data is too large, the system can't calculate it, I need to multiply the non-zero parts in S, how can I do that? Thanks! Here is the example data: S = matrix(rnorm(64),8,8) bw = 3 pick = (row(S)<(col(S) - bw)) | (row(S)>(col(S)+bw)) S[pick] = 0 S.r <- sample(1:8,40,replace = T) S.c <- sample(1:8,40,replace = T) for (i in 1:length(S.r)) { S[S.r[i],S.c[i]] <-0 } S <- as(S,"sparseMatrix") rowlab = collab = NULL for (ii in 1:S@Dim[2]) { n <- S@i[(S@p[ii]+1):S@p[ii+1]] rowlab <-c(rowlab, n) collab <- c(collab, rep(ii,length(n))) lab.b <- data.frame(rowlab = rowlab+1, collab = collab) } b = rnorm(8) And I want to get sum(S*tcrossprod(b)) For the real data, the dimension of S is c(20000,20000), and the dimension of b is c(20000,1). JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
You are running into memory issues due to the dense matrix returned from tcrossprod(b). But as many of the terms of this are getting multiplied by zero we can avoid calculating some of the values (avoid generating the full cross-product). You can grab the indices of the non-zero elements of the sparse matrix S, use these to index the vector b and multiply. Below are a couple of ways to calculate S * tcrossprod(b): library(Matrix) # Several functions to calculate the matrix products: f1 <- function(S, b) S*tcrossprod(b) f2 <- function(S, b) (S * b) %*% Diagonal(x=b) f3 <- function(S, b) {indices = summary(S); S@x = S@x * b[indices$i]*b[indices$j]; S} f4 <- function(S, b) {dp = diff(S@p); j = rep(seq_along(dp),dp); S@x = S@x * b[S@i+1]*b[j]; S} # Check equality of function results all.equal(f1(S, b), f2(S, b)) # [1] TRUE all.equal(f2(S, b), f3(S, b)) # [1] TRUE all.equal(f1(S, b), f3(S, b)) # [1] TRUE all.equal(f1(S, b), f4(S, b)) # [1] TRUE all.equal(f2(S, b), f4(S, b)) # [1] TRUE all.equal(f3(S, b), f4(S, b)) # [1] TRUE Benchmark: rbenchmark::benchmark(f1(S, b), f2(S, b), f3(S, b), f4(S, b), replications=1) # 1 rep as f1() is slow # test replications elapsed relative user.self sys.self user.child sys.child # 1 f1(S, b) 1 12.182 12182 7.972 4.932 0 0 # 2 f2(S, b) 1 0.003 3 0.003 0.000 0 0 # 3 f3(S, b) 1 0.002 2 0.002 0.000 0 0 # 4 f4(S, b) 1 0.001 1 0.001 0.000 0 0 # longer benchmark for the other functions, drop f1() # test replications elapsed relative user.self sys.self user.child sys.child # 1 f2(S, b) 100 0.363 2.556 0.305 0.042 0 0 # 2 f3(S, b) 100 0.279 1.965 0.255 0.024 0 0 # 3 f4(S, b) 100 0.142 1.000 0.142 0.000 0 0 As you just want the sum it is enough to do dp = diff(S@p); j = rep(seq_along(dp),dp); sum(S@x * b[S@i+1]*b[j]) Data: n = 1e4 # I dont have enough memeory on my laptop for 20k set.seed(1) b = rnorm(n) S = matrix(rnorm(n*n), n, n) bw = 3 pick = (row(S)<(col(S) - bw)) | (row(S)>(col(S)+bw)) S[pick] = 0 S.r <- sample(1:n,40,replace = T) S.c <- sample(1:n,40,replace = T) for (i in 1:length(S.r)) { S[S.r[i],S.c[i]] <-0 } S <- as(S,"sparseMatrix")

Related questions

0 votes
    Is there any shortcut method to find an index of an element in a vector, I have an element Q with me ... someone answer this doubt? Select the correct answer from above options...
asked Jan 23, 2022 in Education by JackTerrance
0 votes
    A single element of a character vector is referred as ________ (a) Character string (b) String (c) Data ... Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    How will you check if an element is present in a vector? (a) Match() (b) Dismatch() (c) Mismatch() ... and Debugging of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How can I check if a given value is contained in a vector? Select the correct answer from above options...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    The curl of gradient of a vector is non-zero. State True or False. (a) True (b) ... theory proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 11, 2021 in Education by JackTerrance
0 votes
    In R language, a vector is defined that it can only contain objects of the ________ (a) Same class (b) ... Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    How many atomic vector types does R have? (a) 5 (b) 6 (c) 8 (d) 10 This question was addressed ... Getting Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    In R using the function, ________ one can check the data type of vector. (a) Typeof() (b) Castof() (c) ... In and Out of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Which of the following is an example of a vector graphics device in R? (a) JPEG (b) PNG (c) GIF (d ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I am writing an OpenCL code to assemble a sparse matrix from Finite Element discretization and I would ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    ______ is used to get library location in R. (a) library() (b) search() (c) .libPaths() (d) ... Linear Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    ________ are built in R so that you get HTML. (a) Vignettes (b) Vighnaants (c) Bignats (d) Viddnets ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    All columns in a matrix must have the same mode and the _________ length. (a) Different (b) Same (c) May ... and Out of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Dataframes can be converted into a matrix by calling the following function data ______ (a) matr() (b) matrix ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    To bind a row onto an already existing matrix, the ______ function can be used. (a) Rbind (b) Sbnd (c) ... and Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
...