All possible A of Ax=b with constraints on A
Here's a way to code up your problem with Solve
automatically:
x = {1, 2, 3};
b = Reverse[x];
sol[x_List, b_List] /; Length[x] === Length[b] := Module[{mat, vars},
mat = Array[\[FormalM], Length[x]*{1, 1}]; (* construct a matrix of variables*)
vars = Flatten[mat];
mat /.
Solve[And @@ Thread[mat.x == b] (* Construct the equations *)
&& vars ∈ Integers && And @@ Thread[0 <= vars <= 1] (* constraints *),
vars
]
];
matrixSolutions = sol[x, b]
{{{0, 0, 1}, {0, 1, 0}, {1, 0, 0}}, {{1, 1, 0}, {0, 1, 0}, {1, 0, 0}}}
As you can see, 2 solutions were found. Check that the residuals of the solutions are zero vectors:
#.x - b & /@ matrixSolutions
{{0, 0, 0}, {0, 0, 0}}
It works, but I don't know how scalable this approach is. Instead of Solve
, you can also use NSolve
and/or FindInstance
(you can just replace Solve
with any of those two functions in the code above).