//C++ Matrix Library Essentials
#include <vcl.h>
#pragma hdrstop
#pragma argsused
#include<iostream>
#include <vector>
#include<string>
using namespace std;
//Class definitions
class ValStore{
private:
vector<vector<double> > data; //Note space > > so as not to confuse with operator >>
protected:
ValStore(){resize(1,1);}; //Protected Constructor
public:
//Resize rows and cols of data block and initialize data to zero
void resize(const unsigned rows, const unsigned cols)
{
data.resize(rows);
for (register unsigned i=0; i<data.size(); i++)
{
data[i].resize(cols);
for(register unsigned j=0; j<data[i].size(); j++) data[i][j] = 0;
}
}
//Access methods to get (i,j) element
double& operator() (unsigned i, unsigned j){return data[i-1][j-1];}
const double& operator() (unsigned i, unsigned j) const {return data[i-1][j-1];}
unsigned getrows() const {return data.size();}
unsigned getcols() const {return data[0].size();}
virtual ~ValStore(){}; //Virtual Destructor
};
//-------------------------------------------------------------------------
class Matrix : public ValStore
{
private:
string Name;
public:
//Constructors
Matrix()
{
Name = "";
resize(1, 1);
}
Matrix(const unsigned nrows, const unsigned ncols)
{
Name = "";
resize(nrows, ncols);
}
Matrix(const string& name)
{
Name = name;
resize(1, 1);
}
Matrix(const string& name, const unsigned rows, const unsigned cols)
{
Name = name;
resize(rows, cols);
}
//Functions
string getname() const {return Name;};
void setname(const string& name) {Name = name;};
//Output operator
friend ostream& operator << (ostream& os, Matrix& a)
{
cout.setf(ios::scientific, ios::floatfield);
cout.setf(ios::right, ios::adjustfield);
cout.precision(3);
os << a.getname() << " [" << a.getrows() << "," << a.getcols() << "]" << "\n";
for (register unsigned i=1; i<=a.getrows(); i++)
{
for (register unsigned j=1; j<=a.getcols(); j++)
{
os.width(9); os.precision(2);
os << a(i,j) << " ";
}
os << endl;
}
os << endl;
return os;
}
};
//-------------------------------------------------------------------------
//Special case of Matrix: SquareMatrix
class SquareMatrix : public Matrix
{
public:
void resize(const unsigned rows) //Overides virtual function resize(m, n) in ValStore
{
ValStore::resize(rows, rows);
}
//Constructors
SquareMatrix() //Default constructor
{
setname("");
resize(1);
}
SquareMatrix(const string& name)
{
setname(name);
resize(1);
}
SquareMatrix(const string& name, unsigned size)
{
setname(name);
resize(size);
}
};
//------------------------------------------------------------------------
//Special cases of SquareMatrix : UnitMatrix
class UnitMatrix : public SquareMatrix
{
public:
//Constructors
UnitMatrix() //Default constructor
{
setname("");
(*this).resize(1);
(*this)(1,1) = 1;
}
UnitMatrix(const string& name)
{
setname(name);
(*this).resize(1);
(*this)(1,1) = 1;
}
UnitMatrix(const string& name, const unsigned size)
{
setname(name);
(*this).resize(size);
for(register unsigned i=1; i<=size; i++) (*this)(i,i) = 1;
}
};
//--------------------------------------------------------------------
//Special case of Matrix : Vector
class Vector : public Matrix
{
public:
//Overides virtual function resize(m, n) in ValStore
void Vector::resize(const unsigned rows)
{
ValStore::resize(rows, 1);
}
//Constructors
Vector()
{
setname("");
resize(1);
}
Vector(const string& name)
{
setname(name);
resize(1);
}
Vector(const string& name, unsigned size)
{
setname(name);
resize(size);
}
};
//-------------------------------------------------------------------------
int main()
{
int i;
Matrix M("M",2,3);
SquareMatrix S("S",3);
UnitMatrix U("U",3);
Vector V("V",3);
cout << M;
cout << S;
cout << U;
cout << V;
cin >> i;
return 0;
}
//---------------------------------------------------------------------------
Produces the following output:
M [2,3]
0.00e+00 0.00e+00 0.00e+00
0.00e+00 0.00e+00 0.00e+00
S [3,3]
0.00e+00 0.00e+00 0.00e+00
0.00e+00 0.00e+00 0.00e+00
0.00e+00 0.00e+00 0.00e+00
U [3,3]
1.00e+00 0.00e+00 0.00e+00
0.00e+00 1.00e+00 0.00e+00
0.00e+00 0.00e+00 1.00e+00
V [3,1]
0.00e+00
0.00e+00
0.00e+00