Class: RMatrix::Vector

Inherits:
Matrix
  • Object
show all
Defined in:
lib/rmatrix/vector.rb

Constant Summary

Constants inherited from Matrix

Matrix::OPERATIONS_MAP

Instance Attribute Summary

Attributes inherited from Matrix

#column_label_map, #column_map, #invert_next_operation, #matrix, #narray, #row_label_map, #row_map, #typecode

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Matrix

#*, #==, #_dump, _load, #abs, #adjoint, blank, #box_lines, #coerce, #cofactor_matrix, #columns, #concat, #condensed, #determinant, #diag, #each, #each_column, #each_row, #flat_map, gen_delegator, gen_matrix_delegator, gen_mutator, gen_typeconstructor, gen_vec_or_matrix_delegator, #gplot, #gruff, identity, #is_vector?, #join, #map, #mask, #minor, #mmap, #mult, ones, #parse_map, #round, #rows, seed, #set_all, #size, #sum, #sum_columns, #sum_rows, #threed_gplot, #to_f, #to_i, #to_m, #to_s, #to_significant_figures, #to_tex, #to_type, translate_op, #transpose, #two_dimensional, #zip

Methods included from Indices

#[], #[]=, #build_result_map, #indexify, #method_missing, #raw, #unmap_args, #unmap_index, #walk_indices

Constructor Details

#initialize(source, typecode = Typecode::FLOAT, column_map: nil, row_map: nil) ⇒ Vector

Returns a new instance of Vector.



4
5
6
7
8
9
# File 'lib/rmatrix/vector.rb', line 4

def initialize(source, typecode=Typecode::FLOAT, column_map: nil, row_map: nil)
  super
  unless (shape.length == 2 && [rows, columns].include?(1)) || shape.length == 0
    raise "Invalid dimensions #{shape.join(?x).reverse}. Vector must be eiter Nx1 or 1xM"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RMatrix::Indices

Class Method Details

.[](*inputs, typecode: Typecode::FLOAT, row_map: nil, column_map: nil, column_label_map: nil, row_label_map: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rmatrix/vector.rb', line 11

def self.[](*inputs, typecode: Typecode::FLOAT, row_map: nil, column_map: nil, column_label_map: nil, row_label_map: nil)
  if inputs.length == 1 && [String, Symbol].include?(inputs[0].class)
    if ['byte', 'sint', 'int', 'sfloat', 'float', 'scomplex', 'complex', 'object'].include?(inputs[0].to_s)
      ->(*source){ V.new(source, inputs[0].to_s) }
    else
      Vector.new(inputs[0])
    end
  else
    Vector.new(inputs, typecode, row_map: nil, column_map: nil)
  end
end

.inspect_vector(v) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rmatrix/vector.rb', line 31

def self.inspect_vector(v)
  elms = v.narray.to_a.flatten
  print = elms.first(10)
  has_more = elms.length > 10
  if v.rows == 1
    "Vector(#{v.narray.length})\nV[#{print.join(", ") + (has_more ? ',...' : '')}]"
  else
    "Vector(#{v.narray.length})\nV[\n [#{print.join("],\n [") + (has_more ? "\n  ..." : '')}]\n]"
  end
end

Instance Method Details

#inspectObject



27
28
29
# File 'lib/rmatrix/vector.rb', line 27

def inspect
  self.class.inspect_vector(self)
end

#to_aObject



23
24
25
# File 'lib/rmatrix/vector.rb', line 23

def to_a
  return narray.reshape(narray.length).to_a
end