2

I am trying to find the square roots of transitive Boolean matrices.

Let $B$ be a transitive Boolean matrix, If $A^2= B$, then $A$ is the square root of $B$.

Here is my try

checkTrans[m1_] := Module[{m2}, m2 = m1 . m1; m1 m2 == m2]
n = 3; (*martix order*)
ms = IntegerDigits[#, 2, n^2] & /@ Range[0, 2^(n^2) - 1];
ms = ArrayReshape[#, {n, n}] & /@ ms;

squareRoots = Flatten[Function[{m}, Select[MatrixPower[#, 1/2] & /@ ms, # [Element] Reals && AllTrue[Flatten[#], # == 0 || # == 1 &] && checkTrans[m] &]] /@ ms, 1];

Length[squareRoots] Print["Matrices with Existing Square Roots (Boolean):"]; MatrixForm /@ squareRoots

For 2x2, I am getting 8 martices. How to make sure that I am getting all the possible square roots matrices of each matrix? As expected, for 3x3 its very slow, any suggestions?

It's worth noting that not every Boolean matrix has a square root. Some Boolean matrices may not have a square root, while others may have multiple square roots.

flinty
  • 25,147
  • 2
  • 20
  • 86
Jonathan
  • 55
  • 5
  • For efficiency, replace both validMatrices = Select[ms, MatrixPower[#, 1/2] \[Element] Reals &]; squareRoots = MatrixPower[#, 1/2] & /@ validMatrices; with just squareRoots = Select[MatrixPower[#, 1/2] & /@ ms, # \[Element] Reals &]; – Bob Hanlon Jun 15 '23 at 01:53
  • @BobHanlon the output should be a Boolean matrix... – Jonathan Jun 20 '23 at 00:57

1 Answers1

1

You could turn this into SAT problem and use SatisfiabilityInstances.

Notebook

Generate all $d\times d$ boolean matrices such that their square is weakly transitive

Weakly transitive means $A\ge A^2$

Number of such matrices grows as $\{2, 16, 432, 32242, 739848\}$

ClearAll["Global`*"];
ClearAll["Global`*"];
booleanMatmul[mat1_, mat2_] := 
  Array[Inner[And, mat1[[#1, ;;]], mat2[[;; , #2]], Or] &, {Length[
     mat1], Length[mat2]}];
booleanEquals[mat1_, mat2_] := 
  And @@ Flatten@MapThread[Xnor, {mat1, mat2}, 2];
booleanGreaterEqual[mat1_, mat2_] := 
  And @@ Flatten@MapThread[Or[#1, Not[#2]] &, {mat1, mat2}, 2];

genWeaklyTransitiveSquare[d_] := Block[{x}, mat = Array[x, {d, d}]; vars = Flatten[mat];

mat2 = booleanMatmul[mat, mat]; mat4 = booleanMatmul[mat2, mat2]; sat = booleanGreaterEqual[mat2, mat4];

numSolutions = SatisfiabilityCount[sat, vars]; booleSolutions = SatisfiabilityInstances[sat, vars, numSolutions]; On[Assert]; Assert[Length@booleSolutions == numSolutions]; (Convert solutions to matrix form) matrixSolutions = mat /. Thread[vars -> Boole@#] & /@ booleSolutions ]; MatrixForm /@ genWeaklyTransitiveSquare[3]

Generate all $d\times d$ boolean matrices such that their square is idempotent

Counts grow as ${2,16,420,31114,7192088}$

genIdempotentSquare[d_] := (
   mat = Array[x, {d, d}];
   vars = Flatten[mat];

(construct mat^2 using And/Or instead of/+*) mat2 = Array[ Inner[And, mat[[#1, ;;]], mat[[;; , #2]], Or] &, {d, d}]; mat4 = Array[Inner[And, mat2[[#1, ;;]], mat2[[;; , #2]], Or] &, {d, d}]; sat = And @@ Flatten@MapThread[Xnor, {mat2, mat4}, 2]; numSolutions = SatisfiabilityCount[sat, vars]; booleSolutions = SatisfiabilityInstances[sat, vars, numSolutions]; On[Assert]; Assert[Length@booleSolutions == numSolutions];

(Convert solutions to matrix form) matrixSolutions = mat /. Thread[vars -> Boole@#] & /@ booleSolutions ); MatrixForm /@ genIdempotentSquare[3]

List all boolean idempotent matrices of size $d\times d$

Counts grow as $1, 2, 11, 123, 2360$, A121337

d = 3;
mat = Array[x, {d, d}];
vars = Flatten[mat];

(construct mat^2 using And/Or instead of/+*) mat2 = Array[ Inner[And, mat[[#1, ;;]], mat[[;; , #2]], Or] &, {d, d}]; sat = And @@ Flatten@MapThread[Xnor, {mat, mat2}, 2];

numSolutions = SatisfiabilityCount[sat, vars]; booleSolutions = SatisfiabilityInstances[sat, vars, numSolutions]; On[Assert]; Assert[Length@booleSolutions == numSolutions];

(Convert solutions to matrix form) matrixSolutions = mat /. Thread[vars -> Boole@#] & /@ booleSolutions; Print["Solutions idempotent: ", AllTrue[matrixSolutions, Boole@Positive[# . #] == # &]]; MatrixForm /@ matrixSolutions

List all square roots of given boolean matrix

For IdentityMatrix[d] target, counts grow as A000085

ClearAll["Global`*"];
target = IdentityMatrix[4];

Clear[x]; d = Length[target]; mat = Array[x, {d, d}]; vars = Flatten[mat];

(construct mat^2 using And/Or instead of/+*) mat2 = Array[Inner[And, mat[[#1, ;;]], mat[[;; , #2]], Or] &, {d, d}];

(* construct SAT problem *) posClauses = Extract[mat2, Position[target, 1]]; negClauses = Not /@ Extract[mat2, Position[target, 0]]; sat = (And @@ posClauses)~And~(And @@ negClauses);

(* solve SAT problem *) numSolutions = SatisfiabilityCount[sat, vars]; booleSolutions = SatisfiabilityInstances[sat, vars, numSolutions]; On[Assert]; Assert[Length@booleSolutions == numSolutions];

(* Convert solutions to matrix form *) matrixSolutions = mat /. Thread[vars -> Boole@#] & /@ booleSolutions; MatrixForm /@ matrixSolutions

enter image description here

Yaroslav Bulatov
  • 7,793
  • 1
  • 19
  • 44
  • Thanks for your input...I did a quick check for 2x2 and it produces only two square matrices. Is your code trying to find square root of all the 4x4 Boolean matrices or the transitive Boolean matrices? or just for identity matrices? – Jonathan Jun 20 '23 at 02:27
  • It finds all square roots of a given boolean matrix, stored in variable target – Yaroslav Bulatov Jun 20 '23 at 02:38
  • OK! But I am looking for all the square roots of all the boolean of 2x2 or 3x3 or 4x4... – Jonathan Jun 20 '23 at 02:39
  • @Jonathan hm, that sounds like all matrices. Any boolean matrix mat is a square root of matrix mat.mat – Yaroslav Bulatov Jun 20 '23 at 02:44
  • `n = 2; (matrix order) ms = IntegerDigits[#, 2, n^2] & /@ Range[0, 2^(n^2) - 1]; ms = ArrayReshape[#, {n, n}] & /@ ms;

    booleanSquareRoots = Select[ms, MatrixPower[#, 2] == # && AllTrue[Flatten[#], # == 0 || # == 1 &] &];

    Length[booleanSquareRoots] Print["Boolean Square Root Matrices:"]; MatrixForm /@ booleanSquareRoots` Here is the code for 2x2 but I am not sure about the output number

    – Jonathan Jun 20 '23 at 02:48
  • Are you perhaps asking for a list of all boolean idempotent matrices of given size? – Yaroslav Bulatov Jun 20 '23 at 02:51
  • I am asking for the sqaure root of all 2x2 Boolean matrices which are 2^4=16 in total... – Jonathan Jun 20 '23 at 02:55
  • @Jonathan so you have 16 matrices, but there's only 16 boolean matrices of that size. Every boolean matrix is a square root of some boolean matrix, so it sounds like you are asking how to list all boolean matrices – Yaroslav Bulatov Jun 20 '23 at 03:03
  • Thats right! There are total number of 16 BMs of 2x2 and my code generates square root of 8. – Jonathan Jun 20 '23 at 03:08
  • @Jonathan I think you have a bug in your checkTrans function, you are using pointwise mutliplication m1 m2 instead of matrix multiplication m1.m2, so you are not actually checking if matrix is transitive. I've edited my answer to list all transitive boolean matrices of a given size – Yaroslav Bulatov Jun 20 '23 at 03:13
  • BTW, you can chain the two functions to list all square roots of all transitive matrices, in this notebook I can see there are 420 such matrices of size $3\times 3$ – Yaroslav Bulatov Jun 20 '23 at 03:24
  • Well, as for as transitive BMs are concern, I am not just looking for idempotent but all transitive satisfiying this condition $A^2<=A$. – Jonathan Jun 20 '23 at 03:26
  • @Jonathan ok, I've updated the answer, genIdempotentSquare[3] generates all 3x3 matrices that satisfy your constraint. The number of such matrices grows quite fast with $d$, I see counts ${2, 16, 420, 31114, 7192088}$ – Yaroslav Bulatov Jun 20 '23 at 03:34
  • I think, there is a missunderstanding. here is what I want... I want to generate all the transitive matrices for 2x2 and higher order which satisfies this condition (A is transitive if and only if $A2<=A$). 2. Then I want to find the square root of all those transitive matrices. 3. I want to find the square root of all the Boolean matrices of order 2x2 or higher... . – Jonathan Jun 20 '23 at 03:38
  • @Jonathan this is what genIdempotentSquare[d] does. It generates all $d\times d$ boolean matrices $A$ such that boolean matrix $A^2$ is transitive. For $d=2$ it's all matrices, but for $d=3$, it's $420$ which is less than $512$, the total number of matrices – Yaroslav Bulatov Jun 20 '23 at 03:46
  • For 2x2 we already know that there only 13 TBMs. For details please check this https://mathematica.stackexchange.com/questions/286314/validity-of-the-number-of-transitive-matrices – Jonathan Jun 20 '23 at 03:51
  • The last one "square roots of BM" is just generating the number of BM, i.e, 2x2 is 16, 3x3 is 512, 4x4 is 65536... – Jonathan Jun 24 '23 at 12:15