Class: Linalg::DMatrix

Inherits:
Object
  • Object
show all
Defined in:
lib/appswarm/routing/matrix_math.rb

Instance Method Summary collapse

Instance Method Details

#covarianceObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/appswarm/routing/matrix_math.rb', line 33

def covariance
  m=middle_row
  colCount=self.columns.entries.length
  rowCount=self.rows.entries.length
  r=(0...colCount).to_a.map{|y|
      (0...colCount).to_a.map{|x|
        (0...rowCount).inject(0) {|old,c|
          old+(self[c,y]-m[0,y])*(self[c,x]-m[0,x])
        }/rowCount
      }
  }
  DMatrix[*r]
end

#cut(w, h) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/appswarm/routing/matrix_math.rb', line 47

def cut(w,h)
  r=(0...h).to_a.map{|y|
    (0...w).to_a.map{|x|
      self[y,x]
    }
  }
  DMatrix[*r]
end

#middle_colObject



24
25
26
27
28
29
30
31
# File 'lib/appswarm/routing/matrix_math.rb', line 24

def middle_col
  startA=([[0]]*rows.entries.length)
  start=DMatrix[*startA]
  
  columns.entries.inject(start){|old,new|
    old+new
  }/columns.entries.length
end

#middle_rowObject



16
17
18
19
20
21
22
23
# File 'lib/appswarm/routing/matrix_math.rb', line 16

def middle_row
  startA=([0]*columns.entries.length)
  start=DMatrix[startA]
  
  rows.entries.inject(start){|old,new|
    old+new
  }/rows.entries.length
end

#pseudo_inverse(epsilon = nil) ⇒ Object

Pseudo-inverse.

For matrix m, find x which minimizes

(m*x - DMatrix.identity(m.vsize)).norm


12
13
14
# File 'lib/appswarm/routing/matrix_math.rb', line 12

def pseudo_inverse(epsilon = nil)
   DMatrix.fit(self, DMatrix.identity(vsize))
end