Class: Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/matrix.rb

Instance Method Summary collapse

Instance Method Details

#+(v) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/core_ext/matrix.rb', line 13

def +(v)
  case v
  when Vector
    Vector.Raise ErrDimensionMismatch if size != v.size
    els = collect2(v) {|v1, v2|
      v1 + v2
    }
    Vector.elements(els, false)
  when Matrix
    Matrix.column_vector(self) + v
  when Array
    Vector.Raise ErrDimensionMismatch if size != v.length
    Vector.elements(v) + self
  when Numeric
    Vector.elements([v] * size) + self
  else
    apply_through_coercion(v, __method__)
  end
end

#-(v) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/core_ext/matrix.rb', line 33

def -(v)
  case v
  when Vector
    Vector.Raise ErrDimensionMismatch if size != v.size
    els = collect2(v) {|v1, v2|
      v1 - v2
    }
    Vector.elements(els, false)
  when Matrix
    Matrix.column_vector(self) - v
  when Array
    Vector.Raise ErrDimensionMismatch if size != v.length
    Vector.elements(v) - self
  when Numeric
    Vector.elements([v] * size) - self
  else
    apply_through_coercion(v, __method__)
  end
end

#[](*i) ⇒ Object



9
10
11
# File 'lib/core_ext/matrix.rb', line 9

def [](*i)
  i.length == 1 and i[0].is_a?(Numeric) ? @elements[i[0]] : Vector.elements(@elements[*i])
end

#[]=(pos, value) ⇒ Object



5
6
7
# File 'lib/core_ext/matrix.rb', line 5

def []=(pos, value)
  @elements[pos] = value.to_a
end