Speed up sparse matrix multiplication in R












1















I am trying to multiply a matrix (made up of few 1's and majority O's) with a vector using %*% function in R, this process is taking huge amount of time. Is there a way I can make this faster??



Thanks










share|improve this question


















  • 2





    Hey Naga, welcome to the site. You'll get better answers if you provide an example that is minimal, complete, and reproducible. It helps if there is a minimal example of data that can be used to run your code, the code you are actually using or trying, and the results that you expect. Check out this help file: stackoverflow.com/help/mcve

    – Adam Sampson
    Nov 14 '18 at 20:55








  • 1





    Sometimes it is helpful to also know why you are trying to do what you are doing. Sometimes people will give you better ways to code and sometimes people will answer with a different way to solve the problem more efficiently. But this isn't necessary if you are just looking to solve your exact matrix multiplication.

    – Adam Sampson
    Nov 14 '18 at 20:59






  • 1





    OK, we know that your matrix is sparse mathematically (contains lots of zeros). We don't know if it's sparse computationally (is saved in a format, as in @dmca's answer, where only the non-zero values are explicitly stored). Can you show us e.g. str(X) where X is your matrix?

    – Ben Bolker
    Nov 14 '18 at 21:34











  • You could look into sparse matrix algebra R packages such as Matrix or spam.

    – Florian
    Nov 14 '18 at 21:40
















1















I am trying to multiply a matrix (made up of few 1's and majority O's) with a vector using %*% function in R, this process is taking huge amount of time. Is there a way I can make this faster??



Thanks










share|improve this question


















  • 2





    Hey Naga, welcome to the site. You'll get better answers if you provide an example that is minimal, complete, and reproducible. It helps if there is a minimal example of data that can be used to run your code, the code you are actually using or trying, and the results that you expect. Check out this help file: stackoverflow.com/help/mcve

    – Adam Sampson
    Nov 14 '18 at 20:55








  • 1





    Sometimes it is helpful to also know why you are trying to do what you are doing. Sometimes people will give you better ways to code and sometimes people will answer with a different way to solve the problem more efficiently. But this isn't necessary if you are just looking to solve your exact matrix multiplication.

    – Adam Sampson
    Nov 14 '18 at 20:59






  • 1





    OK, we know that your matrix is sparse mathematically (contains lots of zeros). We don't know if it's sparse computationally (is saved in a format, as in @dmca's answer, where only the non-zero values are explicitly stored). Can you show us e.g. str(X) where X is your matrix?

    – Ben Bolker
    Nov 14 '18 at 21:34











  • You could look into sparse matrix algebra R packages such as Matrix or spam.

    – Florian
    Nov 14 '18 at 21:40














1












1








1








I am trying to multiply a matrix (made up of few 1's and majority O's) with a vector using %*% function in R, this process is taking huge amount of time. Is there a way I can make this faster??



Thanks










share|improve this question














I am trying to multiply a matrix (made up of few 1's and majority O's) with a vector using %*% function in R, this process is taking huge amount of time. Is there a way I can make this faster??



Thanks







r sparse-matrix matrix-multiplication






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 20:02









user9529330user9529330

106




106








  • 2





    Hey Naga, welcome to the site. You'll get better answers if you provide an example that is minimal, complete, and reproducible. It helps if there is a minimal example of data that can be used to run your code, the code you are actually using or trying, and the results that you expect. Check out this help file: stackoverflow.com/help/mcve

    – Adam Sampson
    Nov 14 '18 at 20:55








  • 1





    Sometimes it is helpful to also know why you are trying to do what you are doing. Sometimes people will give you better ways to code and sometimes people will answer with a different way to solve the problem more efficiently. But this isn't necessary if you are just looking to solve your exact matrix multiplication.

    – Adam Sampson
    Nov 14 '18 at 20:59






  • 1





    OK, we know that your matrix is sparse mathematically (contains lots of zeros). We don't know if it's sparse computationally (is saved in a format, as in @dmca's answer, where only the non-zero values are explicitly stored). Can you show us e.g. str(X) where X is your matrix?

    – Ben Bolker
    Nov 14 '18 at 21:34











  • You could look into sparse matrix algebra R packages such as Matrix or spam.

    – Florian
    Nov 14 '18 at 21:40














  • 2





    Hey Naga, welcome to the site. You'll get better answers if you provide an example that is minimal, complete, and reproducible. It helps if there is a minimal example of data that can be used to run your code, the code you are actually using or trying, and the results that you expect. Check out this help file: stackoverflow.com/help/mcve

    – Adam Sampson
    Nov 14 '18 at 20:55








  • 1





    Sometimes it is helpful to also know why you are trying to do what you are doing. Sometimes people will give you better ways to code and sometimes people will answer with a different way to solve the problem more efficiently. But this isn't necessary if you are just looking to solve your exact matrix multiplication.

    – Adam Sampson
    Nov 14 '18 at 20:59






  • 1





    OK, we know that your matrix is sparse mathematically (contains lots of zeros). We don't know if it's sparse computationally (is saved in a format, as in @dmca's answer, where only the non-zero values are explicitly stored). Can you show us e.g. str(X) where X is your matrix?

    – Ben Bolker
    Nov 14 '18 at 21:34











  • You could look into sparse matrix algebra R packages such as Matrix or spam.

    – Florian
    Nov 14 '18 at 21:40








2




2





Hey Naga, welcome to the site. You'll get better answers if you provide an example that is minimal, complete, and reproducible. It helps if there is a minimal example of data that can be used to run your code, the code you are actually using or trying, and the results that you expect. Check out this help file: stackoverflow.com/help/mcve

– Adam Sampson
Nov 14 '18 at 20:55







Hey Naga, welcome to the site. You'll get better answers if you provide an example that is minimal, complete, and reproducible. It helps if there is a minimal example of data that can be used to run your code, the code you are actually using or trying, and the results that you expect. Check out this help file: stackoverflow.com/help/mcve

– Adam Sampson
Nov 14 '18 at 20:55






1




1





Sometimes it is helpful to also know why you are trying to do what you are doing. Sometimes people will give you better ways to code and sometimes people will answer with a different way to solve the problem more efficiently. But this isn't necessary if you are just looking to solve your exact matrix multiplication.

– Adam Sampson
Nov 14 '18 at 20:59





Sometimes it is helpful to also know why you are trying to do what you are doing. Sometimes people will give you better ways to code and sometimes people will answer with a different way to solve the problem more efficiently. But this isn't necessary if you are just looking to solve your exact matrix multiplication.

– Adam Sampson
Nov 14 '18 at 20:59




1




1





OK, we know that your matrix is sparse mathematically (contains lots of zeros). We don't know if it's sparse computationally (is saved in a format, as in @dmca's answer, where only the non-zero values are explicitly stored). Can you show us e.g. str(X) where X is your matrix?

– Ben Bolker
Nov 14 '18 at 21:34





OK, we know that your matrix is sparse mathematically (contains lots of zeros). We don't know if it's sparse computationally (is saved in a format, as in @dmca's answer, where only the non-zero values are explicitly stored). Can you show us e.g. str(X) where X is your matrix?

– Ben Bolker
Nov 14 '18 at 21:34













You could look into sparse matrix algebra R packages such as Matrix or spam.

– Florian
Nov 14 '18 at 21:40





You could look into sparse matrix algebra R packages such as Matrix or spam.

– Florian
Nov 14 '18 at 21:40












1 Answer
1






active

oldest

votes


















2














You can create a sparse matrix using the Matrix package. Matrix/vector multiplication may be faster in this case. For example:



library(Matrix)
library(tictoc)
set.seed(123)
v <- sample(1e4)
m <- Matrix(sample(c(0, 1), length(v) ^ 2, T, c(.99, .01)),
length(v), length(v), sparse = F)
sm <- Matrix(m, sparse = T)
tic("dense")
x <- m %*% v
toc()
#> dense: 0.094 sec elapsed
tic("sparse")
y <- sm %*% v
toc()
#> sparse: 0.006 sec elapsed





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307953%2fspeed-up-sparse-matrix-multiplication-in-r%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can create a sparse matrix using the Matrix package. Matrix/vector multiplication may be faster in this case. For example:



    library(Matrix)
    library(tictoc)
    set.seed(123)
    v <- sample(1e4)
    m <- Matrix(sample(c(0, 1), length(v) ^ 2, T, c(.99, .01)),
    length(v), length(v), sparse = F)
    sm <- Matrix(m, sparse = T)
    tic("dense")
    x <- m %*% v
    toc()
    #> dense: 0.094 sec elapsed
    tic("sparse")
    y <- sm %*% v
    toc()
    #> sparse: 0.006 sec elapsed





    share|improve this answer




























      2














      You can create a sparse matrix using the Matrix package. Matrix/vector multiplication may be faster in this case. For example:



      library(Matrix)
      library(tictoc)
      set.seed(123)
      v <- sample(1e4)
      m <- Matrix(sample(c(0, 1), length(v) ^ 2, T, c(.99, .01)),
      length(v), length(v), sparse = F)
      sm <- Matrix(m, sparse = T)
      tic("dense")
      x <- m %*% v
      toc()
      #> dense: 0.094 sec elapsed
      tic("sparse")
      y <- sm %*% v
      toc()
      #> sparse: 0.006 sec elapsed





      share|improve this answer


























        2












        2








        2







        You can create a sparse matrix using the Matrix package. Matrix/vector multiplication may be faster in this case. For example:



        library(Matrix)
        library(tictoc)
        set.seed(123)
        v <- sample(1e4)
        m <- Matrix(sample(c(0, 1), length(v) ^ 2, T, c(.99, .01)),
        length(v), length(v), sparse = F)
        sm <- Matrix(m, sparse = T)
        tic("dense")
        x <- m %*% v
        toc()
        #> dense: 0.094 sec elapsed
        tic("sparse")
        y <- sm %*% v
        toc()
        #> sparse: 0.006 sec elapsed





        share|improve this answer













        You can create a sparse matrix using the Matrix package. Matrix/vector multiplication may be faster in this case. For example:



        library(Matrix)
        library(tictoc)
        set.seed(123)
        v <- sample(1e4)
        m <- Matrix(sample(c(0, 1), length(v) ^ 2, T, c(.99, .01)),
        length(v), length(v), sparse = F)
        sm <- Matrix(m, sparse = T)
        tic("dense")
        x <- m %*% v
        toc()
        #> dense: 0.094 sec elapsed
        tic("sparse")
        y <- sm %*% v
        toc()
        #> sparse: 0.006 sec elapsed






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 21:29









        dmcadmca

        4431414




        4431414
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307953%2fspeed-up-sparse-matrix-multiplication-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine