-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCIMatrix.py
More file actions
127 lines (105 loc) · 3.93 KB
/
CIMatrix.py
File metadata and controls
127 lines (105 loc) · 3.93 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"Python Classes for CIMatrices"
class NotReducibleError(Exception):
pass
#####################################
class CIMatrixEntry(object):
"Abstraction of the 0,+,-,* thing"
def __init__(self, s):
"s is a string representation of the entry"
self.s = s
if s == "*" : self.star=True
else: self.star=False
def __add__ (self, other):
"""addition - corresponds to Spair formation
This is abuse of notation since it feels more like subtraction !!!
Example:
* + * = *
- + - = *
+ + + = 0
0 + x = x
"""
if self.star:
if other.star:
return CIMatrixEntry("*")
j = int(other.s)
if abs(j) > 1: raise NotImplementedError # Is 'higher star arithmetic' necessary?
if j == 0: return CIMatrixEntry("*")
else: return CIMatrixEntry (str(-j))
else:
if other.star:
i = int(self.s)
if abs(i) > 1: raise NotImplementedError
if i == 0: return CIMatrixEntry("*")
else: return CIMatrixEntry(str(-i))
else:
# Regular Integers
i = int(self.s)
j = int(other.s)
k = i - j
if (k==0 and i<0):
return CIMatrixEntry("*")
else:
return CIMatrixEntry(str(k))
def __sub__ (self, other):
""" subtraction - corresponds to reduction of moves
Raises NotReducibleError if not reducible
"""
if self.star: # Self is a star
if other.star: return CIMatrixEntry("*")
else :
j = int(other.s)
if j == 0 : return CIMatrixEntry("*")
else : return CIMatrixEntry(str(-j))
else: # self.s is an integer
i = int(self.s)
if other.star:
if i == 0: return CIMatrixEntry("*")
else : return CIMatrixEntry(str(i))
else: # Both are integer
j = int(other.s)
k = i-j
if k < 0:
if j <= 0 : return CIMatrixEntry(str(k))
else :
raise NotReducibleError
if k == 0:
if i >= 0 : return CIMatrixEntry("0")
else :
return CIMatrixEntry("*")
if k > 0 :
return CIMatrixEntry(str(k))
def __neg__ (self):
""" Inversion, + -> -, - > +, everything else stays the same """
if self.star: return self
return CIMatrixEntry(str(-int(self.s)))
def __eq__ (self, other):
if self.s == other.s : return True
else: return False
def __neq__(self, other):
if self==other: return False
else: return True
def __repr__(self):
if self.s=="0": return "0"
if self.star: return "*"
i = int(self.s)
if abs (i) >= 2: return str(i)
if i > 0: return "+"
else : return "-"
####### END CIMATRIXENTRY ################
##########################################
class CIMatrix(object):
"A Conditional Independence Matrix, for easy adressing. Entries are strings"
def __init__(self, d1=2, d2=2):
self.rawlist = [ [CIMatrixEntry("0") for i in range(d2)] for i in range(d1) ]
self.dim2 = d2
self.dim1 = d1
def clear(self):
rawlist = [ [CIMatrixEntry("0") for i in range(self.dim2)] for i in range(self.dim1) ]
def printRawList(self): print self.rawlist
def printMatrix(self):
for l in self.rawlist:
print " ".join([str(ll) for ll in l])
def getRawList(self): return self.rawlist
def getval(self, m,n): return self.rawlist[m][n]
def setval(self, m,n, newval): self.rawlist[m][n]=newval
####### END CIMATRIX ############