CS488 PA2
Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | Friends | List of all members
Vector Class Reference

#include <Vector.h>

Public Member Functions

 Vector (int size)
 
 Vector (int size, char orientation)
 
 Vector (const Vector &original)
 
 ~Vector ()
 
double get (int i) const
 
char getOrientation () const
 
int getSize () const
 
double & operator() (int i)
 
Vectoroperator= (std::initializer_list< double > values)
 
Vectoroperator= (const Vector &other)
 

Static Public Attributes

static const char COLUMN = 'C'
 
static const char ROW = 'R'
 

Protected Member Functions

void allocateMemory ()
 
void deallocateMemory ()
 
void setSize (int size, char orientation)
 
void setValues (double value)
 
void setValues (const double *values)
 

Protected Attributes

char orientation
 
double * values
 
int size
 

Friends

double norm (const Vector &a)
 
Vector normalized (const Vector &a)
 
Vector operator+ (const Vector &a, const Vector &b)
 
Vector operator- (const Vector &a, const Vector &b)
 
double operator* (const Vector &a, const Vector &b)
 
Vector operator* (double k, const Vector &a)
 
Vector operator* (const Vector &a, double k)
 
bool operator== (const Vector &a, const Vector &b)
 
bool operator!= (const Vector &a, const Vector &b)
 
Vector trans (const Vector &a)
 

Detailed Description

The Vector class is an encapsulation of both n-dimensional points and n-dimensional direction vectors.

Constructor & Destructor Documentation

◆ Vector() [1/3]

Vector::Vector ( int  size)
explicit

Construct a column vector of given size.

Example of use:

Vector y(3);
Parameters
sizeThe size of the Vector

◆ Vector() [2/3]

Vector::Vector ( int  size,
char  orientation 
)

Construct a column or row vector of given size.

Example of use:

Parameters
sizeThe size of the Vector
orientationThe orientation (either COLUMN or ROW)

◆ Vector() [3/3]

Vector::Vector ( const Vector original)

A copy constructor.

Note: A copy constructor is critical because without one we can't pass a Vector by value (since when one passes by value a copy is made).

Parameters
originalThe Vector to copy

◆ ~Vector()

Vector::~Vector ( )

Destructor.

Member Function Documentation

◆ allocateMemory()

void Vector::allocateMemory ( )
protected

Allocate memory for this Vector.

◆ deallocateMemory()

void Vector::deallocateMemory ( )
protected

Deallocate the memory used by this Vector.

◆ get()

double Vector::get ( int  i) const

Get a particular element of this Vector.

Parameters
iThe index of the element
Exceptions
out_of_rangeexception
Returns
The value of the element at the given index

◆ getOrientation()

char Vector::getOrientation ( ) const

Get the orientation of this Vector.

Returns
The orientation (COLUMN or ROW)

◆ getSize()

int Vector::getSize ( ) const

Get the size of this Vector.

Returns
The size

◆ operator()()

double & Vector::operator() ( int  i)

Access an element of this Vector using the function-call operator.

Note: This method returns by reference so that this operator can be used on the left side of the assignment operator. Though this can be dangerous in general (since the value being referred to may not always exist), in this case it shouldn't cause any problems.

Examples of use:

d = y(1);
y(2) = 5.0;
Parameters
iThe index of the element
Exceptions
out_of_rangeexception
Returns
The value of the element at the given index

◆ operator=() [1/2]

Vector & Vector::operator= ( std::initializer_list< double >  values)

Assign an initializer_list to this Vector.

Examples of use:

y = {1,
2,
3};
x = {4, 5, 6, 7, 8, 9};

Note: This method is not void so that one can write x = y = {1,2} (which first assigns {1,2} to y and then assigns the result of that assignment to x). It returns the result by reference because there is no chance that this will not refer to something.

Parameters
valuesThe initializer_list containing the values
Exceptions
length_errorif the number of values is incorrect
Returns
The result of the assignment

◆ operator=() [2/2]

Vector & Vector::operator= ( const Vector other)

Assign another Vector to this Vector.

The two Vectors must have the same size and orientation.

Note: This method is not void so that one can write x = y = z (which first assigns z to y and then assigns the result of that assignment to x). It returns the result by reference because there is no concern that this will not refer to something.

Parameters
otherThe right-side Vector
Exceptions
length_errorif the sizes or orientations don't match
Returns
The Vector referred to by this

◆ setSize()

void Vector::setSize ( int  size,
char  orientation 
)
protected

Set the size (and orientation) of this Vector. Note: This method should only be called by constructors.

Parameters
sizeThe size (default is 1);
orientationThe orientation (default is COLUMN)

◆ setValues() [1/2]

void Vector::setValues ( double  value)
protected

Set all the elements of this Vector to a given value.

Parameters
valueThe value

◆ setValues() [2/2]

void Vector::setValues ( const double *  values)
protected

Set the values of the elements of this Vector to the corresponding values in an array.

Parameters
valuesThe values

Friends And Related Function Documentation

◆ norm

double norm ( const Vector a)
friend

Calculate the Euclidean norm of a Vector.

Parameters
aThe Vector
Returns
\(||\mathbf{a}||\)

◆ normalized

Vector normalized ( const Vector a)
friend

Calculate the normalized version of a Vector.

Parameters
aThe Vector
Returns
\(\mathbf{a} / ||\mathbf{a}||\)

◆ operator!=

bool operator!= ( const Vector a,
const Vector b 
)
friend

Compare two Vectors to see if they have different elements.

The two Vectors must have the same size and same orientation.

Parameters
aThe first Vector
bThe second Vector
Exceptions
length_errorif the sizes or orientations don't match
Returns
true or false

◆ operator* [1/3]

double operator* ( const Vector a,
const Vector b 
)
friend

Multiply two Vectors (i.e., find the dot/inner product)

The two Vectors must have the same size and same orientation

Parameters
aThe first Vector (a row Vector)
bThe second Vector (a column Vector)
Exceptions
length_errorif the sizes or orientations don't match
Returns
\(\mathbf{a} \cdot \mathbf{b}\) (a scalar)

◆ operator* [2/3]

Vector operator* ( double  k,
const Vector a 
)
friend

Multiply a scalar and a Vector.

Note: This method must return by value because the result Vector is a local variable.

Parameters
kThe scalar
aThe Vector
Returns
\(k \mathbf{a}\)

◆ operator* [3/3]

Vector operator* ( const Vector a,
double  k 
)
friend

Multiply a Vector and a scalar.

Note: This method must return by value because the result Vector is a local variable.

Parameters
aThe Vector
kThe scalar
Returns
\(\mathbf{a} k\)

◆ operator+

Vector operator+ ( const Vector a,
const Vector b 
)
friend

Add the Vector a and the Vector b.

Note: This method must return by value because the result Vector is a local variable.

Parameters
aOne Vector
bThe other Vector
Exceptions
length_errorif the sizes or orientations don't match
Returns
\(\mathbf{a} + \mathbf{b}\)

◆ operator-

Vector operator- ( const Vector a,
const Vector b 
)
friend

Subtract the Vector b from the Vector a (component by component).

The two Vectors must have the same size and orientation.

Note: This method must return by value because the result Vector is a local variable.

Parameters
aOne Vector
bThe other Vector
Exceptions
length_errorif the sizes or orientations don't match
Returns
\(\mathbf{a} - \mathbf{b}\)

◆ operator==

bool operator== ( const Vector a,
const Vector b 
)
friend

Compare two Vectors to see if they have identical (within a pre-defined TOLERANCE) elements.

The two Vectors must have the same size and same orientation.

Parameters
aThe first Vector
bThe second Vector
Exceptions
length_errorif the sizes or orientations don't match
Returns
true or false

◆ trans

Vector trans ( const Vector a)
friend

Create and return a transposed version of a given Vector.

Note: This function must return by value because the result Vector is a local variable.

Parameters
aThe original Vector
Returns
\(\mathbf{a}^T\)

Member Data Documentation

◆ COLUMN

const char Vector::COLUMN = 'C'
static

Constant indicating a column Vector.

◆ orientation

char Vector::orientation
protected

The orientation of this Vector (COLUMN or ROW).

◆ ROW

const char Vector::ROW = 'R'
static

Constant indicating a row Vector.

◆ size

int Vector::size
protected

The size of this Vector.

◆ values

double* Vector::values
protected

The elements of this Vector.


The documentation for this class was generated from the following files: