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

validMatrices = Select[ms, MatrixPower[#, 1/2] \[Element] Reals &]; squareRoots = MatrixPower[#, 1/2] & /@ validMatrices;with justsquareRoots = Select[MatrixPower[#, 1/2] & /@ ms, # \[Element] Reals &];– Bob Hanlon Jun 15 '23 at 01:53