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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
// -*-c++-*-
#ifndef IG_MATRIX_H
#define IG_MATRIX_H
// $Id: matrix.h 184 2008-05-10 19:43:17Z hkuiper $
// CwMtx matrix and vector math library
// Copyright (C) 1999-2001 Harry Kuiper
// Copyright (C) 2000 Will DeVore (template conversion)
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
#include <iostream>
#include <iomanip>
// CWMatrix class
// This library was designed to mirror as closely as possible the
// notation used in mathematical writing. A matrix is indexed:
// matMatrixName[row][col].
// CAUTION!!!
// This matrix library was implemented with emphasis on
// speed. Consequently no attempts were made to trap and report
// errors. It is left entirely to the user to write code that does not
// cause errors within the matrix routines.
namespace CwMtx
{
using std::ostream;
// matrix status values
enum { N_NOTALLOCATED, N_ALLOCATED, N_MAPPED };
// Classes that create unity and zero "objects". The ones directly
// below work only for template arguments that are basic (numerical)
// types that can be initialised by a literal 0 or 1.
template <class T> class CWTUnity
{
public:
operator T() { return 1; }
};
template <class T> class CWTZero
{
public:
operator T() { return 0; }
};
// This template defaults to double. Most of the time this template
// will be working with math functions that only work with
// doubles. For example, the transcendental function sin(x) takes
// and returns a double which would force the compiler to convert
// back and forth from some other data type to a double.
// prefix mat
template < class T = double >
class CWTMatrix
{
public:
typedef T element;
// creates a matrix, does NOT allocate rows and columns
CWTMatrix();
// creates a matrix, allocates rows and columns
CWTMatrix(unsigned, unsigned);
CWTMatrix(const CWTMatrix &);
// sub-matrix mapped into another
CWTMatrix(const CWTMatrix &, unsigned, unsigned, unsigned, unsigned);
// removes matrix elements from free store
~CWTMatrix() { deallocate(); };
// allocates rows and colums
void dimension(unsigned, unsigned);
// maps matrix into another
void mapInto(const CWTMatrix&, unsigned, unsigned, unsigned, unsigned);
// reverses the effect of dimension() and mapInto()
void deallocate();
int getStatus() const { return m_nMatStatus; };
unsigned getRows() const { return m_crow; };
unsigned getCols() const { return m_ccol; };
// basic matrix operations
// returns a row of modifyable elements
T* operator [](unsigned irow) { return m_rgrow[irow]; };
// returns a row of non-modifyable elements
const T* operator [](unsigned irow) const { return m_rgrow[irow]; };
CWTMatrix operator +(const CWTMatrix &) const;
CWTMatrix operator -(const CWTMatrix &) const;
CWTMatrix operator -() const;
CWTMatrix operator *(const T &) const;
CWTMatrix operator *(const CWTMatrix &) const;
// Interesting note here. Because we are defining the "/" operator
// we can't expect to use it inside the definition unless we
// safely restrict it to operating on constants. Without the
// parens the operator does the "* 1" first then does the
// "/value" which then leads to calling the "/" etc... With the
// parens, the intended scalar operation, "1/value", occurs
// first then the Matrix/Scalar operations occur. If the parens
// are missing in the operator below, an ifinite loop
// occurs. -----------------------------------------------V----------V
CWTMatrix operator /(const T &value) const
{
// NOTE: This used to work with g++ <= 3.2.
// return (*this)*static_cast<const T &>(CWTUnity<T>()/value);
T tTmp = CWTUnity<T>();
tTmp /= value;
return (*this)*tTmp;
}
// not inherited
CWTMatrix & operator =(const CWTMatrix &);
CWTMatrix & operator +=(const CWTMatrix &);
CWTMatrix & operator -=(const CWTMatrix &);
CWTMatrix & operator *=(const T &);
CWTMatrix & operator /=(const T &value)
{
// NOTE: This used to work with g++ <= 3.2.
// return (*this) *= static_cast<const T &>(CWTUnity<T>()/value);
T tTmp = CWTUnity<T>();
tTmp /= value;
return (*this) *= tTmp;
}
int operator ==(const CWTMatrix &) const;
int operator !=(const CWTMatrix &mat) const { return !( (*this) == mat ); }
// stores CWMatrix + CWMatrix in this
void storeSum(const CWTMatrix &, const CWTMatrix &);
// stores CWMatrix*CWMatrix in this
void storeProduct(const CWTMatrix &, const CWTMatrix &);
// stores transpose of CWMatrix in this
void storeTranspose(const CWTMatrix &);
// stores CWMatrix at indicated position in this
void storeAtPosition(unsigned, unsigned, const CWTMatrix &);
// fills the whole array with a value.
void fill(const T &);
void interchangeRows(unsigned, unsigned);
void addRowToRow(unsigned, unsigned, const T & = CWTUnity<T>());
void multiplyRow(unsigned, const T &);
private:
// initializes data members
void initialize();
// we keep the data structures used for CWMatrix implementation
// private
...
|