Перейти к содержанию

Ruby/Справочник/Vector

Материал из Викиучебника — открытых книг для открытого мира

Класс Vector

[править]

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

Примеси

ExceptionForMatrix ()

Методы класса

[], elements, new

Методы объекта

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

Vector::[]

[править]

 Vector::[](*array)

Создает Vector из списка элементов.

 Vector[7, 4, ...]

Vector::elements

[править]

 Vector::elements(array, copy = true)

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

Vector::new

[править]

 Vector::new(method, array, copy)

For internal use.

Vector#*

[править]

 *(x)

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

Vector#+

[править]

 +(v)

Vector addition.

Vector#-

[править]

 -(v)

Vector subtraction.

Vector#==

[править]

 ==(other)

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

(еще известен как eqn?)

Vector#[]

[править]

 [](i)

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

Vector#clone

[править]

 clone()

Return a copy of the vector.

Vector#coerce

[править]

 coerce(other)

FIXME: describe Vector#coerce.

Vector#collect

[править]

 collect( {|e| ...}

Like Array#collect.

(еще известен как map)

Vector#collect2

[править]

 collect2(v) {|e1, e2| ...}

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

Vector#compare_by

[править]

 compare_by(elements)

For internal use.

Vector#covector

[править]

 covector()

Creates a single-row matrix from this vector.

Vector#each2

[править]

 each2(v) {|e1, e2| ...}

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

Vector#eqn?

[править]

 eqn?(other)

Alias for #==

Vector#hash

[править]

 hash()

Return a hash-code for the vector.

Vector#init_elements

[править]

 init_elements(array, copy)

For internal use.

Vector#inner_product

[править]

 inner_product(v)

Returns the inner product of this vector with the other.

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

Vector#inspect

[править]

 inspect()

Overrides Object#inspect

Vector#map

[править]

 map(

Alias for #collect

Vector#map2

[править]

 map2(v) {|e1, e2| ...}

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

Vector#r

[править]

 r()

Returns the modulus (Pythagorean distance) of the vector.

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

Vector#size

[править]

 size()

Returns the number of elements in the vector.

Vector#to_a

[править]

 to_a()

Returns the elements of the vector in an array.

Vector#to_s

[править]

 to_s()

Overrides Object#to_s