Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中的XOR线性方程组求解器_Python_Equation_Xor - Fatal编程技术网

Python中的XOR线性方程组求解器

Python中的XOR线性方程组求解器,python,equation,xor,Python,Equation,Xor,我有n行和n+1列矩阵,需要构建这样的系统 例如,矩阵是 x4 x3 x2 x1 result 1 1 0 1 0 1 0 1 0 1 0 1 0 1 1 1 0 1 1 0 那么方程就是(+是XOR) 我需要返回答案作为x1的列表,。。。。。 我们如何在python中实现它?学习高斯,也可以用于XOR。然后编写一个gauss python程序您可以利用Microsoft Z3 solver的python界面: from z3 import * def xor

我有n行和n+1列矩阵,需要构建这样的系统 例如,矩阵是

x4 x3 x2 x1 result
1  1  0  1  0
1  0  1  0  1
0  1  0  1  1
1  0  1  1  0
那么方程就是(+是XOR)

我需要返回答案作为x1的列表,。。。。。
我们如何在python中实现它?

学习高斯,也可以用于XOR。然后编写一个gauss python程序

您可以利用Microsoft Z3 solver的python界面:

from z3 import *

def xor2(a, b):
    return Xor(a, b)

def xor3(a, b, c):
    return Xor(a, Xor(b, c))

#  define Boolean variables
x1 = Bool('x1')
x2 = Bool('x2')
x3 = Bool('x3')
x4 = Bool('x4')

s = Solver()

#  every equation is expressed as one constraint
s.add(Not(xor3(x4, x3, x1)))
s.add(xor2(x4, x2))
s.add(xor2(x3, x1))
s.add(Not(xor3(x4, x2, x1)))

#  solve and output results
print(s.check())
print(s.model())
结果:

sat
[x3 = False, x2 = False, x1 = True, x4 = True]

我看不出矩阵和方程有什么关系。什么是
x4
x3
,等等?是的,对不起,我不太了解numpy,但我敢打赌你可以很容易地做到。这个答案明显缺乏细节,应该只是一个注释。我知道你没有足够的代表评论,但这并不意味着你应该发布一个糟糕的答案。
sat
[x3 = False, x2 = False, x1 = True, x4 = True]