Class: Rust::Matrix
- Inherits:
-
RustDatatype
- Object
- RustDatatype
- Rust::Matrix
- Defined in:
- lib/rust-core.rb
Class Method Summary collapse
Instance Method Summary collapse
- #[](i, j) ⇒ Object
- #[]=(i, j, value) ⇒ Object
- #cols ⇒ Object
-
#initialize(data) ⇒ Matrix
constructor
A new instance of Matrix.
- #load_in_r_as(variable_name) ⇒ Object
- #rows ⇒ Object
Constructor Details
#initialize(data) ⇒ Matrix
Returns a new instance of Matrix.
449 450 451 452 453 454 455 456 457 458 |
# File 'lib/rust-core.rb', line 449 def initialize(data) if data.flatten.size == 0 raise "Empty matrices are not allowed" else raise TypeError, "Expected array of array" unless data.is_a?(Array) && data[0].is_a?(Array) raise TypeError, "Only numeric matrices are supported" unless data.all? { |row| row.all? { |e| e.is_a?(Numeric) } } raise "All the rows must have the same size" unless data.map { |row| row.size }.uniq.size == 1 @data = data.clone end end |
Class Method Details
Instance Method Details
#[](i, j) ⇒ Object
460 461 462 |
# File 'lib/rust-core.rb', line 460 def [](i, j) return @data[i][j] end |
#[]=(i, j, value) ⇒ Object
472 473 474 475 476 |
# File 'lib/rust-core.rb', line 472 def []=(i, j, value) raise "Wrong i" unless i.between?(0, @data.size - 1) raise "Wrong j" unless j.between?(0, @data[0].size - 1) @data[i][j] = value end |
#cols ⇒ Object
468 469 470 |
# File 'lib/rust-core.rb', line 468 def cols @data[0].size end |
#load_in_r_as(variable_name) ⇒ Object
478 479 480 |
# File 'lib/rust-core.rb', line 478 def load_in_r_as(variable_name) Rust._eval("#{variable_name} <- matrix(c(#{@data.flatten.join(",")}), nrow=#{self.rows}, ncol=#{self.cols}, byrow=T)") end |
#rows ⇒ Object
464 465 466 |
# File 'lib/rust-core.rb', line 464 def rows @data.size end |