I am trying to figure out what should I use for LU decomposition algorithm: vectors or arrays. I think the initial choice is very important.
A vector is a dynamic array designed to hold objects of any type (which we do not need in the algorithm), and capable of growing and shrinking as needed (which is advantage).
Algorithm using arrays are running faster than one using vectors (which is a big advantage).
What do you think about this topic? What did student who already took Linear Algebra use?
-V-
In pragmatic terms, I'd say that the quickest way to write working code is to use plain old arrays of double
Try to balance ease of implementation with the knowledge that you are going to be living with this code until Xmas. You don't want to have to do a rewrite to support 5x5 matrices because you initially assumed 3x3, for example.
Using vector might make sense longer term esp. if you are very familiar with STL - you don't have to worry about bounds checking so much as long as you handle exceptions cleanly. Using arrays will make optimization for sparse matrices harder (e.g upper/ lower triangular, the identity matrix) but who cares about this at this point?
In the ideal world (ie. not under time pressure for weekend homework, or delivery of working code to your manager) it is possible to write your code such that the complexity involved in replacing a vector with an array or vice versa is not so complex. For example, when you need to access an entry (i,j) do it via "thin" accessor APIs eg.
Code:
double Matrix::getEntry(unsigned int i, unsigned int j)
{
// check bounds are valid before you do this
return data[i][j];
}
void Matrix::setEntry(const unsigned int i, const unsigned int j, const double value)
{
// check bounds are valid before you do this
data[i][j] = value;
}
class Matrix
{
public:
Matrix(unsigned int i, const unsigned int j)
{
// allocate the array
}
~Matrix()
{
// free the array
}
private:
double array[][];
};
(This code won't compile, this is for illustration only.)
With this approach, you just need to replace two line of code plus the declaration, initialization, and release of the
data field to change this particular subset of your implementation to use a vector instead. We are
encapsulating the storage mechanism within the Matrix class to make it easier to switch in another storage mechanism if the first one is not working for us.