// // Programmer: Craig Stuart Sapp // Creation Date: Wed Feb 24 03:30:03 PST 1999 // Last Modified: Fri May 28 15:40:39 PDT 1999 // Last Modified: Wed Jul 31 17:27:18 PDT 2002 (added getRow() and getColumn()) // Last Modified: Wed Jul 31 17:27:18 PDT 2002 (added setRow() and setColumn()) // Filename: .../sig/maint/code/net/Matrix/Matrix.cpp // Syntax: C++ // $Smake: g++ -O3 -g -c %f && rm %b.o // #ifndef _MATRIX_CPP_INCLUDED #define _MATRIX_CPP_INCLUDED #include "Matrix.h" #ifndef OLDCPP #include using namespace std; #else #include #endif ////////////////////////////// // // Matrix::Matrix -- // template Matrix::Matrix (void) { transposeQ = 0; dim1 = 0; dim2 = 0; storage = NULL; } template Matrix::Matrix(const Matrix& aMatrix) { transposeQ = 0; if (aMatrix.transposeQ) { dim1 = aMatrix.dim2; dim2 = aMatrix.dim1; } else { dim1 = aMatrix.dim1; dim2 = aMatrix.dim2; } int size = dim1 * dim2; storage = new type[size]; for (int r=0; r Matrix::Matrix (int rowCount, int columnCount) { if (rowCount < 1 || columnCount < 1) { cout << "Error: invalid maxtrix dimensions: " << rowCount << ", " << columnCount << endl; exit(1); } transposeQ = 0; dim1 = rowCount; dim2 = columnCount; storage = new type[dim1 * dim2]; } template Matrix::Matrix (int rowCount, int columnCount, type& thing) { if (rowCount < 1 || columnCount < 1) { cout << "Error: invalid maxtrix dimensions: " << rowCount << ", " << columnCount << endl; exit(1); } transposeQ = 0; dim1 = rowCount; dim2 = columnCount; int aSize = dim1 * dim2; storage = new type[aSize]; for (int i=0; i Matrix::Matrix (int columnCount) { if (columnCount < 1) { cout << "Error: invalid maxtrix dimensions: " << columnCount << endl; exit(1); } transposeQ = 0; dim1 = 1; dim2 = columnCount; storage = new type[dim1 * dim2]; } template Matrix::Matrix (type* data, int rowCount, int columnCount) { if (rowCount < 1 || columnCount < 1) { cout << "Error: invalid maxtrix dimension: " << rowCount << ", " << columnCount << endl; exit(1); } transposeQ = 0; dim1 = rowCount; dim2 = columnCount; int size = dim1 * dim2; storage = new type[size]; for (int i=0; i::~Matrix -- // template Matrix::~Matrix() { if (storage != NULL) { delete [] storage; } storage = NULL; } ////////////////////////////// // // Matrix::add -- add element by element // template Matrix& Matrix::add(type scalar) { for (int r=0; r Matrix& Matrix::add(const Matrix& aMatrix) { for (int r=0; r::cell -- returns the specified matrix cell (row, column). // template type& Matrix::cell(int row, int column) { checkdim(row, column); if (transposeQ) { return storage[column * dim2 + row]; } else { return storage[row * dim2 + column]; } } template type Matrix::cell(int row, int column) const { checkdim(row, column); if (transposeQ) { return storage[column * dim2 + row]; } else { return storage[row * dim2 + column]; } } ////////////////////////////// // // Matrix::getRow -- // template void Matrix::getRow(Array& anArray, int index) { int length = getColumnCount(); anArray.setSize(length); int i; for (i=0; i::getRow -- // template void Matrix::getColumn(Array& anArray, int index) { int length = getRowCount(); anArray.setSize(length); int i; for (i=0; i::setRow -- // template void Matrix::setRow(int index, Array& anArray) { int length = getColumnCount(); if (anArray.getSize() < length) { length = anArray.getSize(); } int i; for (i=0; i::setColumn -- // template void Matrix::setColumn(int index, Array& anArray) { int length = getRowCount(); if (anArray.getSize() < length) { length = anArray.getSize(); } int i; for (i=0; i::getColumnCount -- // template int Matrix::getColumnCount (void) const { if (transposeQ) { return dim1; } else { return dim2; } } ////////////////////////////// // // Matrix::getRowCount -- // template int Matrix::getRowCount(void) const { if (transposeQ) { return dim2; } else { return dim1; } } ////////////////////////////// // // Matrix::getSize -- // template int Matrix::getSize(void) const { return dim1 * dim2; } ////////////////////////////// // // Matrix::multiply -- multiply element by element // template Matrix& Matrix::multiply(type scalar) { for (int r=0; r::operator[] -- // template type& Matrix::operator[](int index) { if (dim1 == 1) { if (index < dim2) { return storage[index]; } else { cerr << "Error: invalid matrix access, returning 0" << endl; exit(1); } } else if (dim2 == 1) { if (index < dim1) { return storage[index]; } else { cerr << "Error: invalid matrix access, returning 0" << endl; exit(1); } } else { if (index < dim1 * dim2) { return storage[index]; } else { cerr << "Error: invalid matrix access, returning 0" << endl; exit(1); } } } ////////////////////////////// // // Matrix::operator= -- // template Matrix& Matrix::operator=(const Matrix& aMatrix) { if (this == &aMatrix) { return *this; } if (dim1 * dim2 == aMatrix.dim1 * aMatrix.dim2) { // don't change the storage size } else { delete [] storage; storage = new type[aMatrix.dim1 * aMatrix.dim2]; } dim1 = aMatrix.getRowCount(); dim2 = aMatrix.getColumnCount(); transposeQ = 0; // int size = dim1 * dim2; for (int r=0; r::operator+= -- // template Matrix& Matrix::operator+=(const Matrix& aMatrix) { if (dim1 * dim2 != aMatrix.dim1 * aMatrix.dim2) { cout << "Error in operator += for Matrices" << endl; } int rows = getRowCount(); int cols = getColumnCount(); for (int r=0; r::setSize -- very destructive to previous contents // template void Matrix::setSize(int row, int column) { if (storage != NULL) { delete [] storage; } storage = new type[row * column]; dim1 = row; dim2 = column; transposeQ = 0; } ////////////////////////////// // // Matrix::transpose -- // template void Matrix::transpose(void) { transposeQ = !transposeQ; } ////////////////////////////// // // Matrix::zero -- // template void Matrix::zero(void){ int size = dim1 * dim2; for (int i=0; i::multiply -- // template Matrix& Matrix::multiply(Matrix& output, const Matrix& one, Matrix& two) { if (one.getColumnCount() != two.getRowCount()) { cerr << "Error: sizes of input matrices not condusive" << " for multiplication." << endl; exit(1); } if (output.getRowCount() != one.getRowCount()) { cerr << "Error: Row length of output matrix is not equal to input ." << endl; exit(1); } if (output.getColumnCount() != two.getColumnCount()) { cerr << "Error: Column length of output matrix is not equal to input." << endl; exit(1); } output.zero(); two.transpose(); for (int r=0; r Matrix& Matrix::multiply(Matrix& output, Matrix& one, type aScalar) { for (int i=0; i::add -- add two matrices together // template Matrix& Matrix::add(Matrix& output, const Matrix& one, const Matrix& two) { if (one.getRowCount() != two.getRowCount()) { cerr << "Error: Row length of matrices are not equal for adding." << endl; exit(1); } if (one.getColumnCount() != two.getColumnCount()) { cerr << "Error: Column length of matrices are not equal for adding." << endl; exit(1); } if (one.getRowCount() != output.getRowCount()) { cerr << "Error: Row length of output matrix is not equal to input ." << endl; exit(1); } if (output.getColumnCount() != two.getColumnCount()) { cerr << "Error: Column length of output matrix is not equal to input." << endl; exit(1); } for (int r=0; r::transpose -- // template void Matrix::transpose(Matrix& output, const Matrix& one) { cerr << "Matrix::tranpose note implemented" << endl; } ////////////////////////////// // // Matrix::setAll -- // template void Matrix::setAll(type& thing) { int rows = getRowCount(); int cols = getColumnCount(); int i, j; for (i=0; i::checkdim -- checks that the dimensions are valid or not // template void Matrix::checkdim(int row, int column) const { if (transposeQ) { if (row >= 0 && row < dim2 && column >= 0 && column < dim1) { return; } } else { if (row >= 0 && row < dim1 && column >= 0 && column < dim2) { return; } } cerr << "Error: dimensions of matrix are: " << getRowCount() << ", " << getColumnCount() << " but you accessed: " << row << ", " << column << endl; exit(1); } /////////////////////////////////////////////////////////////////////////// // // external functions // template ostream& operator<<(ostream& out, const Matrix& aMatrix) { for (int r=0; r 1) { out << '\n'; } } return out; } #endif /* _MATRIX_CPP_INCLUDED */ // md5sum: f8233c33179e27f9964058a999432b32 Matrix.cpp [20050403]