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.
594 595 596 597 598 599 600 601 602 603 |
# File 'lib/rust-core.rb', line 594 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
.pull_variable(variable) ⇒ Object
590 591 592 |
# File 'lib/rust-core.rb', line 590 def self.pull_variable(variable) return Rust._pull(variable) end |
Instance Method Details
#[](i, j) ⇒ Object
605 606 607 |
# File 'lib/rust-core.rb', line 605 def [](i, j) return @data[i][j] end |
#[]=(i, j, value) ⇒ Object
617 618 619 620 621 |
# File 'lib/rust-core.rb', line 617 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
613 614 615 |
# File 'lib/rust-core.rb', line 613 def cols @data[0].size end |
#load_in_r_as(variable_name) ⇒ Object
623 624 625 |
# File 'lib/rust-core.rb', line 623 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
609 610 611 |
# File 'lib/rust-core.rb', line 609 def rows @data.size end |