123456789_123456789_123456789_123456789_123456789_

Class: Matrix::Matrix

Relationships & Source Files
Inherits: Object
Defined in: lib/matrix.rb

Class Method Summary

Class Method Details

.column_vector(column)

Creates a single-column matrix where the values of that column are as given in Matrix#column. Matrix.column_vector([4,5,6])

=> 4

5

6

[ GitHub ]

  
# File 'lib/matrix.rb', line 209

def Matrix.column_vector(column)
  column = convert_to_array(column)
  new [column].transpose, 1
end

.combine(*matrices) {|*elements| ... }

Create a matrix by combining matrices entrywise, using the given block

x = Matrix[[6, 6], [4, 4]]
y = Matrix[[1, 2], [3, 4]]
Matrix.combine(x, y) {|a, b| a - b} # => Matrix[[5, 4], [1, 0]]
[ GitHub ]

  
# File 'lib/matrix.rb', line 288

def Matrix.combine(*matrices)
  return to_enum(__method__, *matrices) unless block_given?

  return Matrix.empty if matrices.empty?
  matrices.map!(&CoercionHelper.method(:coerce_to_matrix))
  x = matrices.first
  matrices.each do |m|
    raise ErrDimensionMismatch unless x.row_count == m.row_count && x.column_count == m.column_count
  end

  rows = Array.new(x.row_count) do |i|
    Array.new(x.column_count) do |j|
      yield matrices.map{|m| m[i,j]}
    end
  end
  new rows, x.column_count
end

.empty(row_count = 0, column_count = 0)

Creates a empty matrix of Matrix#row_count x Matrix#column_count. At least one of Matrix#row_count or Matrix#column_count must be 0.

m = Matrix.empty(2, 0)
m == Matrix[ [], [] ]
#  => true
n = Matrix.empty(0, 3)
n == Matrix.columns([ [], [], [] ])
#  => true
m * n
#  => Matrix[[0, 0, 0], [0, 0, 0]]

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/matrix.rb', line 227

def Matrix.empty(row_count = 0, column_count = 0)
  raise ArgumentError, "One size must be 0" if column_count != 0 && row_count != 0
  raise ArgumentError, "Negative size" if column_count < 0 || row_count < 0

  new([[]]*row_count, column_count)
end

.hstack(x, *matrices)

Create a matrix by stacking matrices horizontally

x = Matrix[[1, 2], [3, 4]]
y = Matrix[[5, 6], [7, 8]]
Matrix.hstack(x, y) # => Matrix[[1, 2, 5, 6], [3, 4, 7, 8]]
[ GitHub ]

  
# File 'lib/matrix.rb', line 262

def Matrix.hstack(x, *matrices)
  x = CoercionHelper.coerce_to_matrix(x)
  result = x.send(:rows).map(&:dup)
  total_column_count = x.column_count
  matrices.each do |m|
    m = CoercionHelper.coerce_to_matrix(m)
    if m.row_count != x.row_count
      raise ErrDimensionMismatch, "The given matrices must have #{x.row_count} rows, but one has #{m.row_count}"
    end
    result.each_with_index do |row, i|
      row.concat m.send(:rows)[i]
    end
    total_column_count += m.column_count
  end
  new result, total_column_count
end

I

[ GitHub ]

  
# File 'lib/matrix.rb', line 176

alias_method :I, :identity

.row_vector(row)

Creates a single-row matrix where the values of that row are as given in Matrix#row. Matrix.row_vector([4,5,6])

=> 4 5 6

[ GitHub ]

  
# File 'lib/matrix.rb', line 196

def Matrix.row_vector(row)
  row = convert_to_array(row)
  new [row]
end

.unit

[ GitHub ]

  
# File 'lib/matrix.rb', line 175

alias_method :unit, :identity

.vstack(x, *matrices)

Create a matrix by stacking matrices vertically

x = Matrix[[1, 2], [3, 4]]
y = Matrix[[5, 6], [7, 8]]
Matrix.vstack(x, y) # => Matrix[[1, 2], [3, 4], [5, 6], [7, 8]]
[ GitHub ]

  
# File 'lib/matrix.rb', line 241

def Matrix.vstack(x, *matrices)
  x = CoercionHelper.coerce_to_matrix(x)
  result = x.send(:rows).map(&:dup)
  matrices.each do |m|
    m = CoercionHelper.coerce_to_matrix(m)
    if m.column_count != x.column_count
      raise ErrDimensionMismatch, "The given matrices must have #{x.column_count} columns, but one has #{m.column_count}"
    end
    result.concat(m.send(:rows))
  end
  new result, x.column_count
end

.zero(row_count, column_count = row_count)

Creates a zero matrix. Matrix.zero(2)

=> 0 0

0 0

[ GitHub ]

  
# File 'lib/matrix.rb', line 185

def Matrix.zero(row_count, column_count = row_count)
  rows = Array.new(row_count){Array.new(column_count, 0)}
  new rows, column_count
end