Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/expcalc/numo_expansion.rb

Overview

Tb meter leer matrices con npy

Instance Method Summary collapse

Instance Method Details

#get_hash_values_idxObject



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

def get_hash_values_idx
  x_names_indx = {}
  i = 0
  self.each do |k, values|
    values.each do |val_id|
      query = x_names_indx[val_id]
      if query.nil?
        x_names_indx[val_id] = i
        i += 1
      end
    end
  end
  return x_names_indx
end

#to_bmatrixObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/expcalc/numo_expansion.rb', line 27

def to_bmatrix 
  x_names_indx = self.get_hash_values_idx
  y_names = self.keys
  x_names = x_names_indx.keys
   # row (y), cols (x)
  matrix = Numo::DFloat.zeros(self.length, x_names.length)
  i = 0
  self.each do |id, items|
    items.each do |item_id|
      matrix[i, x_names_indx[item_id]] = 1
    end
    i += 1
  end
  return matrix, y_names, x_names
end

#to_wmatrixObject

TODO: Only works if the resulting matrix will be squared. Replace implementacion taking into account to_bmatrix and its output



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/expcalc/numo_expansion.rb', line 45

def to_wmatrix
  element_names = self.keys
  matrix = Numo::DFloat.zeros(element_names.length, element_names.length)
  i = 0
  self.each do |elementA, relations|
    element_names.each_with_index do |elementB, j|
      if elementA != elementB
        query = relations[elementB]
        if !query.nil?
          matrix[i, j] = query
        else
          matrix[i, j] = self[elementB][elementA]
        end
      end
    end
    i += 1
  end
  return matrix, element_names
end