Module: JBLAS::MatrixGeneralMixin

Included in:
MatrixMixin
Defined in:
lib/jblas/mixin_general.rb

Overview

Mixin for general operations not fitting in any other category.

Collected in MatrixMixin.

Instance Method Summary collapse

Instance Method Details

#as_columnObject

Return a column vector.



181
182
183
184
185
186
187
# File 'lib/jblas/mixin_general.rb', line 181

def as_column
  unless vector?
    self
  else
    column_vector? ? self : self.t
  end
end

#as_rowObject

Return a vector as row vector.



172
173
174
175
176
177
178
# File 'lib/jblas/mixin_general.rb', line 172

def as_row
  unless vector?
    self
  else
    row_vector? ? self : self.t
  end
end

#check_column_index(i) ⇒ Object

Check whether the column index i is valid.



66
67
68
69
70
# File 'lib/jblas/mixin_general.rb', line 66

def check_column_index(i)
  unless 0 <= i and i < columns
    raise IndexError, "column index out of bounds"
  end
end

#check_row_index(i) ⇒ Object

Check whether the row index i is valid.



73
74
75
76
77
# File 'lib/jblas/mixin_general.rb', line 73

def check_row_index(i)
  unless 0 <= i and i < rows
    raise IndexError, "column index out of bounds"
  end
end

#dObject

Returns a proxy for the matrix for which ‘*’ is defined as the scalar product. See also e

  • a * b => matrix multiplication (a.mmul(b))

  • a .d* b => scalar product (a.dot(b))



150
151
152
# File 'lib/jblas/mixin_general.rb', line 150

def d
  MatrixDotProxy.new(self)
end

#dimsObject

Get the size of the matrix as [rows, columns]



55
56
57
# File 'lib/jblas/mixin_general.rb', line 55

def dims
  [rows, columns]
end

#eObject

Returns a proxy of the matrix for which ‘*’ is defined as elementwise.

That is:

  • a * b => matrix multiplication

  • a.e * b => element wise multiplication

For extra coolness, try writing it as “a .e* b”, such that it looks more like the “.e” belongs to the operator, not the object. (Not sure whether this is really worth it, though ;) )



141
142
143
# File 'lib/jblas/mixin_general.rb', line 141

def e
  JBLAS::MatrixElementWiseProxy.new(self)
end

#hcat(y) ⇒ Object

Return a new matrix which consists of the self and y side by side. In general the hcat method should be used sparingly as it creates a new matrix and copies everything on each use. You should always ask yourself if an array of vectors or matrices doesn’t serve you better. That said, you can do funny things with inject. For example,

a = mat[1,2,3]
[a, 2*a, 3*a].inject {|s,x| s = s.hcat(x)}
=> 1.0  2.0  3.0
   2.0  4.0  6.0
   3.0  6.0  9.0


114
115
116
117
118
119
# File 'lib/jblas/mixin_general.rb', line 114

def hcat(y)
  unless self.dims[0] == y.dims[0]
    raise ArgumentError, "Matrices must have same number of rows"
  end
  DoubleMatrix.concat_horizontally(self, y)
end

#invObject

Compute the inverse of self.



85
86
87
88
89
90
91
# File 'lib/jblas/mixin_general.rb', line 85

def inv
  unless square?
    raise ArgumentError, 'Inverses can only be computed from square ' +
      'matrices. Use solve instead!'
  end
  self.solve(self.class.eye(rows))
end

#save_ascii(fn) ⇒ Object

Save as ascii (tab-separated list, every row is a line)



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

def save_ascii(fn)
  o = open(fn, 'w')
  rows.times do |i|
    columns.times do |j|
      o.print get(i,j)
      o.print "\t" if j < columns - 1
    end
    o.puts
  end
  o.close
end

#sizeObject

Get the total number of elements. Synonymous to length.



60
61
62
# File 'lib/jblas/mixin_general.rb', line 60

def size
  length
end

#solve(b) ⇒ Object

Solve the linear equation self * x = b.



94
95
96
97
98
99
100
# File 'lib/jblas/mixin_general.rb', line 94

def solve(b)
  if symmetric?
    Solve.solve_symmetric(self, b)
  else
    Solve.solve(self, b)
  end
end

#symmetric?Boolean

Returns true if the matrix is square and symmetric.

Returns:

  • (Boolean)


80
81
82
# File 'lib/jblas/mixin_general.rb', line 80

def symmetric?
  square? and self.sub(self.t).normmax < 1e-6
end

#tObject

Transpose the matrix. You obtain a transposed view of the matrix. Some operations are not possible on such views, for example most in-place operations. For such, do a compact first.



45
# File 'lib/jblas/mixin_general.rb', line 45

def t; transpose; end

#vcat(y) ⇒ Object

Return a new matrix which consists of the self on top of y. In general the hcat methods should be used sparingly. You should always ask yourself if an array of vectors or matrices doesn’t serve you better. See also hcat.



125
126
127
128
129
130
# File 'lib/jblas/mixin_general.rb', line 125

def vcat(y)
  unless self.dims[1] == y.dims[1]
    raise ArgumentError, "Matrices must have same number of columns"
  end
  DoubleMatrix.concat_vertically(self, y)
end