PIVector2D and PIMathMatrix
completed and optimized git-svn-id: svn://db.shs.com.ru/pip@657 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
@@ -73,63 +73,110 @@ public:
|
|||||||
class Row {
|
class Row {
|
||||||
friend class PIVector2D<T>;
|
friend class PIVector2D<T>;
|
||||||
private:
|
private:
|
||||||
inline Row(PIVector2D<T> * p, size_t row) : p_(p) {st_ = p_->cols_ * row;}
|
inline Row(PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {st_ = p->cols_ * row; sz_ = p->cols_;}
|
||||||
PIVector2D<T> * p_;
|
PIVector<T> * p_;
|
||||||
size_t st_;
|
size_t st_, sz_;
|
||||||
public:
|
public:
|
||||||
inline size_t size() const {return p_->cols_;}
|
inline size_t size() const {return sz_;}
|
||||||
inline T & operator [](size_t index) {return p_->mat[st_ + index];}
|
inline T & operator [](size_t index) {return (*p_)[st_ + index];}
|
||||||
inline const T & operator [](size_t index) const {return p_->mat[st_ + index];}
|
inline const T & operator [](size_t index) const {return (*p_)[st_ + index];}
|
||||||
inline T * data(size_t index = 0) {return p_->mat.data(st_ + index);}
|
inline T * data(size_t index = 0) {return p_->data(st_ + index);}
|
||||||
inline const T * data(size_t index = 0) const {return p_->mat.data(st_ + index);}
|
inline const T * data(size_t index = 0) const {return p_->data(st_ + index);}
|
||||||
inline Row & operator =(const Row & other) {
|
inline Row & operator =(const Row & other) {
|
||||||
if (p_ == other.p_ && st_ == other.st_) return *this;
|
if (p_ == other.p_ && st_ == other.st_) return *this;
|
||||||
size_t sz = piMin<size_t>(p_->cols_, other.p_->cols_);
|
size_t sz = piMin<size_t>(sz_, other.sz_);
|
||||||
p_->copyRow(st_, other.data(), sz);
|
copyRow(st_, other.data(), sz, p_);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
inline Row & operator =(const PIVector<T> & other) {
|
inline Row & operator =(const PIVector<T> & other) {
|
||||||
size_t sz = piMin<size_t>(p_->cols_, other.size());
|
size_t sz = piMin<size_t>(sz, other.size());
|
||||||
p_->copyRow(st_, other.data(), sz);
|
copyRow(st_, other.data(), sz, p_);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
inline PIVector<T> toVector() const {return PIVector<T>(p_->mat.data(st_), p_->cols_);}
|
inline PIVector<T> toVector() const {return PIVector<T>(p_->data(st_), sz_);}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Col {
|
||||||
|
friend class PIVector2D<T>;
|
||||||
|
private:
|
||||||
|
inline Col(PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {step_ = p->cols_; row_ = row; sz_ = p->rows_;}
|
||||||
|
PIVector<T> * p_;
|
||||||
|
size_t step_, row_, sz_;
|
||||||
|
public:
|
||||||
|
inline size_t size() const {return sz_;}
|
||||||
|
inline T & operator [](size_t index) {return (*p_)[index * step_ + row_];}
|
||||||
|
inline const T & operator [](size_t index) const {return (*p_)[index * step_ + row_];}
|
||||||
|
inline T * data(size_t index = 0) {return p_->data(index * step_ + row_);}
|
||||||
|
inline const T * data(size_t index = 0) const {return p_->data(index * step_ + row_);}
|
||||||
|
inline Col & operator =(const Col & other) {
|
||||||
|
if (p_ == other.p_ && row_ == other.row_) return *this;
|
||||||
|
size_t sz = piMin<size_t>(sz_, other.sz_);
|
||||||
|
for (int i=0; i<sz; ++i) (*p_)[i * step_ + row_] = other[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
inline Row & operator =(const PIVector<T> & other) {
|
||||||
|
size_t sz = piMin<size_t>(sz_, other.size());
|
||||||
|
for (int i=0; i<sz; ++i) (*p_)[i * step_ + row_] = other[i];
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
inline PIVector<T> toVector() const {
|
||||||
|
PIVector<T> ret;
|
||||||
|
ret.reserve(sz_);
|
||||||
|
for (size_t i=0; i<sz_; i++) ret << (*p_)[i * step_ + row_];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class RowConst {
|
class RowConst {
|
||||||
friend class PIVector2D<T>;
|
friend class PIVector2D<T>;
|
||||||
private:
|
private:
|
||||||
inline RowConst(const PIVector2D<T> * p, size_t row) : p_(p) {st_ = p_->cols_ * row;}
|
inline RowConst(const PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {st_ = p->cols_ * row; sz_ = p->cols_;}
|
||||||
const PIVector2D<T> * p_;
|
const PIVector<T> * p_;
|
||||||
size_t st_;
|
size_t st_, sz_;
|
||||||
public:
|
public:
|
||||||
inline size_t size() const {return p_->cols_;}
|
inline size_t size() const {return sz_;}
|
||||||
inline const T & operator [](size_t index) const {return p_->mat[st_ + index];}
|
inline const T & operator [](size_t index) const {return (*p_)[st_ + index];}
|
||||||
inline const T * data(size_t index = 0) const {return p_->mat.data(st_ + index);}
|
inline const T * data(size_t index = 0) const {return p_->data(st_ + index);}
|
||||||
inline PIVector<T> toVector() const {return PIVector<T>(p_->mat.data(st_), p_->cols_);}
|
inline PIVector<T> toVector() const {return PIVector<T>(p_->data(st_), sz_);}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class ColConst {
|
||||||
|
friend class PIVector2D<T>;
|
||||||
|
private:
|
||||||
|
inline ColConst(const PIVector2D<T> * p, size_t row) : p_(&(p->mat)) {step_ = p->cols_; row_ = row; sz_ = p->rows_;}
|
||||||
|
const PIVector<T> * p_;
|
||||||
|
size_t step_, row_, sz_;
|
||||||
|
public:
|
||||||
|
inline size_t size() const {return p_->rows_;}
|
||||||
|
inline const T & operator [](size_t index) const {return (*p_)[index * step_ + row_];}
|
||||||
|
inline const T * data(size_t index = 0) const {return p_->data(index * step_ + row_);}
|
||||||
|
inline PIVector<T> toVector() const {
|
||||||
|
PIVector<T> ret;
|
||||||
|
ret.reserve(sz_);
|
||||||
|
for (int i=0; i<rows_; i++) ret << (*p_)[i * step_ + row_];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
inline T & element(size_t row, size_t col) {return mat[row * cols_ + col];}
|
||||||
|
inline const T & element(size_t row, size_t col) const {return mat[row * cols_ + col];}
|
||||||
inline Row operator[](size_t index) {return Row(this, index);}
|
inline Row operator[](size_t index) {return Row(this, index);}
|
||||||
inline RowConst operator[](size_t index) const {return RowConst(this, index);}
|
inline RowConst operator[](size_t index) const {return RowConst(this, index);}
|
||||||
inline T * data(size_t index = 0) {return mat.data(index);}
|
inline T * data(size_t index = 0) {return mat.data(index);}
|
||||||
inline const T * data(size_t index = 0) const {return mat.data(index);}
|
inline const T * data(size_t index = 0) const {return mat.data(index);}
|
||||||
|
|
||||||
inline Row row(size_t index) {return Row(this, index);}
|
inline Row row(size_t index) {return Row(this, index);}
|
||||||
inline PIVector<T> col(size_t index) {
|
inline RowConst row(size_t index) const {return RowConst(this, index);}
|
||||||
PIVector<T> ret;
|
inline Col col(size_t index) {return Col(this, index);}
|
||||||
ret.reserve(rows_);
|
inline ColConst col(size_t index) const {return ColConst(this, index);}
|
||||||
for (int i=0; i<rows_; i++) ret << mat[i*cols_+index];
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
inline PIVector2D<T> & setRow(size_t row, const Row & other) {
|
inline PIVector2D<T> & setRow(size_t row, const Row & other) {
|
||||||
size_t sz = piMin<size_t>(cols_, other.p_->cols_);
|
size_t sz = piMin<size_t>(cols_, other.p_->cols_);
|
||||||
copyRow(cols_ * row, other.data(), sz);
|
copyRow(cols_ * row, other.data(), sz, &mat);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
inline PIVector2D<T> & setRow(size_t row, const PIVector<T> & other) {
|
inline PIVector2D<T> & setRow(size_t row, const PIVector<T> & other) {
|
||||||
size_t sz = piMin<size_t>(cols_, other.size());
|
size_t sz = piMin<size_t>(cols_, other.size());
|
||||||
copyRow(cols_ * row, other.data(), sz);
|
copyRow(cols_ * row, other.data(), sz, &mat);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,12 +203,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
inline void copyRow(size_t start, const T * data, size_t size) {
|
inline void copyRow(size_t start, const T * data, size_t size, PIVector<T> * dst) {
|
||||||
for (size_t i = 0; i < size; i++)
|
for (size_t i = 0; i < size; i++)
|
||||||
mat[start + i] = data[i];
|
(*dst)[start + i] = data[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
//private:
|
|
||||||
size_t rows_, cols_;
|
size_t rows_, cols_;
|
||||||
PIVector<T> mat;
|
PIVector<T> mat;
|
||||||
};
|
};
|
||||||
@@ -187,7 +233,7 @@ inline PICout operator <<(PICout s, const PIVector2D<T> & v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define __PIVECTOR2D_SIMPLE_TYPE__(T) \
|
#define __PIVECTOR2D_SIMPLE_TYPE__(T) \
|
||||||
template<> inline void PIVector2D<T>::copyRow(size_t start, const T * data, size_t size) {memcpy(mat.data(start), data, size * sizeof(T));} \
|
template<> inline void PIVector2D<T>::copyRow(size_t start, const T * data, size_t size, PIVector<T> * dst) {memcpy(dst->data(start), data, size * sizeof(T));} \
|
||||||
template<> inline PIVector2D<T> & PIVector2D<T>::_resizeRaw(size_t r, size_t c) {rows_ = r; cols_ = c; mat._resizeRaw(r*c); return *this;}
|
template<> inline PIVector2D<T> & PIVector2D<T>::_resizeRaw(size_t r, size_t c) {rows_ = r; cols_ = c; mat._resizeRaw(r*c); return *this;}
|
||||||
|
|
||||||
__PIVECTOR2D_SIMPLE_TYPE__(bool)
|
__PIVECTOR2D_SIMPLE_TYPE__(bool)
|
||||||
|
|||||||
@@ -30,16 +30,16 @@
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline bool _PIMathMatrixNullCompare(const T v) {
|
inline bool _PIMathMatrixNullCompare(const T v) {
|
||||||
return (piAbs(v) < T(1E-100));
|
return (piAbs(v) < T(1E-200));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool _PIMathMatrixNullCompare<complexf >(const complexf v) {
|
inline bool _PIMathMatrixNullCompare<complexf >(const complexf v) {
|
||||||
return (abs(v) < float(1E-100));
|
return (abs(v) < float(1E-200));
|
||||||
}
|
}
|
||||||
template<>
|
template<>
|
||||||
inline bool _PIMathMatrixNullCompare<complexd >(const complexd v) {
|
inline bool _PIMathMatrixNullCompare<complexd >(const complexd v) {
|
||||||
return (abs(v) < double(1E-100));
|
return (abs(v) < double(1E-200));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -135,11 +135,6 @@ public:
|
|||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
for (uint j = 0; j < Cols; ++j) if (smat.m[j][i] != 0) ndet = false;
|
|
||||||
if (ndet) {
|
|
||||||
if (ok != 0) *ok = false;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (uint i = 0; i < Cols; ++i) {
|
for (uint i = 0; i < Cols; ++i) {
|
||||||
crow = i;
|
crow = i;
|
||||||
@@ -150,7 +145,7 @@ public:
|
|||||||
for (uint k = i; k < Cols; ++k) smat.m[k][j] -= mul * smat.m[k][i];
|
for (uint k = i; k < Cols; ++k) smat.m[k][j] -= mul * smat.m[k][i];
|
||||||
}
|
}
|
||||||
if (i < Cols - 1) {
|
if (i < Cols - 1) {
|
||||||
if (fabs(smat.m[i+1][i+1]) < Type(1E-100)) {
|
if (fabs(smat.m[i+1][i+1]) < Type(1E-200)) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -177,11 +172,6 @@ public:
|
|||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
for (uint j = 0; j < Cols; ++j) if (smat.m[j][i] != 0) ndet = false;
|
|
||||||
if (ndet) {
|
|
||||||
if (ok != 0) *ok = false;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (uint i = 0; i < Cols; ++i) {
|
for (uint i = 0; i < Cols; ++i) {
|
||||||
crow = i;
|
crow = i;
|
||||||
@@ -197,7 +187,7 @@ public:
|
|||||||
}
|
}
|
||||||
//cout << i << endl << smat << endl;
|
//cout << i << endl << smat << endl;
|
||||||
if (i < Cols - 1) {
|
if (i < Cols - 1) {
|
||||||
if (fabs(smat.m[i+1][i+1]) < Type(1E-100)) {
|
if (fabs(smat.m[i+1][i+1]) < Type(1E-200)) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -337,26 +327,25 @@ class PIP_EXPORT PIMathMatrix : public PIVector2D<Type> {
|
|||||||
typedef PIVector2D<Type> _V2D;
|
typedef PIVector2D<Type> _V2D;
|
||||||
typedef PIMathMatrix<Type> _CMatrix;
|
typedef PIMathMatrix<Type> _CMatrix;
|
||||||
typedef PIMathVector<Type> _CMCol;
|
typedef PIMathVector<Type> _CMCol;
|
||||||
typedef PIMathVector<Type> _CMRow;
|
|
||||||
public:
|
public:
|
||||||
PIMathMatrix(const uint cols = 3, const uint rows = 3) {resize(cols, rows);}
|
PIMathMatrix(const uint cols = 3, const uint rows = 3) {resize(cols, rows);}
|
||||||
PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> & val) {resize(cols, rows); int i=0; PIMM_FOR_I(c, r) (*this)[r][c] = val[i++];}
|
PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> & val) {resize(cols, rows); int i=0; PIMM_FOR_I(c, r) _V2D::element(r, c) = val[i++];}
|
||||||
PIMathMatrix(const PIVector<PIVector<Type> > & val) {_V2D::cols_ = _V2D::rows_ = 0; if(!val.isEmpty()) {resize(val[0].size(), val.size()); PIMM_FOR_I(c, r) (*this)[r][c] = val[r][c];}}
|
PIMathMatrix(const PIVector<PIVector<Type> > & val) {_V2D::cols_ = _V2D::rows_ = 0; if(!val.isEmpty()) {resize(val[0].size(), val.size()); PIMM_FOR_I(c, r) _V2D::element(r, c) = val[r][c];}}
|
||||||
PIMathMatrix(const PIVector2D<Type> & val) {_V2D::cols_ = _V2D::rows_ = 0; if(!val.isEmpty()) {resize(val.cols(), val.rows()); PIMM_FOR_I(c, r) (*this)[r][c] = val[r][c];}}
|
PIMathMatrix(const PIVector2D<Type> & val) {_V2D::cols_ = _V2D::rows_ = 0; if(!val.isEmpty()) {resize(val.cols(), val.rows()); PIMM_FOR_I(c, r) _V2D::element(r, c) = val.element(r, c);}}
|
||||||
|
|
||||||
static _CMatrix identity(const uint cols, const uint rows) {_CMatrix tm(cols, rows); for (uint r = 0; r < rows; ++r) for (uint c = 0; c < cols; ++c) tm[r][c] = (c == r ? Type(1) : Type(0)); return tm;}
|
static _CMatrix identity(const uint cols, const uint rows) {_CMatrix tm(cols, rows); for (uint r = 0; r < rows; ++r) for (uint c = 0; c < cols; ++c) tm.element(r, c) = (c == r ? Type(1) : Type(0)); return tm;}
|
||||||
static _CMatrix matrixRow(const PIMathVector<Type> & val) {return _CMatrix(val.size(), 1, val.toVector());}
|
static _CMatrix matrixRow(const PIMathVector<Type> & val) {return _CMatrix(val.size(), 1, val.toVector());}
|
||||||
static _CMatrix matrixCol(const PIMathVector<Type> & val) {return _CMatrix(1, val.size(), val.toVector());}
|
static _CMatrix matrixCol(const PIMathVector<Type> & val) {return _CMatrix(1, val.size(), val.toVector());}
|
||||||
|
|
||||||
_CMatrix & resize(const uint cols, const uint rows, const Type & new_value = Type()) {_V2D::_resizeRaw(rows, cols); PIMM_FOR_A(i) _V2D::mat[i] = new_value; return *this;}
|
_CMatrix & resize(const uint cols, const uint rows, const Type & new_value = Type()) {_V2D::_resizeRaw(rows, cols); PIMM_FOR_A(i) _V2D::mat[i] = new_value; return *this;}
|
||||||
_CMatrix & swapCols(uint r0, uint r1) {Type t; PIMM_FOR_C(i) {t = (*this)[i][r0]; (*this)[i][r0] = (*this)[i][r1]; (*this)[i][r1] = t;} return *this;}
|
_CMatrix & swapCols(uint r0, uint r1) {PIMM_FOR_C(i) {piSwap(_V2D::element(i, r0), _V2D::element(i, r1));} return *this;}
|
||||||
_CMatrix & swapRows(uint c0, uint c1) {Type t; PIMM_FOR_R(i) {t = (*this)[c0][i]; (*this)[c0][i] = (*this)[c1][i]; (*this)[c1][i] = t;} return *this;}
|
_CMatrix & swapRows(uint c0, uint c1) {PIMM_FOR_R(i) {piSwap(_V2D::element(c0, i), _V2D::element(c1, i));} return *this;}
|
||||||
_CMatrix & fill(const Type & v) {PIMM_FOR_A(i) _V2D::mat[i] = v; return *this;}
|
_CMatrix & fill(const Type & v) {PIMM_FOR_A(i) _V2D::mat[i] = v; return *this;}
|
||||||
bool isSquare() const {return _V2D::cols_ == _V2D::rows_;}
|
bool isSquare() const {return _V2D::cols_ == _V2D::rows_;}
|
||||||
bool isIdentity() const {PIMM_FOR(c, r) if ((c == r) ? (*this)[c][r] != Type(1) : (*this)[c][r] != Type(0)) return false; return true;}
|
bool isIdentity() const {PIMM_FOR(c, r) if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0)) return false; return true;}
|
||||||
bool isNull() const {PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false; return true;}
|
bool isNull() const {PIMM_FOR_A(i) if (_V2D::mat[i] != Type(0)) return false; return true;}
|
||||||
|
|
||||||
_CMatrix & operator =(const PIVector<PIVector<Type> > & v) {*this = PIVector2D<Type>(v); return *this;}
|
_CMatrix & operator =(const PIVector<PIVector<Type> > & v) {*this = _CMatrix(v); return *this;}
|
||||||
bool operator ==(const _CMatrix & sm) const {PIMM_FOR_A(i) if (_V2D::mat[i] != sm.mat[i]) return false; return true;}
|
bool operator ==(const _CMatrix & sm) const {PIMM_FOR_A(i) if (_V2D::mat[i] != sm.mat[i]) return false; return true;}
|
||||||
bool operator !=(const _CMatrix & sm) const {return !(*this == sm);}
|
bool operator !=(const _CMatrix & sm) const {return !(*this == sm);}
|
||||||
void operator +=(const _CMatrix & sm) {PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i];}
|
void operator +=(const _CMatrix & sm) {PIMM_FOR_A(i) _V2D::mat[i] += sm.mat[i];}
|
||||||
@@ -380,7 +369,7 @@ public:
|
|||||||
for (uint c = 0; c < _V2D::cols_; ++c)
|
for (uint c = 0; c < _V2D::cols_; ++c)
|
||||||
for (uint r = 0; r < _V2D::rows_; ++r)
|
for (uint r = 0; r < _V2D::rows_; ++r)
|
||||||
if (r == c)
|
if (r == c)
|
||||||
ret *= m[r][c];
|
ret *= m.element(r, c);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -391,7 +380,7 @@ public:
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||||
ret += (*this)[i][i];
|
ret += _V2D::element(i, i);
|
||||||
}
|
}
|
||||||
if (ok != 0) *ok = true;
|
if (ok != 0) *ok = true;
|
||||||
return ret;
|
return ret;
|
||||||
@@ -408,12 +397,7 @@ public:
|
|||||||
Type mul;
|
Type mul;
|
||||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||||
ndet = true;
|
ndet = true;
|
||||||
for (uint j = 0; j < _V2D::rows_; ++j) if (smat.mat[i][j] != 0) ndet = false;
|
for (uint j = 0; j < _V2D::rows_; ++j) if (smat.element(i, j) != 0) ndet = false;
|
||||||
if (ndet) {
|
|
||||||
if (ok != 0) *ok = false;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
for (uint j = 0; j < _V2D::cols_; ++j) if (smat.mat[j][i] != 0) ndet = false;
|
|
||||||
if (ndet) {
|
if (ndet) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -421,14 +405,14 @@ public:
|
|||||||
}
|
}
|
||||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||||
crow = i;
|
crow = i;
|
||||||
while (smat.mat[i][i] == Type(0))
|
while (smat.element(i, i) == Type(0))
|
||||||
smat.swapRows(i, ++crow);
|
smat.swapRows(i, ++crow);
|
||||||
for (uint j = i + 1; j < _V2D::rows_; ++j) {
|
for (uint j = i + 1; j < _V2D::rows_; ++j) {
|
||||||
mul = smat.mat[i][j] / smat.mat[i][i];
|
mul = smat.element(i, j) / smat.element(i, i);
|
||||||
for (uint k = i; k < _V2D::cols_; ++k) smat.mat[k][j] -= mul * smat.mat[k][i];
|
for (uint k = i; k < _V2D::cols_; ++k) smat.element(k, j) -= mul * smat.element(k, i);
|
||||||
}
|
}
|
||||||
if (i < _V2D::cols_ - 1) {
|
if (i < _V2D::cols_ - 1) {
|
||||||
if (fabs(smat.mat[i+1][i+1]) < Type(1E-100)) {
|
if (_PIMathMatrixNullCompare(smat.element(i+1, i+1))) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@@ -450,12 +434,7 @@ public:
|
|||||||
Type mul, iddiv;
|
Type mul, iddiv;
|
||||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||||
ndet = true;
|
ndet = true;
|
||||||
for (uint j = 0; j < _V2D::rows_; ++j) if (smat[i][j] != Type(0)) ndet = false;
|
for (uint j = 0; j < _V2D::rows_; ++j) if (smat.element(i, j) != Type(0)) ndet = false;
|
||||||
if (ndet) {
|
|
||||||
if (ok != 0) *ok = false;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
for (uint j = 0; j < _V2D::cols_; ++j) if (smat[j][i] != Type(0)) ndet = false;
|
|
||||||
if (ndet) {
|
if (ndet) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
@@ -463,35 +442,35 @@ public:
|
|||||||
}
|
}
|
||||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||||
crow = i;
|
crow = i;
|
||||||
while (smat[i][i] == Type(0)) {
|
while (smat.element(i, i) == Type(0)) {
|
||||||
++crow;
|
++crow;
|
||||||
smat.swapRows(i, crow);
|
smat.swapRows(i, crow);
|
||||||
mtmp.swapRows(i, crow);
|
mtmp.swapRows(i, crow);
|
||||||
if (sv != 0) sv->swap(i, crow);
|
if (sv != 0) sv->swap(i, crow);
|
||||||
}
|
}
|
||||||
for (uint j = i + 1; j < _V2D::rows_; ++j) {
|
for (uint j = i + 1; j < _V2D::rows_; ++j) {
|
||||||
mul = smat[i][j] / smat[i][i];
|
mul = smat.element(i, j) / smat.element(i, i);
|
||||||
for (uint k = i; k < _V2D::cols_; ++k) smat[k][j] -= mul * smat[k][i];
|
for (uint k = i; k < _V2D::cols_; ++k) smat.element(k, j) -= mul * smat.element(k, i);
|
||||||
for (uint k = 0; k < _V2D::cols_; ++k) mtmp[k][j] -= mul * mtmp[k][i];
|
for (uint k = 0; k < _V2D::cols_; ++k) mtmp.element(k, j) -= mul * mtmp.element(k, i);
|
||||||
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
||||||
}
|
}
|
||||||
//cout << i << endl << smat << endl;
|
//cout << i << endl << smat << endl;
|
||||||
if (i < _V2D::cols_ - 1) {
|
if (i < _V2D::cols_ - 1) {
|
||||||
if (_PIMathMatrixNullCompare(smat[i+1][i+1])) {
|
if (_PIMathMatrixNullCompare(smat.element(i+1, i+1))) {
|
||||||
if (ok != 0) *ok = false;
|
if (ok != 0) *ok = false;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
iddiv = smat[i][i];
|
iddiv = smat.element(i, i);
|
||||||
for (uint j = i; j < _V2D::cols_; ++j) smat[j][i] /= iddiv;
|
for (uint j = i; j < _V2D::cols_; ++j) smat.element(j, i) /= iddiv;
|
||||||
for (uint j = 0; j < _V2D::cols_; ++j) mtmp[j][i] /= iddiv;
|
for (uint j = 0; j < _V2D::cols_; ++j) mtmp.element(j, i) /= iddiv;
|
||||||
if (sv != 0) (*sv)[i] /= iddiv;
|
if (sv != 0) (*sv)[i] /= iddiv;
|
||||||
}
|
}
|
||||||
for (uint i = _V2D::cols_ - 1; i > 0; --i) {
|
for (uint i = _V2D::cols_ - 1; i > 0; --i) {
|
||||||
for (uint j = 0; j < i; ++j) {
|
for (uint j = 0; j < i; ++j) {
|
||||||
mul = smat[i][j];
|
mul = smat.element(i, j);
|
||||||
smat[i][j] -= mul;
|
smat.element(i, j) -= mul;
|
||||||
for (uint k = 0; k < _V2D::cols_; ++k) mtmp[k][j] -= mtmp[k][i] * mul;
|
for (uint k = 0; k < _V2D::cols_; ++k) mtmp.element(k, j) -= mul * mtmp.element(k, i);
|
||||||
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -500,7 +479,7 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
_CMatrix inverted(bool * ok = 0) {_CMatrix tm(*this); tm.invert(ok); return tm;}
|
_CMatrix inverted(bool * ok = 0) {_CMatrix tm(*this); tm.invert(ok); return tm;}
|
||||||
_CMatrix transposed() {_CMatrix tm(_V2D::rows_, _V2D::cols_); PIMM_FOR(c, r) tm[c][r] = (*this)[r][c]; return tm;}
|
_CMatrix transposed() {_CMatrix tm(_V2D::rows_, _V2D::cols_); PIMM_FOR(c, r) tm.element(c, r) = _V2D::element(r, c); return tm;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// size_t rows_, cols_;
|
// size_t rows_, cols_;
|
||||||
@@ -510,11 +489,11 @@ private:
|
|||||||
|
|
||||||
#ifdef PIP_STD_IOSTREAM
|
#ifdef PIP_STD_IOSTREAM
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix<Type> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m[c][r]; if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << std::endl << ' ';} s << '}'; return s;}
|
inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix<Type> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << std::endl << ' ';} s << '}'; return s;}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
inline PICout operator <<(PICout s, const PIMathMatrix<Type> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m[r][c]; if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;}
|
inline PICout operator <<(PICout s, const PIMathMatrix<Type> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m.element(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;}
|
||||||
|
|
||||||
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
||||||
template<typename Type>
|
template<typename Type>
|
||||||
@@ -528,8 +507,8 @@ inline PIMathMatrix<Type> operator *(const PIMathMatrix<Type> & fm,
|
|||||||
for (uint i = 0; i < cols1; ++i) {
|
for (uint i = 0; i < cols1; ++i) {
|
||||||
t = Type(0);
|
t = Type(0);
|
||||||
for (uint k = 0; k < cr; ++k)
|
for (uint k = 0; k < cr; ++k)
|
||||||
t += fm[j][k] * sm[k][i];
|
t += fm.element(j, k) * sm.element(k, i);
|
||||||
tm[j][i] = t;
|
tm.element(j, i) = t;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return tm;
|
return tm;
|
||||||
@@ -546,7 +525,7 @@ inline PIMathVector<Type> operator *(const PIMathMatrix<Type> & fm,
|
|||||||
for (uint i = 0; i < r; ++i) {
|
for (uint i = 0; i < r; ++i) {
|
||||||
t = Type(0);
|
t = Type(0);
|
||||||
for (uint j = 0; j < c; ++j)
|
for (uint j = 0; j < c; ++j)
|
||||||
t += fm[j][i] * sv[j];
|
t += fm.element(j, i) * sv[j];
|
||||||
tv[i] = t;
|
tv[i] = t;
|
||||||
}
|
}
|
||||||
return tv;
|
return tv;
|
||||||
@@ -558,7 +537,7 @@ typedef PIMathMatrix<double> PIMathMatrixd;
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
PIMathMatrix<complex<T> > hermitian(const PIMathMatrix<complex<T> > & m) {
|
PIMathMatrix<complex<T> > hermitian(const PIMathMatrix<complex<T> > & m) {
|
||||||
PIMathMatrix<complex<T> > ret(m);
|
PIMathMatrix<complex<T> > ret(m);
|
||||||
for (uint r = 0; r < ret.rows(); ++r) for (uint c = 0; c < ret.cols(); ++c) ret[r][c].imag(-(ret[r][c].imag()));
|
for (uint r = 0; r < ret.rows(); ++r) for (uint c = 0; c < ret.cols(); ++c) ret.element(r, c).imag(-(ret.element(r, c).imag()));
|
||||||
return ret.transposed();
|
return ret.transposed();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -98,7 +98,7 @@ void PIMathSolver::fromTF(const TransferFunction & TF) {
|
|||||||
a1 /= a0;
|
a1 /= a0;
|
||||||
b1 /= a0;
|
b1 /= a0;
|
||||||
|
|
||||||
d[0] = b1[0]; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> d
|
d[0] = b1[0];
|
||||||
for (uint i = 1; i < size + 1; ++i) {
|
for (uint i = 1; i < size + 1; ++i) {
|
||||||
sum = 0.;
|
sum = 0.;
|
||||||
for (uint m = 0; m < i; ++m)
|
for (uint m = 0; m < i; ++m)
|
||||||
@@ -106,7 +106,7 @@ void PIMathSolver::fromTF(const TransferFunction & TF) {
|
|||||||
d[i] = b1[i] - sum;
|
d[i] = b1[i] - sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint i = 0; i < size - 1; ++i) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>
|
for (uint i = 0; i < size - 1; ++i)
|
||||||
for (uint j = 0; j < size; ++j)
|
for (uint j = 0; j < size; ++j)
|
||||||
A[j][i] = (j == i + 1);
|
A[j][i] = (j == i + 1);
|
||||||
for (uint i = 0; i < size; ++i)
|
for (uint i = 0; i < size; ++i)
|
||||||
|
|||||||
Reference in New Issue
Block a user