Module: JBLAS::MatrixClassMixin

Included in:
ComplexDoubleMatrix, ComplexFloatMatrix, DoubleMatrix, FloatMatrix
Defined in:
lib/jblas/mixin_class.rb

Overview

Mixin for the Matrix classes. These basically add the [] construction method (such that you can say DoubleMatrix

Instance Method Summary collapse

Instance Method Details

#[](*data) ⇒ Object

Create a new matrix. For example, you can say DoubleMatrix.

See also from_array.



42
43
44
# File 'lib/jblas/mixin_class.rb', line 42

def [](*data)
  from_array data
end

#from_array(data) ⇒ Object

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],]

See also [], JBLAS#mat



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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