Class: MatrixExtended

Inherits:
Matrix
  • Object
show all
Defined in:
lib/matrix_extensions.rb

Overview

An extension to the Ruby Matrix class.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert_to_matrix_extended(m) ⇒ MatrixExtended

Converts a Matrix to an MatrixExtended.

Parameters:

  • m (Matrix)

    matrix

Returns:

Raises:

  • (TypeError)

    if matrices are not of type Matrix



11
12
13
14
15
# File 'lib/matrix_extensions.rb', line 11

def self.convert_to_matrix_extended(m)
  raise TypeError, "#{m.class} is not a Matrix" unless m.is_a?(Matrix)

  self.columns(m.column_vectors)
end

.convert_vector_to_matrix(v, dimension) ⇒ MatrixExtended

Convert vector to matrix for arithmetic operations.

Parameters:

  • vector (Vector)

    vector

  • dimension (Symbol)

    :row or :column

Returns:

Raises:

  • (TypeError)

    if vector ist not of type Vector



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/matrix_extensions.rb', line 190

def self.convert_vector_to_matrix(v, dimension)
  raise TypeError, "#{v.class} is not a Vector" unless v.is_a? Vector    

  if dimension == :row
    self.row_vector(v)
  elsif dimension == :column
    self.column_vector(v)
  else
    raise ArgumentError
  end
end

.hconcat(*matrices) ⇒ MatrixExtended

Concatenates two matrices horizontally (resulting in more columns).

Parameters:

Returns:

Raises:

  • (ErrDimensionMismatch)

    if dimensions don’t match

  • (TypeError)

    if matrices are not of type Matrix or Vector



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/matrix_extensions.rb', line 37

def self.hconcat(*matrices)        
  columns = []
  matrices.each do |m|
    raise TypeError, "#{m.class} is not a Matrix or Vector" unless m.is_a?(Matrix) || m.is_a?(Vector)
    
    # convert Vector to Matrix
    m = self.convert_vector_to_matrix(m, :column) if m.is_a? Vector

    # check if dimensions match
    row_count ||= m.row_count
    Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count
    
    # prepare array of columns
    m.column_vectors.each do |v|
      columns << v.to_a
    end
  end

  # create new matrix
  self.columns(columns)
end

.ones(rows = 1, columns = 1) ⇒ MatrixExtended

Matrix prefilled with ones.

Parameters:

Returns:



28
29
30
# File 'lib/matrix_extensions.rb', line 28

def self.ones(rows = 1, columns = 1)
  MatrixExtended.build(rows, columns) { 1 }
end

.vconcat(*matrices) ⇒ MatrixExtended

Concatenates two matrices vertically (resulting in more rows).

Parameters:

Returns:

Raises:

  • (ErrDimensionMismatch)

    if dimensions don’t match

  • (TypeError)

    if matrices are not of type Matrix or Vector



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/matrix_extensions.rb', line 64

def self.vconcat(*matrices)
  rows = []
  matrices.each do |m|
    raise TypeError, "#{m.class} is not a Matrix or Vector" unless m.is_a?(Matrix) || m.is_a?(Vector)
    
    # convert Vector to Matrix
    m = self.convert_vector_to_matrix(m, :row) if m.is_a? Vector

    # check if dimensions match
    column_count ||= m.column_count
    Matrix.Raise ErrDimensionMismatch unless column_count == m.column_count
    
    # prepare array of columns
    m.row_vectors.each do |v|
      rows << v.to_a
    end
  end

  # create new matrix
  self.rows(rows)
end

.zeros(rows = 1, columns = 1) ⇒ MatrixExtended

Matrix prefilled with zeros.

Parameters:

Returns:



20
21
22
# File 'lib/matrix_extensions.rb', line 20

def self.zeros(rows = 1, columns = 1)
  MatrixExtended.build(rows, columns) { 0 }
end

Instance Method Details

#element_division(m) ⇒ MatrixExtended

Element-wise division.

Parameters:

Returns:

Raises:

  • (ErrDimensionMismatch)

    if dimensions don’t match



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/matrix_extensions.rb', line 90

def element_division(m)
  case m
  when Numeric
    return self./ m
  when Vector            
    if row_count > column_count
      # Matrix is of dimension X * 1 (or there'll be an ErrDimensionMismatch)
      m = self.class.convert_vector_to_matrix(m, :column)      
    else
      # Matrix is of dimension 1 * X (or there'll be an ErrDimensionMismatch)
      m = self.class.convert_vector_to_matrix(m, :row)
    end
  when Matrix
  else
    return apply_through_coercion(m, __method__)
  end

  Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count

  rows = Array.new(row_count) do |i|
    Array.new(column_count) do|j|
      self[i, j] / m[i, j]
    end
  end
  new_matrix rows, column_count  
end

#element_exponentiation(m) ⇒ MatrixExtended

Element-wise exponentiation.

Parameters:

Returns:

Raises:

  • (ErrDimensionMismatch)

    if dimensions don’t match



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/matrix_extensions.rb', line 152

def element_exponentiation(m)
  case m
  when Numeric
    # self.** m will break
    rows = @rows.collect {|row|
      row.collect {|e| e ** m }
    }
    return new_matrix rows, column_count
  when Vector            
    if row_count > column_count
      # Matrix is of dimension X * 1 (or there'll be an ErrDimensionMismatch)
      m = self.class.convert_vector_to_matrix(m, :column)      
    else
      # Matrix is of dimension 1 * X (or there'll be an ErrDimensionMismatch)
      m = self.class.convert_vector_to_matrix(m, :row)
    end
  when Matrix
  else
    return apply_through_coercion(m, __method__)
  end

  Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count

  rows = Array.new(row_count) do |i|
    Array.new(column_count) do|j|
      self[i, j] ** m[i, j]
    end
  end
  new_matrix rows, column_count 
end

#element_multiplication(m) ⇒ MatrixExtended

Element-wise multiplication.

Parameters:

Returns:

Raises:

  • (ErrDimensionMismatch)

    if dimensions don’t match



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/matrix_extensions.rb', line 121

def element_multiplication(m)
  case m
  when Numeric
    return self.* m
  when Vector            
    if row_count > column_count
      # Matrix is of dimension X * 1 (or there'll be an ErrDimensionMismatch)
      m = self.class.convert_vector_to_matrix(m, :column)      
    else
      # Matrix is of dimension 1 * X (or there'll be an ErrDimensionMismatch)
      m = self.class.convert_vector_to_matrix(m, :row)
    end
  when Matrix
  else
    return apply_through_coercion(m, __method__)
  end

  Matrix.Raise ErrDimensionMismatch unless row_count == m.row_count && column_count == m.column_count

  rows = Array.new(row_count) do |i|
    Array.new(column_count) do|j|
      self[i, j] * m[i, j]
    end
  end
  new_matrix rows, column_count  
end