Module | JBLAS::MatrixClassMixin |
In: |
lib/jblas/mixin_class.rb
|
Mixin for the Matrix classes. These basically add the [] construction method (such that you can say DoubleMatrix[1,2,3]
Create a new matrix. For example, you can say DoubleMatrix[1,2,3].
See also from_array.
# File lib/jblas/mixin_class.rb, line 42 def [](*data) from_array data end
Create a new matrix. There are two ways to use this function
pass an array: | Constructs a column vector. For example: DoubleMatrix.from_array 1, 2, 3 |
pass an array of arrays: | Constructs a matrix, inner arrays are rows. For example: DoubleMatrix.from_array [[1,2,3],[4,5,6]] |
See also [], JBLAS#mat
# File lib/jblas/mixin_class.rb, line 55 def from_array(data) n = data.length if data.reject{|l| Numeric === l}.size == 0 a = self.new(n, 1) (0...data.length).each do |i| a[i, 0] = data[i] end return a else begin lengths = data.collect{|v| v.length} rescue raise "All columns must be arrays" end raise "All columns must have equal length!" if lengths.min < lengths.max a = self.new(n, lengths.max) for i in 0...n for j in 0...lengths.max a[i,j] = data[i][j] end end return a end end