Source: rank/matrix.h


Annotated List
Files
Globals
Hierarchy
Index
/******************************************************************************/
/*** Template class for matrix operations                                   ***/
/***------------------------------------------------------------------------***/
/*** Ingmar Bauer / ingmar@metacrawler.de                                   ***/
/*** 31.02.2003                                                             ***/
/******************************************************************************/

/**Matrix Template-Class
  *@author Ingmar Bauer
  */
#include 
#include 
#include 
#include 

using namespace std;

typedef vector dblVect;
typedef vector intVect;
typedef vector > intMatrix;
typedef vector > dblMatrix;
typedef dblVect::iterator dblVectIt;

template class matrix : vector > {
public:
  matrix(int n, double *a);
  matrix(int n);
  matrix();
    
  ~matrix();
  void print(void);

  int save(string fname);
  int load(string fname);

  void transpose(void);
  matrix operator*(matrix &a);
  
public:
  unsigned int n;
  vector > m;
};


template void matrix::transpose(void) {
  // do nothing up to now
}

template matrix matrix::operator*(matrix &a) {
  static matrix result = matrix(n);
    
  for(int y=0; ym[y][i]*a.m[i][x]);
      }
    }
  }
  return result;
}

template void matrix::print() {
  for(int y=0; y matrix::matrix(int n, double *a) {
  this->n = n;
  this->m = vector >(n, vector(n));
    
  for(int i=0; im[i][j] = a[i*n+j];
    }
  }
}

template matrix::matrix(int n) {
  this->n = n;
  this->m = vector >(n, vector(n));
    
  for(int i=0; im[i][j] = 0.0;
    }
  }
}

template matrix::matrix() {
    this->n = 0;
}

template matrix::~matrix() {
}

template int matrix::save(string str_fname) {

  std::ofstream f_matrix(str_fname.c_str(), ios::out | ios::binary);

  if(!f_matrix) {
    cerr << "Kann folgende Datei nicht oeffnen: " << str_fname << endl;
    return 0;
  }

  f_matrix.write((char *)&n, 4);

  // tupel: n_el, el_1..el_n
  unsigned int n_el=0;
  unsigned int i, j;
  unsigned long n_elpos=0;
  
  for(j=0; jn; j++) {

    f_matrix.write((char *)&j, 4);  // save line number
    
    n_elpos = f_matrix.tellp();
    f_matrix.write("\0\0\0\0", 4);  // placeholder for n_el, will be updated later
    n_el = 0;

    // check a line and save positions
    for(i=0; in; i++) {
      if(m[j][i] > 0) {
        n_el++;
        f_matrix.write((char *)&i, 4);
        f_matrix.write((char *)&m[j][i], sizeof(m[j][i]));
      }
    }

    // all elements added, now update n_el
    
    if(n_el > 0) {
      f_matrix.seekp(n_elpos, ios_base::beg);
      f_matrix.write((char *)&n_el, 4);
      f_matrix.seekp(0, ios_base::end);
    } else {
      f_matrix.seekp(n_elpos-4, ios_base::beg); // if empty line save nothing
    }
  }
  
  f_matrix.close();
  return 1;
}

template int matrix::load(string str_fname) {
  std::ifstream f_matrix(str_fname.c_str(), ios::in | ios::binary);

  if(!f_matrix) {
    cerr << "Kann folgende Datei nicht oeffnen: " << str_fname << endl;
    return 0;
  }

  f_matrix.read((char *)&n, 4);

  unsigned int i, j, pos;
  unsigned int n_el=0;
  unsigned int line=0;

  for(j=0; jn; j++) {

    f_matrix.read((char *)&line, 4);
    f_matrix.read((char *)&n_el, 4);

    m[j].clear();
    m[j].insert(m[j].begin(), n, 0.0);
    
    for(i=0; i

Generated by: ingmar on pluto on Fri Aug 8 21:29:16 2003, using kdoc 2.0a54.