Module | JBLAS::MatrixGeneralMixin |
In: |
lib/jblas/mixin_general.rb
|
Mixin for general operations not fitting in any other category.
Collected in MatrixMixin.
Return a column vector.
# File lib/jblas/mixin_general.rb, line 181 def as_column unless vector? self else column_vector? ? self : self.t end end
Return a vector as row vector.
# File lib/jblas/mixin_general.rb, line 172 def as_row unless vector? self else row_vector? ? self : self.t end end
Check whether the column index i is valid.
# 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 whether the row index i is valid.
# 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
Returns true if self is a column vector.
# File lib/jblas/mixin_general.rb, line 162 def column_vector?; JAVA_METHOD; end
Returns a proxy of the matrix for which ’*’ is defined as elementwise.
That is:
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 ;) )
# File lib/jblas/mixin_general.rb, line 141 def e JBLAS::MatrixElementWiseProxy.new(self) end
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
# 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
Compute the inverse of self.
# 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
Returns true if self is a row vector.
# File lib/jblas/mixin_general.rb, line 159 def row_vector?; JAVA_METHOD; end
Save as ascii (tab-separated list, every row is a line)
# 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
Returns the first entry of self.
# File lib/jblas/mixin_general.rb, line 168 def scalar; JAVA_METHOD; end
Get the total number of elements. Synonymous to length.
# File lib/jblas/mixin_general.rb, line 60 def size length end
Solve the linear equation self * x = b.
# 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
Returns true if the matrix is square and symmetric.
# File lib/jblas/mixin_general.rb, line 80 def symmetric? square? and self.sub(self.t).normmax < 1e-6 end
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.
# File lib/jblas/mixin_general.rb, line 45 def t; transpose; end
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.
# 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