Class: Matrix
- Inherits:
-
Object
- Object
- Matrix
- Defined in:
- lib/rubyml.rb
Overview
Monkey patching the Matrix class to implement matrix splicing.
Instance Method Summary collapse
- #[](i, j) ⇒ Object
- #redirect(i, j) ⇒ Object
- #redirect2(i, j) ⇒ Object
- #redirect3(i, j) ⇒ Object
- #redirect4(rs, re, cs, ce, orig) ⇒ Object
- #redirect5(rs, re, cs, ce, orig) ⇒ Object
- #redirect6(rs, re, cs, ce) ⇒ Object
- #redirect7(rs, re, cs, ce) ⇒ Object
- #redirect8(rs, re, cs, ce) ⇒ Object
Instance Method Details
#[](i, j) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/rubyml.rb', line 9 def [](i, j) if i.class == Fixnum && j == ':' row(i) elsif j.class == Fixnum && i == ':' column(j) else redirect(i, j) end end |
#redirect(i, j) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rubyml.rb', line 19 def redirect(i, j) if i.class == String && j.class == String redirect2(i, j) else old_element(i, j) end end |
#redirect2(i, j) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/rubyml.rb', line 27 def redirect2(i, j) if i.include?(':') || j.include?(':') redirect3(i, j) else old_element(i, j) end end |
#redirect3(i, j) ⇒ Object
35 36 37 38 39 |
# File 'lib/rubyml.rb', line 35 def redirect3(i, j) rs, re = i.split(':').map { |e| e == '' ? 0 : Integer(e) } cs, ce = j.split(':').map { |e| e == '' ? 0 : Integer(e) } redirect4(rs, re, cs, ce, [i, j]) end |
#redirect4(rs, re, cs, ce, orig) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rubyml.rb', line 41 def redirect4(rs, re, cs, ce, orig) if orig[0] == ':' rs = 0 re = row_count end if orig[1] == ':' cs = 0 ce = column_count end redirect5(rs, re, cs, ce, orig) end |
#redirect5(rs, re, cs, ce, orig) ⇒ Object
53 54 55 56 57 |
# File 'lib/rubyml.rb', line 53 def redirect5(rs, re, cs, ce, orig) re = rs + 1 unless orig[0].include?(':') ce = cs + 1 unless orig[1].include?(':') redirect6(rs, re, cs, ce) end |
#redirect6(rs, re, cs, ce) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/rubyml.rb', line 59 def redirect6(rs, re, cs, ce) rs = rs.nil? ? 0 : rs cs = cs.nil? ? 0 : cs re = re.nil? ? row_count : re ce = ce.nil? ? column_count : ce redirect7(rs, re, cs, ce) end |
#redirect7(rs, re, cs, ce) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/rubyml.rb', line 67 def redirect7(rs, re, cs, ce) return Matrix.rows([]) if rs >= re && cs >= ce return Matrix.rows([[]] * (re - rs)) if cs == ce return Matrix.columns([[]] * (ce - cs)) if re == rs redirect8(rs, re, cs, ce) end |
#redirect8(rs, re, cs, ce) ⇒ Object
74 75 76 77 78 |
# File 'lib/rubyml.rb', line 74 def redirect8(rs, re, cs, ce) rv = row_vectors[rs..re - 1].map(&:to_a) nrv = rv.map { |e| e[cs..ce - 1] } Matrix.rows(nrv) end |