Class: Mgmg::Mat

Inherits:
Object
  • Object
show all
Defined in:
lib/mgmg/utils.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(m, n, value = nil) ⇒ Mat

Returns a new instance of Mat.



340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/mgmg/utils.rb', line 340

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

#bodyObject

Returns the value of attribute body.



353
354
355
# File 'lib/mgmg/utils.rb', line 353

def body
  @body
end

Class Method Details

.h_array(*ary) ⇒ Object



448
449
450
451
452
# File 'lib/mgmg/utils.rb', line 448

def h_array(*ary)
  new(1, ary.length) do |i, j|
    ary[j]
  end
end

.v_array(*ary) ⇒ Object



443
444
445
446
447
# File 'lib/mgmg/utils.rb', line 443

def v_array(*ary)
  new(ary.length, 1) do |i, j|
    ary[i]
  end
end

Instance Method Details

#col_sizeObject



360
361
362
# File 'lib/mgmg/utils.rb', line 360

def col_size
  @body[0].length
end

#each_with_indexObject



366
367
368
369
370
371
372
373
# File 'lib/mgmg/utils.rb', line 366

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



354
355
356
# File 'lib/mgmg/utils.rb', line 354

def initialize_copy(obj)
  @body = obj.body.map(&:dup)
end

#map_with_index(&block) ⇒ Object



382
383
384
# File 'lib/mgmg/utils.rb', line 382

def map_with_index(&block)
  dup.map_with_index!(&block)
end

#map_with_index!Object



374
375
376
377
378
379
380
381
# File 'lib/mgmg/utils.rb', line 374

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



436
437
438
439
440
# File 'lib/mgmg/utils.rb', line 436

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



428
429
430
431
432
433
434
435
# File 'lib/mgmg/utils.rb', line 428

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_sizeObject



357
358
359
# File 'lib/mgmg/utils.rb', line 357

def row_size
  @body.length
end

#scalar(value) ⇒ Object



422
423
424
# File 'lib/mgmg/utils.rb', line 422

def scalar(value)
  self.dup.scalar!(value)
end

#scalar!(value) ⇒ Object



417
418
419
420
421
# File 'lib/mgmg/utils.rb', line 417

def scalar!(value)
  self.map_with_index! do |e, i, j|
    e * value
  end
end

#shapeObject



363
364
365
# File 'lib/mgmg/utils.rb', line 363

def shape
  [row_size(), col_size()]
end

#submat_add!(is, js, other) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/mgmg/utils.rb', line 385

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

#sumObject



425
426
427
# File 'lib/mgmg/utils.rb', line 425

def sum
  @body.map(&:sum).sum
end