You have given equations where the variables can take on the values 0 and 1 (false = 0 true = 1) and the result of the equation will always be 1:
X Y Z V W
1, 0, 1, 0, 0
1, 1, 1, 1, 1
0, 0, 0, 1, 1
0, 1, 0, 0, 0
You can rewrite it as: (“+” = XOR)
X + Z = 1
X + Y + Z + V + W = 1
V + W = 1
Y = 1
This example should have 4 solutions:
X Y Z V W
1)0, 1, 1, 0, 1
2)0, 1, 1, 1, 0
3)1, 1, 0, 0, 1
4)1, 1, 0, 1, 0
Is there any way to calculate this without trying all the options?
I tried to use numpy, but I didn't find any function for counting with boolean variables.
you get all the solutions the same way you would for any system of linear equations:
0, 1, 1, 0, 1)you can then add any solution of the homogenous equation to the particular solution.
in your case: the system of equations (written as matrix) looks like:
if you apply gaussian elimination to that you get:
this is now meant to be multiplied with the vector of the variables. so the for the general solution to the homogenous equation can read off that the following must hold:
and you are free to choose
xhandvh.add any of those to the particular solution and you get all the solutions:
to get:
there are no unnecessary loops. all settings of the variables are solutions to your equations.
Update:
(disclaimer: none of this is really tested...)
in order to get the echelon form of your equations, you could install
galoisandsympyand use them like this:for this you need to:
then:
gaussian elimination:
i could not find a function in numpy that directly finds a particular solution. such a function might exist (?).
i counted the number of variables and the number of equations, set
n_vars-n_eqsvariables to zero and hoped to find a solution for the remaining variables usingsolve:then, for the general solution i used
sympy(which is unaware of GF(2) and might not always work as expected?)the
-zshows, thatsympydoes not know about GF(2). but in this case it works all the same.