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 1573 1573: def initialize(array) 1574: # No checking is done at this point. 1575: @elements = array 1576: end
Multiplies the vector by x, where x is a number or another vector.
# 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.
# 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.
# 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.
# 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
# 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.
# 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.
# 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.
# 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.
# 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
Creates a single-row matrix from this vector.
# 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 and v in conjunction.
# 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
# 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
# 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
# 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
# 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
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 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
# 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
# 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
Like Vector#collect2, but returns a Vector instead of an Array.
# 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
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 1800 1800: def normalize 1801: n = magnitude 1802: raise ZeroVectorError, "Zero vectors can not be normalized" if n == 0 1803: self / n 1804: end