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, ...]

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1558
1558:   def Vector.[](*array)
1559:     new convert_to_array(array, false)
1560:   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/backports/1.9.2/stdlib/matrix.rb, line 1566
1566:   def Vector.elements(array, copy = true)
1567:     new convert_to_array(array, copy)
1568:   end

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1573
1573:   def initialize(array)
1574:     # No checking is done at this point.
1575:     @elements = array
1576:   end

Public Instance methods

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1680
1680:   def *(x)
1681:     case x
1682:     when Numeric
1683:       els = @elements.collect{|e| e * x}
1684:       self.class.elements(els, false)
1685:     when Matrix
1686:       Matrix.column_vector(self) * x
1687:     when Vector
1688:       Vector.Raise ErrOperationNotDefined, "*", self.class, x.class
1689:     else
1690:       apply_through_coercion(x, __method__)
1691:     end
1692:   end

Vector addition.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1697
1697:   def +(v)
1698:     case v
1699:     when Vector
1700:       Vector.Raise ErrDimensionMismatch if size != v.size
1701:       els = collect2(v) {|v1, v2|
1702:         v1 + v2
1703:       }
1704:       self.class.elements(els, false)
1705:     when Matrix
1706:       Matrix.column_vector(self) + v
1707:     else
1708:       apply_through_coercion(v, __method__)
1709:     end
1710:   end

Vector subtraction.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1715
1715:   def -(v)
1716:     case v
1717:     when Vector
1718:       Vector.Raise ErrDimensionMismatch if size != v.size
1719:       els = collect2(v) {|v1, v2|
1720:         v1 - v2
1721:       }
1722:       self.class.elements(els, false)
1723:     when Matrix
1724:       Matrix.column_vector(self) - v
1725:     else
1726:       apply_through_coercion(v, __method__)
1727:     end
1728:   end

Vector division.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1733
1733:   def /(x)
1734:     case x
1735:     when Numeric
1736:       els = @elements.collect{|e| e / x}
1737:       self.class.elements(els, false)
1738:     when Matrix, Vector
1739:       Vector.Raise ErrOperationNotDefined, "/", self.class, x.class
1740:     else
1741:       apply_through_coercion(x, __method__)
1742:     end
1743:   end

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1649
1649:   def ==(other)
1650:     return false unless Vector === other
1651:     @elements == other.elements
1652:   end

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1583
1583:   def [](i)
1584:     @elements[i]
1585:   end

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1589
1589:   def []=(i, v)
1590:     @elements[i]= v
1591:   end

Return a copy of the vector.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1662
1662:   def clone
1663:     self.class.elements(@elements)
1664:   end

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.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1846
1846:   def coerce(other)
1847:     case other
1848:     when Numeric
1849:       return Matrix::Scalar.new(other), self
1850:     else
1851:       raise TypeError, "#{self.class} can't be coerced into #{other.class}"
1852:     end
1853:   end

Like Array#collect.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1766
1766:   def collect(&block) # :yield: e
1767:     return to_enum(:collect) unless block_given?
1768:     els = @elements.collect(&block)
1769:     self.class.elements(els, false)
1770:   end

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1633
1633:   def collect2(v) # :yield: e1, e2
1634:     raise TypeError, "Integer is not like Vector" if v.kind_of?(Integer)
1635:     Vector.Raise ErrDimensionMismatch if size != v.size
1636:     return to_enum(:collect2, v) unless block_given?
1637:     Array.new(size) do |i|
1638:       yield @elements[i], v[i]
1639:     end
1640:   end
component(i)

Alias for #[]

Creates a single-row matrix from this vector.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1813
1813:   def covector
1814:     Matrix.row_vector(self)
1815:   end

Iterate over the elements of this vector

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1610
1610:   def each(&block)
1611:     return to_enum(:each) unless block_given?
1612:     @elements.each(&block)
1613:     self
1614:   end

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1619
1619:   def each2(v) # :yield: e1, e2
1620:     raise TypeError, "Integer is not like Vector" if v.kind_of?(Integer)
1621:     Vector.Raise ErrDimensionMismatch if size != v.size
1622:     return to_enum(:each2, v) unless block_given?
1623:     size.times do |i|
1624:       yield @elements[i], v[i]
1625:     end
1626:     self
1627:   end
element(i)

Alias for #[]

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1824
1824:   def elements_to_f
1825:     warn "#{caller(1)[0]}: warning: Vector#elements_to_f is deprecated"
1826:     map(&:to_f)
1827:   end

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1829
1829:   def elements_to_i
1830:     warn "#{caller(1)[0]}: warning: Vector#elements_to_i is deprecated"
1831:     map(&:to_i)
1832:   end

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1834
1834:   def elements_to_r
1835:     warn "#{caller(1)[0]}: warning: Vector#elements_to_r is deprecated"
1836:     map(&:to_r)
1837:   end

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1654
1654:   def eql?(other)
1655:     return false unless Vector === other
1656:     @elements.eql? other.elements
1657:   end

Return a hash-code for the vector.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1669
1669:   def hash
1670:     @elements.hash
1671:   end

Returns the inner product of this vector with the other.

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1753
1753:   def inner_product(v)
1754:     Vector.Raise ErrDimensionMismatch if size != v.size
1755: 
1756:     p = 0
1757:     each2(v) {|v1, v2|
1758:       p += v1 * v2
1759:     }
1760:     p
1761:   end

Overrides Object#inspect

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1869
1869:   def inspect
1870:     "Vector" + @elements.inspect
1871:   end

Returns the modulus (Pythagorean distance) of the vector.

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1777
1777:   def magnitude
1778:     Math.sqrt(@elements.inject(0) {|v, e| v + e*e})
1779:   end
map()

Alias for collect

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1786
1786:   def map2(v, &block) # :yield: e1, e2
1787:     return to_enum(:map2, v) unless block_given?
1788:     els = collect2(v, &block)
1789:     self.class.elements(els, false)
1790:   end
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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1800
1800:   def normalize
1801:     n = magnitude
1802:     raise ZeroVectorError, "Zero vectors can not be normalized" if n == 0
1803:     self / n
1804:   end
r()

Alias for magnitude

set_component(i, v)

Alias for #[]=

set_element(i, v)

Alias for #[]=

Returns the number of elements in the vector.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1599
1599:   def size
1600:     @elements.size
1601:   end

Returns the elements of the vector in an array.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1820
1820:   def to_a
1821:     @elements.dup
1822:   end

Overrides Object#to_s

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1862
1862:   def to_s
1863:     "Vector[" + @elements.join(", ") + "]"
1864:   end

[Validate]