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.

Method Catalogue

To create a Vector:

To access elements:

  • [](i)

To enumerate the elements:

  • each2(v)
  • collect2(v)

Vector arithmetic:

  • *(x) "is matrix or number"
  • +(v)
  • -(v)

Vector functions:

Conversion to other data types:

String representations:

Methods

*   +   -   ==   []   []   clone   coerce   collect   collect2   compare_by   covector   each2   elements   eqn?   hash   init_elements   inner_product   inspect   map   map2   new   r   size   to_a   to_s  

Included Modules

ExceptionForMatrix

Public Class methods

Creates a Vector from a list of elements.

  Vector[7, 4, ...]

[Source]

      # File lib/matrix.rb, line 1005
1005:   def Vector.[](*array)
1006:     new(:init_elements, array, copy = false)
1007:   end

Creates a vector from an Array. The optional second argument specifies whether the array itself or a copy is used internally.

[Source]

      # File lib/matrix.rb, line 1013
1013:   def Vector.elements(array, copy = true)
1014:     new(:init_elements, array, copy)
1015:   end

For internal use.

[Source]

      # File lib/matrix.rb, line 1020
1020:   def initialize(method, array, copy)
1021:     self.send(method, array, copy)
1022:   end

Public Instance methods

Multiplies the vector by x, where x is a number or another vector.

[Source]

      # 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.

[Source]

      # 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.

[Source]

      # 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 true iff the two vectors have the same elements in the same order.

[Source]

      # File lib/matrix.rb, line 1085
1085:   def ==(other)
1086:     return false unless Vector === other
1087:     
1088:     other.compare_by(@elements)
1089:   end

Returns element number i (starting at zero) of the vector.

[Source]

      # File lib/matrix.rb, line 1040
1040:   def [](i)
1041:     @elements[i]
1042:   end

Return a copy of the vector.

[Source]

      # File lib/matrix.rb, line 1102
1102:   def clone
1103:     Vector.elements(@elements)
1104:   end

FIXME: describe Vector#coerce.

[Source]

      # 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.

[Source]

      # 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.

[Source]

      # 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.

[Source]

      # File lib/matrix.rb, line 1095
1095:   def compare_by(elements)
1096:     @elements == elements
1097:   end

Creates a single-row matrix from this vector.

[Source]

      # File lib/matrix.rb, line 1234
1234:   def covector
1235:     Matrix.row_vector(self)
1236:   end

Iterate over the elements of this vector and v in conjunction.

[Source]

      # File lib/matrix.rb, line 1058
1058:   def each2(v) # :yield: e1, e2
1059:     Vector.Raise ErrDimensionMismatch if size != v.size
1060:     0.upto(size - 1) do
1061:       |i|
1062:       yield @elements[i], v[i]
1063:     end
1064:   end
eqn?(other)

Alias for #==

Return a hash-code for the vector.

[Source]

      # File lib/matrix.rb, line 1109
1109:   def hash
1110:     @elements.hash
1111:   end

For internal use.

[Source]

      # 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

[Source]

      # 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

[Source]

      # File lib/matrix.rb, line 1271
1271:   def inspect
1272:     str = "Vector"+@elements.inspect
1273:   end
map(

Alias for collect

Like Vector#collect2, but returns a Vector instead of an Array.

[Source]

      # 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

[Source]

      # 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

Returns the number of elements in the vector.

[Source]

      # File lib/matrix.rb, line 1047
1047:   def size
1048:     @elements.size
1049:   end

Returns the elements of the vector in an array.

[Source]

      # File lib/matrix.rb, line 1241
1241:   def to_a
1242:     @elements.dup
1243:   end

Overrides Object#to_s

[Source]

      # File lib/matrix.rb, line 1264
1264:   def to_s
1265:     "Vector[" + @elements.join(", ") + "]"
1266:   end

[Validate]