Vector.Raise ErrDimensionMismatch unless v.size == size
Vector[-@elements[1], @elements[0]]
Vector[ v[2]*@elements[1] - v[1]*@elements[2],
v[0]*@elements[2] - v[2]*@elements[0],
v[1]*@elements[0] - v[0]*@elements[1] ]
rows = self, *vs, Array.new(size) {|i| Vector.basis(size: size, index: i) }
Matrix.rows(rows).laplace_expansion(row: size - 1)
alias_method :cross, :cross_product
def collect(&block) # :yield: e
return to_enum(:collect) unless block_given?
els = @elements.collect(&block)
self.class.elements(els, false)
# Returns the modulus (Pythagorean distance) of the vector.
# Vector[5,8,2].r => 9.643650761
Math.sqrt(@elements.inject(0) {|v, e| v + e.abs2})
# Like Vector#collect2, but returns a Vector instead of an Array.
def map2(v, &block) # :yield: e1, e2
return to_enum(:map2, v) unless block_given?
els = collect2(v, &block)
self.class.elements(els, false)
class ZeroVectorError < StandardError
# 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]
raise ZeroVectorError, "Zero vectors can not be normalized" if n == 0
# Returns an angle with another vector. Result is within the [0...Math::PI].
# Vector[1,0].angle_with(Vector[0,1])
raise TypeError, "Expected a Vector, got a #{v.class}" unless v.is_a?(Vector)
Vector.Raise ErrDimensionMismatch if size != v.size
prod = magnitude * v.magnitude
raise ZeroVectorError, "Can't get angle of zero vector" if prod == 0
Math.acos( inner_product(v) / prod )
# Creates a single-row matrix from this vector.
# Returns the elements of the vector in an array.
# Return a single-column matrix from this vector
Matrix.column_vector(self)
warn "Vector#elements_to_f is deprecated", uplevel: 1
warn "Vector#elements_to_i is deprecated", uplevel: 1
warn "Vector#elements_to_r is deprecated", uplevel: 1
# 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.
return Matrix::Scalar.new(other), self
raise TypeError, "#{self.class} can't be coerced into #{other.class}"
# PRINTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"Vector[" + @elements.join(", ") + "]"
# Overrides Object#inspect
"Vector" + @elements.inspect