next up previous contents index
Next: Real-Valued Matrices ( matrix Up: Number Types and Linear Previous: A Floating Point Filter   Contents   Index


Real-Valued Vectors ( vector )

Definition

An instance of data type vector is a vector of variables of type double.

#include < LEDA/vector.h >

Creation

vector v creates an instance v of type vector; v is initialized to the zero-dimensional vector.

vector v(int d) creates an instance v of type vector; v is initialized to the zero vector of dimension d.

vector v(double a, double b) creates an instance v of type vector; v is initialized to the two-dimensional vector (a, b).

vector v(double a, double b, double c)
    creates an instance v of type vector; v is initialized to the three-dimensional vector (a, b, c).

vector v(vector w, int prec) creates an instance v of type vector; v is initialized to a copy of w. The second argument is for compatibility with rat_vector.

Operations

int v.dim() returns the dimension of v.

double& v[int i] returns i-th component of v.
Precondition 0 < = i < = v.dim()-1.

double v.hcoord(int i) for compatibility with rat_vector.

double v.coord(int i) for compatibility with rat_vector.

double v.sqr_length() returns the square of the Euclidean length of v.

double v.length() returns the Euclidean length of v.

vector v.norm() returns v normalized.

double v.angle(vector w) returns the angle between v and w.

vector v.rotate90() returns the v rotated by 90 degrees.
Precondition v.dim() = 2

vector v.rotate(double a) returns the v rotated by an angle of a.
Precondition v.dim() = 2

vector v + v1 Addition.
Precondition v.dim() = v1.dim().

vector v - v1 Subtraction.
Precondition v.dim() = v1.dim().

double v * v1 Scalar multiplication.
Precondition v.dim() = v1.dim().

vector v * double r Componentwise multiplication with double r.

bool v == w Test for equality.

bool v != w Test for inequality.

void v.print(ostream& O) prints v componentwise to ostream O.

void v.print() prints v to cout.

void v.read(istream& I) reads d = v.dim() numbers from input stream I and writes them into v[0]...v[d - 1].

void v.read() reads v from cin.

ostream& ostream& O << v writes v componentwise to the output stream O.

istream& istream& I >> vector& v reads v componentwise from the input stream I.

Additional Operations for vectors in two and three-dimensional space

double v.xcoord() returns the zero-th cartesian coordinate of v.

double v.ycoord() returns the first cartesian coordinate of v.

double v.zcoord() returns the second cartesian coordinate of v.

int compare_by_angle(vector v1, vector v2)
    For a non-zero vector v let $ \alpha$(v) be the angle by which the positive x-axis has to be turned counter-clockwise until it aligns with v. The function compares the angles defined by v1 and v2, respectively. The zero-vector precedes all non-zero vectors in the angle-order.

Implementation

Vectors are implemented by arrays of real numbers. All operations on a vector v take time O(v.dim()), except for dim and [ ] which take constant time. The space requirement is O(v.dim()).

Be aware that the operations on vectors and matrices incur rounding errors and hence are not completely reliable. For example, if M is a matrix, b is a vector, and x is computed by x = M.solve(b) it is not necessarily true that the test b == M * b evaluates to true. The types integer_vector and integer_matrix provide exact linear algebra.


next up previous contents index
Next: Real-Valued Matrices ( matrix Up: Number Types and Linear Previous: A Floating Point Filter   Contents   Index
LEDA research project
2000-02-09