Class: Matrix
- Inherits:
-
Object
- Object
- Matrix
- Defined in:
- lib/y_petri/simulation/matrix.rb
Overview
Patches built-in Matrix class with methods that generate code for direct vector access. This is used in simulation. It is imaginable and desirable that this way of simulation be refactored, and also, a faster way of simulation provided using NMatrix class.
Direct Known Subclasses
Class Method Summary collapse
-
.column_vector_access_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!")) ⇒ Object
Builds a code string for accessing the vector values at given indices.
-
.column_vector_assignment_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!")) ⇒ Object
Builds a code string for assigning to a vector at given indices.
-
.column_vector_increment_by_array_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!")) ⇒ Object
Builds a code string for incrementing a vector at given indices.
-
.column_vector_increment_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!")) ⇒ Object
Builds a code string for incrementing a vector at given indices.
Instance Method Summary collapse
-
#assign_at_indices_closure(indices: (fail ArgumentError, "No indices!")) ⇒ Object
Builds a closure for assigning to a column at given indices.
-
#increment_at_indices_closure(indices: (fail ArgumentError, "No indices!")) ⇒ Object
Builds a closure for incrementing a column at given indices.
Class Method Details
.column_vector_access_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!")) ⇒ Object
Builds a code string for accessing the vector values at given indices.
14 15 16 17 |
# File 'lib/y_petri/simulation/matrix.rb', line 14 def column_vector_access_code vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!") indices.map { |i| "#{vector}[#{i}, 0]" }.join( ", " ) end |
.column_vector_assignment_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!")) ⇒ Object
Builds a code string for assigning to a vector at given indices.
21 22 23 24 25 26 27 28 |
# File 'lib/y_petri/simulation/matrix.rb', line 21 def column_vector_assignment_code vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!") code_lines = indices.map.with_index do |i, source_pos| "#{vector}.send( :[]=, #{i}, 0, #{source}.fetch( #{source_pos} ) )" if i end code_lines.compact.join( "\n" ) << "\n" end |
.column_vector_increment_by_array_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!")) ⇒ Object
Builds a code string for incrementing a vector at given indices. Source is an array.
46 47 48 49 50 51 52 53 54 |
# File 'lib/y_petri/simulation/matrix.rb', line 46 def column_vector_increment_by_array_code vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!") code_lines = indices.map.with_index do |i, source_pos| "#{vector}.send( :[]=, #{i}, 0, %s )" % "#{vector}[#{i}, 0] + #{source}[#{source_pos}]" if i end code_lines.compact.join( "\n" ) << "\n" end |
.column_vector_increment_code(vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!")) ⇒ Object
Builds a code string for incrementing a vector at given indices. Source is a vector.
33 34 35 36 37 38 39 40 41 |
# File 'lib/y_petri/simulation/matrix.rb', line 33 def column_vector_increment_code vector: (fail ArgumentError, "No vector!"), indices: (fail ArgumentError, "No indices!"), source: (fail ArgumentError, "No source!") code_lines = indices.map.with_index do |i, source_pos| "#{vector}.send( :[]=, #{i}, 0, %s )" % "#{vector}[#{i}, 0] + #{source}[#{source_pos}, 0]" if i end code_lines.compact.join( "\n" ) << "\n" end |
Instance Method Details
#assign_at_indices_closure(indices: (fail ArgumentError, "No indices!")) ⇒ Object
Builds a closure for assigning to a column at given indices.
69 70 71 72 73 74 75 |
# File 'lib/y_petri/simulation/matrix.rb', line 69 def assign_at_indices_closure indices: (fail ArgumentError, "No indices!") v = self eval "-> ass do\n%s\nend" % self.class.column_vector_assignment_code( vector: ?v, indices: indices, source: "ass" ) end |
#increment_at_indices_closure(indices: (fail ArgumentError, "No indices!")) ⇒ Object
Builds a closure for incrementing a column at given indices.
59 60 61 62 63 64 65 |
# File 'lib/y_petri/simulation/matrix.rb', line 59 def increment_at_indices_closure indices: (fail ArgumentError, "No indices!") v = self eval "-> delta do\n%s\nend" % self.class.column_vector_increment_code( vector: "v", indices: indices, source: "delta" ) end |