class Silicium::Sparse::SparseMatrix

addition for SparseMatrix class

here goes the Sparse class

here goes tha addition to SparseMatrix class

here goes the Sparse class

addition for SparseMatrix class

here goes tha addition to SparseMatrix class

Attributes

m[R]
n[R]
triplets[R]

Public Class Methods

new(rows, cols) click to toggle source

@param [Integer] rows - Count of rows @param [Integer] cols - Count of columns

Initialize matrix with count of rows and columns

# File lib/silicium/sparse.rb, line 20
def initialize(rows, cols)
  @n = rows
  @m = cols
  @triplets = []
end
sparse(mat) click to toggle source

# A static method for initializing sparse matrix from a regular one @param [Array] mat - Source matrix for conversion

# File lib/silicium/conversions.rb, line 8
def self.sparse(mat)
  new = SparseMatrix.new(mat.count, mat[0].count)
  i = 0
  mat.each do |row|
    j = 0
    row.each do |elem|
      new.add(i, j, elem) unless elem.zero?
      j += 1
    end
    i += 1
  end
  new
end

Public Instance Methods

*(other) click to toggle source

@param [SparseMatrix::Object] other - A matrix to multiply to @raise [ArgumentError] if count of columns of right matrix doesn't match count of rows of left matrix

Returns a matrix in its regular view but multiplied by other matrix

# File lib/silicium/sugar.rb, line 31
def *(other)
  multiply(other)
end
+(other) click to toggle source

@param [SparseMatrix] other - second matrix for adding @raise [ArgumentError] If the size of the first matrix doesn't match the size of the second matrix @return [SparseMatrix] Matrix as the sum of the other two matrices

Makes the sum of two matrix

# File lib/silicium/sugar.rb, line 11
def +(other)
  adding(other)
end
-(other) click to toggle source

@param [SparseMatrix] other - second matrix for adding @raise [ArgumentError] If the size of the first matrix doesn't match the size of the second matrix @return [SparseMatrix] Matrix as the sum of the other two matrices

Makes the sum of two matrix

# File lib/silicium/sugar.rb, line 21
def -(other)
  adding(other.mult_by_num(-1))
end
add(i_pos, j_pos, elem) click to toggle source

@param [Integer] i_pos - The i position of an element @param [Integer] j_pos - The j position of an element @param [Integer] elem - The value of an element to be added

Adds an element to matrix by its position and value

# File lib/silicium/sparse.rb, line 44
def add(i_pos, j_pos, elem)
  if i_pos > @n || j_pos > @m || i_pos.negative? || j_pos.negative?
    raise 'out of range'
  end

  f = false
  @triplets.each do |item|
    if item[0] == i_pos && item[1] == j_pos
      item = [i_pos, j_pos, elem]
      f =  true
      break
    end
  end
  @triplets.push([i_pos, j_pos, elem]) unless f
end
adding(matrix) click to toggle source

@param [SparseMatrix] matrix - second matrix for adding @raise [ArgumentError] If the size of the first matrix doesn't match the size of the second matrix @return [SparseMatrix] Matrix as the sum of the other two matrices

Makes the sum of two matrix

# File lib/silicium/adding.rb, line 12
def adding(matrix)
  raise 'wrong argument' if @n != matrix.m

  res = SparseMatrix.new(@n, @m)
  (0..@n).each { |i|
    help_row1 = get_row(i)
    help_row2 = matrix.get_row(i)
    res_row = Array.new(@m, 0)
    j = 0
    help_row1.each do |elem|
      res_row[j] = elem + help_row2[j]
      j = j + 1
    end
    k = 0
    res_row.each do |elem|
      if (elem != 0)
        res.add(i, k, elem)
      end
      k = k+1
    end
  }
  res
end
copy() click to toggle source

@return [SparseMatrix::Object] - Returns a copy of a SparseMatrix object

Creates a copy of matrix object

# File lib/silicium/sparse.rb, line 30
def copy
  new = SparseMatrix.new(@n, @m)
  triplets.each do |triplet|
    new.add(triplet[0], triplet[1], triplet[2])
  end
  new
end
get(i_pos, j_pos) click to toggle source

@param [Integer] i_pos - The i position of an element @param [Integer] j_pos - The j position of an element @return [Integer] The element on i,j position

Returns an element by its position

# File lib/silicium/sparse.rb, line 66
def get(i_pos, j_pos)
  triplets.each do |triplet|
    if triplet[0] == i_pos && triplet[1] == j_pos
      return triplet[2]
    end
  end
  0
end
get_col(pos) click to toggle source

@param [Integer] pos - Position of a column to return @raise [ArgumentError] if position was less or bigger than count of rows @return [Array] The array that contains elements of column

Returns a column of sparse matrix by its position

# File lib/silicium/multi.rb, line 21
def get_col(pos)
  get_dimension({dimension: 1, position: 0}, pos)
end
get_row(pos) click to toggle source

@param [Integer] pos - Position of a row to return @raise [ArgumentError] if position was less or bigger than count of cols @return [Array] The array contains elements of row

Returns a row of sparse matrix by its position

# File lib/silicium/multi.rb, line 11
def get_row(pos)
  get_dimension({dimension: 0, position: 1}, pos)
end
mult_by_num(num) click to toggle source

@param [Integer] num - A number to multiply to

Multiplies matrix by a number

# File lib/silicium/multi.rb, line 58
def mult_by_num(num)
  return SparseMatrix.new(@n, @m) if num.zero?

  res = copy
  res.triplets.each do |triplet|
    triplet[2] *= num
  end
  res
end
multiply(matrix) click to toggle source

@param [SparseMatrix::Object] matrix - A matrix to multiply to @raise [ArgumentError] if count of columns of right matrix doesn't match count of rows of left matrix

Returns a matrix in its regular view but multiplied by other matrix

# File lib/silicium/multi.rb, line 38
def multiply(matrix)
  raise 'wrong argument' if @n != matrix.m

  rows = regular_view
  result = Array.new(@n) { Array.new }
  (0...@n).each { |i|
    (0...matrix.m).each { |j|
      result[i] << matrix
                       .get_col(j)
                       .zip(rows[i])
                       .inject(0) { |acc, current| acc + current[0] * current[1] }
    }
  }
  result
end
regular_view() click to toggle source

@return [Array] The array that contains rows of matrix Returns sparse matrix in its regular view

# File lib/silicium/multi.rb, line 28
def regular_view
  Array.new(@n) { |i| get_row(i) }
end
transpose() click to toggle source

Returns a transposed copy of matrix

# File lib/silicium/trans.rb, line 7
def transpose
  new = copy
  new.triplets.each do |triplet|
    triplet[0], triplet[1] = triplet[1], triplet[0]
  end
  new
end
transpose!() click to toggle source

Transposes matrix

# File lib/silicium/trans.rb, line 17
def transpose!
  triplets.each do |triplet|
    triplet[0], triplet[1] = triplet[1], triplet[0]
  end
end

Private Instance Methods

get_dimension(selector, pos) click to toggle source
# File lib/silicium/multi.rb, line 70
def get_dimension(selector, pos)
  raise 'wrong argument' if pos.negative? || pos > @m

  result = Array.new(@m, 0)
  @triplets
      .select { |triplet| triplet[selector[:dimension]] == pos }
      .each { |triplet| result[triplet[selector[:position]]] = triplet[2] }
  result
end