Search:
Class Vector
Direct Known Subclasses:
class
Vector
The Vector class includes instance methods and static objects/methods. It has no dependencies but is automatically included with Matrix.js.
Defined in
Vector.js
Variable Summary
Number[]
components
the components of the Vector
int
n
the dimension of the Vector (length of components array)
Function Summary
Adds a new element to the vector and updates the dimension.
Finds and returns the angle between two vectors.
Sets and or returns the dimension of the vector: if no parameter supplied, returns dimension; if number given, sets dimension.
Returns the distance between the vector and an object, as if the vector were a point in its n-space.
Returns the scalar dot product of this vector and the supplied vector.
Method for setting and getting the elements of a vector.
Gets the component of the vector at index.
Returns the length of the vector by modulus definition: length = sqrt(this.dot(this))
Returns the first occurence of x in the vector.
Tests if two vectors are antiparallel.
Tests if two vectors are parallel.
Tests if two vectors are perpendicular.
Tests to see if 2 vectors are linearly dependent.
Tests to see if 2 vectors are linearly independent from each other.
Finds and returns the element with the largest absolute value.
Multiplies a vector by a scalar k.
Creates and returns a new vector with n random elements less than or equal to max.
Reflects the Vector or mathnetics.point2D/3D in the given point, line, or plane
Sets all values within range of mathnetics.zero (set in mathnetics.js) to val.
Converts a Vector into a 1-column matrix.
Performs a supplied function on every element of the vector.
Converts a Vector to a point object of appropriate dimension in homogeneous coordinates.
Converts a Vector into a 1-row matrix.
Returns a string representation of the vector.
Variable Details
variable Number[] components
the components of the Vector
variable public static Vector i
The unit vector [1,0,0]
variable public static Vector j
The unit vector [0,1,0]
variable public static Vector k
The unit vector [0,0,1]
variable int n
the dimension of the Vector (length of components array)
Constructor Details
constructor Vector
Vector(Number[]
elements)
Vector(Vector
vec)
Constructor function. Takes an Array or a Vector and creates a Vector object.
Parameters:
fromArray
elements
- Array to become the components of the Vector clone
vec
- the vector to clone Function Details
function add
Adds a vector to the supplied vector.
Parameters:
vec
- the vector to be added.
Returns:
a new vector that is the sum of the original and supplied.
function addElement
public
Vector
addElement(Number
el)
Adds a new element to the vector and updates the dimension.
Parameters:
el
- the number to add to the vector
Returns:
this vector, updated
function angleBetween
public
Number
angleBetween(Vector
vec)
Finds and returns the angle between two vectors.
Parameters:
vec
- the vector from which to find the angle
Returns:
the angle (in radians) between the two vectors
function cross
Computes the cross product of two 3-dimensional vectors.
Parameters:
vec
- the vector with which to take the cross product
Returns:
a new vector that is the cross product; null if either of the vectors is not 3-dimensional
function dimension
public
int
dimension([int
n])
Sets and or returns the dimension of the vector: if no parameter supplied, returns dimension; if number given, sets dimension.
Parameters:
[n]
- the dimension to set the array. If n > current dimension, 0s added as all new components; if n < current dimension, all components greater than that dimension are deleted.
Returns:
the dimension of the vector.
function distanceFrom
public
Number
distanceFrom(Object
obj)
Returns the distance between the vector and an object, as if the vector were a point in its n-space.
Parameters:
obj
- the object (Vector, Line, Plane, etc) from which to get the distance
Returns:
the distance between the vector and the object
function dot
public
Number
dot(Vector
vec)
Returns the scalar dot product of this vector and the supplied vector.
Parameters:
vec
- the vector with which to compute the dot product
Returns:
null if vector is not a vector or is a different dimension, the dot product of the two vectors otherwise.
function elements
public
Number[]
elements([Number[]
els])
Method for setting and getting the elements of a vector.
Parameters:
[els]
- if supplied, will set the vector's elements to els.
Returns:
the elements of the vector as an array
function equalTo
public
boolean
equalTo(Vector
vec)
Tests to see if two vectors are equal to each other.
Parameters:
vec
- the vector to compare to
Returns:
true iff vectors are equal (all components are equal and have same dimension), false otherwise
function get
public
Number
get(int
index)
Gets the component of the vector at index.
Parameters:
index
- the index for which you want the component
Returns:
{Number} the (index)'th component of the vector
function getLength
public
Number
getLength()
Returns the length of the vector by modulus definition: length = sqrt(this.dot(this))
Returns:
the length of the vector
function indexOf
public
int
indexOf(Number
x)
Returns the first occurence of x in the vector.
Parameters:
x
- the element to search for
Returns:
the index of the first occurence of element x; null if never occurs
function isAntiParallel
public
boolean
isAntiParallel(Vector
vec)
Tests if two vectors are antiparallel.
Parameters:
vec
- the vector to compare
Returns:
true iff vectors are antiparallel, false or null otherwise
function isParallel
public
boolean
isParallel(Vector
vec)
Tests if two vectors are parallel.
Parameters:
vec
- the vector to compare
Returns:
true iff vectors are parallel, false or null otherwise
function isPerpendicular
public
boolean
isPerpendicular(Vector
vec)
Tests if two vectors are perpendicular.
Parameters:
vec
- the vector to compare
Returns:
true iff vectors are perpendicular, false or null otherwise
function linearlyDependent
public
boolean
linearlyDependent(Vector
vec)
Tests to see if 2 vectors are linearly dependent.
Parameters:
vec
- the vector to compare with
Returns:
true iff vectors are dependent, false otherwise
function linearlyIndependent
public
boolean
linearlyIndependent(Vector
vec)
Tests to see if 2 vectors are linearly independent from each other.
Parameters:
vec
- the vector to test for independence
Returns:
true iff vectors are independent, false otherwise (null if vectors not of same size)
function make3D
public
Vector
make3D()
Makes a copy of the given vector in 3 dimensions.
Returns:
a new vector that is 3-D, with components decided according to the dimension() method (i.e. 0 if n<3, removed if n > 3)
See also:
function map
public
Vector
map(Function
func)
Maps the vector to a new vector by the supplied function.
Parameters:
func
- the mapping function
Returns:
{Vector} the new vector generated by the map
function max
public
Number
max()
Finds and returns the element with the largest absolute value.
Returns:
the absolute largest element
function multiplyBy
public
Vector
multiplyBy(Number
k)
Multiplies a vector by a scalar k.
Parameters:
k
- the scalar by which to multiply the vector
Returns:
a new vector that is the scalar multiplication
function negative
public
Vector
negative()
Returns a negative copy of the Vector (or point).
Returns:
a new, negative copy of the original Vector or mathnetics.point2D/3D
function normalize
public
Vector
normalize()
Normalizes the vector and returns the new unit vector.
Returns:
a new vector that is the normalized version of the original vector
function random
public static
Vector
random(int
n, [Number
max])
Creates and returns a new vector with n random elements less than or equal to max.
Parameters:
n
- the number of components the vector should be
[max]
- the maximum value the elements can be; if not specified, values will be in rand [0,1)
Returns:
a new vector with random components
function reflectionIn
public
Vector
reflectionIn(Object
obj)
Reflects the Vector or mathnetics.point2D/3D in the given point, line, or plane
Parameters:
obj
- the Line, Plane, or Vector/mathnetics.point2D/3D in which to reflect this
Returns:
a new Vector or mathnetics.point3D (depends on which calls this function) that is reflected
function resolute
Projects one Vector onto another. Gives the component of this Vector in the supplied Vector's direction.
Parameters:
vec
- (or mathnetics.point2D/3D object) the vector onto which to project this vector
Returns:
a new Vector (or mathnetics.point2D/3D object), the original projected onto vec
function round
public
Vector
round()
Rounds all the elements of the vector and returns the copy.
Returns:
a copy of the original vector, but rounded
function snapTo
public
Vector
snapTo(Number
val)
Sets all values within range of mathnetics.zero (set in mathnetics.js) to val.
Parameters:
val
- the number to snap the vector to
Returns:
this Vector snapped to the given value
function subtract
Subtracts a vector to the supplied vector.
Parameters:
vec
- the vector to be subtracted.
Returns:
a new vector that is the result of the subtraction.
function toColumnMatrix
public
Matrix
toColumnMatrix()
Converts a Vector into a 1-column matrix.
Returns:
the column Matrix representation of the Vector
function toEach
public
void
toEach(Function
func)
Performs a supplied function on every element of the vector. Used by map.
Parameters:
func
the function to perform on each element function toPoint
public
Object
toPoint()
Converts a Vector to a point object of appropriate dimension in homogeneous coordinates. If called from a mathnetics.point2D or mathnetics.point3D object, acts as another way to clone the point.
Returns:
either a mathnetics.point2D or mathnetics.point3D object, depending on size of Vector, with same coordinates as the Vector's components
function toRowMatrix
public
Matrix
toRowMatrix()
Converts a Vector into a 1-row matrix.
Returns:
the row Matrix representation of the Vector
function toString
public
String
toString()
Returns a string representation of the vector.
Returns:
the string representation of the vector.
function zero
public static
Vector
zero(int
n)
Creates and returns a new vector of dimension n with all 0 components.
Parameters:
n
- the dimension of the new zero vector
Returns:
a new zero vector of dimension n