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 1556
1556:   def Vector.[](*array)
1557:     new convert_to_array(array, false)
1558:   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 1564
1564:   def Vector.elements(array, copy = true)
1565:     new convert_to_array(array, copy)
1566:   end

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

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1571
1571:   def initialize(array)
1572:     # No checking is done at this point.
1573:     @elements = array
1574:   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 1678
1678:   def *(x)
1679:     case x
1680:     when Numeric
1681:       els = @elements.collect{|e| e * x}
1682:       self.class.elements(els, false)
1683:     when Matrix
1684:       Matrix.column_vector(self) * x
1685:     when Vector
1686:       Vector.Raise ErrOperationNotDefined, "*", self.class, x.class
1687:     else
1688:       apply_through_coercion(x, __method__)
1689:     end
1690:   end

Vector addition.

[Source]

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

Vector subtraction.

[Source]

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

Vector division.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1731
1731:   def /(x)
1732:     case x
1733:     when Numeric
1734:       els = @elements.collect{|e| e / x}
1735:       self.class.elements(els, false)
1736:     when Matrix, Vector
1737:       Vector.Raise ErrOperationNotDefined, "/", self.class, x.class
1738:     else
1739:       apply_through_coercion(x, __method__)
1740:     end
1741:   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 1647
1647:   def ==(other)
1648:     return false unless Vector === other
1649:     @elements == other.elements
1650:   end

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

[Source]

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

[Source]

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

Return a copy of the vector.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1660
1660:   def clone
1661:     self.class.elements(@elements)
1662:   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 1844
1844:   def coerce(other)
1845:     case other
1846:     when Numeric
1847:       return Matrix::Scalar.new(other), self
1848:     else
1849:       raise TypeError, "#{self.class} can't be coerced into #{other.class}"
1850:     end
1851:   end

Like Array#collect.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1764
1764:   def collect(&block) # :yield: e
1765:     return to_enum(:collect) unless block_given?
1766:     els = @elements.collect(&block)
1767:     self.class.elements(els, false)
1768:   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 1631
1631:   def collect2(v) # :yield: e1, e2
1632:     raise TypeError, "Integer is not like Vector" if v.kind_of?(Integer)
1633:     Vector.Raise ErrDimensionMismatch if size != v.size
1634:     return to_enum(:collect2, v) unless block_given?
1635:     Array.new(size) do |i|
1636:       yield @elements[i], v[i]
1637:     end
1638:   end
component(i)

Alias for #[]

Creates a single-row matrix from this vector.

[Source]

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

Iterate over the elements of this vector

[Source]

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

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

[Source]

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

Alias for #[]

[Source]

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

[Source]

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

[Source]

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

[Source]

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

Return a hash-code for the vector.

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1667
1667:   def hash
1668:     @elements.hash
1669:   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 1751
1751:   def inner_product(v)
1752:     Vector.Raise ErrDimensionMismatch if size != v.size
1753: 
1754:     p = 0
1755:     each2(v) {|v1, v2|
1756:       p += v1 * v2
1757:     }
1758:     p
1759:   end

Overrides Object#inspect

[Source]

      # File lib/backports/1.9.2/stdlib/matrix.rb, line 1867
1867:   def inspect
1868:     "Vector" + @elements.inspect
1869:   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 1775
1775:   def magnitude
1776:     Math.sqrt(@elements.inject(0) {|v, e| v + e*e})
1777:   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 1784
1784:   def map2(v, &block) # :yield: e1, e2
1785:     return to_enum(:map2, v) unless block_given?
1786:     els = collect2(v, &block)
1787:     self.class.elements(els, false)
1788:   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 1798
1798:   def normalize
1799:     n = magnitude
1800:     raise ZeroVectorError, "Zero vectors can not be normalized" if n == 0
1801:     self / n
1802:   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 1597
1597:   def size
1598:     @elements.size
1599:   end

Returns the elements of the vector in an array.

[Source]

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

Overrides Object#to_s

[Source]

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

[Validate]