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:

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 999
 999:   def Vector.[](*array)
1000:     new(:init_elements, array, copy = false)
1001:   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 1007
1007:   def Vector.elements(array, copy = true)
1008:     new(:init_elements, array, copy)
1009:   end

For internal use.

[Source]

      # File lib/matrix.rb, line 1014
1014:   def initialize(method, array, copy)
1015:     self.send(method, array, copy)
1016:   end

Public Instance methods

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

[Source]

      # File lib/matrix.rb, line 1114
1114:   def *(x)
1115:     case x
1116:     when Numeric
1117:       els = @elements.collect{|e| e * x}
1118:       Vector.elements(els, false)
1119:     when Matrix
1120:       Matrix.column_vector(self) * x
1121:     else
1122:       s, x = x.coerce(self)
1123:       s * x
1124:     end
1125:   end

Vector addition.

[Source]

      # File lib/matrix.rb, line 1130
1130:   def +(v)
1131:     case v
1132:     when Vector
1133:       Vector.Raise ErrDimensionMismatch if size != v.size
1134:       els = collect2(v) {
1135:         |v1, v2|
1136:         v1 + v2
1137:       }
1138:       Vector.elements(els, false)
1139:     when Matrix
1140:       Matrix.column_vector(self) + v
1141:     else
1142:       s, x = v.coerce(self)
1143:       s + x
1144:     end
1145:   end

Vector subtraction.

[Source]

      # File lib/matrix.rb, line 1150
1150:   def -(v)
1151:     case v
1152:     when Vector
1153:       Vector.Raise ErrDimensionMismatch if size != v.size
1154:       els = collect2(v) {
1155:         |v1, v2|
1156:         v1 - v2
1157:       }
1158:       Vector.elements(els, false)
1159:     when Matrix
1160:       Matrix.column_vector(self) - v
1161:     else
1162:       s, x = v.coerce(self)
1163:       s - x
1164:     end
1165:   end

Returns true iff the two vectors have the same elements in the same order.

[Source]

      # File lib/matrix.rb, line 1079
1079:   def ==(other)
1080:     return false unless Vector === other
1081:     
1082:     other.compare_by(@elements)
1083:   end

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

[Source]

      # File lib/matrix.rb, line 1034
1034:   def [](i)
1035:     @elements[i]
1036:   end

Return a copy of the vector.

[Source]

      # File lib/matrix.rb, line 1096
1096:   def clone
1097:     Vector.elements(@elements)
1098:   end

FIXME: describe Vector#coerce.

[Source]

      # File lib/matrix.rb, line 1242
1242:   def coerce(other)
1243:     case other
1244:     when Numeric
1245:       return Scalar.new(other), self
1246:     else
1247:       raise TypeError, "#{self.class} can't be coerced into #{other.class}"
1248:     end
1249:   end

Like Array#collect.

[Source]

      # File lib/matrix.rb, line 1189
1189:   def collect # :yield: e
1190:     els = @elements.collect {
1191:       |v|
1192:       yield v
1193:     }
1194:     Vector.elements(els, false)
1195:   end

Collects (as in Enumerable#collect) over the elements of this vector and v in conjunction.

[Source]

      # File lib/matrix.rb, line 1064
1064:   def collect2(v) # :yield: e1, e2
1065:     Vector.Raise ErrDimensionMismatch if size != v.size
1066:     (0 .. size - 1).collect do
1067:       |i|
1068:       yield @elements[i], v[i]
1069:     end
1070:   end

For internal use.

[Source]

      # File lib/matrix.rb, line 1089
1089:   def compare_by(elements)
1090:     @elements == elements
1091:   end

Creates a single-row matrix from this vector.

[Source]

      # File lib/matrix.rb, line 1228
1228:   def covector
1229:     Matrix.row_vector(self)
1230:   end

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

[Source]

      # File lib/matrix.rb, line 1052
1052:   def each2(v) # :yield: e1, e2
1053:     Vector.Raise ErrDimensionMismatch if size != v.size
1054:     0.upto(size - 1) do
1055:       |i|
1056:       yield @elements[i], v[i]
1057:     end
1058:   end
eqn?(other)

Alias for #==

Return a hash-code for the vector.

[Source]

      # File lib/matrix.rb, line 1103
1103:   def hash
1104:     @elements.hash
1105:   end

For internal use.

[Source]

      # File lib/matrix.rb, line 1021
1021:   def init_elements(array, copy)
1022:     if copy
1023:       @elements = array.dup
1024:     else
1025:       @elements = array
1026:     end
1027:   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 1175
1175:   def inner_product(v)
1176:     Vector.Raise ErrDimensionMismatch if size != v.size
1177:     
1178:     p = 0
1179:     each2(v) {
1180:       |v1, v2|
1181:       p += v1 * v2
1182:     }
1183:     p
1184:   end

Overrides Object#inspect

[Source]

      # File lib/matrix.rb, line 1265
1265:   def inspect
1266:     str = "Vector"+@elements.inspect
1267:   end
map(

Alias for collect

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

[Source]

      # File lib/matrix.rb, line 1201
1201:   def map2(v) # :yield: e1, e2
1202:     els = collect2(v) {
1203:       |v1, v2|
1204:       yield v1, v2
1205:     }
1206:     Vector.elements(els, false)
1207:   end

Returns the modulus (Pythagorean distance) of the vector.

  Vector[5,8,2].r => 9.643650761

[Source]

      # File lib/matrix.rb, line 1213
1213:   def r
1214:     v = 0
1215:     for e in @elements
1216:       v += e*e
1217:     end
1218:     return Math.sqrt(v)
1219:   end

Returns the number of elements in the vector.

[Source]

      # File lib/matrix.rb, line 1041
1041:   def size
1042:     @elements.size
1043:   end

Returns the elements of the vector in an array.

[Source]

      # File lib/matrix.rb, line 1235
1235:   def to_a
1236:     @elements.dup
1237:   end

Overrides Object#to_s

[Source]

      # File lib/matrix.rb, line 1258
1258:   def to_s
1259:     "Vector[" + @elements.join(", ") + "]"
1260:   end

[Validate]