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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
|
#ifndef MATRIX_H
#define MATRIX_H
/**
* @file Matrix.h
* @author breadbread1984 <breadbread1984@163.com>
* @date Sat Jul 21 15:12:00 2012
*
* @brief The Matrix template class written for simplify the matrix manipulation.
*
* @copyright GPLv3
*/
#include <cassert>
using namespace std;
template<typename T>
class Matrix {
int rows,cols;
T * data;
public:
/**
* Default constructor.
*/
Matrix();
/**
* Copy constructor.
*
* @param m the Matrix object to be copied.
*/
Matrix(const Matrix<T> & m);
/**
* Constructor.
*
* @param rows the row number of the Matrix object.
* @param cols the column number of the Matrix object.
*/
Matrix(const int rows,const int cols);
/**
* Destructor
*/
~Matrix();
/**
* Get element from the matrix.
*
* @param row the row number of the element.
* @param col the column number of the element.
*/
T & operator()(int row,int col) const;
/**
* Get the row number of the matrix.
*/
int size1() const;
/**
* Get the column number of the matrix.
*/
int size2() const;
/**
* Get the remaining matrix after slicing off a row and a column.
*
* @param rows the row to be sliced.
* @param cols the column to be sliced.
*/
Matrix<T> minor(int rows,int cols) const;
/**
* Assign the value of another matrix to the current one.
*
* @param b the other matrix whose value will be copied
*/
Matrix<T> & operator=(const Matrix<T> & b);
/**
* Multiplication of two matrices.
*/
template<typename N> friend Matrix<N> operator*(const Matrix<N> & a,const Matrix<N> & b);
/**
* The inverse of a matrix.
*/
template<typename N> friend Matrix<N> inv(const Matrix<N> & a);
/**
* The transpos of a matrix.
*/
template<typename N> friend Matrix<N> trans(const Matrix<N> & a);
/**
* The determinant of a matrix.
*/
template<typename N> friend N det(const Matrix<N> & a);
};
template<typename T>
Matrix<T>::Matrix()
:rows(0),cols(0),data(0)
{
}
template<typename T>
Matrix<T>::Matrix(const Matrix<T> & m)
:rows(m.rows),cols(m.cols),data(0)
{
T * tmp = new T[rows * cols]();
for(int i = 0 ; i < rows * cols ; i++) tmp[i] = m.data[i];
data = tmp;
}
template<typename T>
Matrix<T>::Matrix(const int row,const int col)
:rows(row),cols(col)
{
#ifndef NDEBUG
assert(rows > 0 && cols > 0);
#endif
data = new T[rows * cols]();
}
template<typename T>
Matrix<T>::~Matrix()
{
if(data) delete[] data;
}
template<typename T>
T & Matrix<T>::operator()(int row,int col) const
{
#ifndef NDEBUG
assert(0 <= row && row < rows);
assert(0 <= col && col < cols);
#endif
return data[row * cols + col];
}
template<typename T>
int Matrix<T>::size1() const
{
return rows;
}
template<typename T>
int Matrix<T>::size2() const
{
return cols;
}
template<typename T>
Matrix<T> Matrix<T>::minor(int r,int c) const
{
#ifndef NDEBUG
assert(0 <= r && r < rows);
assert(0 <= c && c < cols);
assert(rows >= 2 && cols >= 2);
#endif
Matrix<T> retVal(rows - 1,cols - 1);
for(int h = 0 ; h < rows ; h++)
for(int w = 0 ; w < cols ; w++) {
if(h == r || w == c) continue;
int newrows = (h >= r)?h-1:h;
int newcols = (w >= c)?w-1:w;
retVal(newrows,newcols) = data[h * cols + w];
}
return retVal;
}
template<typename T>
Matrix<T> & Matrix<T>::operator=(const Matrix<T> & b)
{
rows = b.rows; cols = b.cols;
T * tmp = new T[rows * cols]();
for(int i = 0; i < rows * cols ; i++) tmp[i] = b.data[i];
if(data) delete[] data;
data = tmp;
return *this;
}
template<typename T>
Matrix<T> operator*(const Matrix<T> & a,const Matrix<T> & b)
{
#ifndef NDEBUG
assert(a.cols == b.rows);
#endif
Matrix<T> retVal(a.rows,b.cols);
for(int h = 0 ; h < a.rows ; h++)
for(int w = 0 ; w < b.cols ; w++) {
T sum = 0;
for(int i = 0 ; i < a.cols ; i++)
sum += a(h,i) * b(i,w);
retVal(h,w) = sum;
}
return retVal;
}
template<typename T>
Matrix<T> inv(const Matrix<T> & m)
{
#ifndef NDEBUG
assert(m.cols && m.rows);
assert(m.cols == m.rows); //必须是方阵
assert(m.cols > 1 && m.rows > 1);
assert(0 != det(m));
#endif
Matrix<T> retVal = m;
T * a = retVal.data;
int n = retVal.rows;
int * is= new int[n];
int * js= new int[n];
for (int k=0; k<=n-1; k++) {
T d=0.0;
for (int i=k; i<=n-1; i++)
for (int j=k; j<=n-1; j++) {
int l=i*n+j; T p=((a[l] > 0)?a[l]:-a[l]);
if (p>d) { d=p; is[k]=i; js[k]=j;}
}
if (d + 1.0 == 1.0) {
delete[] is; delete[] js;
assert(0);
}
if (is[k]!=k)
for (int j=0; j<=n-1; j++) {
int u=k*n+j; int v=is[k]*n+j;
T p=a[u]; a[u]=a[v]; a[v]=p;
}
if (js[k]!=k)
for (int i=0; i<=n-1; i++) {
int u=i*n+k; int v=i*n+js[k];
T p=a[u]; a[u]=a[v]; a[v]=p;
}
int l = k*n+k;
a[l]=1.0/a[l];
for (int j=0; j<=n-1; j++)
if (j!=k) { int u=k*n+j; a[u]=a[u]*a[l];}
for (int i=0; i<=n-1; i++)
if (i!=k)
for (int j=0; j<=n-1; j++)
if (j!=k) {
int u=i*n+j;
a[u]=a[u]-a[i*n+k]*a[k*n+j];
}
for (int i=0; i<=n-1; i++)
if (i!=k) {
int u=i*n+k;
a[u]=-a[u]*a[l];
}
}
for (int k=n-1; k>=0; k--) {
if (js[k]!=k)
for (int j=0; j<=n-1; j++) {
int u=k*n+j; int v=js[k]*n+j;
T p=a[u]; a[u]=a[v]; a[v]=p;
}
if (is[k]!=k)
for (int i=0; i<=n-1; i++) {
int u=i*n+k; int v=i*n+is[k];
T p=a[u]; a[u]=a[v]; a[v]=p;
}
}
delete[] is;
delete[] js;
return retVal;
}
template<typename T>
Matrix<T> trans(const Matrix<T> & m)
{
#ifndef NDEBUG
assert(m.cols && m.rows);
#endif
Matrix<T> retVal(m.cols,m.rows);
for(int h = 0 ; h < m.rows ; h++)
for(int w = 0 ; w < m.cols ; w++)
retVal(w,h) = m(h,w);
return retVal;
}
template<typename T>
T det(const Matrix<T> & m)
{
#ifndef NDEBUG
assert(m.cols && m.rows);
assert(m.cols == m.rows);
#endif
double retVal = 0;
if(m.rows == 1) retVal = m(0,0);
else if(m.rows == 2) retVal = m(0,0) * m(1,1) - m(1,0) * m(0,1);
else {
for(int w = 0 ; w < m.cols ; w++) {
Matrix<T> minor = m.minor(0,w);
retVal += ((w+1)%2 + (w+1)%2 - 1) * m(0,w) * det(minor);
}
}
return retVal;
}
#endif
|