Class: Gosling::FastMatrix
- Inherits:
-
Object
- Object
- Gosling::FastMatrix
- Defined in:
- lib/gosling/transform.rb
Constant Summary collapse
- @@multiply_buffer =
[]
- @@cache =
[]
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#column_count ⇒ Object
Returns the value of attribute column_count.
-
#row_count ⇒ Object
Returns the value of attribute row_count.
Class Method Summary collapse
Instance Method Summary collapse
- #copy_from(matrix) ⇒ Object
- #destroy ⇒ Object
- #fast_multiply(mat2, result = nil) ⇒ Object
- #reset ⇒ Object
- #to_matrix ⇒ Object
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
8 9 10 |
# File 'lib/gosling/transform.rb', line 8 def array @array end |
#column_count ⇒ Object
Returns the value of attribute column_count.
9 10 11 |
# File 'lib/gosling/transform.rb', line 9 def column_count @column_count end |
#row_count ⇒ Object
Returns the value of attribute row_count.
9 10 11 |
# File 'lib/gosling/transform.rb', line 9 def row_count @row_count end |
Class Method Details
.combine_matrices(*matrices) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/gosling/transform.rb', line 94 def self.combine_matrices(*matrices) raise ArgumentError.new("Transform.combine_matrices expects one or more matrices") unless matrices.reject { |m| m.is_a?(Matrix) }.empty? fast_matrices = matrices.map { |mat| FastMatrix.create.copy_from(mat) } result = nil fast_matrices.each do |fast_matrix| if result result.fast_multiply(fast_matrix, result) else result = fast_matrix end end result end |
.create ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/gosling/transform.rb', line 20 def self.create if @@cache.empty? FastMatrix.new else @@cache.pop.reset end end |
Instance Method Details
#copy_from(matrix) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/gosling/transform.rb', line 45 def copy_from(matrix) if matrix.is_a?(Matrix) @array = matrix.to_a.flatten @row_count = matrix.row_size @column_count = matrix.column_size elsif matrix.is_a?(FastMatrix) @array = matrix.array @row_count = matrix.row_count @column_count = matrix.column_count else raise ArgumentError.new("Cannot copy from #{matrix.inspect}!") end self end |
#destroy ⇒ Object
28 29 30 31 32 |
# File 'lib/gosling/transform.rb', line 28 def destroy reset @@cache.push(self) nil end |
#fast_multiply(mat2, result = nil) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/gosling/transform.rb', line 67 def fast_multiply(mat2, result = nil) raise ArgumentError.new() unless mat2.is_a?(FastMatrix) && result.is_a?(FastMatrix) Matrix.Raise ErrDimensionMismatch if @column_count != mat2.row_count i = 0 while i < @row_count do j = 0 while j < mat2.column_count do k = 0 sum = 0 while k < @column_count do sum += @array[i * @column_count + k] * mat2.array[k * mat2.column_count + j] k += 1 end @@multiply_buffer[i * @column_count + j] = sum j += 1 end i += 1 end result ||= FastMatrix.create result.array.replace(@@multiply_buffer) result.row_count = @row_count result.column_count = mat2.column_count result end |
#reset ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gosling/transform.rb', line 34 def reset if @array @array.clear else @array = [] end @row_count = 0 @column_count = 0 self end |
#to_matrix ⇒ Object
60 61 62 63 64 65 |
# File 'lib/gosling/transform.rb', line 60 def to_matrix rows = (0...@row_count).map do |i| @array[(@column_count * i)...(@column_count * (i + 1))] end Matrix.rows(rows) end |