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.

[View source]

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

#bodyObject

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

[View source]

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

[View source]

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_sizeObject

[View source]

372
373
374
# File 'lib/mgmg/utils.rb', line 372

def col_size
  @body[0].length
end

#each_with_indexObject

[View source]

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

[View source]

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

[View source]

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

[View source]

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

[View source]

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

[View source]

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_sizeObject

[View source]

369
370
371
# File 'lib/mgmg/utils.rb', line 369

def row_size
  @body.length
end

#scalar(value) ⇒ Object

[View source]

434
435
436
# File 'lib/mgmg/utils.rb', line 434

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

#scalar!(value) ⇒ Object

[View source]

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

#shapeObject

[View source]

375
376
377
# File 'lib/mgmg/utils.rb', line 375

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

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

[View source]

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

#sumObject

[View source]

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

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