Main Page | Namespace List | Alphabetical List | Class List | File List | Class Members | File Members

MVector Class Reference

Simple mathematical vectors as used in linear algebra. More...

#include <MVector.h>

List of all members.

Public Member Functions

Constructors, destructor, and conversions
Note:
No need for an explicit destructor nor for an explicit copy constructor: the default ones, automatically provided by C++, are fine.


 MVector (int n=0, double x=0.0)
 Constructor (also usable as default constructor).
Assignments
Note:
No need for a copy assignment operator; the default one, automatically provided by C++ is fine.


MVectoroperator+= (const MVector &mv)
MVectoroperator-= (const MVector &mv)
Accessors
int ncomps () const
 Number of elements.
int size () const
 Number of elements.
double norm () const
 Compute the Euclidian norm of the vector.
Subscript
double & operator[] (int i)
 Subscript for non-constant vectors.
double operator[] (int i) const
 Subscript for constant vectors.

Private Attributes

int _ncomps
 number of elements (redundant yet convenient)
vector< double > _components
 the std::vector containing the elements

Friends

ostream & operator<< (ostream &os, const MVector &mv)
 IO operation: just display the contents.
Arithmetic operations
MVector operator+ (const MVector &mv1, const MVector &mv2)
 Addition.
MVector operator- (const MVector &mv1, const MVector &mv2)
 Subtraction.
double operator * (const MVector &mv1, const MVector &mv2)
 Scalar product.
double dot (const MVector &mv1, const MVector &mv2)
 Another denotation for scalar product.
Relational operations
bool operator== (const MVector &mv1, const MVector &mv2)
 Equality.
bool operator!= (const MVector &mv1, const MVector &mv2)
 Unequality.

Classes

class  Bad_Dimensions
class  Out_Of_Bounds


Detailed Description

Simple mathematical vectors as used in linear algebra.

MVector is implemented using an STL vector<double>.

Definition at line 23 of file MVector.h.


Constructor & Destructor Documentation

MVector::MVector int  n = 0,
double  x = 0.0
 

Constructor (also usable as default constructor).

No surprise here: we simply call the adequate constructor of std::vector.

Parameters:
n number of elements
x the common value to initialize all components with

Definition at line 17 of file MVector.cpp.

00018     : _ncomps(n), _components(n, x) 
00019 {
00020     // Nothing else to do
00021 }


Member Function Documentation

int MVector::ncomps  )  const [inline]
 

Number of elements.

Definition at line 68 of file MVector.h.

References _ncomps.

Referenced by size().

00068 {return _ncomps;}

double MVector::norm  )  const
 

Compute the Euclidian norm of the vector.

We use the scalar product of the vector by itself.

Note:
The syntax may seem strange, due to so many stars!

Definition at line 28 of file MVector.cpp.

Referenced by main().

00029 {  
00030     return *this * *this;  
00031 }

MVector& MVector::operator+= const MVector mv  )  [inline]
 

Definition at line 58 of file MVector.h.

00058 {return *this = *this + mv;}

MVector& MVector::operator-= const MVector mv  )  [inline]
 

Definition at line 59 of file MVector.h.

00059 {return *this = *this - mv;}

double MVector::operator[] int  i  )  const
 

Subscript for constant vectors.

Definition at line 91 of file MVector.cpp.

References _components, and _ncomps.

00092 {
00093     if (i < 0 || i >= _ncomps) throw Out_Of_Bounds();
00094     return _components[i];
00095 }

double & MVector::operator[] int  i  ) 
 

Subscript for non-constant vectors.

Definition at line 85 of file MVector.cpp.

References _components, and _ncomps.

00086 {
00087     if (i < 0 || i >= _ncomps) throw Out_Of_Bounds();
00088     return _components[i];
00089 }

int MVector::size  )  const [inline]
 

Number of elements.

Definition at line 70 of file MVector.h.

References ncomps().

00070 {return ncomps();}

Here is the call graph for this function:


Friends And Related Function Documentation

double dot const MVector mv1,
const MVector mv2
[friend]
 

Another denotation for scalar product.

Definition at line 88 of file MVector.h.

00089     {
00090         return mv1 * mv2; 
00091     }    

double operator * const MVector mv1,
const MVector mv2
[friend]
 

Scalar product.

Since std::vector have no arithmetic operations, we have to do it ourselves.

Definition at line 74 of file MVector.cpp.

00075 {
00076     if (mv1._ncomps != mv2._ncomps) throw MVector::Bad_Dimensions();
00077 
00078     int n = mv1._ncomps;
00079     double prod = 0.0;
00080     for (int i = 0; i < n; ++i)
00081         prod += mv1._components[i] * mv2._components[i];
00082     return prod;
00083 }

bool operator!= const MVector mv1,
const MVector mv2
[friend]
 

Unequality.

Definition at line 102 of file MVector.h.

00103     {
00104         return ! (mv1 == mv2);
00105     }

MVector operator+ const MVector mv1,
const MVector mv2
[friend]
 

Addition.

Since std::vector have no arithmetic operations, we have to do it ourselves.

Definition at line 46 of file MVector.cpp.

00047 {
00048     if (mv1._ncomps != mv2._ncomps) throw MVector::Bad_Dimensions();
00049 
00050     int n = mv1._ncomps;
00051     MVector mv(n);    
00052     for (int i = 0; i < n; ++i)
00053         mv._components[i] = mv1._components[i] + mv2._components[i];
00054     return mv;
00055 }

MVector operator- const MVector mv1,
const MVector mv2
[friend]
 

Subtraction.

Since std::vector have no arithmetic operations, we have to do it ourselves.

Definition at line 60 of file MVector.cpp.

00061 {
00062     if (mv1._ncomps != mv2._ncomps) throw MVector::Bad_Dimensions();
00063 
00064     int n = mv1._ncomps;
00065     MVector mv(n);    
00066     for (int i = 0; i < n; ++i)
00067         mv._components[i] = mv1._components[i] - mv2._components[i];
00068     return mv;
00069 }

ostream& operator<< ostream &  os,
const MVector mv
[friend]
 

IO operation: just display the contents.

Enclose the list of component values between square brackets.

Definition at line 100 of file MVector.cpp.

00101 {
00102     os << "[ ";
00103     for (int i = 0; i < mv._ncomps; ++i)
00104         os << mv._components[i] << ' ';
00105     os << ']';
00106     return os;
00107 }

bool operator== const MVector mv1,
const MVector mv2
[friend]
 

Equality.

To check for equality, simply use operator== for std::vector.

Definition at line 37 of file MVector.cpp.

00038 {
00039     if (mv1._ncomps != mv2._ncomps) return false;
00040     return mv1._components == mv2._components;
00041 }


Member Data Documentation

vector<double> MVector::_components [private]
 

the std::vector containing the elements

Definition at line 28 of file MVector.h.

Referenced by operator *(), operator+(), operator-(), operator<<(), operator==(), and operator[]().

int MVector::_ncomps [private]
 

number of elements (redundant yet convenient)

Definition at line 27 of file MVector.h.

Referenced by ncomps(), operator *(), operator+(), operator-(), operator<<(), operator==(), and operator[]().


The documentation for this class was generated from the following files:
Generated on Mon Dec 12 18:25:46 2005 for Vectors_and_Matrices by  doxygen 1.4.3