-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathstochastic.py
More file actions
52 lines (35 loc) · 1012 Bytes
/
Copy pathstochastic.py
File metadata and controls
52 lines (35 loc) · 1012 Bytes
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
# Author Dario Clavijo 2016
# GPLv3
# don't know if the theory is right
a = [0.0, 1.0, 0.0, 1.0]
b = [0.0, 0.0, 1.0, 1.0]
aANDb = [0, 0, 0, 1]
aORb = [0, 1, 1, 1]
aXORb = [0, 1, 1, 0]
# def f(p,q):
# return (p*q) / ((p*q) + (1-p)*(1-q))
def stochastic_average_fAND(p, q):
return (p * q) / 2
def stochastic_average_fOR(p, q):
return (p + q) / 2
def stochastic_average_fXOR(p, q):
return ((p - q) * (p - q)) / 2
def AND(p, q):
return float(stochastic_average_fAND(p, q) > 0)
def OR(p, q):
return float(stochastic_average_fOR(p, q) > 0)
def XOR(p, q):
return float(stochastic_average_fXOR(p, q) > 0)
print("a\tb\tand\tor\txor")
for i in range(0, 4):
print(
a[i],
b[i],
stochastic_average_fAND(a[i], b[i]),
stochastic_average_fOR(a[i], b[i]),
stochastic_average_fXOR(a[i], b[i]),
)
print("a\tb\tand\tor\txor")
for i in range(0, 4):
print(a[i], b[i], AND(a[i], b[i]), OR(a[i], b[i]), XOR(a[i], b[i]))