-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.py
190 lines (145 loc) · 4.6 KB
/
matrix.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import vector
class Matrix(object):
def __init__(self, rows, columns ):
"""
This is the constructor for the generic matrix
Args:
:rows: int, how many rows you want in the matrix
:columns: int, how many columns you want in the matrix
"""
self.data = [vector.Vector([0]*rows) for c in xrange(columns)]
self.__columns = columns
self.__rows = rows
def set_to_value(self, value):
"""
This function sets the whole matrix to a specifc value
Args:
:value: float, the value to set the matrix to
"""
it = self.column_iterator()
for c in it:
c.set_to_value(value)
it.merge(c)
def zero(self):
"""
Zero out the matrix
"""
self.reset_to_value(0)
def __str__(self):
"""
Print out the matrix
:todo: aligning caracters based maybe on float precision?
"""
to_return = ""
for r in xrange(self.__rows):
tmp = ""
for c in xrange(self.__columns):
tmp += (str(self.data[c][r]) + " ")
tmp += "\n"
to_return +=tmp
return to_return
def columns(self):
"""
Retunrs the orizontal size of the matrix
"""
return len(self.data)
def rows(self):
"""
Returns the vertical size of the matrix
"""
return len(self.data[0])
def column_iterator(self):
"""
Return a column iterator for the matrix
:returns: iterator
"""
return ColumnIterator(self)
def diagonal_iterator(self):
"""
Returns a diagonal iteerator for the matrix
:returns: iterator
"""
return DiagonalIterator(self)
def __getitem__(self, index):
"""
Subsctiption operator returns the colum vector
at the given index not tested with slice object
:index: int, the index we want to access
"""
return self.data[index]
def __setitem__(self, index ,data):
"""
Subscrition setter operator, not tested with slice object
:index: int, the index we want to set
:data: Vector, no checks has been done whatsoever on the dimension of the
vector compared to the matrix, it is up to the user to do not make
mistakes.
"""
self.data[index] = data
class ColumnIterator(object):
"""
This class implements a column iterator for the given matrix,
it will slice out one at the time the column and give it to the user
for manipulation
"""
def __init__(self, data):
"""
This is the constructor
Args:
:data: Matrix, the matrix to iterate over
"""
self.data = data
self.counter = 0
self.columns = data.columns()
self.rows = data.rows()
def __iter__(self):
return self
def next(self):
if self.counter < self.columns:
to_return = self.data[self.counter]
self.counter +=1
return to_return
else:
raise StopIteration()
def merge(self, data):
self.data[self.counter -1] = data
def reset(self):
self.counter = 0
class DiagonalIterator(object):
def __init__(self, data):
"""
This is the interator used to iterate the matrix in a diagonal
fashion
"""
self.data = data
self.counter = 0
self.columns = data.columns()
self.rows = data.rows()
def __iter__(self):
return self
def next(self):
if self.counter < self.columns:
if self.counter == 0:
TL = vector.Vector([])
else:
TL = vector.Vector(self.data[self.counter][:self.counter])
value = self.data[self.counter][self.counter]
BL = vector.Vector(self.data[self.counter][self.counter+1:])
self.counter +=1
return [TL, value, BL]
else:
raise StopIteration()
def merge(self, TL, value, BL):
self.data[self.counter -1] = vector.Vector(TL.values + [value] + BL.values)
class Matrix44(Matrix):
def __init__(self, identity =True):
Matrix.__init__(self,4,4)
if identity:
self.identity()
def identity(self):
dt = self.diagonal_iterator()
for TL, value, BL in dt:
TL.zero()
value=1
BL.zero()
dt.merge(TL, value, BL)