enable complex type for PIMathVectorT and PIMathMatrixT

TODO: add precision to invert and test vector
This commit is contained in:
2024-10-16 22:10:28 +03:00
parent 92a87a0c64
commit d3d7235338
4 changed files with 258 additions and 207 deletions

View File

@@ -63,7 +63,7 @@ class PIP_EXPORT PIMathMatrixT {
typedef PIMathMatrixT<Cols, Rows, Type> _CMatrixI;
typedef PIMathVectorT<Rows, Type> _CMCol;
typedef PIMathVectorT<Cols, Type> _CMRow;
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
static_assert(std::is_arithmetic<Type>::value || is_complex<Type>::value, "Type must be arithmetic or complex");
static_assert(Rows > 0, "Row count must be > 0");
static_assert(Cols > 0, "Column count must be > 0");
@@ -386,7 +386,7 @@ public:
//! \brief Деление с присваиванием с матрицей `v`.
//! \param sm матрица для деления с присваиванием.
void operator/=(const Type & v) {
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
PIMM_FOR m[r][c] /= v;
}
@@ -453,7 +453,7 @@ public:
//! \param v делитель.
//! \return результат деления.
PIMathMatrixT<Rows, Cols, Type> operator/(const Type & v) const {
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
assert(std::abs(v) > PIMATHVECTOR_ZERO_CMP);
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>(*this);
PIMM_FOR tm.m[r][c] /= v;
return tm;
@@ -517,7 +517,7 @@ public:
for (uint i = 0; i < Cols; ++i) {
ndet = true;
for (uint j = 0; j < Rows; ++j)
if (smat.m[i][j] != 0) ndet = false;
if (smat.m[i][j] != Type{}) ndet = false;
if (ndet) {
if (ok != 0) *ok = false;
return *this;
@@ -525,15 +525,16 @@ public:
}
for (uint i = 0; i < Cols; ++i) {
crow = i;
while (smat.m[i][i] == Type(0))
while (smat.m[i][i] == Type{}) {
smat.swapRows(i, ++crow);
}
for (uint j = i + 1; j < Rows; ++j) {
mul = smat.m[i][j] / smat.m[i][i];
for (uint k = i; k < Cols; ++k)
smat.m[k][j] -= mul * smat.m[k][i];
}
if (i < Cols - 1) {
if (piAbs<Type>(smat.m[i + 1][i + 1]) < Type(1E-200)) {
if (std::abs(smat.m[i + 1][i + 1]) < PIMATHVECTOR_ZERO_CMP) {
if (ok != 0) *ok = false;
return *this;
}
@@ -562,8 +563,9 @@ public:
Type mul, iddiv;
for (uint i = 0; i < Cols; ++i) {
ndet = true;
for (uint j = 0; j < Rows; ++j)
if (smat.m[i][j] != 0) ndet = false;
for (uint j = 0; j < Rows; ++j) {
if (std::abs(smat.m[i][j]) >= PIMATHVECTOR_ZERO_CMP) ndet = false;
}
if (ndet) {
if (ok != 0) *ok = false;
return *this;
@@ -571,7 +573,7 @@ public:
}
for (uint i = 0; i < Cols; ++i) {
crow = i;
while (smat.m[i][i] == Type(0)) {
while (std::abs(smat.m[i][i]) < PIMATHVECTOR_ZERO_CMP) {
++crow;
smat.swapRows(i, crow);
mtmp.swapRows(i, crow);
@@ -584,7 +586,7 @@ public:
mtmp.m[k][j] -= mul * mtmp.m[k][i];
}
if (i < Cols - 1) {
if (piAbs<Type>(smat.m[i + 1][i + 1]) < Type(1E-200)) {
if (std::abs(smat.m[i + 1][i + 1]) < PIMATHVECTOR_ZERO_CMP) {
if (ok != 0) *ok = false;
return *this;
}
@@ -650,7 +652,7 @@ public:
static_assert(Rows == 2 && Cols == 2, "Works only with 2x2 matrix");
Type c = std::cos(angle);
Type s = std::sin(angle);
PIMathMatrixT<2u, 2u> tm;
PIMathMatrixT<2u, 2u, Type> tm;
tm[0][0] = tm[1][1] = c;
tm[0][1] = -s;
tm[1][0] = s;
@@ -671,7 +673,7 @@ public:
PIMathMatrixT<Cols, Rows, Type> outm;
Type c = std::cos(angle);
Type s = std::sin(angle);
PIMathMatrixT<2u, 2u> tm;
PIMathMatrixT<2u, 2u, Type> tm;
tm[0][0] = tm[1][1] = c;
tm[0][1] = -s;
tm[1][0] = s;