class Rust::Matrix

Public Class Methods

new(data) click to toggle source
# 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
pull_variable(variable) click to toggle source
# File lib/rust-core.rb, line 590
def self.pull_variable(variable)
    return Rust._pull(variable)
end

Public Instance Methods

[](i, j) click to toggle source
# File lib/rust-core.rb, line 605
def [](i, j)
    return @data[i][j]
end
[]=(i, j, value) click to toggle source
# 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() click to toggle source
# File lib/rust-core.rb, line 613
def cols
    @data[0].size
end
load_in_r_as(variable_name) click to toggle source
# 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() click to toggle source
# File lib/rust-core.rb, line 609
def rows
    @data.size
end