1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#ifndef matrix_h_
#define matrix_h_
#include <iostream>
#include <vector>
using namespace std;
// A simple node class storing the row, col and value for the matrix
// entry and pointers to the node for the next non-zero entry in the
// row and in the column
class Node {
public:
Node( ) : next_in_row(0), next_in_col(0) {}
Node( unsigned int r, unsigned int c, double v )
: row(r), col(c), value(v), next_in_row(0), next_in_col(0) {}
unsigned row, col;
double value;
Node * next_in_row;
Node * next_in_col;
};
// The matrix header itself. A few of the functions have been inlined.
class matrix {
public:
matrix( ); // Empty --- 0 rows and 0 columns
matrix( unsigned nrows, unsigned ncols ); // initialize all values to 0
matrix( matrix const& old ); // copy constructor
~matrix();
matrix& operator=( matrix const& rhs );
void resize( unsigned new_nrows, unsigned new_ncols );
unsigned nrows() const;
unsigned ncols() const;
// This function allows statements of the form x = M(r,c)
double operator() ( unsigned row, unsigned col ) const ;
// This function allows assignment
void set( unsigned row, unsigned col, double value );
matrix transpose() const;
matrix operator+( matrix const& rhs ) const;
matrix operator*( matrix const& rhs ) const;
void print_row( ostream& ostr, unsigned row ) const;
void print_col( ostream& ostr, unsigned col ) const;
void print_matrix( ostream& ostr ) const;
private:
vector<Node*> row_vec;
vector<Node*> col_vec;
};
#endif
|