|
| 1 | +module ExceptionForMatrix |
| 2 | + class ErrNotRegular < Exception |
| 3 | + def initialize |
| 4 | + super("Not Regular Matrix") |
| 5 | + end |
| 6 | + end |
| 7 | + |
| 8 | + class ErrDimensionMismatch < Exception |
| 9 | + def initialize(name) |
| 10 | + super("#{name} dimension mismatch") |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + class ErrOperationNotDefined |
| 15 | + def initialize(op, name, on) |
| 16 | + super("Operation(#{op}) can't be defined: #{name} op #{on}") |
| 17 | + end |
| 18 | + end |
| 19 | +end |
| 20 | + |
| 21 | +class Matrix |
| 22 | + def self.[](*rows) |
| 23 | + rows(rows, false) |
| 24 | + end |
| 25 | + |
| 26 | + def self.rows(rows, copy = true) |
| 27 | + end |
| 28 | + |
| 29 | + def self.columns(columns) |
| 30 | + rows(columns, false).transpose |
| 31 | + end |
| 32 | + |
| 33 | + def self.empty(rows = 0, columns = 0) |
| 34 | + aise ArgumentError, "One size must be 0" if columns != 0 && rows != 0 |
| 35 | + raise ArgumentError, "Negative size" if columns < 0 || row < 0 |
| 36 | + |
| 37 | + new([[]] * rows, columns) |
| 38 | + end |
| 39 | + |
| 40 | + def self.build(rows, columns = rows, &block) |
| 41 | + rows = rows.to_int |
| 42 | + columns = columns.to_int |
| 43 | + |
| 44 | + raise ArgumentError if rows < 0 || columns < 0 |
| 45 | + return to_enum :build, rows, columns unless block |
| 46 | + |
| 47 | + rows = Array.new(row_count) {|i| |
| 48 | + Array.new(columns) {|j| |
| 49 | + yield i, j |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + new rows, columns |
| 54 | + end |
| 55 | + |
| 56 | + def self.diagonal(*values) |
| 57 | + new Array.new(values.length) {|j| |
| 58 | + Array.new(values.length, 0).tap {|row| |
| 59 | + row[j] = values[j] |
| 60 | + } |
| 61 | + } |
| 62 | + end |
| 63 | + |
| 64 | + def self.scalar(n, value) |
| 65 | + diagonal(*Array.new(n, value)) |
| 66 | + end |
| 67 | + |
| 68 | + def self.identity(n) |
| 69 | + scalar(n,1) |
| 70 | + end |
| 71 | + |
| 72 | + def self.zero(rows, columns = rows) |
| 73 | + new Array.new(rows) { Array.new(columns, 0) }, columns |
| 74 | + end |
| 75 | + |
| 76 | + def self.row_vector(row) |
| 77 | + new [Convert.to_array(row)] |
| 78 | + end |
| 79 | + |
| 80 | + def self.column_vector(column) |
| 81 | + new [column].transpose, 1 |
| 82 | + end |
| 83 | + |
| 84 | + def initialize(rows, column_count = rows[-1].size) |
| 85 | + @rows = rows |
| 86 | + @column_count = columns |
| 87 | + end |
| 88 | + |
| 89 | + def [](i, j) |
| 90 | + @rows.fetch(i) { |
| 91 | + return nil |
| 92 | + }[j] |
| 93 | + end |
| 94 | + |
| 95 | + alias element [] |
| 96 | + alias component [] |
| 97 | + |
| 98 | + def []=(i, j, v) |
| 99 | + @rows[i][j] = v |
| 100 | + end |
| 101 | + |
| 102 | + alias set_element []= |
| 103 | + alias set_component []= |
| 104 | + |
| 105 | + def row_count |
| 106 | + @rows.length |
| 107 | + end |
| 108 | + |
| 109 | + alias row_size row_count |
| 110 | + |
| 111 | + attr_reader :column_count |
| 112 | + alias column_size column_count |
| 113 | + |
| 114 | + module Convert |
| 115 | + def self.to_array(obj, copy = false) |
| 116 | + case obj |
| 117 | + when Array |
| 118 | + copy ? obj.dup : obj |
| 119 | + |
| 120 | + when Vector |
| 121 | + obj.to_a |
| 122 | + |
| 123 | + else |
| 124 | + obj.to_ary |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
| 129 | + |
| 130 | +class Vector |
| 131 | + def self.[](*array) |
| 132 | + new Matrix::Convert.to_array(array, false) |
| 133 | + end |
| 134 | + |
| 135 | + def self.elements(array, copy = true) |
| 136 | + new Matrix::Convert.to_array(array, copy) |
| 137 | + end |
| 138 | + |
| 139 | + def initialize(array) |
| 140 | + @elements = array |
| 141 | + end |
| 142 | + |
| 143 | + def [](i) |
| 144 | + @elements[i] |
| 145 | + end |
| 146 | + |
| 147 | + alias element [] |
| 148 | + alias component [] |
| 149 | + |
| 150 | + def []=(i, v) |
| 151 | + @elements[i] = v |
| 152 | + end |
| 153 | + |
| 154 | + alias set_element []= |
| 155 | + alias set_component []= |
| 156 | + |
| 157 | + def size |
| 158 | + @elements.size |
| 159 | + end |
| 160 | + |
| 161 | + def each(&block) |
| 162 | + return enum_for :each unless block |
| 163 | + |
| 164 | + @elements.each(&block) |
| 165 | + |
| 166 | + self |
| 167 | + end |
| 168 | + |
| 169 | + def along(enum, other, &block) |
| 170 | + raise TypeError, "Integer is not like Vector" if Integer === other |
| 171 | + |
| 172 | + if size != other.size |
| 173 | + raise ExceptionForMatrix::ErrDimensionMismatch, :each2, Vector, Vector |
| 174 | + end |
| 175 | + |
| 176 | + |
| 177 | + end |
| 178 | + |
| 179 | + def each2(other, &block) |
| 180 | + return enum_for :each2, other unless block |
| 181 | + |
| 182 | + size.times {|i| |
| 183 | + yield @elements[i], other[i] |
| 184 | + } |
| 185 | + end |
| 186 | + |
| 187 | + def collect2(other, &block) |
| 188 | + raise TypeError, "Integer is not like Vector" if Integer === other |
| 189 | + |
| 190 | + if size != other.size |
| 191 | + raise ExceptionForMatrix::ErrDimensionMismatch, :collect2, Vector, Vector |
| 192 | + end |
| 193 | + |
| 194 | + Array.new(size) {|i| |
| 195 | + yield @elements[i], other[i] |
| 196 | + } |
| 197 | + end |
| 198 | + |
| 199 | + def ==(other) |
| 200 | + return false unless Vector == other |
| 201 | + |
| 202 | + @elements == other.elements |
| 203 | + end |
| 204 | + |
| 205 | + def eql?(other) |
| 206 | + return false unless Vector === other |
| 207 | + |
| 208 | + @elements.eql? other.elements |
| 209 | + end |
| 210 | + |
| 211 | + def clone |
| 212 | + self.class.elements(@elements) |
| 213 | + end |
| 214 | + |
| 215 | + def hash |
| 216 | + @elements.hash |
| 217 | + end |
| 218 | + |
| 219 | + def *(other) |
| 220 | + case other |
| 221 | + when Numeric |
| 222 | + self.class.elements(@elements.map(&:*), false) |
| 223 | + |
| 224 | + when Matrix |
| 225 | + Matrix.column_vector(self) * other |
| 226 | + |
| 227 | + when Vector |
| 228 | + raise ExceptionForMatrix::ErrOperationNotDefined, :*, Vector, Vector |
| 229 | + end |
| 230 | + end |
| 231 | + |
| 232 | + def +(other) |
| 233 | + case other |
| 234 | + when Vector |
| 235 | + if size != other.size |
| 236 | + raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector |
| 237 | + end |
| 238 | + |
| 239 | + self.class.elements collect2(other, &:+), false |
| 240 | + |
| 241 | + when Matrix |
| 242 | + Matrix.column_vector(self) + other |
| 243 | + end |
| 244 | + end |
| 245 | + |
| 246 | + def -(other) |
| 247 | + case other |
| 248 | + when Vector |
| 249 | + if size != other.size |
| 250 | + raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector |
| 251 | + end |
| 252 | + |
| 253 | + self.class.elements collect2(other, &:-), false |
| 254 | + |
| 255 | + when Matrix |
| 256 | + Matrix.column_vector(self) - other |
| 257 | + end |
| 258 | + end |
| 259 | + |
| 260 | + def /(other) |
| 261 | + case other |
| 262 | + when Numeric |
| 263 | + self.class.elements @elements.collect(&:/), false |
| 264 | + |
| 265 | + when Matrix, Vector |
| 266 | + raise ExceptionForMatrix::ErrOperationNotDefined, :*, Vector, other.class |
| 267 | + end |
| 268 | + end |
| 269 | + |
| 270 | + def inner_product(other) |
| 271 | + if size != other.size |
| 272 | + raise ExceptionForMatrix::ErrDimensionMismatch, :+, Vector, Vector |
| 273 | + end |
| 274 | + |
| 275 | + p = 0 |
| 276 | + each2(other) {|v1, v2| |
| 277 | + p += v1 * v2.conj |
| 278 | + } |
| 279 | + p |
| 280 | + end |
| 281 | + |
| 282 | + def collect(&block) |
| 283 | + return enum_for :collect unless block |
| 284 | + |
| 285 | + self.class.elements @elements.collect(&block), false |
| 286 | + end |
| 287 | + |
| 288 | + alias map collect |
| 289 | + |
| 290 | + def magnitude |
| 291 | + `Math.sqrt(#{@elements.reduce(0) { |v, e| `v + Math.pow(Math.abs(e), 2)` }})` |
| 292 | + end |
| 293 | + |
| 294 | + alias r magnitude |
| 295 | + alias norm magnitude |
| 296 | +end |
2 commit comments
elia commentedon Jan 26, 2014
Not everyone fits a matrix impl inside an IE8 fix lol
meh commentedon Jan 26, 2014
What the fuck. How did this happen I don't know.