1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| def AND(x1, x2): w1, w2, theta = 0.5, 0.5, 0.7 tmp = x1 * w1 + x2 * w2 if tmp <= theta: return 0 else: return 1
def OR(x1, x2): w1, w2, theta = 0.5, 0.5, 0.2 tmp = x1 * w1 + x2 * w2 if tmp <= theta: return 0 else: return 1
def NOT(x1, x2): w1, w2, theta = -1, 0, 0 tmp = x1 * w1 + x2 * w2 + 1 return tmp
def XOR(x1, x2): return AND(OR(x1, x2), not AND(x1, x2))
print(AND(0, 1), AND(1, 1), OR(0, 1), OR(0, 0)) print(NOT(0, 1), NOT(1, 1), NOT(0, 0), NOT(1, 0)) print(XOR(0, 1), XOR(1, 1), XOR(0, 0), XOR(1, 0))
|