Class: Vector
- Inherits:
-
Object
- Object
- Vector
- Extended by:
- Matrix::ConversionHelper
- Includes:
- Enumerable, ExceptionForMatrix, Matrix::CoercionHelper
- Defined in:
- lib/rubysl/matrix/matrix.rb
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.
-
#/(x) ⇒ Object
Vector division.
-
#==(other) ⇒ Object
Returns
trueiff the two vectors have the same elements in the same order. -
#[](i) ⇒ Object
(also: #element, #component)
Returns element number
i(starting at zero) of the vector. - #[]=(i, v) ⇒ Object (also: #set_element, #set_component)
-
#clone ⇒ Object
Return a copy of the vector.
-
#coerce(other) ⇒ Object
The coerce method provides support for Ruby type coercion.
-
#collect(&block) ⇒ Object
(also: #map)
Like Array#collect.
-
#collect2(v) ⇒ Object
Collects (as in Enumerable#collect) over the elements of this vector and
vin conjunction. -
#covector ⇒ Object
Creates a single-row matrix from this vector.
-
#each(&block) ⇒ Object
Iterate over the elements of this vector.
-
#each2(v) ⇒ Object
Iterate over the elements of this vector and
vin conjunction. - #elements_to_f ⇒ Object
- #elements_to_i ⇒ Object
- #elements_to_r ⇒ Object
- #eql?(other) ⇒ Boolean
-
#hash ⇒ Object
Return a hash-code for the vector.
-
#initialize(array) ⇒ Vector
constructor
Vector.new is private; use Vector[] or Vector.elements to create.
-
#inner_product(v) ⇒ Object
Returns the inner product of this vector with the other.
-
#inspect ⇒ Object
Overrides Object#inspect.
-
#map2(v, &block) ⇒ 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.
Methods included from Matrix::CoercionHelper
Constructor Details
#initialize(array) ⇒ Vector
Vector.new is private; use Vector[] or Vector.elements to create.
1254 1255 1256 1257 |
# File 'lib/rubysl/matrix/matrix.rb', line 1254 def initialize(array) # No checking is done at this point. @elements = array end |
Class Method Details
.[](*array) ⇒ Object
Creates a Vector from a list of elements.
Vector[7, 4, ...]
1239 1240 1241 |
# File 'lib/rubysl/matrix/matrix.rb', line 1239 def Vector.[](*array) new convert_to_array(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.
1247 1248 1249 |
# File 'lib/rubysl/matrix/matrix.rb', line 1247 def Vector.elements(array, copy = true) new convert_to_array(array, copy) end |
Instance Method Details
#*(x) ⇒ Object
Multiplies the vector by x, where x is a number or another vector.
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 |
# File 'lib/rubysl/matrix/matrix.rb', line 1361 def *(x) case x when Numeric els = @elements.collect{|e| e * x} Vector.elements(els, false) when Matrix Matrix.column_vector(self) * x when Vector Vector.Raise ErrOperationNotDefined, "*", self.class, x.class else apply_through_coercion(x, __method__) end end |
#+(v) ⇒ Object
Vector addition.
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 |
# File 'lib/rubysl/matrix/matrix.rb', line 1378 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 apply_through_coercion(v, __method__) end end |
#-(v) ⇒ Object
Vector subtraction.
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 |
# File 'lib/rubysl/matrix/matrix.rb', line 1396 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 apply_through_coercion(v, __method__) end end |
#/(x) ⇒ Object
Vector division.
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 |
# File 'lib/rubysl/matrix/matrix.rb', line 1414 def /(x) case x when Numeric els = @elements.collect{|e| e / x} Vector.elements(els, false) when Matrix, Vector Vector.Raise ErrOperationNotDefined, "/", self.class, x.class else apply_through_coercion(x, __method__) end end |
#==(other) ⇒ Object
Returns true iff the two vectors have the same elements in the same order.
1330 1331 1332 1333 |
# File 'lib/rubysl/matrix/matrix.rb', line 1330 def ==(other) return false unless Vector === other @elements == other.elements end |
#[](i) ⇒ Object Also known as: element, component
Returns element number i (starting at zero) of the vector.
1264 1265 1266 |
# File 'lib/rubysl/matrix/matrix.rb', line 1264 def [](i) @elements[i] end |
#[]=(i, v) ⇒ Object Also known as: set_element, set_component
1270 1271 1272 |
# File 'lib/rubysl/matrix/matrix.rb', line 1270 def []=(i, v) @elements[i]= v end |
#clone ⇒ Object
Return a copy of the vector.
1343 1344 1345 |
# File 'lib/rubysl/matrix/matrix.rb', line 1343 def clone Vector.elements(@elements) end |
#coerce(other) ⇒ Object
The coerce method provides support for Ruby type coercion. This coercion mechanism is used by Ruby to handle mixed-type numeric operations: it is intended to find a compatible common type between the two operands of the operator. See also Numeric#coerce.
1511 1512 1513 1514 1515 1516 1517 1518 |
# File 'lib/rubysl/matrix/matrix.rb', line 1511 def coerce(other) case other when Numeric return Matrix::Scalar.new(other), self else raise TypeError, "#{self.class} can't be coerced into #{other.class}" end end |
#collect(&block) ⇒ Object Also known as: map
Like Array#collect.
1447 1448 1449 1450 1451 |
# File 'lib/rubysl/matrix/matrix.rb', line 1447 def collect(&block) # :yield: e return to_enum(:collect) unless block_given? els = @elements.collect(&block) Vector.elements(els, false) end |
#collect2(v) ⇒ Object
Collects (as in Enumerable#collect) over the elements of this vector and v in conjunction.
1314 1315 1316 1317 1318 1319 1320 1321 |
# File 'lib/rubysl/matrix/matrix.rb', line 1314 def collect2(v) # :yield: e1, e2 raise TypeError, "Integer is not like Vector" if v.kind_of?(Integer) Vector.Raise ErrDimensionMismatch if size != v.size return to_enum(:collect2, v) unless block_given? size.times.collect do |i| yield @elements[i], v[i] end end |
#covector ⇒ Object
Creates a single-row matrix from this vector.
1478 1479 1480 |
# File 'lib/rubysl/matrix/matrix.rb', line 1478 def covector Matrix.row_vector(self) end |
#each(&block) ⇒ Object
Iterate over the elements of this vector
1291 1292 1293 1294 1295 |
# File 'lib/rubysl/matrix/matrix.rb', line 1291 def each(&block) return to_enum(:each) unless block_given? @elements.each(&block) self end |
#each2(v) ⇒ Object
Iterate over the elements of this vector and v in conjunction.
1300 1301 1302 1303 1304 1305 1306 1307 1308 |
# File 'lib/rubysl/matrix/matrix.rb', line 1300 def each2(v) # :yield: e1, e2 raise TypeError, "Integer is not like Vector" if v.kind_of?(Integer) Vector.Raise ErrDimensionMismatch if size != v.size return to_enum(:each2, v) unless block_given? size.times do |i| yield @elements[i], v[i] end self end |
#elements_to_f ⇒ Object
1489 1490 1491 1492 |
# File 'lib/rubysl/matrix/matrix.rb', line 1489 def elements_to_f warn "#{caller(1)[0]}: warning: Vector#elements_to_f is deprecated" map(&:to_f) end |
#elements_to_i ⇒ Object
1494 1495 1496 1497 |
# File 'lib/rubysl/matrix/matrix.rb', line 1494 def elements_to_i warn "#{caller(1)[0]}: warning: Vector#elements_to_i is deprecated" map(&:to_i) end |
#elements_to_r ⇒ Object
1499 1500 1501 1502 |
# File 'lib/rubysl/matrix/matrix.rb', line 1499 def elements_to_r warn "#{caller(1)[0]}: warning: Vector#elements_to_r is deprecated" map(&:to_r) end |
#eql?(other) ⇒ Boolean
1335 1336 1337 1338 |
# File 'lib/rubysl/matrix/matrix.rb', line 1335 def eql?(other) return false unless Vector === other @elements.eql? other.elements end |
#hash ⇒ Object
Return a hash-code for the vector.
1350 1351 1352 |
# File 'lib/rubysl/matrix/matrix.rb', line 1350 def hash @elements.hash end |
#inner_product(v) ⇒ Object
Returns the inner product of this vector with the other.
Vector[4,7].inner_product Vector[10,1] => 47
1434 1435 1436 1437 1438 1439 1440 1441 1442 |
# File 'lib/rubysl/matrix/matrix.rb', line 1434 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
1534 1535 1536 |
# File 'lib/rubysl/matrix/matrix.rb', line 1534 def inspect str = "Vector"+@elements.inspect end |
#map2(v, &block) ⇒ Object
Like Vector#collect2, but returns a Vector instead of an Array.
1457 1458 1459 1460 1461 |
# File 'lib/rubysl/matrix/matrix.rb', line 1457 def map2(v, &block) # :yield: e1, e2 return to_enum(:map2, v) unless block_given? els = collect2(v, &block) Vector.elements(els, false) end |
#r ⇒ Object
Returns the modulus (Pythagorean distance) of the vector.
Vector[5,8,2].r => 9.643650761
1467 1468 1469 |
# File 'lib/rubysl/matrix/matrix.rb', line 1467 def r Math.sqrt(@elements.inject(0) {|v, e| v + e*e}) end |
#size ⇒ Object
Returns the number of elements in the vector.
1280 1281 1282 |
# File 'lib/rubysl/matrix/matrix.rb', line 1280 def size @elements.size end |
#to_a ⇒ Object
Returns the elements of the vector in an array.
1485 1486 1487 |
# File 'lib/rubysl/matrix/matrix.rb', line 1485 def to_a @elements.dup end |
#to_s ⇒ Object
Overrides Object#to_s
1527 1528 1529 |
# File 'lib/rubysl/matrix/matrix.rb', line 1527 def to_s "Vector[" + @elements.join(", ") + "]" end |