How to reverse engineer original array from boolean indexed array?










1















Ok so I wrote some code for vectorizing a symmetric matrix, it just takes the unique elements and turns them into a 1d vector, while also multiplying the off diagonal elements by root2:



def vectorize_mat(mat):
assert mat.shape[0] == mat.shape[1], 'Matrix is not square'

n = int(mat.shape[0])
vec_len = 0.5*n*(n+1)
weight_mat = (np.tri(n,k=-1)*np.sqrt(2))+np.identity(n)
mask_mat = np.tri(n).astype(bool)
vec_mat = (mat*weight_mat)[mask_mat]

return vec_mat


and this works really well, now I'm trying to figure out how to reconstruct the original array from the vector. I've gotten the original matrix dimensions like so:



v = len(vec_mat)
n = isqrt(2*v)


where isqrt() is an integer square root from:Integer square root in python



but I'm struggling with what to do next. I can now reconstruct the weight and mask matrices. So obviously I could vectorize the weight matrix and divide the vector by it, or divide the reconstructed matrix by the weight matrix to undo that step, but it's the reshaping and stuff (from the boolean indexing) that I don't know how to do. Maybe there's some super simple answer out there,but I can't seem to see it.










share|improve this question


























    1















    Ok so I wrote some code for vectorizing a symmetric matrix, it just takes the unique elements and turns them into a 1d vector, while also multiplying the off diagonal elements by root2:



    def vectorize_mat(mat):
    assert mat.shape[0] == mat.shape[1], 'Matrix is not square'

    n = int(mat.shape[0])
    vec_len = 0.5*n*(n+1)
    weight_mat = (np.tri(n,k=-1)*np.sqrt(2))+np.identity(n)
    mask_mat = np.tri(n).astype(bool)
    vec_mat = (mat*weight_mat)[mask_mat]

    return vec_mat


    and this works really well, now I'm trying to figure out how to reconstruct the original array from the vector. I've gotten the original matrix dimensions like so:



    v = len(vec_mat)
    n = isqrt(2*v)


    where isqrt() is an integer square root from:Integer square root in python



    but I'm struggling with what to do next. I can now reconstruct the weight and mask matrices. So obviously I could vectorize the weight matrix and divide the vector by it, or divide the reconstructed matrix by the weight matrix to undo that step, but it's the reshaping and stuff (from the boolean indexing) that I don't know how to do. Maybe there's some super simple answer out there,but I can't seem to see it.










    share|improve this question
























      1












      1








      1








      Ok so I wrote some code for vectorizing a symmetric matrix, it just takes the unique elements and turns them into a 1d vector, while also multiplying the off diagonal elements by root2:



      def vectorize_mat(mat):
      assert mat.shape[0] == mat.shape[1], 'Matrix is not square'

      n = int(mat.shape[0])
      vec_len = 0.5*n*(n+1)
      weight_mat = (np.tri(n,k=-1)*np.sqrt(2))+np.identity(n)
      mask_mat = np.tri(n).astype(bool)
      vec_mat = (mat*weight_mat)[mask_mat]

      return vec_mat


      and this works really well, now I'm trying to figure out how to reconstruct the original array from the vector. I've gotten the original matrix dimensions like so:



      v = len(vec_mat)
      n = isqrt(2*v)


      where isqrt() is an integer square root from:Integer square root in python



      but I'm struggling with what to do next. I can now reconstruct the weight and mask matrices. So obviously I could vectorize the weight matrix and divide the vector by it, or divide the reconstructed matrix by the weight matrix to undo that step, but it's the reshaping and stuff (from the boolean indexing) that I don't know how to do. Maybe there's some super simple answer out there,but I can't seem to see it.










      share|improve this question














      Ok so I wrote some code for vectorizing a symmetric matrix, it just takes the unique elements and turns them into a 1d vector, while also multiplying the off diagonal elements by root2:



      def vectorize_mat(mat):
      assert mat.shape[0] == mat.shape[1], 'Matrix is not square'

      n = int(mat.shape[0])
      vec_len = 0.5*n*(n+1)
      weight_mat = (np.tri(n,k=-1)*np.sqrt(2))+np.identity(n)
      mask_mat = np.tri(n).astype(bool)
      vec_mat = (mat*weight_mat)[mask_mat]

      return vec_mat


      and this works really well, now I'm trying to figure out how to reconstruct the original array from the vector. I've gotten the original matrix dimensions like so:



      v = len(vec_mat)
      n = isqrt(2*v)


      where isqrt() is an integer square root from:Integer square root in python



      but I'm struggling with what to do next. I can now reconstruct the weight and mask matrices. So obviously I could vectorize the weight matrix and divide the vector by it, or divide the reconstructed matrix by the weight matrix to undo that step, but it's the reshaping and stuff (from the boolean indexing) that I don't know how to do. Maybe there's some super simple answer out there,but I can't seem to see it.







      python arrays numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 11:08









      bidbybidby

      92112




      92112






















          1 Answer
          1






          active

          oldest

          votes


















          1














          To answer your headline question. Indexing - including boolean indexing - can be used for assignment.



          Here is an example. Let us first extract the lower triangle using a mask.



          >>> a = np.arange(25).reshape(5, 5)
          >>> y, x = np.ogrid[:5, :5]
          >>> lower = y>=x
          >>> b = a[lower]


          Now b contains the lower triangle. We can use the same mask to reconstruct the lower triangle and fill the upper triangle symmetrically:



          >>> recon = np.empty_like(a)
          >>> recon[lower] = b
          >>> recon.T[lower] = b
          >>> recon
          array([[ 0, 5, 10, 15, 20],
          [ 5, 6, 11, 16, 21],
          [10, 11, 12, 17, 22],
          [15, 16, 17, 18, 23],
          [20, 21, 22, 23, 24]])





          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%2f53318116%2fhow-to-reverse-engineer-original-array-from-boolean-indexed-array%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









            1














            To answer your headline question. Indexing - including boolean indexing - can be used for assignment.



            Here is an example. Let us first extract the lower triangle using a mask.



            >>> a = np.arange(25).reshape(5, 5)
            >>> y, x = np.ogrid[:5, :5]
            >>> lower = y>=x
            >>> b = a[lower]


            Now b contains the lower triangle. We can use the same mask to reconstruct the lower triangle and fill the upper triangle symmetrically:



            >>> recon = np.empty_like(a)
            >>> recon[lower] = b
            >>> recon.T[lower] = b
            >>> recon
            array([[ 0, 5, 10, 15, 20],
            [ 5, 6, 11, 16, 21],
            [10, 11, 12, 17, 22],
            [15, 16, 17, 18, 23],
            [20, 21, 22, 23, 24]])





            share|improve this answer



























              1














              To answer your headline question. Indexing - including boolean indexing - can be used for assignment.



              Here is an example. Let us first extract the lower triangle using a mask.



              >>> a = np.arange(25).reshape(5, 5)
              >>> y, x = np.ogrid[:5, :5]
              >>> lower = y>=x
              >>> b = a[lower]


              Now b contains the lower triangle. We can use the same mask to reconstruct the lower triangle and fill the upper triangle symmetrically:



              >>> recon = np.empty_like(a)
              >>> recon[lower] = b
              >>> recon.T[lower] = b
              >>> recon
              array([[ 0, 5, 10, 15, 20],
              [ 5, 6, 11, 16, 21],
              [10, 11, 12, 17, 22],
              [15, 16, 17, 18, 23],
              [20, 21, 22, 23, 24]])





              share|improve this answer

























                1












                1








                1







                To answer your headline question. Indexing - including boolean indexing - can be used for assignment.



                Here is an example. Let us first extract the lower triangle using a mask.



                >>> a = np.arange(25).reshape(5, 5)
                >>> y, x = np.ogrid[:5, :5]
                >>> lower = y>=x
                >>> b = a[lower]


                Now b contains the lower triangle. We can use the same mask to reconstruct the lower triangle and fill the upper triangle symmetrically:



                >>> recon = np.empty_like(a)
                >>> recon[lower] = b
                >>> recon.T[lower] = b
                >>> recon
                array([[ 0, 5, 10, 15, 20],
                [ 5, 6, 11, 16, 21],
                [10, 11, 12, 17, 22],
                [15, 16, 17, 18, 23],
                [20, 21, 22, 23, 24]])





                share|improve this answer













                To answer your headline question. Indexing - including boolean indexing - can be used for assignment.



                Here is an example. Let us first extract the lower triangle using a mask.



                >>> a = np.arange(25).reshape(5, 5)
                >>> y, x = np.ogrid[:5, :5]
                >>> lower = y>=x
                >>> b = a[lower]


                Now b contains the lower triangle. We can use the same mask to reconstruct the lower triangle and fill the upper triangle symmetrically:



                >>> recon = np.empty_like(a)
                >>> recon[lower] = b
                >>> recon.T[lower] = b
                >>> recon
                array([[ 0, 5, 10, 15, 20],
                [ 5, 6, 11, 16, 21],
                [10, 11, 12, 17, 22],
                [15, 16, 17, 18, 23],
                [20, 21, 22, 23, 24]])






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 12:20









                Paul PanzerPaul Panzer

                30.3k21744




                30.3k21744





























                    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%2f53318116%2fhow-to-reverse-engineer-original-array-from-boolean-indexed-array%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







                    這個網誌中的熱門文章

                    What does pagestruct do in Eviews?

                    Dutch intervention in Lombok and Karangasem

                    Channel Islands