Class | ODE::Vector |
In: |
lib/ode/vector.rb
(CVS)
|
Parent: | Object |
Instance of this class represent a quantity that has a magnitude and a direction.
Version | = | /([\d\.]+)/.match( %q{$Revision: 1.3 $} )[1] | Class constants | |
Rcsid | = | %q$Id: vector.rb 91 2005-07-27 23:45:01Z ged $ | ||
X | = | 0; | ||
Y | = | 1; | ||
Z | = | 2 |
elements | [RW] | Internal array of elements |
Instantiate and return a new ODE::Vector. Defaults to a 3rd-order zero vector if no arguments are given.
# File lib/ode/vector.rb, line 55 def initialize( *args ) args.flatten! case args.length when 0 @elements = [ 0.0, 0.0, 0.0 ] when 1 if args[0].respond_to?( :to_ary ) args = args.to_ary elsif !args[0].is_a?( Numeric ) raise TypeError, "no implicit conversion from %s to %s" % [ args[0].class.name, self.class.name ] end end @elements = args end
Multiplication operator — Multiply the receiving vector with the specified scalar and return the result as a new instance of the receiver.
# File lib/ode/vector.rb, line 259 def *( scalar ) scalar = Float(scalar) return self.class.new( self.collect {|elem| elem * scalar} ) end
Addition operator — Add the receiving vector to the otherVector (which must be of the same size), and return the result as a new instance of the receiver.
# File lib/ode/vector.rb, line 233 def +( otherVector ) raise ArgumentError, "Cannot add vectors of different dimensions" unless self.size == otherVector.size rvec = self.copy rvec.each_index {|i| rvec[i] += otherVector[i] } return rvec end
Subtraction operator — Subtract the otherVector from the receiving vector (which must be of the same size), and return the result as a new instance of the receiver.
# File lib/ode/vector.rb, line 246 def -( otherVector ) raise ArgumentError, "Cannot add vectors of different dimensions" unless self.size == otherVector.size rvec = self.copy rvec.each_index {|i| rvec[i] -= otherVector[i] } return rvec end
Division operator — Divide the receiving vector by the specified scalar and return the result as a new instance of the receiver.
# File lib/ode/vector.rb, line 268 def /( scalar ) scalar = Float(scalar) raise ZeroDivisionError if scalar == 0.0 return self.class.new( self.collect {|elem| elem / scalar} ) end
Equality operator — returns true if the receiver and otherObj are of the same class, and each element of the receiver is the same as the corresponding element of the otherObj.
# File lib/ode/vector.rb, line 298 def ==( otherObj ) return false unless otherObj.is_a?( self.class ) return self.to_ary == otherObj.to_ary end
Element reference operator — returns the ith element of the vector.
# File lib/ode/vector.rb, line 119 def [](i) @elements[i] end
Element assignment operator — assigns the value x to the ith element of the vector.
# File lib/ode/vector.rb, line 126 def []=(i, x) @elements[i] = Float(x) end
Return a new instance of the receiver by calling the specified block once for each element, passing that element and the equivalent element from otherVector. The new object will be created with the Array of all the return values of the block.
# File lib/ode/vector.rb, line 222 def collect2( otherVector ) ary = [] @elements.each_with_index {|elem, i| ary << yield(elem, otherVector[i]) } self.class.new( *ary ) end
Return a distinct copy of the vector (as opposed to dup, which only returns a shallow copy).
# File lib/ode/vector.rb, line 149 def copy return self.dup.copy_object( self ) end
Return the cross-product of the receiver and the otherVector as a new instance of the receiving class.
# File lib/ode/vector.rb, line 199 def cross( otherVector ) raise TypeError, "wrong argument type '%s': Expected an ODE::Vector" % otherVector.class.name unless otherVector.is_a?( ODE::Vector ) raise ArgumentError, "Can only cross 3rd-order vectors" unless self.size == 3 and otherVector.size == 3 self.class.new( [@elements[1]*otherVector[2] - @elements[2]*otherVector[1], @elements[2]*otherVector[0] - @elements[0]*otherVector[2], @elements[0]*otherVector[1] - @elements[1]*otherVector[0]]) end
Return the dot-product of the receiving vector and otherVector.
# File lib/ode/vector.rb, line 173 def dot( otherVector ) raise ArgumentError, "Cannot calculate the dot-product of vectors of different dimensions" unless self.size == otherVector.size scalar = 0.0 @elements.each_index {|i| scalar += @elements[i] * otherVector[i] } return scalar end
Call the given block once with each element in the vector.
# File lib/ode/vector.rb, line 213 def each @elements.each end
Return the geometric product of the receiver and the otherVector as an ODE::Quaternion.
# File lib/ode/vector.rb, line 185 def gp( otherVector ) raise TypeError, "wrong argument type '%s': Expected an ODE::Vector" % otherVector.class.name unless otherVector.is_a?( ODE::Vector ) raise ArgumentError, "Cannot calculate geometric product of vectors of different dimensions." unless self.size == otherVector.size ODE::Quaternion::sv2q( self.dot(v), self.cross(v) ) end
Returns true if the vector has a length close to 1.0, measured in the Euclidean norm.
# File lib/ode/vector.rb, line 112 def isUnitVector? return (1.0 - self.mag) < 1e-10 end
Returns true if the receiver is a zero vector.
# File lib/ode/vector.rb, line 104 def isZeroVector? return @elements.find_all {|i| i.zero?}.length == 0 end
Returns the magnitude of the vector, measured in the Euclidean norm.
# File lib/ode/vector.rb, line 132 def mag Math.sqrt( self.sqr ) end
Return a vector collinear to the given vector and having a length of 1.0, measured in the Euclidean norm. This will fail if the receiver vector is the zero vector.
# File lib/ode/vector.rb, line 157 def normalize self.copy.normalize! end
Normalizes the vector in place.
# File lib/ode/vector.rb, line 164 def normalize! mag = self.mag @elements = @elements.collect {|elem| elem / mag } return self end
Similarity tests — returns true if the receiver and otherObj are of the same class, and each element of the receiver is within 1e-10 of the corresponding element of the otherObj.
# File lib/ode/vector.rb, line 308 def similarTo?( otherObj ) return false unless otherObj.is_a?( self.class ) otherAry = otherObj.to_ary rval = true self.to_ary.each_with_index {|elem,i| if (elem - otherAry[i]).abs > 1e-10 rval = false break end } return rval end
Returns the dot product of the vector with itself, which is also the squared length of the vector, as measured in the Euclidean norm.
# File lib/ode/vector.rb, line 141 def sqr self.dot( self ) end
Return the receiver as an Array of its elements.
# File lib/ode/vector.rb, line 283 def to_ary return @elements.to_ary end
Return a nicely stringified version of the vector
# File lib/ode/vector.rb, line 289 def to_s return "|%s|" % @elements.collect {|e| "%0.2f" % e}.join(", ") end