Mvector, mathematical vectors

This class represents an n-dimensional vector

R. Brown, 1/2011





CLASS Mvector

No superclass.

State variables:

float* arr
Array representing vector elements

int len
Length of the array arr

Constructors:

Mvector
Standard constructor

Arguments:

int length
Desired length of this Mvector

State change:
This instance of Mvector is initialized with an array of length length zeroes.

Mvector
Default constructor

No arguments.

State change:
This instance of Mvector is initialized with an array of length default_len zeroes.

Mvector
Copy constructor

Arguments:

const Mvector & mvec
Mvector object to be copied

State change:
This instance of Mvector is initialized with an array of length mvec.len with elements copied from mvec.arr[].

Destructor:
~Mvector
Destructor

State change:
Dynamically allocated memory for this instance of Mvector is deallocated.

Methods:

Mvector & operator=
Assignment operator

Arguments:

const Mvec & mvec
Mvector object to be copied

State change:
This instance of Mvector is replaced by a copy of mvec.

Return value:
This instance of Mvector, after replacement.

int len
Length access

No arguments.

State change:
None

Return value:
The value of the state variable len.

float & operator[]
Element operator

Arguments:

int index
Index of the element to access, where 0 ≤ index < len

State change:
None; except, causes a crash if index is out of range.

Return value:
The l-value (location value) of the element of vec[] with index index.

Mvector operator*
Scalar product operator

Arguments:

float val
Scalar value

State change:
A new Mvector mv is allocated with length len, and each element of mv is assigned the product of val with the element of this Mvector having the same index.

Return value:
The object mv.

Mvector operator*
Dot product operator

Arguments:

const Mvector & vec
Mvector value, where vec.len == len of this Mvector instance.

State change:
If the length of mvec differs from len, then the program crashes. Otherwise, a new Mvector object mv is allocated with length len, and each element of mv is assigned the product of the elements of arr[] and vec.arr[] having the same index.

Return value:
The object mv.

Mvector operator+
Vector addition operator

Arguments:

const Mvector & vec
Mvector value, where vec.len == len of this Mvector instance.

State change:
If the length of mvec differs from len, then the program crashes. Otherwise, a new Mvector object mv is allocated with length len, and each element of mv is assigned the sum of the elements of arr[] and vec.arr[] having the same index.

Return value:
The object mv.