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.
To create a Vector:
To access elements:
To enumerate the elements:
Vector arithmetic:
Vector functions:
Conversion to other data types:
String representations:
elements | [R] | INSTANCE CREATION |
Vector.new is private; use Vector[] or Vector.elements to create.
# 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
Multiplies the vector by x, where x is a number or another vector.
# 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.
# 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.
# 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.
# 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
# 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.
# 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.
# 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.
# 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.
# 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
Creates a single-row matrix from this vector.
# 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 and v in conjunction.
# 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
# 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
# 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
# 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
# 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
Returns the inner product of this vector with the other.
Vector[4,7].inner_product Vector[10,1] => 47
# 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
# 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
# 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
Like Vector#collect2, but returns a Vector instead of an Array.
# 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
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
# 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