Class: Vector

Sylvester version: 0.1.0 onwards

Class methods: create, i, j, k, Random, Zero

Instance methods: add, angleFrom, cross, dimensions, distanceFrom, dot, dup, e, each, eql, forEach, indexOf, inspect, isAntiparallelTo, isParallelTo, isPerpendicularTo, liesIn, liesOn, map, max, modulus, multiply, reflectionIn, rotate, round, setElements, snapTo, subtract, to3D, toDiagonalMatrix, toUnitVector, x

Instance variables:

  • elements – an array containing the vector’s elements

Overview

The Vector class is designed to model vectors in any number of dimensions. All the elements of a vector must be real numbers. Depending on what you’re using them for, it can be helpful to think of a vector either as a point in n-dimensional space, or as a line connecting the origin to that same point.

Class methods

Vector.create(elements) 0.1.0

Creates and returns new Vector instance from the array elements. Example usage:

var v = Vector.create([6,2,9]);

This method is aliased as $V.

Vector.i, Vector.j, Vector.k 0.1.0

Predefined Vector instances representing the 3-dimensional i, j and k vectors respectively.

Vector.Random(n) 0.1.0

Returns a vector with n elements, each of which is a random number between 0 and 1.

Vector.Zero(n) 0.1.0

Returns a vector with n elements, all of which are zero.

Instance methods

add(vector) 0.1.0

If the receiver and vector have the same number of elements, returns a Vector formed by adding them together. Otherwise, returns null.

angleFrom(vector) 0.1.0

Returns the angle in radians between vector and the receiver. The return value will always be between 0 and inclusive. If the vectors have unequal numbers of elements, null is returned.

cross(vector) 0.1.0

Returns the cross product (aka the vector product) of the receiver with vector, in the order in which they are written. For example, a.cross(b) is the same as the mathematical statement a × b. Both vectors must have three elements, otherwise this method returns null.

dimensions() 0.1.0

Returns the number of elements the receiver has – that is, the dimensionality of the vector space it inhabits.

distanceFrom(vector) 0.1.0

Returns the (always positive) distance of the receiver from vector. That is, a.distanceFrom(b) is equivalent to |ab|.

dot(vector) 0.1.0

Returns the value of the dot product (aka the scalar product) of the receiver with vector. That is, a.dot(b) is the same as ab. Returns null if the vectors have unequal dimensions.

dup() 0.1.0

Returns a copy of the receiver.

e(i) 0.1.0

Returns the ith element of the receiver. Element indexes begin at 1, not 0 like array indexes. This is consistent with mathematical notation.

each(iterator, context) 0.1.1

Alias for forEach(iterator, context).

eql(vector) 0.1.0

Returns true iff the receiver is equal to vector, that is, if all their elements are equal. See note on accuracy.

indexOf(x) 0.1.0

Returns the index position (numbered from 1, just as for e()) of the first element exactly equal to x. If no match is found, returns null.

inspect() 0.1.0

Returns a string representation of the receiver, useful for debugging purposes. Example:

alert($V([7,-2,5]).inspect())
// alerts "[7, -2, 5]"

isAntiparallelTo(vector) 0.1.0

Returns true iff vector’s direction is exactly opposed to that of the receiver, that is, if the angle between them is π. See note on accuracy.

isParallelTo(vector) 0.1.0

Returns true iff vector’s direction is exactly aligned with that of the receiver, that is, if the angle between them is zero. See note on accuracy.

isPerpendicularTo(vector) 0.1.0

Returns true iff vector’s direction is perpendicular to that of the receiver, that is, if the angle between them is π/2. See note on accuracy.

liesIn(plane) 0.1.0

Returns true iff the receiver is a point in the given Plane. Only works on 3-dimensional vectors.

liesOn(line) 0.1.0

Returns true iff the receiver is a point on the given Line. Only works on 3-dimensional vectors.

map(iterator) 0.1.0

Maps the receiver to another vector by calling iterator with each element in turn. iterator is also passed the index (numbered from 1) of each element as the second argument. Some examples:

var a = $V([3,4,5]);

// To square the elements of a
var b = a.map(function(x) { return x * x; });

// To add each element's index to its value
var c = a.map(function(x, i) { return x + i; });

max() 0.1.0

Returns the element of the receiver with the largest absolute value. For example, $V([0,-6,5]).max() returns -6.

modulus() 0.1.0

Returns the modulus of the receiver, given by |v| = √( Σ vi² ).

multiply(k) 0.1.0

Returns the vector obtained by multiplying all the elements of the receiver by the scalar quantity k. Aliased as x(k).

reflectionIn(object) 0.1.0

Returns the vector that results from reflecting the receiver in object, which can be a Vector, a Line or a Plane. If object is a Vector, then a.reflectionIn(b) returns b + (ba). Otherwise, the same formula applies but b is the closest point on the line or plane to a.

rotate(angle, axis) 0.1.0

Returns a copy of the receiver rotated by angle radians about axis. If the receiver is a 2-vector then axis should also be a 2-vector, and the method returns the result of rotating the receiver about the point given by axis. The rotation is performed anticlockwise from the point of view of someone looking down on the x-y plane, so for example:

var a = $V([10,5]);
var b = $V([5,5]);

var c = a.rotate(Math.PI/2, b);
// c is the point (5,10)

If the receiver is a 3-vector, then axis should be a Line, and the rotation is performed in a right-handed fashion around the line’s direction. Be careful that the line points in the right direction when using this method!

round() 0.1.0

Returns a copy of the receiver with all its elements rounded to the nearest integer.

setElements(els) 0.1.0

Sets the receiver’s elements property equal to the array els. Since version 0.1.1, the numericality of els’s elements is not checked for performance reasons. It is assumed you’ll be using this with numbers, and if you throw anything else in then most method calls won’t work.

snapTo(x) 0.1.0

Returns a copy of the receiver with any elements that differ from x by less than the value of Sylvester.precision set exactly equal to x. This can be useful for working around rounding errors.

subtract(vector) 0.1.0

If the receiver and vector have the same number of elements, returns a Vector formed by subtracting the latter from the former. Otherwise, returns null.

to3D() 0.1.0

If the receiver is 3-dimensional, it returns a copy of the receiver. If it is 2-dimensional, it returns a copy of the receiver with an additional third element, which is set to zero. For all other sizes, it returns null. Something is similar is done in many of the methods of the Line and Plane classes, although for performance reasons they don’t use this method.

toDiagonalMatrix() 0.1.0

Returns an n×n square Matrix, where n is the number of elements in the receiver, whose leading-diagonal elements are the elements of the receiver. All the off-diagonal elements are zero.

toUnitVector() 0.1.0

Returns a copy of the receiver, whose elements have been scaled such that the modulus of the new vector is equal to 1. If the receiver has zero modulus then a copy of it is returned unchanged.

x(k) 0.1.0

Alias for multiply(k).