根据前面的评论,这里是一个 Python 实现的示例,以蛮力方式检查两个条件是否相等。我基本上只是测试输入逻辑值的所有可能组合。这导致n2n操作。例如,在我的电脑上大约需要 3 秒n=23. 对于较低的值当然要快得多n,这可能是最常发生的情况。
import numpy as np
def exp1(x):
return (x[0,:] & (x[1,:] | x[8,:] | x[9,:]) & \
(x[2,:] | x[3,:] | x[4,:]) & \
(x[5,:] | x[6,:]) & x[7,:]) | \
(x[0,:] & (x[1,:] | x[8,:] | x[9,:]) & \
(x[10,:] | x[11,:]) & x[7,:])
def exp2(x):
return x[0,:] & (x[1,:] | x[8,:] | x[9,:]) & x[7,:] & \
(((x[2,:] | x[3,:] | x[4,:]) & \
(x[5,:] | x[6,:])) | x[10,:] | x[11,:])
# for other logical operations in Python:
# https://numpy.org/doc/stable/reference/arrays.ndarray.html#arithmetic-matrix-multiplication-and-comparison-operations
def compareBoolConditions(exp1,exp2,n):
## 1 - generate all possible combinations
# from itertools import product
# comb = np.array(tuple(product(np.array([True, False]), repeat=n))).T
## 1 - 2nd solution
comb = np.array(np.meshgrid( *[[True, False] for i in range(n)] ) , order='F').T.reshape(-1,n)
## 1 - 3rd solution
# see https://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays
## 2 - vectorized check
if np.all( exp1(comb) == exp2(comb) ):
print('equivalent conditions')
else:
print('conditions are not equivalent')
compareBoolConditions(exp1, exp2, 12)
我找到了多种生成组合的解决方案,因此根据问题的大小,您可能想要测试并选择更快的一个。