Class: Vector
Overview
The Vector
class represents a mathematical vector, which is useful in its own right, and also constitutes a row or column of a Matrix.
Method Catalogue
To create a Vector:
-
Vector.[](*array)
-
Vector.elements(array, copy = true)
To access elements:
-
[](i)
To enumerate the elements:
-
#each2(v)
-
#collect2(v)
Vector arithmetic:
-
*(x) "is matrix or number"
-
+(v)
-
-(v)
Vector functions:
-
#inner_product(v)
-
#collect
-
#map
-
#map2(v)
-
#r
-
#size
Conversion to other data types:
-
#covector
-
#to_a
-
#coerce(other)
String representations:
-
#to_s
-
#inspect
Class Method Summary collapse
-
.[](*array) ⇒ Object
Creates a Vector from a list of elements.
-
.elements(array, copy = true) ⇒ Object
Creates a vector from an Array.
Instance Method Summary collapse
-
#*(x) ⇒ Object
Multiplies the vector by
x
, wherex
is a number or another vector. -
#+(v) ⇒ Object
Vector addition.
-
#-(v) ⇒ Object
Vector subtraction.
-
#==(other) ⇒ Object
(also: #eql?)
Returns
true
iff the two vectors have the same elements in the same order. -
#[](i) ⇒ Object
Returns element number
i
(starting at zero) of the vector. -
#clone ⇒ Object
Return a copy of the vector.
-
#coerce(other) ⇒ Object
FIXME: describe Vector#coerce.
-
#collect ⇒ Object
(also: #map)
Like Array#collect.
-
#collect2(v) ⇒ Object
Collects (as in Enumerable#collect) over the elements of this vector and
v
in conjunction. -
#compare_by(elements) ⇒ Object
For internal use.
-
#covector ⇒ Object
Creates a single-row matrix from this vector.
-
#each2(v) ⇒ Object
Iterate over the elements of this vector and
v
in conjunction. -
#hash ⇒ Object
Return a hash-code for the vector.
-
#init_elements(array, copy) ⇒ Object
For internal use.
-
#initialize(method, array, copy) ⇒ Vector
constructor
For internal use.
-
#inner_product(v) ⇒ Object
Returns the inner product of this vector with the other.
-
#inspect ⇒ Object
Overrides Object#inspect.
-
#map2(v) ⇒ Object
Like Vector#collect2, but returns a Vector instead of an Array.
-
#r ⇒ Object
Returns the modulus (Pythagorean distance) of the vector.
-
#size ⇒ Object
Returns the number of elements in the vector.
-
#to_a ⇒ Object
Returns the elements of the vector in an array.
-
#to_s ⇒ Object
Overrides Object#to_s.
Constructor Details
#initialize(method, array, copy) ⇒ Vector
For internal use.
1020 1021 1022 |
# File 'lib/matrix.rb', line 1020 def initialize(method, array, copy) self.send(method, array, copy) end |
Class Method Details
.[](*array) ⇒ Object
Creates a Vector from a list of elements.
Vector[7, 4, ...]
1005 1006 1007 |
# File 'lib/matrix.rb', line 1005 def Vector.[](*array) new(:init_elements, array, copy = false) end |
.elements(array, copy = true) ⇒ Object
Creates a vector from an Array. The optional second argument specifies whether the array itself or a copy is used internally.
1013 1014 1015 |
# File 'lib/matrix.rb', line 1013 def Vector.elements(array, copy = true) new(:init_elements, array, copy) end |
Instance Method Details
#*(x) ⇒ Object
Multiplies the vector by x
, where x
is a number or another vector.
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 |
# File 'lib/matrix.rb', line 1120 def *(x) case x when Numeric els = @elements.collect{|e| e * x} Vector.elements(els, false) when Matrix Matrix.column_vector(self) * x else s, x = x.coerce(self) s * x end end |
#+(v) ⇒ Object
Vector addition.
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 |
# File 'lib/matrix.rb', line 1136 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 else s, x = v.coerce(self) s + x end end |
#-(v) ⇒ Object
Vector subtraction.
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 |
# File 'lib/matrix.rb', line 1156 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 else s, x = v.coerce(self) s - x end end |
#==(other) ⇒ Object Also known as: eql?
Returns true
iff the two vectors have the same elements in the same order.
1085 1086 1087 1088 1089 |
# File 'lib/matrix.rb', line 1085 def ==(other) return false unless Vector === other other.compare_by(@elements) end |
#[](i) ⇒ Object
Returns element number i
(starting at zero) of the vector.
1040 1041 1042 |
# File 'lib/matrix.rb', line 1040 def [](i) @elements[i] end |
#clone ⇒ Object
Return a copy of the vector.
1102 1103 1104 |
# File 'lib/matrix.rb', line 1102 def clone Vector.elements(@elements) end |
#coerce(other) ⇒ Object
FIXME: describe Vector#coerce.
1248 1249 1250 1251 1252 1253 1254 1255 |
# File 'lib/matrix.rb', line 1248 def coerce(other) case other when Numeric return Scalar.new(other), self else raise TypeError, "#{self.class} can't be coerced into #{other.class}" end end |
#collect ⇒ Object Also known as: map
Like Array#collect.
1195 1196 1197 1198 1199 1200 1201 |
# File 'lib/matrix.rb', line 1195 def collect # :yield: e els = @elements.collect { |v| yield v } Vector.elements(els, false) end |
#collect2(v) ⇒ Object
Collects (as in Enumerable#collect) over the elements of this vector and v
in conjunction.
1070 1071 1072 1073 1074 1075 1076 |
# File 'lib/matrix.rb', line 1070 def collect2(v) # :yield: e1, e2 Vector.Raise ErrDimensionMismatch if size != v.size (0 .. size - 1).collect do |i| yield @elements[i], v[i] end end |
#compare_by(elements) ⇒ Object
For internal use.
1095 1096 1097 |
# File 'lib/matrix.rb', line 1095 def compare_by(elements) @elements == elements end |
#covector ⇒ Object
Creates a single-row matrix from this vector.
1234 1235 1236 |
# File 'lib/matrix.rb', line 1234 def covector Matrix.row_vector(self) end |
#each2(v) ⇒ Object
Iterate over the elements of this vector and v
in conjunction.
1058 1059 1060 1061 1062 1063 1064 |
# File 'lib/matrix.rb', line 1058 def each2(v) # :yield: e1, e2 Vector.Raise ErrDimensionMismatch if size != v.size 0.upto(size - 1) do |i| yield @elements[i], v[i] end end |
#hash ⇒ Object
Return a hash-code for the vector.
1109 1110 1111 |
# File 'lib/matrix.rb', line 1109 def hash @elements.hash end |
#init_elements(array, copy) ⇒ Object
For internal use.
1027 1028 1029 1030 1031 1032 1033 |
# File 'lib/matrix.rb', line 1027 def init_elements(array, copy) if copy @elements = array.dup else @elements = array end end |
#inner_product(v) ⇒ Object
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 |
# File 'lib/matrix.rb', line 1181 def inner_product(v) Vector.Raise ErrDimensionMismatch if size != v.size p = 0 each2(v) { |v1, v2| p += v1 * v2 } p end |
#inspect ⇒ Object
Overrides Object#inspect
1271 1272 1273 |
# File 'lib/matrix.rb', line 1271 def inspect str = "Vector"+@elements.inspect end |
#map2(v) ⇒ Object
Like Vector#collect2, but returns a Vector instead of an Array.
1207 1208 1209 1210 1211 1212 1213 |
# File 'lib/matrix.rb', line 1207 def map2(v) # :yield: e1, e2 els = collect2(v) { |v1, v2| yield v1, v2 } Vector.elements(els, false) end |
#r ⇒ Object
Returns the modulus (Pythagorean distance) of the vector.
Vector[5,8,2].r => 9.643650761
1219 1220 1221 1222 1223 1224 1225 |
# File 'lib/matrix.rb', line 1219 def r v = 0 for e in @elements v += e*e end return Math.sqrt(v) end |
#size ⇒ Object
Returns the number of elements in the vector.
1047 1048 1049 |
# File 'lib/matrix.rb', line 1047 def size @elements.size end |
#to_a ⇒ Object
Returns the elements of the vector in an array.
1241 1242 1243 |
# File 'lib/matrix.rb', line 1241 def to_a @elements.dup end |
#to_s ⇒ Object
Overrides Object#to_s
1264 1265 1266 |
# File 'lib/matrix.rb', line 1264 def to_s "Vector[" + @elements.join(", ") + "]" end |