Generating a random sparse hermitian matrix in Python











up vote
1
down vote

favorite












I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?



Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.



Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
$$ d = frac{mathrm{nnz}}{n^2} $$
where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).



EDIT



Calculating the density should be easy:



density = np.count_nonzero(A)/n**2


should do the trick.










share|cite|improve this question




























    up vote
    1
    down vote

    favorite












    I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?



    Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.



    Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
    $$ d = frac{mathrm{nnz}}{n^2} $$
    where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).



    EDIT



    Calculating the density should be easy:



    density = np.count_nonzero(A)/n**2


    should do the trick.










    share|cite|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?



      Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.



      Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
      $$ d = frac{mathrm{nnz}}{n^2} $$
      where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).



      EDIT



      Calculating the density should be easy:



      density = np.count_nonzero(A)/n**2


      should do the trick.










      share|cite|improve this question















      I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?



      Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.



      Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
      $$ d = frac{mathrm{nnz}}{n^2} $$
      where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).



      EDIT



      Calculating the density should be easy:



      density = np.count_nonzero(A)/n**2


      should do the trick.







      matrices numerical-linear-algebra random python sparse-matrices






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited 2 hours ago

























      asked 2 hours ago









      Peiffap

      326




      326






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          This generates a random mask with of a given size n and density dens, you just need to apply it to a dense matrix of your choice



          import numpy as np
          np.random.seed(0)

          n = 300
          dens = 0.2
          mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])

          print(np.count_nonzero(mask) / n**2)


          The result of this is



          >>> 0.20224444444444445


          To force the result to be Hermitian just use



          $$
          H = frac{1}{2}(A + A^*)
          $$






          share|cite|improve this answer





















          • Alright, I see how this works but how do I apply the mask to a matrix?
            – Peiffap
            40 mins ago










          • @Peiffap Define the matrix M and then run A = M[mask]
            – caverac
            39 mins ago












          • M[mask], no? Thanks!
            – Peiffap
            38 mins ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          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',
          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
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3019821%2fgenerating-a-random-sparse-hermitian-matrix-in-python%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








          up vote
          1
          down vote













          This generates a random mask with of a given size n and density dens, you just need to apply it to a dense matrix of your choice



          import numpy as np
          np.random.seed(0)

          n = 300
          dens = 0.2
          mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])

          print(np.count_nonzero(mask) / n**2)


          The result of this is



          >>> 0.20224444444444445


          To force the result to be Hermitian just use



          $$
          H = frac{1}{2}(A + A^*)
          $$






          share|cite|improve this answer





















          • Alright, I see how this works but how do I apply the mask to a matrix?
            – Peiffap
            40 mins ago










          • @Peiffap Define the matrix M and then run A = M[mask]
            – caverac
            39 mins ago












          • M[mask], no? Thanks!
            – Peiffap
            38 mins ago















          up vote
          1
          down vote













          This generates a random mask with of a given size n and density dens, you just need to apply it to a dense matrix of your choice



          import numpy as np
          np.random.seed(0)

          n = 300
          dens = 0.2
          mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])

          print(np.count_nonzero(mask) / n**2)


          The result of this is



          >>> 0.20224444444444445


          To force the result to be Hermitian just use



          $$
          H = frac{1}{2}(A + A^*)
          $$






          share|cite|improve this answer





















          • Alright, I see how this works but how do I apply the mask to a matrix?
            – Peiffap
            40 mins ago










          • @Peiffap Define the matrix M and then run A = M[mask]
            – caverac
            39 mins ago












          • M[mask], no? Thanks!
            – Peiffap
            38 mins ago













          up vote
          1
          down vote










          up vote
          1
          down vote









          This generates a random mask with of a given size n and density dens, you just need to apply it to a dense matrix of your choice



          import numpy as np
          np.random.seed(0)

          n = 300
          dens = 0.2
          mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])

          print(np.count_nonzero(mask) / n**2)


          The result of this is



          >>> 0.20224444444444445


          To force the result to be Hermitian just use



          $$
          H = frac{1}{2}(A + A^*)
          $$






          share|cite|improve this answer












          This generates a random mask with of a given size n and density dens, you just need to apply it to a dense matrix of your choice



          import numpy as np
          np.random.seed(0)

          n = 300
          dens = 0.2
          mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])

          print(np.count_nonzero(mask) / n**2)


          The result of this is



          >>> 0.20224444444444445


          To force the result to be Hermitian just use



          $$
          H = frac{1}{2}(A + A^*)
          $$







          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered 50 mins ago









          caverac

          11.7k21027




          11.7k21027












          • Alright, I see how this works but how do I apply the mask to a matrix?
            – Peiffap
            40 mins ago










          • @Peiffap Define the matrix M and then run A = M[mask]
            – caverac
            39 mins ago












          • M[mask], no? Thanks!
            – Peiffap
            38 mins ago


















          • Alright, I see how this works but how do I apply the mask to a matrix?
            – Peiffap
            40 mins ago










          • @Peiffap Define the matrix M and then run A = M[mask]
            – caverac
            39 mins ago












          • M[mask], no? Thanks!
            – Peiffap
            38 mins ago
















          Alright, I see how this works but how do I apply the mask to a matrix?
          – Peiffap
          40 mins ago




          Alright, I see how this works but how do I apply the mask to a matrix?
          – Peiffap
          40 mins ago












          @Peiffap Define the matrix M and then run A = M[mask]
          – caverac
          39 mins ago






          @Peiffap Define the matrix M and then run A = M[mask]
          – caverac
          39 mins ago














          M[mask], no? Thanks!
          – Peiffap
          38 mins ago




          M[mask], no? Thanks!
          – Peiffap
          38 mins ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • 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.


          Use MathJax to format equations. MathJax reference.


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fmath.stackexchange.com%2fquestions%2f3019821%2fgenerating-a-random-sparse-hermitian-matrix-in-python%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

          Cabo Verde

          Gyllenstierna