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