Cut-Off The Matrix

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
6
down vote

favorite












Task



Given is a square matrix of any dimension and any integer n.



Output all possible matrices(without duplicates) by removing columns and rows from the input matrix such that the determinant of these new matrices is n.



Rules



Output should include original if determinant of original is n.



Output should be all the chopped off matrices.
In case no output matrix is possible, return 0 or None or anything similar.



The input matrix will be at least of size 2x2 and all elements will be integers.



If n=1 empty matrix must also be in the output.



Test Cases



inputs:
[[1,0,5],
[1,0,5],
[1,2,4]],2

[[1, 3, 5],
[1, 3, 5],
[1, 45, 4]],0

[[1, 22, 5, 4],
[1, 3, 5, 5],
[1, 45, 4, 6],
[3, 4, 1, 4]],5

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],-54

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],1

outputs:
[[[2]], [[1, 0], [1, 2]]]

[[[1, 3], [1, 3]], [[1, 5], [1, 5]], [[3, 5], [3, 5]], [[1, 3, 5], [1, 3, 5], [1, 45, 4]]]

[[[5]], [[5, 4], [5, 5]]]

[[[1, 22], [1, -32]], [[9, 0], [-1, -6]]]

[, [[1]], [[1, 4], [1, 5]]]


The shortest code wins.










share|improve this question



















  • 2




    +1 for making me think about why the determinant of the empty matrix really should be 1.
    – ngm
    yesterday















up vote
6
down vote

favorite












Task



Given is a square matrix of any dimension and any integer n.



Output all possible matrices(without duplicates) by removing columns and rows from the input matrix such that the determinant of these new matrices is n.



Rules



Output should include original if determinant of original is n.



Output should be all the chopped off matrices.
In case no output matrix is possible, return 0 or None or anything similar.



The input matrix will be at least of size 2x2 and all elements will be integers.



If n=1 empty matrix must also be in the output.



Test Cases



inputs:
[[1,0,5],
[1,0,5],
[1,2,4]],2

[[1, 3, 5],
[1, 3, 5],
[1, 45, 4]],0

[[1, 22, 5, 4],
[1, 3, 5, 5],
[1, 45, 4, 6],
[3, 4, 1, 4]],5

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],-54

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],1

outputs:
[[[2]], [[1, 0], [1, 2]]]

[[[1, 3], [1, 3]], [[1, 5], [1, 5]], [[3, 5], [3, 5]], [[1, 3, 5], [1, 3, 5], [1, 45, 4]]]

[[[5]], [[5, 4], [5, 5]]]

[[[1, 22], [1, -32]], [[9, 0], [-1, -6]]]

[, [[1]], [[1, 4], [1, 5]]]


The shortest code wins.










share|improve this question



















  • 2




    +1 for making me think about why the determinant of the empty matrix really should be 1.
    – ngm
    yesterday













up vote
6
down vote

favorite









up vote
6
down vote

favorite











Task



Given is a square matrix of any dimension and any integer n.



Output all possible matrices(without duplicates) by removing columns and rows from the input matrix such that the determinant of these new matrices is n.



Rules



Output should include original if determinant of original is n.



Output should be all the chopped off matrices.
In case no output matrix is possible, return 0 or None or anything similar.



The input matrix will be at least of size 2x2 and all elements will be integers.



If n=1 empty matrix must also be in the output.



Test Cases



inputs:
[[1,0,5],
[1,0,5],
[1,2,4]],2

[[1, 3, 5],
[1, 3, 5],
[1, 45, 4]],0

[[1, 22, 5, 4],
[1, 3, 5, 5],
[1, 45, 4, 6],
[3, 4, 1, 4]],5

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],-54

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],1

outputs:
[[[2]], [[1, 0], [1, 2]]]

[[[1, 3], [1, 3]], [[1, 5], [1, 5]], [[3, 5], [3, 5]], [[1, 3, 5], [1, 3, 5], [1, 45, 4]]]

[[[5]], [[5, 4], [5, 5]]]

[[[1, 22], [1, -32]], [[9, 0], [-1, -6]]]

[, [[1]], [[1, 4], [1, 5]]]


The shortest code wins.










share|improve this question















Task



Given is a square matrix of any dimension and any integer n.



Output all possible matrices(without duplicates) by removing columns and rows from the input matrix such that the determinant of these new matrices is n.



Rules



Output should include original if determinant of original is n.



Output should be all the chopped off matrices.
In case no output matrix is possible, return 0 or None or anything similar.



The input matrix will be at least of size 2x2 and all elements will be integers.



If n=1 empty matrix must also be in the output.



Test Cases



inputs:
[[1,0,5],
[1,0,5],
[1,2,4]],2

[[1, 3, 5],
[1, 3, 5],
[1, 45, 4]],0

[[1, 22, 5, 4],
[1, 3, 5, 5],
[1, 45, 4, 6],
[3, 4, 1, 4]],5

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],-54

[[1, 22, 5, 4],
[1, -32, 51, 5],
[1, 45, 9, 0],
[3, 4, -1, -6]],1

outputs:
[[[2]], [[1, 0], [1, 2]]]

[[[1, 3], [1, 3]], [[1, 5], [1, 5]], [[3, 5], [3, 5]], [[1, 3, 5], [1, 3, 5], [1, 45, 4]]]

[[[5]], [[5, 4], [5, 5]]]

[[[1, 22], [1, -32]], [[9, 0], [-1, -6]]]

[, [[1]], [[1, 4], [1, 5]]]


The shortest code wins.







code-golf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Vedant Kandoi

3278




3278







  • 2




    +1 for making me think about why the determinant of the empty matrix really should be 1.
    – ngm
    yesterday













  • 2




    +1 for making me think about why the determinant of the empty matrix really should be 1.
    – ngm
    yesterday








2




2




+1 for making me think about why the determinant of the empty matrix really should be 1.
– ngm
yesterday





+1 for making me think about why the determinant of the empty matrix really should be 1.
– ngm
yesterday











3 Answers
3






active

oldest

votes

















up vote
5
down vote














Jelly, 20 16 bytes



ZŒPZœcLƊ€ẎQÆḊ⁼¥Ƈ


Try it online!



Challenge finished. Unfortunately, my brain wasn't in its best mood, so Dennis pushed it until I finally managed to get here (you can see how it went). ~1 hour.






share|improve this answer





























    up vote
    4
    down vote













    JavaScript (ES6), 246 238 196 192 bytes



    Credits: the function $D$ is derived from this answer by edc65.



    Takes input as (integer)(matrix). Prints the results to the console.





    d=>g=m=>(F=(a,i)=>a.filter(_=>i--),m.map((_,i)=>m.map((_,j)=>g(F(m,i).map(r=>F(r,j))))),D=m=>m+m?m.reduce((n,[r],i)=>n+(i&1?-r:r)*D(F(m,i).map(r=>F(r,0))),0):1)(m)-d||g[m]||console.log(g[m]=m)


    Try it online!



    How?



    Removing rows and columns



    The helper function $F$ creates a copy of a given array $a$ with the $i^textth$ item removed (0-indexed).



    F = (a, i) => a.filter(_ => i--)


    Computing the determinant



    The recursive function $D$ computes the determinant of a matrix $m$, using Laplace expansion.



    D = m => // m = matrix
    m + m ? // if the matrix is not empty:
    m.reduce((n, [r], i) => // for each value r at (0, i):
    n // take the previous sum n
    + (i & 1 ? -r : r) // using the parity of i, either add or subtract r
    * D( // multiplied by the result of a recursive call:
    F(m, i) // using m with the i-th row removed
    .map(r => F(r, 0)) // and the first column removed
    ), // end of recursive call
    0 // start with n = 0
    ) // end of reduce()
    : // else (the matrix is empty):
    1 // determinant = 1


    Example:



    $$m=beginbmatrixcolorred1&colorred2&colorred3\colorgreen4&colorgreen5&colorgreen6\colorblue7&colorblue8&colorblue9endbmatrix$$



    First iteration:



    $$|m|=
    colorred1timesleft|beginbmatrixcolorgreen5&colorgreen6\colorblue8&colorblue9endbmatrixright|
    -colorgreen4timesleft|beginbmatrixcolorred2&colorred3\colorblue8&colorblue9endbmatrixright|
    +colorblue7timesleft|beginbmatrixcolorred2&colorred3\colorgreen5&colorgreen6endbmatrixright|$$



    which eventually leads to:



    $$1times(5times9-8times6)-4times(2times9-8times3)+7times(2times6-5times3) = 0$$



    Main function



    We recursively remove rows and columns from the original matrix $m$ and invoke $D$ on all resulting matrices, printing those whose determinant is equal to the expected value $d$.



    d => // d = expected determinant value
    g = m => // g = recursive function taking the matrix m,
    ( // also used to store valid matrices
    m.map((_, i) => // for each i in [0 ... m.length - 1]:
    m.map((_, j) => // for each j in [0 ... m.length - 1]:
    g( // do a recursive call with:
    F(m, i) // the i-th row of m removed
    .map(r => F(r, j)) // the j-th column of m removed
    ) // end of recursive call
    ) // end of inner map()
    ), // end of outer map()
    D // invoke D ...
    )(m) // ... on m
    - d // provided that the result is equal to d
    || g[m] // and provided that this matrix was not already printed,
    || console.log(g[m] = m) // print m and store it in the underlying object of g





    share|improve this answer





























      up vote
      2
      down vote














      R, 129 bytes





      f=function(m,n,o=,z=nrow(m))for(i in r<-1:z)for(j in r)o=c(o,if(round(det(m))==n)list(m),if(z)f(m[-i,-j,drop=F],n));unique(o)


      Try it online!



      Recursive function returning a list of submatrices. Outputs NULL for no solution. It is a bit unfortunate that the determinant value has to be explicitly rounded (otherwise some tests fail due to numerical imprecision), but at least we have a built-in for calculating it.






      share|improve this answer




















      • Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
        – ngm
        6 hours 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.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "200"
      ;
      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: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      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%2fcodegolf.stackexchange.com%2fquestions%2f175848%2fcut-off-the-matrix%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      5
      down vote














      Jelly, 20 16 bytes



      ZŒPZœcLƊ€ẎQÆḊ⁼¥Ƈ


      Try it online!



      Challenge finished. Unfortunately, my brain wasn't in its best mood, so Dennis pushed it until I finally managed to get here (you can see how it went). ~1 hour.






      share|improve this answer


























        up vote
        5
        down vote














        Jelly, 20 16 bytes



        ZŒPZœcLƊ€ẎQÆḊ⁼¥Ƈ


        Try it online!



        Challenge finished. Unfortunately, my brain wasn't in its best mood, so Dennis pushed it until I finally managed to get here (you can see how it went). ~1 hour.






        share|improve this answer
























          up vote
          5
          down vote










          up vote
          5
          down vote










          Jelly, 20 16 bytes



          ZŒPZœcLƊ€ẎQÆḊ⁼¥Ƈ


          Try it online!



          Challenge finished. Unfortunately, my brain wasn't in its best mood, so Dennis pushed it until I finally managed to get here (you can see how it went). ~1 hour.






          share|improve this answer















          Jelly, 20 16 bytes



          ZŒPZœcLƊ€ẎQÆḊ⁼¥Ƈ


          Try it online!



          Challenge finished. Unfortunately, my brain wasn't in its best mood, so Dennis pushed it until I finally managed to get here (you can see how it went). ~1 hour.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered yesterday









          Erik the Outgolfer

          30.3k428100




          30.3k428100




















              up vote
              4
              down vote













              JavaScript (ES6), 246 238 196 192 bytes



              Credits: the function $D$ is derived from this answer by edc65.



              Takes input as (integer)(matrix). Prints the results to the console.





              d=>g=m=>(F=(a,i)=>a.filter(_=>i--),m.map((_,i)=>m.map((_,j)=>g(F(m,i).map(r=>F(r,j))))),D=m=>m+m?m.reduce((n,[r],i)=>n+(i&1?-r:r)*D(F(m,i).map(r=>F(r,0))),0):1)(m)-d||g[m]||console.log(g[m]=m)


              Try it online!



              How?



              Removing rows and columns



              The helper function $F$ creates a copy of a given array $a$ with the $i^textth$ item removed (0-indexed).



              F = (a, i) => a.filter(_ => i--)


              Computing the determinant



              The recursive function $D$ computes the determinant of a matrix $m$, using Laplace expansion.



              D = m => // m = matrix
              m + m ? // if the matrix is not empty:
              m.reduce((n, [r], i) => // for each value r at (0, i):
              n // take the previous sum n
              + (i & 1 ? -r : r) // using the parity of i, either add or subtract r
              * D( // multiplied by the result of a recursive call:
              F(m, i) // using m with the i-th row removed
              .map(r => F(r, 0)) // and the first column removed
              ), // end of recursive call
              0 // start with n = 0
              ) // end of reduce()
              : // else (the matrix is empty):
              1 // determinant = 1


              Example:



              $$m=beginbmatrixcolorred1&colorred2&colorred3\colorgreen4&colorgreen5&colorgreen6\colorblue7&colorblue8&colorblue9endbmatrix$$



              First iteration:



              $$|m|=
              colorred1timesleft|beginbmatrixcolorgreen5&colorgreen6\colorblue8&colorblue9endbmatrixright|
              -colorgreen4timesleft|beginbmatrixcolorred2&colorred3\colorblue8&colorblue9endbmatrixright|
              +colorblue7timesleft|beginbmatrixcolorred2&colorred3\colorgreen5&colorgreen6endbmatrixright|$$



              which eventually leads to:



              $$1times(5times9-8times6)-4times(2times9-8times3)+7times(2times6-5times3) = 0$$



              Main function



              We recursively remove rows and columns from the original matrix $m$ and invoke $D$ on all resulting matrices, printing those whose determinant is equal to the expected value $d$.



              d => // d = expected determinant value
              g = m => // g = recursive function taking the matrix m,
              ( // also used to store valid matrices
              m.map((_, i) => // for each i in [0 ... m.length - 1]:
              m.map((_, j) => // for each j in [0 ... m.length - 1]:
              g( // do a recursive call with:
              F(m, i) // the i-th row of m removed
              .map(r => F(r, j)) // the j-th column of m removed
              ) // end of recursive call
              ) // end of inner map()
              ), // end of outer map()
              D // invoke D ...
              )(m) // ... on m
              - d // provided that the result is equal to d
              || g[m] // and provided that this matrix was not already printed,
              || console.log(g[m] = m) // print m and store it in the underlying object of g





              share|improve this answer


























                up vote
                4
                down vote













                JavaScript (ES6), 246 238 196 192 bytes



                Credits: the function $D$ is derived from this answer by edc65.



                Takes input as (integer)(matrix). Prints the results to the console.





                d=>g=m=>(F=(a,i)=>a.filter(_=>i--),m.map((_,i)=>m.map((_,j)=>g(F(m,i).map(r=>F(r,j))))),D=m=>m+m?m.reduce((n,[r],i)=>n+(i&1?-r:r)*D(F(m,i).map(r=>F(r,0))),0):1)(m)-d||g[m]||console.log(g[m]=m)


                Try it online!



                How?



                Removing rows and columns



                The helper function $F$ creates a copy of a given array $a$ with the $i^textth$ item removed (0-indexed).



                F = (a, i) => a.filter(_ => i--)


                Computing the determinant



                The recursive function $D$ computes the determinant of a matrix $m$, using Laplace expansion.



                D = m => // m = matrix
                m + m ? // if the matrix is not empty:
                m.reduce((n, [r], i) => // for each value r at (0, i):
                n // take the previous sum n
                + (i & 1 ? -r : r) // using the parity of i, either add or subtract r
                * D( // multiplied by the result of a recursive call:
                F(m, i) // using m with the i-th row removed
                .map(r => F(r, 0)) // and the first column removed
                ), // end of recursive call
                0 // start with n = 0
                ) // end of reduce()
                : // else (the matrix is empty):
                1 // determinant = 1


                Example:



                $$m=beginbmatrixcolorred1&colorred2&colorred3\colorgreen4&colorgreen5&colorgreen6\colorblue7&colorblue8&colorblue9endbmatrix$$



                First iteration:



                $$|m|=
                colorred1timesleft|beginbmatrixcolorgreen5&colorgreen6\colorblue8&colorblue9endbmatrixright|
                -colorgreen4timesleft|beginbmatrixcolorred2&colorred3\colorblue8&colorblue9endbmatrixright|
                +colorblue7timesleft|beginbmatrixcolorred2&colorred3\colorgreen5&colorgreen6endbmatrixright|$$



                which eventually leads to:



                $$1times(5times9-8times6)-4times(2times9-8times3)+7times(2times6-5times3) = 0$$



                Main function



                We recursively remove rows and columns from the original matrix $m$ and invoke $D$ on all resulting matrices, printing those whose determinant is equal to the expected value $d$.



                d => // d = expected determinant value
                g = m => // g = recursive function taking the matrix m,
                ( // also used to store valid matrices
                m.map((_, i) => // for each i in [0 ... m.length - 1]:
                m.map((_, j) => // for each j in [0 ... m.length - 1]:
                g( // do a recursive call with:
                F(m, i) // the i-th row of m removed
                .map(r => F(r, j)) // the j-th column of m removed
                ) // end of recursive call
                ) // end of inner map()
                ), // end of outer map()
                D // invoke D ...
                )(m) // ... on m
                - d // provided that the result is equal to d
                || g[m] // and provided that this matrix was not already printed,
                || console.log(g[m] = m) // print m and store it in the underlying object of g





                share|improve this answer
























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  JavaScript (ES6), 246 238 196 192 bytes



                  Credits: the function $D$ is derived from this answer by edc65.



                  Takes input as (integer)(matrix). Prints the results to the console.





                  d=>g=m=>(F=(a,i)=>a.filter(_=>i--),m.map((_,i)=>m.map((_,j)=>g(F(m,i).map(r=>F(r,j))))),D=m=>m+m?m.reduce((n,[r],i)=>n+(i&1?-r:r)*D(F(m,i).map(r=>F(r,0))),0):1)(m)-d||g[m]||console.log(g[m]=m)


                  Try it online!



                  How?



                  Removing rows and columns



                  The helper function $F$ creates a copy of a given array $a$ with the $i^textth$ item removed (0-indexed).



                  F = (a, i) => a.filter(_ => i--)


                  Computing the determinant



                  The recursive function $D$ computes the determinant of a matrix $m$, using Laplace expansion.



                  D = m => // m = matrix
                  m + m ? // if the matrix is not empty:
                  m.reduce((n, [r], i) => // for each value r at (0, i):
                  n // take the previous sum n
                  + (i & 1 ? -r : r) // using the parity of i, either add or subtract r
                  * D( // multiplied by the result of a recursive call:
                  F(m, i) // using m with the i-th row removed
                  .map(r => F(r, 0)) // and the first column removed
                  ), // end of recursive call
                  0 // start with n = 0
                  ) // end of reduce()
                  : // else (the matrix is empty):
                  1 // determinant = 1


                  Example:



                  $$m=beginbmatrixcolorred1&colorred2&colorred3\colorgreen4&colorgreen5&colorgreen6\colorblue7&colorblue8&colorblue9endbmatrix$$



                  First iteration:



                  $$|m|=
                  colorred1timesleft|beginbmatrixcolorgreen5&colorgreen6\colorblue8&colorblue9endbmatrixright|
                  -colorgreen4timesleft|beginbmatrixcolorred2&colorred3\colorblue8&colorblue9endbmatrixright|
                  +colorblue7timesleft|beginbmatrixcolorred2&colorred3\colorgreen5&colorgreen6endbmatrixright|$$



                  which eventually leads to:



                  $$1times(5times9-8times6)-4times(2times9-8times3)+7times(2times6-5times3) = 0$$



                  Main function



                  We recursively remove rows and columns from the original matrix $m$ and invoke $D$ on all resulting matrices, printing those whose determinant is equal to the expected value $d$.



                  d => // d = expected determinant value
                  g = m => // g = recursive function taking the matrix m,
                  ( // also used to store valid matrices
                  m.map((_, i) => // for each i in [0 ... m.length - 1]:
                  m.map((_, j) => // for each j in [0 ... m.length - 1]:
                  g( // do a recursive call with:
                  F(m, i) // the i-th row of m removed
                  .map(r => F(r, j)) // the j-th column of m removed
                  ) // end of recursive call
                  ) // end of inner map()
                  ), // end of outer map()
                  D // invoke D ...
                  )(m) // ... on m
                  - d // provided that the result is equal to d
                  || g[m] // and provided that this matrix was not already printed,
                  || console.log(g[m] = m) // print m and store it in the underlying object of g





                  share|improve this answer














                  JavaScript (ES6), 246 238 196 192 bytes



                  Credits: the function $D$ is derived from this answer by edc65.



                  Takes input as (integer)(matrix). Prints the results to the console.





                  d=>g=m=>(F=(a,i)=>a.filter(_=>i--),m.map((_,i)=>m.map((_,j)=>g(F(m,i).map(r=>F(r,j))))),D=m=>m+m?m.reduce((n,[r],i)=>n+(i&1?-r:r)*D(F(m,i).map(r=>F(r,0))),0):1)(m)-d||g[m]||console.log(g[m]=m)


                  Try it online!



                  How?



                  Removing rows and columns



                  The helper function $F$ creates a copy of a given array $a$ with the $i^textth$ item removed (0-indexed).



                  F = (a, i) => a.filter(_ => i--)


                  Computing the determinant



                  The recursive function $D$ computes the determinant of a matrix $m$, using Laplace expansion.



                  D = m => // m = matrix
                  m + m ? // if the matrix is not empty:
                  m.reduce((n, [r], i) => // for each value r at (0, i):
                  n // take the previous sum n
                  + (i & 1 ? -r : r) // using the parity of i, either add or subtract r
                  * D( // multiplied by the result of a recursive call:
                  F(m, i) // using m with the i-th row removed
                  .map(r => F(r, 0)) // and the first column removed
                  ), // end of recursive call
                  0 // start with n = 0
                  ) // end of reduce()
                  : // else (the matrix is empty):
                  1 // determinant = 1


                  Example:



                  $$m=beginbmatrixcolorred1&colorred2&colorred3\colorgreen4&colorgreen5&colorgreen6\colorblue7&colorblue8&colorblue9endbmatrix$$



                  First iteration:



                  $$|m|=
                  colorred1timesleft|beginbmatrixcolorgreen5&colorgreen6\colorblue8&colorblue9endbmatrixright|
                  -colorgreen4timesleft|beginbmatrixcolorred2&colorred3\colorblue8&colorblue9endbmatrixright|
                  +colorblue7timesleft|beginbmatrixcolorred2&colorred3\colorgreen5&colorgreen6endbmatrixright|$$



                  which eventually leads to:



                  $$1times(5times9-8times6)-4times(2times9-8times3)+7times(2times6-5times3) = 0$$



                  Main function



                  We recursively remove rows and columns from the original matrix $m$ and invoke $D$ on all resulting matrices, printing those whose determinant is equal to the expected value $d$.



                  d => // d = expected determinant value
                  g = m => // g = recursive function taking the matrix m,
                  ( // also used to store valid matrices
                  m.map((_, i) => // for each i in [0 ... m.length - 1]:
                  m.map((_, j) => // for each j in [0 ... m.length - 1]:
                  g( // do a recursive call with:
                  F(m, i) // the i-th row of m removed
                  .map(r => F(r, j)) // the j-th column of m removed
                  ) // end of recursive call
                  ) // end of inner map()
                  ), // end of outer map()
                  D // invoke D ...
                  )(m) // ... on m
                  - d // provided that the result is equal to d
                  || g[m] // and provided that this matrix was not already printed,
                  || console.log(g[m] = m) // print m and store it in the underlying object of g






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 13 hours ago

























                  answered yesterday









                  Arnauld

                  68.5k584289




                  68.5k584289




















                      up vote
                      2
                      down vote














                      R, 129 bytes





                      f=function(m,n,o=,z=nrow(m))for(i in r<-1:z)for(j in r)o=c(o,if(round(det(m))==n)list(m),if(z)f(m[-i,-j,drop=F],n));unique(o)


                      Try it online!



                      Recursive function returning a list of submatrices. Outputs NULL for no solution. It is a bit unfortunate that the determinant value has to be explicitly rounded (otherwise some tests fail due to numerical imprecision), but at least we have a built-in for calculating it.






                      share|improve this answer




















                      • Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
                        – ngm
                        6 hours ago














                      up vote
                      2
                      down vote














                      R, 129 bytes





                      f=function(m,n,o=,z=nrow(m))for(i in r<-1:z)for(j in r)o=c(o,if(round(det(m))==n)list(m),if(z)f(m[-i,-j,drop=F],n));unique(o)


                      Try it online!



                      Recursive function returning a list of submatrices. Outputs NULL for no solution. It is a bit unfortunate that the determinant value has to be explicitly rounded (otherwise some tests fail due to numerical imprecision), but at least we have a built-in for calculating it.






                      share|improve this answer




















                      • Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
                        – ngm
                        6 hours ago












                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote










                      R, 129 bytes





                      f=function(m,n,o=,z=nrow(m))for(i in r<-1:z)for(j in r)o=c(o,if(round(det(m))==n)list(m),if(z)f(m[-i,-j,drop=F],n));unique(o)


                      Try it online!



                      Recursive function returning a list of submatrices. Outputs NULL for no solution. It is a bit unfortunate that the determinant value has to be explicitly rounded (otherwise some tests fail due to numerical imprecision), but at least we have a built-in for calculating it.






                      share|improve this answer













                      R, 129 bytes





                      f=function(m,n,o=,z=nrow(m))for(i in r<-1:z)for(j in r)o=c(o,if(round(det(m))==n)list(m),if(z)f(m[-i,-j,drop=F],n));unique(o)


                      Try it online!



                      Recursive function returning a list of submatrices. Outputs NULL for no solution. It is a bit unfortunate that the determinant value has to be explicitly rounded (otherwise some tests fail due to numerical imprecision), but at least we have a built-in for calculating it.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered 6 hours ago









                      Kirill L.

                      3,1961118




                      3,1961118











                      • Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
                        – ngm
                        6 hours ago
















                      • Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
                        – ngm
                        6 hours ago















                      Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
                      – ngm
                      6 hours ago




                      Here's a case where the growing object o seems like it must be explicitly initialized to NULL and the built-in abuse of F or T won't work.
                      – ngm
                      6 hours ago

















                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175848%2fcut-off-the-matrix%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay