Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Defined in:
- lib/expcalc/numo_expansion.rb
Overview
Tb meter leer matrices con npy
Instance Method Summary collapse
- #get_hash_values_idx ⇒ Object
- #to_bmatrix ⇒ Object
- #to_wmatrix(squared: true, symm: true) ⇒ Object
- #to_wmatrix_rectangular(symm: true) ⇒ Object
- #to_wmatrix_squared(symm: true) ⇒ Object
Instance Method Details
#get_hash_values_idx ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# 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| val_id = val_id.first if val_id.class == Array 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_bmatrix ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/expcalc/numo_expansion.rb', line 28 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_wmatrix(squared: true, symm: true) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/expcalc/numo_expansion.rb', line 45 def to_wmatrix(squared: true, symm: true) if squared matrix, element_names = to_wmatrix_squared(symm: symm) return matrix, element_names else matrix, y_names, x_names = to_wmatrix_rectangular(symm: symm) return matrix, y_names, x_names end end |
#to_wmatrix_rectangular(symm: true) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/expcalc/numo_expansion.rb', line 75 def to_wmatrix_rectangular(symm: true) y_names = self.keys x_names = self.get_hash_values_idx.keys p x_names matrix = Numo::DFloat.zeros(y_names.length, x_names.length) i = 0 self.each do |elementA, relations| x_names.each_with_index do |elementB, j| query = relations[elementB] if !query.nil? matrix[i, j] = query elsif symm query = self.dig(elementB, elementA) matrix[i, j] = query if !query.nil? end end i += 1 end return matrix, y_names, x_names end |
#to_wmatrix_squared(symm: true) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/expcalc/numo_expansion.rb', line 55 def to_wmatrix_squared(symm: true) 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 elsif symm matrix[i, j] = self[elementB][elementA] end end end i += 1 end return matrix, element_names end |