Module: OpenSkill::Models::Common

Defined in:
lib/openskill/models/common.rb

Overview

Common utility functions shared across models

Class Method Summary collapse

Class Method Details

.matrix_transpose(matrix) ⇒ Array<Array>

Transpose a 2D matrix

Parameters:

  • the input matrix

Returns:

  • the transposed matrix



34
35
36
37
38
# File 'lib/openskill/models/common.rb', line 34

def self.matrix_transpose(matrix)
  return [] if matrix.empty? || matrix[0].empty?

  matrix[0].zip(*matrix[1..])
end

.normalize(vector, target_min, target_max) ⇒ Array<Float>

Normalize a vector to a target range

Parameters:

  • the input vector

  • the target minimum value

  • the target maximum value

Returns:

  • the normalized vector



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/openskill/models/common.rb', line 13

def self.normalize(vector, target_min, target_max)
  return [] if vector.empty?

  source_min = vector.min
  source_max = vector.max
  source_range = source_max - source_min

  # If all values are the same, return target_min for all
  return Array.new(vector.size, target_min.to_f) if source_range.zero?

  target_range = target_max - target_min

  vector.map do |value|
    ((value - source_min) / source_range) * target_range + target_min
  end
end

.unwind(tenet, objects) ⇒ Array<(Array, Array<Numeric>)>

Sort objects by tenet and return both sorted objects and indices to restore order

Parameters:

  • values to sort by

  • objects to sort

Returns:

  • sorted objects and restoration indices



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openskill/models/common.rb', line 45

def self.unwind(tenet, objects)
  return [[], []] if objects.empty?

  # Create array of [tenet_value, [object, original_index]]
  indexed = tenet.each_with_index.map { |t, i| [t, [objects[i], i]] }

  # Sort by tenet value
  sorted = indexed.sort_by { |t, _| t }

  # Extract sorted objects and their indices
  sorted_objects = sorted.map { |_, (obj, _)| obj }
  restoration_indices = sorted.map { |_, (_, idx)| idx }

  [sorted_objects, restoration_indices]
end