Matrix.cc


#include "Matrix.h"
#include <string.h>

Matrix::Matrix() {
  nCols = 0;
  nRows = 0;
}

Matrix::Matrix(int m, int n) {
  nCols = m;
  nRows = n;
  data  = new double[m*n];
}

Matrix::Matrix(double *src, int m, int n) {
  // create a matrix from a Fortran-like vector (without copy!)

  nCols = m;
  nRows = n;
  data  = src;
}
    
Matrix::~Matrix() {
  delete [] data;
}


Matrix &Matrix::copy() {
  // return a deep copy of this
  Matrix &theCopy = *new Matrix(nCols, nRows);
  memcpy(theCopy.data, data, nCols*nRows*sizeof(double));
  return theCopy;
}


int Matrix::getColumnDimension() {
  return nCols;
}

int Matrix::getRowDimension() {
  return nRows;
}

double* Matrix::getArray() {
  return data;
}


double Matrix::get(int i, int j) {
  // element read access, first index = 1
  return data[(i-1) + nCols*(j-1)];
}

void Matrix::set(int i, int j, double val) {
  // element write access, first index = 1
  data[(i-1) + nCols*(j-1)] = val;
}

previous    contents     next

Peter Junglas 20.6.2000