Minimal Centrosymmetrization
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.
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:
$$\begin{align}w=2,&&(X,Y)=(0,1),&&m = \pmatrix{4,5,4\\1,2,3}\end{align}\\ M=\pmatrix{\color{gray}0,\color{gray}0,\color{gray}0\\4,5,4\\1,2,3}$$
We test whether we can complete the matrix such that it's centrosymmetric.
Example:
$$M^\prime=\pmatrix{\color{blue}3,\color{blue}2,\color{blue}1\\4,5,4\\1,2,3}$$
- We increment \$w\$ until we find such a valid configuration.