Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : MATRIX.H

 
Output of file : MATRIX.H contained in archive : BLFMATH.ZIP
////////////////////////////////////////////////////////////
// matrix.h: Matrix class template.
// Copyright(c) 1993 Azarona Software. All rights reserved.
////////////////////////////////////////////////////////////
#ifndef H_MATRIX
#define H_MATRIX
#include "vector.h"

#define INLINE

template
class Matrix {
protected:
Vector data;
unsigned nrows, ncols, rowstride, colstride;
public:
Matrix(unsigned nr=0, unsigned nc=0, const TYPE *s = 0);
Matrix(const Matrix &m);
Matrix(const Matrix &m, SliceType styp,
unsigned sr=0, unsigned sc=0,
unsigned nr=0, unsigned nc=0);
void Copy(const Matrix &m);
void Share(const Matrix &m);
Matrix Transpose();
Matrix &operator=(const Matrix &m);
Matrix &operator=(const TYPE &x);
#ifndef NO_RANGE_CHECK
unsigned CheckRow(unsigned i) const;
unsigned CheckCol(unsigned i) const;
#endif
Vector operator[](unsigned r);
const Vector operator[](unsigned r) const;
TYPE &operator()(unsigned r, unsigned c);
const TYPE &operator()(unsigned r, unsigned c) const;
Vector Row(unsigned r, SliceType styp=SHARED);
Vector Col(unsigned c, SliceType styp=SHARED);
Vector Diag(SliceType styp=SHARED);
Vector All(SliceType styp=SHARED);
const Vector Row(unsigned r, SliceType styp=SHARED) const;
const Vector Col(unsigned c, SliceType styp=SHARED) const;
const Vector Diag(SliceType styp=SHARED) const;
const Vector All(SliceType styp=SHARED) const;
// Mid-level hooks. Use these at your own risk:
VecPtr RowPtr(unsigned r=0);
VecPtr ColPtr(unsigned c=0);
VecPtr DiagPtr();
VecPtr PtrToAll();
VecPtr RowPtr(unsigned r=0) const;
VecPtr ColPtr(unsigned c=0) const;
VecPtr DiagPtr() const;
VecPtr PtrToAll() const;
int IsNull() const;
int IsUnique() const;
Matrix Clone() const;
int EnsureUnique();
unsigned NCols() const;
unsigned NRows() const;
unsigned RowStride() const;
unsigned ColStride() const;
int IsRowMajor() const;
int IsColMajor() const;
int IsSquare() const;
};

template
INLINE Matrix &Matrix::operator=(const Matrix &m)
// Share semantics used for assignment.
{
if (this != &m) Share(m); // Note trap for assignment to self.
return *this;
}

template
INLINE Vector Matrix::operator[](unsigned r)
// Row subscripting operator for non-const matrices.
// This routine does the same thing as Row(r,SHARED).
{
return Vector(data, SHARED,
ncols, rowstride, CHECKROW(r)*colstride);
}

template
INLINE const Vector Matrix::operator[](unsigned r) const
// Row subscripting operator for const matrices.
// This routine does the same thing as Row(r,SHARED).
{
return Vector(data, SHARED,
ncols, rowstride, CHECKROW(r)*colstride);
}

template
INLINE VecPtr Matrix::RowPtr(unsigned r)
// Returns vector pointer to row r. No reference counting
// done. Use at your own risk.
{
return VecPtr(data.start+CHECKROW(r)*colstride, rowstride);
}

template
INLINE VecPtr Matrix::RowPtr(unsigned r) const
// Returns vector pointer to row r. No reference counting
// done. Use at your own risk.
{
return VecPtr(data.start+CHECKROW(r)*colstride, rowstride);
}


template
INLINE VecPtr Matrix::ColPtr(unsigned c)
// Returns vector pointer to column c. No reference counting
// done. Use at your own risk.
{
return VecPtr(data.start+CHECKCOL(c)*rowstride, colstride);
}

template
INLINE VecPtr Matrix::ColPtr(unsigned c) const
// Returns vector pointer to column c. No reference counting
// done. Use at your own risk.
{
return VecPtr(data.start+CHECKCOL(c)*rowstride, colstride);
}

template
INLINE VecPtr Matrix::DiagPtr()
// Returns vector pointer to right/downward diagonal.
{
unsigned diastride = (rowstride == 1) ? colstride : rowstride;
diastride++;
return VecPtr(data.start, diastride);
}

template
INLINE VecPtr Matrix::DiagPtr() const
// Returns vector pointer to right/downward diagonal.
{
unsigned diastride = (rowstride == 1) ? colstride : rowstride;
diastride++;
return VecPtr(data.start, diastride);
}

template
INLINE VecPtr Matrix::PtrToAll()
// Returns vector pointer to underlying 1D vector.
// No reference counting done, so use at your own risk.
{
return VecPtr(data.start, 1);
}

template
INLINE VecPtr Matrix::PtrToAll() const
// Returns vector pointer to underlying 1D vector.
// No reference counting done, so use at your own risk.
{
return VecPtr(data.start, 1);
}

template
INLINE int Matrix::IsNull() const
// Returns true if the matrix references null_rep.
{
return data.IsNull();
}

template
INLINE int Matrix::IsUnique() const
// Returns true of matrix has only reference to shared data.
{
return data.IsUnique();
}

template
INLINE unsigned Matrix::NCols() const
{
return ncols;
}

template
INLINE unsigned Matrix::NRows() const
{
return nrows;
}

template
INLINE unsigned Matrix::RowStride() const
// Returns the stride that a row vector would have
// in the matrix. For normal row-major matrices,
// the row stride will be 1. For shared submatrices,
// the rowstride is that of the parent matrix.
{
return rowstride;
}

template
INLINE unsigned Matrix::ColStride() const
// Returns the stride that a column vector would have
// in the matrix. For a column-major matrix, the
// column stride will be 1. For shared submatrices.
// the colstride is that of the parent matrix.
{
return colstride;
}

template
INLINE int Matrix::IsRowMajor() const
{
return rowstride == 1;
}

template
INLINE int Matrix::IsColMajor() const
{
return colstride == 1;
}

template
INLINE int Matrix::IsSquare() const
{
return nrows == ncols;
}

#undef INLINE

// Whether or not we should include the non-line methods for
// our class templates here is implementation dependent.

#include "matrix.mth"

#endif


  3 Responses to “Category : C++ Source Code
Archive   : BLFMATH.ZIP
Filename : MATRIX.H

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/