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, wherexis a number or another vector. -
#+(v) ⇒ Object
Vector addition.
-
#-(v) ⇒ Object
Vector subtraction.
-
#==(other) ⇒ Object
(also: #eqn?)
Returns
trueiff 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
vin 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
vin 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.
1014 1015 1016 |
# File 'lib/matrix.rb', line 1014 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, ...]
999 1000 1001 |
# File 'lib/matrix.rb', line 999 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.
1007 1008 1009 |
# File 'lib/matrix.rb', line 1007 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.
1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 |
# File 'lib/matrix.rb', line 1114 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.
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 |
# File 'lib/matrix.rb', line 1130 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.
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 |
# File 'lib/matrix.rb', line 1150 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: eqn?
Returns true iff the two vectors have the same elements in the same order.
1079 1080 1081 1082 1083 |
# File 'lib/matrix.rb', line 1079 def ==(other) return false unless Vector === other other.compare_by(@elements) end |
#[](i) ⇒ Object
Returns element number i (starting at zero) of the vector.
1034 1035 1036 |
# File 'lib/matrix.rb', line 1034 def [](i) @elements[i] end |
#clone ⇒ Object
Return a copy of the vector.
1096 1097 1098 |
# File 'lib/matrix.rb', line 1096 def clone Vector.elements(@elements) end |
#coerce(other) ⇒ Object
FIXME: describe Vector#coerce.
1242 1243 1244 1245 1246 1247 1248 1249 |
# File 'lib/matrix.rb', line 1242 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.
1189 1190 1191 1192 1193 1194 1195 |
# File 'lib/matrix.rb', line 1189 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.
1064 1065 1066 1067 1068 1069 1070 |
# File 'lib/matrix.rb', line 1064 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.
1089 1090 1091 |
# File 'lib/matrix.rb', line 1089 def compare_by(elements) @elements == elements end |
#covector ⇒ Object
Creates a single-row matrix from this vector.
1228 1229 1230 |
# File 'lib/matrix.rb', line 1228 def covector Matrix.row_vector(self) end |
#each2(v) ⇒ Object
Iterate over the elements of this vector and v in conjunction.
1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/matrix.rb', line 1052 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.
1103 1104 1105 |
# File 'lib/matrix.rb', line 1103 def hash @elements.hash end |
#init_elements(array, copy) ⇒ Object
For internal use.
1021 1022 1023 1024 1025 1026 1027 |
# File 'lib/matrix.rb', line 1021 def init_elements(array, copy) if copy @elements = array.dup else @elements = array end end |
#inner_product(v) ⇒ Object
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 |
# File 'lib/matrix.rb', line 1175 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
1265 1266 1267 |
# File 'lib/matrix.rb', line 1265 def inspect str = "Vector"+@elements.inspect end |
#map2(v) ⇒ Object
Like Vector#collect2, but returns a Vector instead of an Array.
1201 1202 1203 1204 1205 1206 1207 |
# File 'lib/matrix.rb', line 1201 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
1213 1214 1215 1216 1217 1218 1219 |
# File 'lib/matrix.rb', line 1213 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.
1041 1042 1043 |
# File 'lib/matrix.rb', line 1041 def size @elements.size end |
#to_a ⇒ Object
Returns the elements of the vector in an array.
1235 1236 1237 |
# File 'lib/matrix.rb', line 1235 def to_a @elements.dup end |
#to_s ⇒ Object
Overrides Object#to_s
1258 1259 1260 |
# File 'lib/matrix.rb', line 1258 def to_s "Vector[" + @elements.join(", ") + "]" end |