Class: Rust::Matrix

Inherits:
RustDatatype show all
Defined in:
lib/rust-core.rb

Class Method Summary collapse

Instance Method Summary collapse

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

.pull_variable(variable) ⇒ Object



445
446
447
# File 'lib/rust-core.rb', line 445

def self.pull_variable(variable)
    return Rust._pull(variable)
end

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

#colsObject



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

#rowsObject



464
465
466
# File 'lib/rust-core.rb', line 464

def rows
    @data.size
end