-
Notifications
You must be signed in to change notification settings - Fork 2
/
Matrix.rb
385 lines (327 loc) · 8 KB
/
Matrix.rb
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
class Matrix
require 'extArray'
# the input 2d array (valMatrix) is always constructed such that
# each row is a sub-array
attr_accessor :colHeader, :rowHeader, :numRows, :numCols
def initialize(valMatrix, colHeader, rowHeader)
#validate input
if colHeader.length != valMatrix.first.length
raise "colHeader must be an array of names equal in size to the length of valMatrix"
end
if rowHeader.length != valMatrix.length
raise "rowHeader must be an array of names equal in size to the length of each valMatrix sub-array"
end
#store info
@colHeader = colHeader
@numCols = colHeader.length
@rowHeader = rowHeader
@numRows = rowHeader.length
@valMatrix = valMatrix
# first time sums are called, we need to actually
# calculate them, but not until then
@colSumUpdate = true
@rowSumUpdate = true
end
#---------------------
def rowSums
# returns an array of row sums. For efficiency, we only
# update the row sums if a change has been made
# to the matrix
if @rowSumUpdate
#returns an array of row sums
@rowSums = @valMatrix.collect{|x| x.sumNum}
@rowSumUpdate = false
return @rowSums
else
return @rowSums
end
end
#---------------------
def colSums
# returns an array of col sums. For efficiency, we only
# update the col sums if a change has been made
# to the matrix
if @colSumUpdate
@colSums = Array.new(@numCols,0)
@valMatrix.each{|row|
row.each_index{|i|
@colSums[i] += row[i]
}
}
@colSumUpdate = false
return @colSums
else
return @colSums
end
end
#---------------------
def sortByRowSums
@rowSumUpdate = true
return @valMatrix.sort{|a,b| b.sumNum <=> a.sumNum}
end
def sortByRowSums!
@rowSumUpdate = true
@valMatrix.sort!{|a,b| b.sumNum <=> a.sumNum}
end
#---------------------
def sortByColSums
@colSumUpdate = true
return @valMatrix.transpose.sort{|a,b| b.sumNum <=> a.sumNum}.transpose
end
def sortByColSums!
@colSumUpdate = true
@valMatrix = @valMatrix.transpose.sort{|a,b| b.sumNum <=> a.sumNum}.transpose
end
#---------------------
# removes a column from the matrix, and returns the array of values associated with it
def removeCol(index)
@rowSumUpdate = true
@colSumUpdate = true
@colHeader.delete_at(index)
@numCols = numCols-1
arr = []
@valMatrix.each_index{|i|
arr << @valMatrix[i].delete_at(index)
}
return arr
end
#---------------------
def removeColByName(name)
return removeCol(@colHeader.index(name))
end
#---------------------
def getColByName(name)
index = @colHeader.index(name)
arr = []
@valMatrix.each_index{|i|
arr << @valMatrix[i][index]
}
return arr
end
#alias
def getColumnByName(name)
return getColByName(name)
end
#---------------------
def getColByIndex(index)
arr = []
@valMatrix.each_index{|i|
arr << @valMatrix[i][index]
}
return arr
end
#alias
def getColumnByIndex(index)
return getColByIndex(index)
end
#---------------------
def insertCol(name,arr)
@rowSumUpdate = true
@colSumUpdate = true
if arr.length != @valMatrix.length
raise "column being inserted into matrix must be the same size as the matrix"
end
@valMatrix.each_index{|i|
@valMatrix[i] << arr[i]
}
@colHeader << name
@numCols += 1
end
#---------------------
#removes a row from the matrix, and returns the array of values associated with it
def removeRow(index)
@rowSumUpdate = true
@colSumUpdate = true
@rowHeader.delete_at(index)
@numRows = numRows-1
return @valMatrix.delete_at(index)
end
#---------------------
def removeRowByName(name)
index = rowHeader.index(name)
if index.nil?
return nil
else
return removeRow(index)
end
end
#---------------------
def insertRow(name,arr)
if arr.length != @valMatrix.first.length
raise "row being inserted into matrix must be the same size as the matrix"
end
@valMatrix << arr
@rowHeader << name
@numRows += 1
@rowSumUpdate = true
@colSumUpdate = true
end
#---------------------
def getRowByName(name)
index = @rowHeader.index(name)
return getRowByIndex(index)
end
#---------------------
def getRowByIndex(index)
return @valMatrix[index]
end
#---------------------
#returns an array of col values from the row at the given index
def valuesByRow(rowIndex)
return @valMatrix[rowIndex]
end
#---------------------
#returns an array of row values from the col at the given index
def valuesByCol(colIndex)
arr = []
@valMatrix.each_index{|ii|
arr << @valMatrix[ii][colIndex]
}
return arr
end
#---------------------
def print
puts @valMatrix.transpose.collect{|x| x.join("\t")}.join("\n")
end
#---------------------
def print_arff
@rowHeader.each{|name|
puts "@attribute #{name} {1,0}"
}
puts ""
puts "@data"
0.upto(@numCols-1){|i|
puts valuesByCol(i)
}
end
#---------------------
def valByRowNameColName(rowName,colName)
rindex = @rowHeader.index(rowName)
cindex = @colHeader.index(colName)
if cindex.nil? || rindex.nil?
return nil
else
return @valMatrix[rindex][cindex]
end
end
def setValByRowNameColName(rowName,colName,val)
rindex = @rowHeader.index(rowName)
cindex = @colHeader.index(colName)
if cindex.nil? || rindex.nil?
return nil
else
@valMatrix[rindex][cindex] = val
end
@rowSumUpdate = true
@colSumUpdate = true
end
#---------------------
def to_binary
a = Array.new
@valMatrix.each{|row|
temp = []
row.each{|val|
case val
when "true", 1, "1", true
temp << true
when "false", 0, "0", false
temp << false
else
raise "error: to convert to binary, a matrix must contain only the values:\n0,1,'true','false', or true/false binary values"
end
}
a << temp
}
return a
end
def to_binary!
@valMatrix = self.to_binary
end
#---------------------
def to_f
a = Array.new
@valMatrix.each{|row|
temp = []
row.each{|val|
temp << val.to_f
}
a << temp
}
return a
end
def to_f!
@valMatrix = self.to_f
end
#---------------------
def to_i
a = Array.new
@valMatrix.each{|row|
temp = []
row.each{|val|
temp << val.to_i
}
a << temp
}
return a
end
def to_i!
@valMatrix = self.to_i
end
#---------------------
def copy
tmp = []
@valMatrix.each{|arr| tmp << Array.new(arr)}
return Matrix.new(tmp, Array.new(@colHeader), Array.new(@rowHeader))
end
#---------------------
def transpose!
@valMatrix = @valMatrix.transpose
tmp = @colHeader
@colHeader = @rowHeader
@rowHeader = tmp
tmp = @numCols
@numCols = @numRows
@numRows = tmp
@rowSumUpdate = true
@colSumUpdate = true
end
#---------------------
def shuffleCols
self.transpose!
#attach the names to the rows
@rowHeader.each_index{|i|
@valMatrix[i] << @rowHeader[i]
}
#mix them up
@valMatrix.shuffle!
#now remove names back to header
@rowHeader = []
@valMatrix.each{|row|
@rowHeader << row.pop
}
self.transpose!
@colSumUpdate = true
end
#---------------------
def print
temp = Array.new(@colHeader)
temp.unshift("")
puts temp.join("\t")
@rowHeader.each_index{|i|
temp = Array.new(@valMatrix[i])
temp.unshift(rowHeader[i])
puts temp.join("\t")
}
end
#-------------------------
def stderrPrint
temp = Array.new(@colHeader)
temp.unshift("")
$stderr.puts temp.join("\t")
@rowHeader.each_index{|i|
temp = Array.new(@valMatrix[i])
temp.unshift(rowHeader[i])
$stderr.puts temp.join("\t")
}
end
end