Minimal Centrosymmetrization

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











up vote
10
down vote

favorite












Topically related.



Objective: Given a matrix of positive integers $M$, output the smallest centrosymmetric matrix which contains $M$ (this matrix may contain non-positive integers as well).



A centrosymmetric matrix is a square matrix with rotational symmetry of order 2—i.e. it remains the same matrix after rotating it twice. For example, a centrosymmetric matrix has the top-left element the same as the bottom-right, and the element above the center the same as the one below the center. A useful visualization can be found here.



More formally, given a matrix $M$, produce a square matrix $N$ such that $N$ is centrosymmetric and $Msubseteq N$, and there is no other square matrix $K$ such that $dim K<dim N$.



$A$ is a subset of $B$ (notation: $Asubseteq B$) if and only if each value $A_i,j$ appears at index $B_i+i^prime,j+j^prime$ for some pair of integers $(i^prime, j^prime)$.



Note: some matrices have multiple solutions (e.g. [[3,3],[1,2]] being solved as [[2,1,0],[3,3,3],[0,1,2]] or [[3,3,3],[1,2,1],[3,3,3]]); you must output at least one of the valid solutions.



Test cases



input
example output

[[1, 2, 3],
[4, 5, 6]]
[[1, 2, 3, 0],
[4, 5, 6, 0],
[0, 6, 5, 4],
[0, 3, 2, 1]]

[[9]]
[[9]]

[[9, 10]]
[[9, 10],
[10, 9]]

[[100, 200, 300]]
[[100, 200, 300],
[ 0, 0, 0],
[300, 200, 100]]

[[1, 2, 3],
[4, 5, 4]]
[[1, 2, 3],
[4, 5, 4]
[3, 2, 1]]

[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]
[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]

[[4, 5, 4],
[1, 2, 3]]
[[3, 2, 1],
[4, 5, 4],
[1, 2, 3]]

[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1]]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]






share|improve this question





















  • Why do Centrosymmetric matrices have to be square?
    – W W
    yesterday











  • @WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition
    – Conor O'Brien
    yesterday










  • I was wondering why you made that choice
    – W W
    yesterday






  • 2




    @WW it was a simplification I thought to be useful for clarity
    – Conor O'Brien
    yesterday














up vote
10
down vote

favorite












Topically related.



Objective: Given a matrix of positive integers $M$, output the smallest centrosymmetric matrix which contains $M$ (this matrix may contain non-positive integers as well).



A centrosymmetric matrix is a square matrix with rotational symmetry of order 2—i.e. it remains the same matrix after rotating it twice. For example, a centrosymmetric matrix has the top-left element the same as the bottom-right, and the element above the center the same as the one below the center. A useful visualization can be found here.



More formally, given a matrix $M$, produce a square matrix $N$ such that $N$ is centrosymmetric and $Msubseteq N$, and there is no other square matrix $K$ such that $dim K<dim N$.



$A$ is a subset of $B$ (notation: $Asubseteq B$) if and only if each value $A_i,j$ appears at index $B_i+i^prime,j+j^prime$ for some pair of integers $(i^prime, j^prime)$.



Note: some matrices have multiple solutions (e.g. [[3,3],[1,2]] being solved as [[2,1,0],[3,3,3],[0,1,2]] or [[3,3,3],[1,2,1],[3,3,3]]); you must output at least one of the valid solutions.



Test cases



input
example output

[[1, 2, 3],
[4, 5, 6]]
[[1, 2, 3, 0],
[4, 5, 6, 0],
[0, 6, 5, 4],
[0, 3, 2, 1]]

[[9]]
[[9]]

[[9, 10]]
[[9, 10],
[10, 9]]

[[100, 200, 300]]
[[100, 200, 300],
[ 0, 0, 0],
[300, 200, 100]]

[[1, 2, 3],
[4, 5, 4]]
[[1, 2, 3],
[4, 5, 4]
[3, 2, 1]]

[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]
[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]

[[4, 5, 4],
[1, 2, 3]]
[[3, 2, 1],
[4, 5, 4],
[1, 2, 3]]

[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1]]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]






share|improve this question





















  • Why do Centrosymmetric matrices have to be square?
    – W W
    yesterday











  • @WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition
    – Conor O'Brien
    yesterday










  • I was wondering why you made that choice
    – W W
    yesterday






  • 2




    @WW it was a simplification I thought to be useful for clarity
    – Conor O'Brien
    yesterday












up vote
10
down vote

favorite









up vote
10
down vote

favorite











Topically related.



Objective: Given a matrix of positive integers $M$, output the smallest centrosymmetric matrix which contains $M$ (this matrix may contain non-positive integers as well).



A centrosymmetric matrix is a square matrix with rotational symmetry of order 2—i.e. it remains the same matrix after rotating it twice. For example, a centrosymmetric matrix has the top-left element the same as the bottom-right, and the element above the center the same as the one below the center. A useful visualization can be found here.



More formally, given a matrix $M$, produce a square matrix $N$ such that $N$ is centrosymmetric and $Msubseteq N$, and there is no other square matrix $K$ such that $dim K<dim N$.



$A$ is a subset of $B$ (notation: $Asubseteq B$) if and only if each value $A_i,j$ appears at index $B_i+i^prime,j+j^prime$ for some pair of integers $(i^prime, j^prime)$.



Note: some matrices have multiple solutions (e.g. [[3,3],[1,2]] being solved as [[2,1,0],[3,3,3],[0,1,2]] or [[3,3,3],[1,2,1],[3,3,3]]); you must output at least one of the valid solutions.



Test cases



input
example output

[[1, 2, 3],
[4, 5, 6]]
[[1, 2, 3, 0],
[4, 5, 6, 0],
[0, 6, 5, 4],
[0, 3, 2, 1]]

[[9]]
[[9]]

[[9, 10]]
[[9, 10],
[10, 9]]

[[100, 200, 300]]
[[100, 200, 300],
[ 0, 0, 0],
[300, 200, 100]]

[[1, 2, 3],
[4, 5, 4]]
[[1, 2, 3],
[4, 5, 4]
[3, 2, 1]]

[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]
[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]

[[4, 5, 4],
[1, 2, 3]]
[[3, 2, 1],
[4, 5, 4],
[1, 2, 3]]

[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1]]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]






share|improve this question













Topically related.



Objective: Given a matrix of positive integers $M$, output the smallest centrosymmetric matrix which contains $M$ (this matrix may contain non-positive integers as well).



A centrosymmetric matrix is a square matrix with rotational symmetry of order 2—i.e. it remains the same matrix after rotating it twice. For example, a centrosymmetric matrix has the top-left element the same as the bottom-right, and the element above the center the same as the one below the center. A useful visualization can be found here.



More formally, given a matrix $M$, produce a square matrix $N$ such that $N$ is centrosymmetric and $Msubseteq N$, and there is no other square matrix $K$ such that $dim K<dim N$.



$A$ is a subset of $B$ (notation: $Asubseteq B$) if and only if each value $A_i,j$ appears at index $B_i+i^prime,j+j^prime$ for some pair of integers $(i^prime, j^prime)$.



Note: some matrices have multiple solutions (e.g. [[3,3],[1,2]] being solved as [[2,1,0],[3,3,3],[0,1,2]] or [[3,3,3],[1,2,1],[3,3,3]]); you must output at least one of the valid solutions.



Test cases



input
example output

[[1, 2, 3],
[4, 5, 6]]
[[1, 2, 3, 0],
[4, 5, 6, 0],
[0, 6, 5, 4],
[0, 3, 2, 1]]

[[9]]
[[9]]

[[9, 10]]
[[9, 10],
[10, 9]]

[[100, 200, 300]]
[[100, 200, 300],
[ 0, 0, 0],
[300, 200, 100]]

[[1, 2, 3],
[4, 5, 4]]
[[1, 2, 3],
[4, 5, 4]
[3, 2, 1]]

[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]
[[1, 2, 3],
[5, 6, 5],
[3, 2, 1]]

[[4, 5, 4],
[1, 2, 3]]
[[3, 2, 1],
[4, 5, 4],
[1, 2, 3]]

[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1]]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1],
[9, 9, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]








share|improve this question












share|improve this question




share|improve this question








edited yesterday
























asked yesterday









Conor O'Brien

27.7k260155




27.7k260155











  • Why do Centrosymmetric matrices have to be square?
    – W W
    yesterday











  • @WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition
    – Conor O'Brien
    yesterday










  • I was wondering why you made that choice
    – W W
    yesterday






  • 2




    @WW it was a simplification I thought to be useful for clarity
    – Conor O'Brien
    yesterday
















  • Why do Centrosymmetric matrices have to be square?
    – W W
    yesterday











  • @WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition
    – Conor O'Brien
    yesterday










  • I was wondering why you made that choice
    – W W
    yesterday






  • 2




    @WW it was a simplification I thought to be useful for clarity
    – Conor O'Brien
    yesterday















Why do Centrosymmetric matrices have to be square?
– W W
yesterday





Why do Centrosymmetric matrices have to be square?
– W W
yesterday













@WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition
– Conor O'Brien
yesterday




@WW in a general sense, I don't suppose it has to be. For this question, however, they must be square by definition
– Conor O'Brien
yesterday












I was wondering why you made that choice
– W W
yesterday




I was wondering why you made that choice
– W W
yesterday




2




2




@WW it was a simplification I thought to be useful for clarity
– Conor O'Brien
yesterday




@WW it was a simplification I thought to be useful for clarity
– Conor O'Brien
yesterday










4 Answers
4






active

oldest

votes

















up vote
6
down vote














Brachylog, 12 bytes



ṁ↔ᵐ↔?aaᵐ.&≜∧


Try it online!



Contrary to most Brachylog answers, this takes the input through the Output variable ., and outputs the result through the Input variable ? (confusing, I know).



Explanation



ṁ We expect a square matrix
↔ᵐ↔? When we reverse the rows and then the matrix, we get the initial matrix back
?a Take an adfix (prefix or suffix) of that square matrix
aᵐ Take an adfix of each row of that adfix matrix
. It must be the input matrix
&≜ Assign values to cells which are still variables (will assign 0)
∧ (disable implicit unification between the input and the output)


8 bytes, gives all valid matrices



Technically, this program also works:



ṁ↔ᵐ↔?aaᵐ



But this will leave as variables the cells which can take any value (they show as _XXXXX, which is an internal Prolog variable name). So technically this is even better than what is asked, but I guess it's not what the challenge asks for.






share|improve this answer





















  • I wish ≜ did delayed labeling...
    – Erik the Outgolfer
    23 hours ago










  • @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
    – Fatalize
    23 hours ago

















up vote
3
down vote













JavaScript (ES6), 192 180 177 bytes





f=(m,v=[w=0],S=c=>v.some(c))=>S(Y=>S(X=>!m[w+1-Y]&!m[0][w+1-X]&!S(y=>S(x=>(k=(m[y-Y]||0)[x-X],g=y=>((r=a[y]=a[y]||)[x]=r[x]||k|0)-k)(y)|g(w-y,x=w-x)),a=)))?a:f(m,[...v,++w])


Try it online!



Algorithm



Starting with $w=0$:



  • We consider a square container matrix $M$ of width $w+1$.


  • We consider each pair $(X,Y)$ such that the input matrix $m$ can fit within the container matrix when it's inserted at these coordinates.



    Example:



$$beginalignw=2,&&(X,Y)=(0,1),&&m = pmatrix4,5,4\1,2,3endalign\
M=pmatrixcolorgray0,colorgray0,colorgray0\4,5,4\1,2,3$$




  • We test whether we can complete the matrix such that it's centrosymmetric.



    Example:



$$M^prime=pmatrixcolorblue3,colorblue2,colorblue1\4,5,4\1,2,3$$



  • We increment $w$ until we find such a valid configuration.





share|improve this answer























  • (The MathJax rendering bug has been reported in this post.)
    – Arnauld
    yesterday

















up vote
1
down vote














Python 2, 242 227 bytes





r=range
def f(m):
w,h=len(m),len(m[0]);W=max(w,h)
while 1:
for x in r(1+W-w):
for y in r(1+W-h):
n=[W*[0]for _ in r(W)];exec"for i in r(w):n[i+x][y:y+h]=m[i]nN=n;n=[l[::-1]for l in n[::-1]]n"*2
if n==N:return n
W+=1


Try it online!






share|improve this answer






























    up vote
    1
    down vote














    Jelly, 27 bytes



    oZ0ṁz0o⁸ṚUŻ€Z$2¡LСo⁸ṚU$ƑƇḢ


    Try it online!



    Newlines added to actual output over TIO for clarity.






    share|improve this answer





















      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: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      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%2f170020%2fminimal-centrosymmetrization%23new-answer', 'question_page');

      );

      Post as a guest






























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      6
      down vote














      Brachylog, 12 bytes



      ṁ↔ᵐ↔?aaᵐ.&≜∧


      Try it online!



      Contrary to most Brachylog answers, this takes the input through the Output variable ., and outputs the result through the Input variable ? (confusing, I know).



      Explanation



      ṁ We expect a square matrix
      ↔ᵐ↔? When we reverse the rows and then the matrix, we get the initial matrix back
      ?a Take an adfix (prefix or suffix) of that square matrix
      aᵐ Take an adfix of each row of that adfix matrix
      . It must be the input matrix
      &≜ Assign values to cells which are still variables (will assign 0)
      ∧ (disable implicit unification between the input and the output)


      8 bytes, gives all valid matrices



      Technically, this program also works:



      ṁ↔ᵐ↔?aaᵐ



      But this will leave as variables the cells which can take any value (they show as _XXXXX, which is an internal Prolog variable name). So technically this is even better than what is asked, but I guess it's not what the challenge asks for.






      share|improve this answer





















      • I wish ≜ did delayed labeling...
        – Erik the Outgolfer
        23 hours ago










      • @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
        – Fatalize
        23 hours ago














      up vote
      6
      down vote














      Brachylog, 12 bytes



      ṁ↔ᵐ↔?aaᵐ.&≜∧


      Try it online!



      Contrary to most Brachylog answers, this takes the input through the Output variable ., and outputs the result through the Input variable ? (confusing, I know).



      Explanation



      ṁ We expect a square matrix
      ↔ᵐ↔? When we reverse the rows and then the matrix, we get the initial matrix back
      ?a Take an adfix (prefix or suffix) of that square matrix
      aᵐ Take an adfix of each row of that adfix matrix
      . It must be the input matrix
      &≜ Assign values to cells which are still variables (will assign 0)
      ∧ (disable implicit unification between the input and the output)


      8 bytes, gives all valid matrices



      Technically, this program also works:



      ṁ↔ᵐ↔?aaᵐ



      But this will leave as variables the cells which can take any value (they show as _XXXXX, which is an internal Prolog variable name). So technically this is even better than what is asked, but I guess it's not what the challenge asks for.






      share|improve this answer





















      • I wish ≜ did delayed labeling...
        – Erik the Outgolfer
        23 hours ago










      • @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
        – Fatalize
        23 hours ago












      up vote
      6
      down vote










      up vote
      6
      down vote










      Brachylog, 12 bytes



      ṁ↔ᵐ↔?aaᵐ.&≜∧


      Try it online!



      Contrary to most Brachylog answers, this takes the input through the Output variable ., and outputs the result through the Input variable ? (confusing, I know).



      Explanation



      ṁ We expect a square matrix
      ↔ᵐ↔? When we reverse the rows and then the matrix, we get the initial matrix back
      ?a Take an adfix (prefix or suffix) of that square matrix
      aᵐ Take an adfix of each row of that adfix matrix
      . It must be the input matrix
      &≜ Assign values to cells which are still variables (will assign 0)
      ∧ (disable implicit unification between the input and the output)


      8 bytes, gives all valid matrices



      Technically, this program also works:



      ṁ↔ᵐ↔?aaᵐ



      But this will leave as variables the cells which can take any value (they show as _XXXXX, which is an internal Prolog variable name). So technically this is even better than what is asked, but I guess it's not what the challenge asks for.






      share|improve this answer














      Brachylog, 12 bytes



      ṁ↔ᵐ↔?aaᵐ.&≜∧


      Try it online!



      Contrary to most Brachylog answers, this takes the input through the Output variable ., and outputs the result through the Input variable ? (confusing, I know).



      Explanation



      ṁ We expect a square matrix
      ↔ᵐ↔? When we reverse the rows and then the matrix, we get the initial matrix back
      ?a Take an adfix (prefix or suffix) of that square matrix
      aᵐ Take an adfix of each row of that adfix matrix
      . It must be the input matrix
      &≜ Assign values to cells which are still variables (will assign 0)
      ∧ (disable implicit unification between the input and the output)


      8 bytes, gives all valid matrices



      Technically, this program also works:



      ṁ↔ᵐ↔?aaᵐ



      But this will leave as variables the cells which can take any value (they show as _XXXXX, which is an internal Prolog variable name). So technically this is even better than what is asked, but I guess it's not what the challenge asks for.







      share|improve this answer













      share|improve this answer



      share|improve this answer











      answered yesterday









      Fatalize

      26.2k448131




      26.2k448131











      • I wish ≜ did delayed labeling...
        – Erik the Outgolfer
        23 hours ago










      • @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
        – Fatalize
        23 hours ago
















      • I wish ≜ did delayed labeling...
        – Erik the Outgolfer
        23 hours ago










      • @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
        – Fatalize
        23 hours ago















      I wish ≜ did delayed labeling...
      – Erik the Outgolfer
      23 hours ago




      I wish ≜ did delayed labeling...
      – Erik the Outgolfer
      23 hours ago












      @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
      – Fatalize
      23 hours ago




      @EriktheOutgolfer Instant labeling is still useful when we need to enumerate things, so ideally we would need two different predicates…
      – Fatalize
      23 hours ago










      up vote
      3
      down vote













      JavaScript (ES6), 192 180 177 bytes





      f=(m,v=[w=0],S=c=>v.some(c))=>S(Y=>S(X=>!m[w+1-Y]&!m[0][w+1-X]&!S(y=>S(x=>(k=(m[y-Y]||0)[x-X],g=y=>((r=a[y]=a[y]||)[x]=r[x]||k|0)-k)(y)|g(w-y,x=w-x)),a=)))?a:f(m,[...v,++w])


      Try it online!



      Algorithm



      Starting with $w=0$:



      • We consider a square container matrix $M$ of width $w+1$.


      • We consider each pair $(X,Y)$ such that the input matrix $m$ can fit within the container matrix when it's inserted at these coordinates.



        Example:



      $$beginalignw=2,&&(X,Y)=(0,1),&&m = pmatrix4,5,4\1,2,3endalign\
      M=pmatrixcolorgray0,colorgray0,colorgray0\4,5,4\1,2,3$$




      • We test whether we can complete the matrix such that it's centrosymmetric.



        Example:



      $$M^prime=pmatrixcolorblue3,colorblue2,colorblue1\4,5,4\1,2,3$$



      • We increment $w$ until we find such a valid configuration.





      share|improve this answer























      • (The MathJax rendering bug has been reported in this post.)
        – Arnauld
        yesterday














      up vote
      3
      down vote













      JavaScript (ES6), 192 180 177 bytes





      f=(m,v=[w=0],S=c=>v.some(c))=>S(Y=>S(X=>!m[w+1-Y]&!m[0][w+1-X]&!S(y=>S(x=>(k=(m[y-Y]||0)[x-X],g=y=>((r=a[y]=a[y]||)[x]=r[x]||k|0)-k)(y)|g(w-y,x=w-x)),a=)))?a:f(m,[...v,++w])


      Try it online!



      Algorithm



      Starting with $w=0$:



      • We consider a square container matrix $M$ of width $w+1$.


      • We consider each pair $(X,Y)$ such that the input matrix $m$ can fit within the container matrix when it's inserted at these coordinates.



        Example:



      $$beginalignw=2,&&(X,Y)=(0,1),&&m = pmatrix4,5,4\1,2,3endalign\
      M=pmatrixcolorgray0,colorgray0,colorgray0\4,5,4\1,2,3$$




      • We test whether we can complete the matrix such that it's centrosymmetric.



        Example:



      $$M^prime=pmatrixcolorblue3,colorblue2,colorblue1\4,5,4\1,2,3$$



      • We increment $w$ until we find such a valid configuration.





      share|improve this answer























      • (The MathJax rendering bug has been reported in this post.)
        – Arnauld
        yesterday












      up vote
      3
      down vote










      up vote
      3
      down vote









      JavaScript (ES6), 192 180 177 bytes





      f=(m,v=[w=0],S=c=>v.some(c))=>S(Y=>S(X=>!m[w+1-Y]&!m[0][w+1-X]&!S(y=>S(x=>(k=(m[y-Y]||0)[x-X],g=y=>((r=a[y]=a[y]||)[x]=r[x]||k|0)-k)(y)|g(w-y,x=w-x)),a=)))?a:f(m,[...v,++w])


      Try it online!



      Algorithm



      Starting with $w=0$:



      • We consider a square container matrix $M$ of width $w+1$.


      • We consider each pair $(X,Y)$ such that the input matrix $m$ can fit within the container matrix when it's inserted at these coordinates.



        Example:



      $$beginalignw=2,&&(X,Y)=(0,1),&&m = pmatrix4,5,4\1,2,3endalign\
      M=pmatrixcolorgray0,colorgray0,colorgray0\4,5,4\1,2,3$$




      • We test whether we can complete the matrix such that it's centrosymmetric.



        Example:



      $$M^prime=pmatrixcolorblue3,colorblue2,colorblue1\4,5,4\1,2,3$$



      • We increment $w$ until we find such a valid configuration.





      share|improve this answer















      JavaScript (ES6), 192 180 177 bytes





      f=(m,v=[w=0],S=c=>v.some(c))=>S(Y=>S(X=>!m[w+1-Y]&!m[0][w+1-X]&!S(y=>S(x=>(k=(m[y-Y]||0)[x-X],g=y=>((r=a[y]=a[y]||)[x]=r[x]||k|0)-k)(y)|g(w-y,x=w-x)),a=)))?a:f(m,[...v,++w])


      Try it online!



      Algorithm



      Starting with $w=0$:



      • We consider a square container matrix $M$ of width $w+1$.


      • We consider each pair $(X,Y)$ such that the input matrix $m$ can fit within the container matrix when it's inserted at these coordinates.



        Example:



      $$beginalignw=2,&&(X,Y)=(0,1),&&m = pmatrix4,5,4\1,2,3endalign\
      M=pmatrixcolorgray0,colorgray0,colorgray0\4,5,4\1,2,3$$




      • We test whether we can complete the matrix such that it's centrosymmetric.



        Example:



      $$M^prime=pmatrixcolorblue3,colorblue2,colorblue1\4,5,4\1,2,3$$



      • We increment $w$ until we find such a valid configuration.






      share|improve this answer















      share|improve this answer



      share|improve this answer








      edited yesterday


























      answered yesterday









      Arnauld

      60.3k472253




      60.3k472253











      • (The MathJax rendering bug has been reported in this post.)
        – Arnauld
        yesterday
















      • (The MathJax rendering bug has been reported in this post.)
        – Arnauld
        yesterday















      (The MathJax rendering bug has been reported in this post.)
      – Arnauld
      yesterday




      (The MathJax rendering bug has been reported in this post.)
      – Arnauld
      yesterday










      up vote
      1
      down vote














      Python 2, 242 227 bytes





      r=range
      def f(m):
      w,h=len(m),len(m[0]);W=max(w,h)
      while 1:
      for x in r(1+W-w):
      for y in r(1+W-h):
      n=[W*[0]for _ in r(W)];exec"for i in r(w):n[i+x][y:y+h]=m[i]nN=n;n=[l[::-1]for l in n[::-1]]n"*2
      if n==N:return n
      W+=1


      Try it online!






      share|improve this answer



























        up vote
        1
        down vote














        Python 2, 242 227 bytes





        r=range
        def f(m):
        w,h=len(m),len(m[0]);W=max(w,h)
        while 1:
        for x in r(1+W-w):
        for y in r(1+W-h):
        n=[W*[0]for _ in r(W)];exec"for i in r(w):n[i+x][y:y+h]=m[i]nN=n;n=[l[::-1]for l in n[::-1]]n"*2
        if n==N:return n
        W+=1


        Try it online!






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote










          Python 2, 242 227 bytes





          r=range
          def f(m):
          w,h=len(m),len(m[0]);W=max(w,h)
          while 1:
          for x in r(1+W-w):
          for y in r(1+W-h):
          n=[W*[0]for _ in r(W)];exec"for i in r(w):n[i+x][y:y+h]=m[i]nN=n;n=[l[::-1]for l in n[::-1]]n"*2
          if n==N:return n
          W+=1


          Try it online!






          share|improve this answer
















          Python 2, 242 227 bytes





          r=range
          def f(m):
          w,h=len(m),len(m[0]);W=max(w,h)
          while 1:
          for x in r(1+W-w):
          for y in r(1+W-h):
          n=[W*[0]for _ in r(W)];exec"for i in r(w):n[i+x][y:y+h]=m[i]nN=n;n=[l[::-1]for l in n[::-1]]n"*2
          if n==N:return n
          W+=1


          Try it online!







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited yesterday


























          answered yesterday









          TFeld

          10.2k1832




          10.2k1832




















              up vote
              1
              down vote














              Jelly, 27 bytes



              oZ0ṁz0o⁸ṚUŻ€Z$2¡LСo⁸ṚU$ƑƇḢ


              Try it online!



              Newlines added to actual output over TIO for clarity.






              share|improve this answer

























                up vote
                1
                down vote














                Jelly, 27 bytes



                oZ0ṁz0o⁸ṚUŻ€Z$2¡LСo⁸ṚU$ƑƇḢ


                Try it online!



                Newlines added to actual output over TIO for clarity.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote










                  Jelly, 27 bytes



                  oZ0ṁz0o⁸ṚUŻ€Z$2¡LСo⁸ṚU$ƑƇḢ


                  Try it online!



                  Newlines added to actual output over TIO for clarity.






                  share|improve this answer














                  Jelly, 27 bytes



                  oZ0ṁz0o⁸ṚUŻ€Z$2¡LСo⁸ṚU$ƑƇḢ


                  Try it online!



                  Newlines added to actual output over TIO for clarity.







                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered 13 hours ago









                  Erik the Outgolfer

                  29.1k42697




                  29.1k42697






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f170020%2fminimal-centrosymmetrization%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What is the equation of a 3D cone with generalised tilt?

                      Color the edges and diagonals of a regular polygon

                      Relationship between determinant of matrix and determinant of adjoint?