Class: Matrix

Inherits:
Object show all
Defined in:
lib/y_support/stdlib_ext/matrix/misc.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(row_size, column_size = row_size) ⇒ Object

Creates a matrix of size row_size x column_size. It fills the values by calling the given block, passing the current row and column. Returns an enumerator if no block is given.

m = Matrix.build(2, 4) {|row, col| col - row }
  => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
m = Matrix.build(3) { rand }
  => a 3x3 matrix with random elements

Raises:

  • (ArgumentError)


112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 112

def Matrix.build(row_size, column_size = row_size)
  row_size = CoercionHelper.coerce_to_int(row_size)
  column_size = CoercionHelper.coerce_to_int(column_size)
  raise ArgumentError if row_size < 0 || column_size < 0
  return to_enum :build, row_size, column_size unless block_given?
  rows = Array.new(row_size) do |i|
    Array.new(column_size) do |j|
      yield i, j
    end
  end
  new rows, column_size
end

.correspondence_matrix(array1, array2) ⇒ Object

Given two arrays, creates correspondence matrix, with no. of cols equal to the 1st array, and no. of rows to the 2nd. This matrix can be used eg. for conversion between column vectors corresponding to the 1st and 2nd array:

Matrix.correspondence_matrix( array1, array2 ) * col_vector_1 #=> col_vector_2



37
38
39
40
41
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 37

def self.correspondence_matrix( array1, array2 )
  return Matrix.empty 0, array1.size if array2.empty?
  return Matrix.empty array2.size, 0 if array1.empty?
  self[ *array2.map { |e2| array1.map { |e1| e1 == e2 ? 1 : 0 } } ] # FIXME: Ordinary zero
end

.cv(*aa, &b) ⇒ Object

Shorter aliases for #row_vector, #column_vector



55
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 55

def self.cv *aa, &b; column_vector *aa, &b end

.empty(row_size = 0, column_size = 0) ⇒ Object

Creates a empty matrix of row_size x column_size. At least one of row_size or column_size 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]]


94
95
96
97
98
99
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 94

def Matrix.empty(row_size = 0, column_size = 0)
  Matrix.Raise ArgumentError, "One size must be 0" if column_size != 0 && row_size != 0
  Matrix.Raise ArgumentError, "Negative size" if column_size < 0 || row_size < 0

  new([[]]*row_size, column_size)
end

.rv(*aa, &b) ⇒ Object



56
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 56

def self.rv *aa, &b; row_vector *aa, &b end

Instance Method Details

#column_to_a(n = 0) ⇒ Object

Converts a column into array. If argument is given, it chooses column number, otherwise column 0 is assumed.



46
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 46

def column_to_a n=0; ( col = column( n ) ) ? col.to_a : nil end

#join_bottom(other) ⇒ Object

join_bottom method

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 60

def join_bottom other;
  raise ArgumentError, "Column size mismatch" unless
    column_size == other.column_size
  return other.map { |e| e } if row_size == 0
  return Matrix.empty row_size + other.row_size, 0 if column_size == 0
  self.class[ *( row_vectors + other.row_vectors ) ]
end

#join_right(other) ⇒ Object

join_right methods

Raises:

  • (ArgumentError)


70
71
72
73
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 70

def join_right other;
  raise ArgumentError, "Row size mismatch" unless row_size == other.row_size
  ( t.join_bottom( other.t ) ).t
end

#pretty_inspectObject

Pretty inspect



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 5

def pretty_inspect
  return inspect if row_size == 0 or column_size == 0
  aa = send( :rows ).each.with_object [] do |row, memo|
    memo << row.map { |o|
      os = o.to_s
      case o
      when Numeric then os[0] == '-' ? os : ' ' + os
      else o.to_s end
    }
  end
  width = aa.map { |row| row.map( &:size ).max }.max + 1
  aa.each_with_object "" do |row, memo|
    row.each { |e| memo << e << ' ' * ( width - e.size ) }
    memo << "\n"
  end
end

#pretty_printObject Also known as: pp

Pretty print



23
24
25
26
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 23

def pretty_print
  print pretty_inspect
  return nil
end

#row_to_a(n = 0) ⇒ Object

Converts a row into array. If argument is given, it chooses row number, otherwise row 0 is assumed.



51
# File 'lib/y_support/stdlib_ext/matrix/misc.rb', line 51

def row_to_a n=0; ( r = row( n ) ) ? r.to_a : nil end