Module: JBLAS::MatrixConvertMixin

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

Overview

Mixin for conversion options.

Collected in MatrixMixin.

Instance Method Summary collapse

Instance Method Details

#columns_to_aObject



78
79
80
81
82
# File 'lib/jblas/mixin_convert.rb', line 78

def columns_to_a
  (0...columns).map do |j|
    column(j).to_a
  end
end

#inspectObject

Return an a representation of this object.



68
69
70
# File 'lib/jblas/mixin_convert.rb', line 68

def inspect
  s = "<#{self.class} of size #{rows} #{columns}: #{to_s}>"
end

#rows_to_aObject



72
73
74
75
76
# File 'lib/jblas/mixin_convert.rb', line 72

def rows_to_a
  (0...rows).map do |i|
    row(i).to_a
  end
end

#to_aObject

Return the matrix as an Enumerable (just returns self).



92
93
94
# File 'lib/jblas/mixin_convert.rb', line 92

def to_a #:nodoc:
  data.to_a
end

#to_aryObject

Convert the matrix to an array.

If the matrix



87
88
89
# File 'lib/jblas/mixin_convert.rb', line 87

def to_ary
  to_a
end

#to_matObject

:nodoc:



96
97
98
# File 'lib/jblas/mixin_convert.rb', line 96

def to_mat #:nodoc:
  self
end

#to_matrixObject



100
101
102
# File 'lib/jblas/mixin_convert.rb', line 100

def to_matrix
  self
end

#to_s(fmt = nil, coljoin = ', ', rowjoin = '; ') ⇒ Object

Convert this matrix to a string.

This methods takes a few extra arguments to control how the result looks like.

fmt is a format as used by sprintf, coljoin is the string used to join column, rowjoin is what is used to join rows. For example,

x.to_s('%.1f', ' ', "\n")

Returns a matrix where columns are separated by spaces, rows by newlines and each element is shown with one digit after the comma.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jblas/mixin_convert.rb', line 52

def to_s(fmt=nil, coljoin=', ', rowjoin='; ')
  if fmt
    x = rows_to_a
    '[' + x.map do |r|
      if Enumerable === r
        r.map {|e| sprintf(fmt, e)}.join(coljoin)
      else
        sprintf(fmt, r)
      end
    end.join(rowjoin) + ']'
  else
    toString
  end
end