2017-03-18 01:30:31 +01:00
|
|
|
#include "matrix.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define abs(x) ((x)<0 ? -(x) : (x))
|
|
|
|
|
|
|
|
Matrix::~Matrix()
|
|
|
|
{
|
|
|
|
if (isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
delete[] _m;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix::Matrix(size_t h, size_t w)
|
|
|
|
{
|
|
|
|
_h = h; _w = w;
|
|
|
|
|
|
|
|
if (isNull())
|
|
|
|
_m = 0;
|
|
|
|
else
|
|
|
|
_m = new double[_h * _w];
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix::Matrix(const Matrix& M)
|
|
|
|
{
|
|
|
|
_h = M._h; _w = M._w;
|
|
|
|
|
|
|
|
if (isNull())
|
|
|
|
_m = 0;
|
|
|
|
else
|
|
|
|
_m = new double[_h * _w];
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _h; i++)
|
|
|
|
for (size_t j = 0; j < _w; j++)
|
|
|
|
m(i,j) = M.m(i,j);
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix &Matrix::operator=(const Matrix &M)
|
|
|
|
{
|
2019-07-10 23:08:58 +02:00
|
|
|
if (this != &M) {
|
|
|
|
if (_h != M._h || _w != M._w) {
|
|
|
|
if (!isNull())
|
|
|
|
delete[] _m;
|
|
|
|
|
|
|
|
_h = M._h; _w = M._w;
|
|
|
|
if (isNull())
|
|
|
|
_m = 0;
|
|
|
|
else
|
|
|
|
_m = new double[_h * _w];
|
|
|
|
}
|
2017-03-18 01:30:31 +01:00
|
|
|
|
2019-07-10 23:08:58 +02:00
|
|
|
for (size_t i = 0; i < _h; i++)
|
|
|
|
for (size_t j = 0; j < _w; j++)
|
|
|
|
m(i,j) = M.m(i,j);
|
|
|
|
}
|
2017-03-18 01:30:31 +01:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Matrix::eliminate(double epsilon)
|
|
|
|
{
|
|
|
|
size_t i, j, k, maxrow;
|
|
|
|
double temp;
|
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < _h; i++) {
|
|
|
|
maxrow = i;
|
|
|
|
for (j = i+1; j < _h; j++)
|
|
|
|
if (abs(m(j, i)) > abs(m(maxrow, i)))
|
|
|
|
maxrow = j;
|
|
|
|
for (j = 0; j < _w; j++) {
|
|
|
|
temp = m(i, j);
|
|
|
|
m(i, j) = m(maxrow, j);
|
|
|
|
m(maxrow, j) = temp;
|
|
|
|
}
|
|
|
|
if (abs(m(i, i)) <= epsilon)
|
|
|
|
return false;
|
|
|
|
for (j = i+1; j<_h; j++) {
|
|
|
|
temp = m(j, i) / m(i, i);
|
|
|
|
for (k = i; k < _w; k++)
|
|
|
|
m(j, k) -= m(i, k) * temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = _h-1; i < i+1; i--) {
|
|
|
|
temp = m(i, i);
|
|
|
|
for (j = 0; j < i; j++)
|
|
|
|
for (k = _w-1; k >= i; k--)
|
|
|
|
m(j, k) -= m(i, k) * m(j, i) / temp;
|
|
|
|
m(i, i) /= temp;
|
|
|
|
for (j = _h; j < _w; j++)
|
|
|
|
m(i, j) /= temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix Matrix::augemented(const Matrix &M) const
|
|
|
|
{
|
|
|
|
if (_h != M._h)
|
|
|
|
return Matrix();
|
|
|
|
|
|
|
|
Matrix A(_h, _w + M._w);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _h; i++)
|
|
|
|
for (size_t j = 0; j < _w; j++)
|
|
|
|
A.m(i, j) = m(i, j);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < _h; i++)
|
|
|
|
for (size_t j = _w; j < A._w; j++)
|
|
|
|
A.m(i, j) = M.m(i, j-_w);
|
|
|
|
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Matrix::zeroize()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < _h * _w; i++)
|
|
|
|
_m[i] = 0;
|
|
|
|
}
|
2017-08-14 11:16:48 +02:00
|
|
|
|
2018-02-13 23:03:18 +01:00
|
|
|
#ifndef QT_NO_DEBUG
|
2017-08-14 11:16:48 +02:00
|
|
|
QDebug operator<<(QDebug dbg, const Matrix &matrix)
|
|
|
|
{
|
|
|
|
dbg.nospace() << "Matrix(" << endl;
|
|
|
|
for (size_t i = 0; i < matrix.h(); i++) {
|
|
|
|
for (size_t j = 0; j < matrix.w(); j++)
|
|
|
|
dbg << "\t" << matrix.m(i, j);
|
|
|
|
dbg << endl;
|
|
|
|
}
|
|
|
|
dbg << ")";
|
|
|
|
|
2017-08-15 15:13:34 +02:00
|
|
|
return dbg.space();
|
2017-08-14 11:16:48 +02:00
|
|
|
}
|
2018-02-13 23:03:18 +01:00
|
|
|
#endif // QT_NO_DEBUG
|