Class Vector
In: lib/backports/1.9.2/stdlib/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   component   covector   each   each2   element   elements   elements_to_f   elements_to_i   elements_to_r   eql?   hash   inner_product   inspect   magnitude   map   map2   new   norm   normalize   r   set_component   set_element   size   to_a   to_s  

Included Modules

ExceptionForMatrix Enumerable Matrix::CoercionHelper

Classes and Modules

Class Vector::ZeroVectorError

Attributes

elements  [R]  INSTANCE CREATION

Public Class methods

Creates a Vector from a list of elements.

  Vector[7, 4, ...]

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

Vector.new is private; use Vector[] or Vector.elements to create.

Public Instance methods

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

Vector addition.

Vector subtraction.

Vector division.

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

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

Return a copy of the vector.

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.

Like Array#collect.

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

component(i)

Alias for #[]

Creates a single-row matrix from this vector.

Iterate over the elements of this vector

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

element(i)

Alias for #[]

Return a hash-code for the vector.

Returns the inner product of this vector with the other.

  Vector[4,7].inner_product Vector[10,1]  => 47

Overrides Object#inspect

Returns the modulus (Pythagorean distance) of the vector.

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

Alias for collect

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

norm()

Alias for magnitude

Returns a new vector with the same direction but with norm 1.

  v = Vector[5,8,2].normalize
  # => Vector[0.5184758473652127, 0.8295613557843402, 0.20739033894608505]
  v.norm => 1.0
r()

Alias for magnitude

set_component(i, v)

Alias for #[]=

set_element(i, v)

Alias for #[]=

Returns the number of elements in the vector.

Returns the elements of the vector in an array.

Overrides Object#to_s

[Validate]