Module JBLAS::MatrixGeneralMixin
In: lib/jblas/mixin_general.rb

Mixin for general operations not fitting in any other category.

Collected in MatrixMixin.

Methods

as_column   as_row   check_column_index   check_row_index   column_vector?   d   dims   e   hcat   inv   row_vector?   save_ascii   scalar   scalar?   size   solve   symmetric?   t   transpose   vcat   vector?  

Public Instance methods

Return a column vector.

[Source]

# 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.

[Source]

# 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.

[Source]

# 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.

[Source]

# 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.

[Source]

# File lib/jblas/mixin_general.rb, line 162
      def column_vector?; JAVA_METHOD; end

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))

[Source]

# File lib/jblas/mixin_general.rb, line 150
    def d
      MatrixDotProxy.new(self)
    end

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

[Source]

# File lib/jblas/mixin_general.rb, line 55
    def dims
      [rows, columns]
    end

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 ;) )

[Source]

# 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

[Source]

# 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.

[Source]

# 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.

[Source]

# 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)

[Source]

# 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.

[Source]

# File lib/jblas/mixin_general.rb, line 168
      def scalar; JAVA_METHOD; end

Returns true if self is a scalar.

[Source]

# File lib/jblas/mixin_general.rb, line 165
      def scalar?; JAVA_METHOD; end

Get the total number of elements. Synonymous to length.

[Source]

# File lib/jblas/mixin_general.rb, line 60
    def size
      length
    end

Solve the linear equation self * x = b.

[Source]

# 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.

[Source]

# 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.

[Source]

# File lib/jblas/mixin_general.rb, line 45
    def t; transpose; end

Transpose the matrix. See t.

[Source]

# File lib/jblas/mixin_general.rb, line 49
      def transpose
        JAVA_METHOD
      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.

[Source]

# 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

Returns true if self is a vector

[Source]

# File lib/jblas/mixin_general.rb, line 156
      def vector?; JAVA_METHOD; end

[Validate]