Module: Eigen::MatrixConstructor

Included in:
MatrixComplex, MatrixDouble, MatrixFloat
Defined in:
lib/eigen.rb

Instance Method Summary collapse

Instance Method Details

#[](*rows) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/eigen.rb', line 5

def [](*rows)
  ret = new(rows.size, rows[0].size)
  rows.each_with_index{|rw, i|
    ret.__set_row__(i, rw)
  }
  return ret
end

#block_diagonal(*ms) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eigen.rb', line 39

def block_diagonal(*ms)
  row_size = ms.inject(0){|sum, m|
   if m.respond_to?(:rows)
     raise(Eigen::EigenRuntimeError, "rows and cols are not equal") unless m.rows == m.cols
     sum + m.rows
   elsif m.is_a?(Numeric)
     sum + 1
   elsif m.is_a?(Array)
     sum + m.size
   else
     raise Eigen::EigenRuntimeError
   end
  }
  col_size = row_size
  ret = new(row_size, row_size)
  ret.setZero
  row_ind = 0
  ms.each{|m|
    if m.respond_to?(:rows)
      ret[row_ind, row_ind] = m
      row_ind += m.rows
    elsif m.is_a?(Array)
      m0 = self[*m]
      raise(Eigen::EigenRuntimeError, "rows and cols are not equal") unless m0.rows == m0.cols
      ret[row_ind, row_ind] = m0
      row_ind += m.size
    else
      ret[row_ind, row_ind] = m
      row_ind += 1
    end
  }
  return ret
end

#hstack(*ms) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/eigen.rb', line 13

def hstack(*ms)
  col_size = ms.inject(0){|sum, m| m.cols + sum }
  row_size = ms[0].rows
  ret = new(row_size, col_size)
  col_ind = 0
  ms.each{|m|
    raise Eigen::EigenRuntimeError, "row sizes are different" unless row_size == m.rows
    ret[0,col_ind] = m
    col_ind += m.cols
  }
  return ret
end

#vstack(*ms) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/eigen.rb', line 26

def vstack(*ms)
  row_size = ms.inject(0){|sum, m| m.rows + sum }
  col_size = ms[0].cols
  ret = new(row_size, col_size)
  row_ind = 0
  ms.each{|m|
    raise Eigen::EigenRuntimeError, "col sizes are different" unless col_size == m.cols
    ret[row_ind,0] = m
    row_ind += m.rows
  }
  return ret
end