Class: Mgmg::Mat
- Inherits:
-
Object
- Object
- Mgmg::Mat
- Defined in:
- lib/mgmg/utils.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
Class Method Summary collapse
Instance Method Summary collapse
- #col_size ⇒ Object
- #each_with_index ⇒ Object
-
#initialize(m, n, value = nil) ⇒ Mat
constructor
A new instance of Mat.
- #initialize_copy(obj) ⇒ Object
- #map_with_index(&block) ⇒ Object
- #map_with_index! ⇒ Object
- #padd(other) ⇒ Object
- #pprod(other) ⇒ Object
- #row_size ⇒ Object
- #scalar(value) ⇒ Object
- #scalar!(value) ⇒ Object
- #shape ⇒ Object
- #submat_add!(is, js, other) ⇒ Object
- #sum ⇒ Object
Constructor Details
#initialize(m, n, value = nil) ⇒ Mat
Returns a new instance of Mat.
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/mgmg/utils.rb', line 352 def initialize(m, n, value=nil) if block_given? @body = Array.new(m) do |i| Array.new(n) do |j| yield(i, j) end end else @body = Array.new(m) do Array.new(n, value) end end end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
365 366 367 |
# File 'lib/mgmg/utils.rb', line 365 def body @body end |
Class Method Details
.h_array(*ary) ⇒ Object
460 461 462 463 464 |
# File 'lib/mgmg/utils.rb', line 460 def h_array(*ary) new(1, ary.length) do |i, j| ary[j] end end |
.v_array(*ary) ⇒ Object
455 456 457 458 459 |
# File 'lib/mgmg/utils.rb', line 455 def v_array(*ary) new(ary.length, 1) do |i, j| ary[i] end end |
Instance Method Details
#col_size ⇒ Object
372 373 374 |
# File 'lib/mgmg/utils.rb', line 372 def col_size @body[0].length end |
#each_with_index ⇒ Object
378 379 380 381 382 383 384 385 |
# File 'lib/mgmg/utils.rb', line 378 def each_with_index @body.each.with_index do |row, i| row.each.with_index do |e, j| yield(e, i, j) end end self end |
#initialize_copy(obj) ⇒ Object
366 367 368 |
# File 'lib/mgmg/utils.rb', line 366 def initialize_copy(obj) @body = obj.body.map(&:dup) end |
#map_with_index(&block) ⇒ Object
394 395 396 |
# File 'lib/mgmg/utils.rb', line 394 def map_with_index(&block) dup.map_with_index!(&block) end |
#map_with_index! ⇒ Object
386 387 388 389 390 391 392 393 |
# File 'lib/mgmg/utils.rb', line 386 def map_with_index! @body.each.with_index do |row, i| row.map!.with_index do |e, j| yield(e, i, j) end end self end |
#padd(other) ⇒ Object
448 449 450 451 452 |
# File 'lib/mgmg/utils.rb', line 448 def padd(other) ret = self.class.new([row_size, other.row_size].max, [col_size, other.col_size].max, 0) ret.submat_add!(0...row_size, 0...col_size, self) ret.submat_add!(0...(other.row_size), 0...(other.col_size), other) end |
#pprod(other) ⇒ Object
440 441 442 443 444 445 446 447 |
# File 'lib/mgmg/utils.rb', line 440 def pprod(other) r, c = row_size, col_size ret = self.class.new(r+other.row_size-1, c+other.col_size-1, 0) other.each_with_index do |o, i, j| ret.submat_add!(i...(i+r), j...(j+c), scalar(o)) end ret end |
#row_size ⇒ Object
369 370 371 |
# File 'lib/mgmg/utils.rb', line 369 def row_size @body.length end |
#scalar(value) ⇒ Object
434 435 436 |
# File 'lib/mgmg/utils.rb', line 434 def scalar(value) self.dup.scalar!(value) end |
#scalar!(value) ⇒ Object
429 430 431 432 433 |
# File 'lib/mgmg/utils.rb', line 429 def scalar!(value) self.map_with_index! do |e, i, j| e * value end end |
#shape ⇒ Object
375 376 377 |
# File 'lib/mgmg/utils.rb', line 375 def shape [row_size(), col_size()] end |
#submat_add!(is, js, other) ⇒ Object
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/mgmg/utils.rb', line 397 def submat_add!(is, js, other) i_s, i_e = index_treatment(is, row_size) j_s, j_e = index_treatment(js, col_size) i_s.upto(i_e).with_index do |i, io| row = @body[i] o_row = other.body[io] j_s.upto(j_e).with_index do |j, jo| row[j] += o_row[jo] end end self end |
#sum ⇒ Object
437 438 439 |
# File 'lib/mgmg/utils.rb', line 437 def sum @body.map(&:sum).sum end |