Module: JBLAS::MatrixAccessMixin

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

Overview

Mixin for all kinds of element access.

This mixin is collected into MatrixMixin.

You have the following options of accessing elements:

  • Linear access: accessing all elements linearly, going down rows first (x, x.get(i), x.put(i))

  • Two-dimensional access: Accessing element in row i, column j (x, x.get(i,j), x.put(i,j)

  • Accessing rows and columns: get_row(i), get_column(i), put_row(i), put_column(i), and also get_rows(i), get_columns(i).

  • Accessing row and column ranges: get_row_range, get_column_range, get_range

As indices, you can use one of the following:

  • Numbers: x, x, x.getRow(3), etc.

  • Indices as int[] arrays: Unfortunately, these are bit hard to construct in JRuby ([1,2,3].to_java :int), therefore the emthod to_indices is added to Numeric, Enumerable, Array, and the matrices. Then: ‘i = [1,2,3]; x`.

  • Matrices: In that case, x.find_indices is called to find the non-zero elements and use those as indices.

  • Ranges: See JBLAS::Ranges

For some access functions (in particular getting rows and columns), you can also specify where to copy the result to get better performance through suppressing object creations.

Instance Method Summary collapse

Instance Method Details

#[](i, j = nil) ⇒ Object

Get the entry at i, j. If j is omitted, linear addressing is used (that is, i just enumerates all entries going down rows first.)

As indices you can use numbers, int[] arrays, matrices (non-zero elements are taken as indices then), and ranges.



90
91
92
93
94
95
96
# File 'lib/jblas/mixin_access.rb', line 90

def [](i, j=nil)
  if j
    get(i.to_indices, j.to_indices)
  else
    get(i.to_indices)
  end
end

#[]=(i, j, v = nil) ⇒ Object

Set the entry at i, j to v. If j is omitted, linear addressing is used (that is, i just enumerates all entries going down rows first.)

As indices you can use numbers, int[] arrays, matrices (non-zero elements are taken as indices then), and ranges.



104
105
106
107
108
109
110
# File 'lib/jblas/mixin_access.rb', line 104

def []=(i, j, v=nil)
  if v
    put(i.to_indices, j.to_indices, v)
  else
    put(i.to_indices, j)
  end
end

#get_column(i) ⇒ Object

Get a column of a matrix. Unlike column(i) method, this method returns a copy of the given column.



125
# File 'lib/jblas/mixin_access.rb', line 125

def get_column(i); JAVA_METHOD; end

#get_column_range(i, j1, j2) ⇒ Object

Get a copy of columns j1 .. j2 - 1 from row i.



137
# File 'lib/jblas/mixin_access.rb', line 137

def get_column_range(i, j1, j2); JAVA_METHOD; end

#get_columns(i) ⇒ Object

Get a number of rows.

As indices you can use numbers, int[] arrays, matrices (non-zero elements are taken as indices then), and ranges.



131
# File 'lib/jblas/mixin_access.rb', line 131

def get_columns(i); JAVA_METHOD; end

#get_range(i1, i2, j1, j2) ⇒ Object

Get a copy of the submatrix with rows i1 .. i2 - 1 and columns j1 .. j2 - 1.



141
# File 'lib/jblas/mixin_access.rb', line 141

def get_range(i1, i2, j1, j2); JAVA_METHOD; end

#get_row(i, result = nil) ⇒ Object

Get row of a matrix. Unlike the row(i) method, this method returns a copy of the given row. If result is given, the row is copied in that matrix.



115
# File 'lib/jblas/mixin_access.rb', line 115

def get_row(i, result=nil); JAVA_METHOD; end

#get_row_range(i1, i2, j) ⇒ Object

Get a copy of rows i1 .. i2 - 1 from column j.



134
# File 'lib/jblas/mixin_access.rb', line 134

def get_row_range(i1, i2, j); JAVA_METHOD; end

#get_rows(i) ⇒ Object

Get a number of rows.

As indices you can use numbers, int[] arrays, matrices (non-zero elements are taken as indices then), and ranges.



121
# File 'lib/jblas/mixin_access.rb', line 121

def get_rows(i); JAVA_METHOD; end

#to_indicesObject

Return an array usable as an index.



144
145
146
# File 'lib/jblas/mixin_access.rb', line 144

def to_indices
  self
end