simple_matrix 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/simple_matrix.rb +181 -0
- metadata +46 -0
@@ -0,0 +1,181 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'yaml'
|
3
|
+
require 'matrix'
|
4
|
+
|
5
|
+
class SimpleMatrix
|
6
|
+
attr_reader :colnames, :rownames
|
7
|
+
|
8
|
+
def initialize(*args) # todo args are yet used, it should populate the columns/rows if provided
|
9
|
+
@colnames = []
|
10
|
+
@rownames = []
|
11
|
+
@rows = [ ]
|
12
|
+
@cols = [ ]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Set all column names.
|
16
|
+
def colnames=(names)
|
17
|
+
@colnames = names
|
18
|
+
names.each_with_index { |e, i| @cols[i] = [] } if @cols.length <= 0
|
19
|
+
end
|
20
|
+
|
21
|
+
# Set the column name at the given index
|
22
|
+
def colname(i, name)
|
23
|
+
@colnames[i] = name
|
24
|
+
end
|
25
|
+
|
26
|
+
# Set row name at a given index
|
27
|
+
def rowname(i, name)
|
28
|
+
@rownames[i] = name
|
29
|
+
end
|
30
|
+
|
31
|
+
# Set all rownames
|
32
|
+
def rownames=(names)
|
33
|
+
@rownames = names.map { |e| e.to_s }
|
34
|
+
names.each_with_index { |e, i| @rows[i] = [] } if @rows.length <= 0
|
35
|
+
end
|
36
|
+
|
37
|
+
# Returns all columns.
|
38
|
+
def columns
|
39
|
+
@cols
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns all rows.
|
43
|
+
def rows
|
44
|
+
@rows
|
45
|
+
end
|
46
|
+
|
47
|
+
# Retrieve column by name. If multiple columns have the same name only the first one will be returned.
|
48
|
+
def column(name)
|
49
|
+
index = @colnames.index(name.to_s)
|
50
|
+
@cols[index]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Retrieve row by name. If multiple rows have the same name only the first one will be returned.
|
54
|
+
def row(name)
|
55
|
+
index = @rownames.index(name.to_s)
|
56
|
+
@rows[index]
|
57
|
+
end
|
58
|
+
|
59
|
+
# Add row to the matrix.
|
60
|
+
# Parameter:
|
61
|
+
# - name: Row name, does not need to be unique. However if it is not then retrieving elements by named row
|
62
|
+
# will not be predictable.
|
63
|
+
# - row: Array of values of the same length as the current rows.
|
64
|
+
def add_row(name, row)
|
65
|
+
raise ArgumentError, "Row was #{row.length}, expected #{@colnames.length} elements." unless row.length.eql? @colnames.length
|
66
|
+
raise ArgumentError, "Duplicate row names not allowed: #{name}" if @rownames.index(name)
|
67
|
+
|
68
|
+
@rows << row.to_a
|
69
|
+
row.each_with_index do |r, i|
|
70
|
+
@cols[i] << r
|
71
|
+
end
|
72
|
+
@rownames << name.to_s
|
73
|
+
end
|
74
|
+
|
75
|
+
# Add column to the matrix.
|
76
|
+
# Parameter:
|
77
|
+
# - name: Column name, must be unique.
|
78
|
+
# - col: Array of values of the same length as the current columns.
|
79
|
+
def add_column(name, col)
|
80
|
+
raise ArgumentError, "Column was #{col.length}, expected #{@rownames.length} elements." unless col.length.eql? @rownames.length
|
81
|
+
raise ArgumentError, "Duplicate column names not allowed: #{name}" if @colnames.index(name)
|
82
|
+
|
83
|
+
@cols << col.to_a
|
84
|
+
col.each_with_index do |c, i|
|
85
|
+
@rows[i] << c
|
86
|
+
end
|
87
|
+
@colnames << name.to_s
|
88
|
+
end
|
89
|
+
|
90
|
+
# Update element at a given position
|
91
|
+
# Parameters:
|
92
|
+
# - row: Row name or index.
|
93
|
+
# - col: Column name or index.
|
94
|
+
# - e: Element to be added at the coordinates specified.
|
95
|
+
def update_element(row, col, e)
|
96
|
+
if (row.is_a? String or col.is_a? String)
|
97
|
+
i = @rownames.index(row.to_s)
|
98
|
+
raise ArgumentError, "Row '#{row}' does not exist." if i.nil?
|
99
|
+
j = @colnames.index(col.to_s)
|
100
|
+
raise ArgumentError, "Column '#{col}' does not exist." if j.nil?
|
101
|
+
else
|
102
|
+
i = row; j = col
|
103
|
+
raise ArgumentError, "Row(#{i}) or Col(#{j}) is out of bounds" unless (@rows[i] and @cols[j])
|
104
|
+
end
|
105
|
+
@rows[i][j] = e
|
106
|
+
@cols[j][i] = e
|
107
|
+
end
|
108
|
+
|
109
|
+
# WARNING THIS CHANGES ALL VALUES FOR A GIVEN COLUMN
|
110
|
+
def update_column(col, values)
|
111
|
+
raise ArgumentError, "Expect #{@rownames.length} values for the #{col} column." unless values.length.eql? @rownames.length
|
112
|
+
|
113
|
+
i = col
|
114
|
+
if col.is_a? String
|
115
|
+
i = @colnames.index(col)
|
116
|
+
end
|
117
|
+
raise ArgumentError, "No column found for #{col}" if i.nil? or @cols[i].nil?
|
118
|
+
|
119
|
+
@cols[i] = values
|
120
|
+
values.each_with_index do |e, j|
|
121
|
+
@rows[j][i] = e
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Returns element at given location. Row/column names can be provided,
|
126
|
+
# but if one is a name both are treated as such. Otherwise they are expected
|
127
|
+
# to be indicies.
|
128
|
+
def element(row, col)
|
129
|
+
if (row.is_a? String or col.is_a? String)
|
130
|
+
i = @rownames.index(row.to_s)
|
131
|
+
j = @colnames.index(col.to_s)
|
132
|
+
else
|
133
|
+
i = row; j = col
|
134
|
+
end
|
135
|
+
return @rows[i][j]
|
136
|
+
end
|
137
|
+
|
138
|
+
# Return array [total rows, total columns]
|
139
|
+
def size
|
140
|
+
return [@rows.length, @cols.length]
|
141
|
+
end
|
142
|
+
|
143
|
+
# Returns matrix as a string.
|
144
|
+
# Opts:
|
145
|
+
# :rownames => true (default) Output row names.
|
146
|
+
# :colnames => true (default) Output column names.
|
147
|
+
def to_s(rownames = true, colnames = true)
|
148
|
+
matrix_string = ""
|
149
|
+
matrix_string = "\t" unless rownames.eql? false
|
150
|
+
matrix_string += @colnames.join("\t") + "\n" unless colnames.eql? false # unless (opts[:colnames] and opts[:colnames].eql?false)
|
151
|
+
rowname = ""
|
152
|
+
@rows.each_with_index do |row, i|
|
153
|
+
rowname = "#{@rownames[i]}\t" unless rownames.eql? false # opts[:rownames].eql?false
|
154
|
+
row = row.to_a
|
155
|
+
matrix_string += rowname + row.join("\t") + "\n"
|
156
|
+
end
|
157
|
+
return matrix_string
|
158
|
+
end
|
159
|
+
|
160
|
+
# Write the matrix to a file or to STDOUT
|
161
|
+
# Opts:
|
162
|
+
# :rownames => true (default) Output row names.
|
163
|
+
# :colnames => true (default) Output column names.
|
164
|
+
def write(file = nil, opts = {})
|
165
|
+
matrix_string = self.to_s(opts[:rownames], opts[:colnames])
|
166
|
+
if file
|
167
|
+
File.open(file, 'w') { |fout| fout.write(matrix_string) }
|
168
|
+
puts "#{file} written."
|
169
|
+
else
|
170
|
+
puts matrix_string
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# Get the Ruby Matrix object that is usable for matrix mathematical operations.
|
175
|
+
def to_matrix
|
176
|
+
puts "Rows of #{self.name} are not integers. Matrix math operations may not work." if (rows.select { |e| !e.is_a? Integer }.empty?)
|
177
|
+
return Matrix::Matrix.rows(self.rows)
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_matrix
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Sarah Killcoyne
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-02 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ''
|
15
|
+
email: sarah.killcoyne@uni.lu
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/simple_matrix.rb
|
21
|
+
homepage:
|
22
|
+
licenses:
|
23
|
+
- http://www.apache.org/licenses/LICENSE-2.0.html
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 1.8.24
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: Simple, updatable matrix object with named rows/columns.
|
46
|
+
test_files: []
|