code format
This commit is contained in:
@@ -5,34 +5,36 @@
|
||||
//! \~russian Математическая матрица
|
||||
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
PIMathMatrix
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
PIMathMatrix
|
||||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||||
|
||||
This program 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program 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 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program 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.
|
||||
This program 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 program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIMATHMATRIX_H
|
||||
#define PIMATHMATRIX_H
|
||||
|
||||
#include "pimathvector.h"
|
||||
#include "pimathcomplex.h"
|
||||
#include "pimathvector.h"
|
||||
|
||||
|
||||
/// Matrix templated
|
||||
|
||||
#define PIMM_FOR for (uint r = 0; r < Rows; ++r) for (uint c = 0; c < Cols; ++c)
|
||||
#define PIMM_FOR \
|
||||
for (uint r = 0; r < Rows; ++r) \
|
||||
for (uint c = 0; c < Cols; ++c)
|
||||
#define PIMM_FOR_C for (uint i = 0; i < Cols; ++i)
|
||||
#define PIMM_FOR_R for (uint i = 0; i < Rows; ++i)
|
||||
|
||||
@@ -42,7 +44,8 @@
|
||||
//! \brief A class for fixed size and type matrix.
|
||||
//! \tparam `Rows` rows number of matrix.
|
||||
//! \tparam `Сols` columns number of matrix.
|
||||
//! \tparam `Type` is the data type of the matrix. There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /)
|
||||
//! \tparam `Type` is the data type of the matrix. There are can be basic C++ language data and different classes where the arithmetic
|
||||
//! operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /)
|
||||
//! of the C++ language are implemented
|
||||
//! \~russian
|
||||
//! \brief Класс для работы с матрицами фиксированного размера и типа данных.
|
||||
@@ -53,7 +56,7 @@
|
||||
//! \tparam `Rows` количество строк матрицы.
|
||||
//! \tparam `Сols` количество столбцов матрицы.
|
||||
//! \tparam `Type`тип данных матрицы. Здесь можеть быть базовый тип данных C++ или различные классы,
|
||||
//! где реализованы арифметические операторы(=, +=, -=, *=, /=, ==, !=, +, -, *, /) языка C++.
|
||||
//! где реализованы арифметические операторы(=, +=, -=, *=, /=, ==, !=, +, -, *, /) языка C++.
|
||||
template<uint Rows, uint Cols = Rows, typename Type = double>
|
||||
class PIP_EXPORT PIMathMatrixT {
|
||||
typedef PIMathMatrixT<Rows, Cols, Type> _CMatrix;
|
||||
@@ -63,30 +66,32 @@ class PIP_EXPORT PIMathMatrixT {
|
||||
static_assert(std::is_arithmetic<Type>::value, "Type must be arithmetic");
|
||||
static_assert(Rows > 0, "Row count must be > 0");
|
||||
static_assert(Cols > 0, "Column count must be > 0");
|
||||
|
||||
public:
|
||||
//! \~english
|
||||
//! \brief Constructs \a PIMathMatrixT that is filled by \a new_value.
|
||||
//! \~russian
|
||||
//! \brief Создает \a PIMathMatrixT и заполняет её из \a new_value.
|
||||
PIMathMatrixT(const Type &new_value = Type()) {PIMM_FOR m[r][c] = new_value;}
|
||||
PIMathMatrixT(const Type & new_value = Type()) { PIMM_FOR m[r][c] = new_value; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Contructs \a PIMathMatrixT from \a PIVector.
|
||||
//! \~russian
|
||||
//! \brief Создает \a PIMathMatrixT и заполняет её из \a PIVector.
|
||||
PIMathMatrixT(const PIVector<Type> &val) {
|
||||
assert(Rows*Cols == val.size());
|
||||
int i = 0;
|
||||
PIMathMatrixT(const PIVector<Type> & val) {
|
||||
assert(Rows * Cols == val.size());
|
||||
int i = 0;
|
||||
PIMM_FOR m[r][c] = val[i++];
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Contructs \a PIMathMatrixT from [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
|
||||
//! \~russian
|
||||
//! \brief Создает \a PIMathMatrixT и заполняет её из [списка инициализации C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
|
||||
//! \brief Создает \a PIMathMatrixT и заполняет её из [списка инициализации
|
||||
//! C++11](https://ru.cppreference.com/w/cpp/utility/initializer_list).
|
||||
PIMathMatrixT(std::initializer_list<Type> init_list) {
|
||||
assert(Rows*Cols == init_list.size());
|
||||
int i = 0;
|
||||
assert(Rows * Cols == init_list.size());
|
||||
int i = 0;
|
||||
PIMM_FOR m[r][c] = init_list.begin()[i++];
|
||||
}
|
||||
|
||||
@@ -98,7 +103,7 @@ public:
|
||||
//! \return единичная матрица типа \a PIMathMatrixT.
|
||||
static PIMathMatrixT<Rows, Cols, Type> identity() {
|
||||
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>();
|
||||
PIMM_FOR tm.m[r][c] = (c == r ? Type(1) : Type(0));
|
||||
PIMM_FOR tm.m[r][c] = (c == r ? Type(1) : Type(0));
|
||||
return tm;
|
||||
}
|
||||
|
||||
@@ -108,7 +113,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Метод возвращающий количество столбцов в матрице.
|
||||
//! \return \a uint количество столбцов.
|
||||
constexpr uint cols() const {return Cols;}
|
||||
constexpr uint cols() const { return Cols; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Method which returns number of rows in matrix.
|
||||
@@ -116,7 +121,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Метод возвращающий количество строк в матрице.
|
||||
//! \return \a uint количество строк.
|
||||
constexpr uint rows() const {return Rows;}
|
||||
constexpr uint rows() const { return Rows; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Method which returns the selected column in PIMathVectorT format.
|
||||
@@ -160,8 +165,8 @@ public:
|
||||
//! \details Если вы введете индекс вне границ матрицы, то поведение не определено ("undefined behavior").
|
||||
//! \param index номер выбранного столбца.
|
||||
//! \param v вектор типа \a PIMathVectorT<Rows, Type>, которым необходимо заполнить столбец.
|
||||
//! \return матрица типа \a PIMathMatrixT<Rows, Cols, Type>.
|
||||
PIMathMatrixT<Rows, Cols, Type> &setCol(uint index, const PIMathVectorT<Rows, Type> &v) {
|
||||
//! \return матрица типа \a PIMathMatrixT<Rows, Cols, Type>.
|
||||
PIMathMatrixT<Rows, Cols, Type> & setCol(uint index, const PIMathVectorT<Rows, Type> & v) {
|
||||
PIMM_FOR_R m[i][index] = v[i];
|
||||
return *this;
|
||||
}
|
||||
@@ -178,7 +183,7 @@ public:
|
||||
//! \param index номер выбранной строки.
|
||||
//! \param v вектор типа \a PIMathVectorT<Cols, Type>, которым необходимо заполнить строку.
|
||||
//! \return матрица типа \a PIMathMatrixT<Rows, Cols, Type>.
|
||||
PIMathMatrixT<Rows, Cols, Type> &setRow(uint index, const PIMathVectorT<Cols, Type> &v) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & setRow(uint index, const PIMathVectorT<Cols, Type> & v) {
|
||||
PIMM_FOR_C m[index][i] = v[i];
|
||||
return *this;
|
||||
}
|
||||
@@ -195,7 +200,7 @@ public:
|
||||
//! \param rf номер первой выбранной строки.
|
||||
//! \param rs номер второй выбранной строки.
|
||||
//! \return матрица типа \a PIMathMatrixT<Rows, Cols, Type>.
|
||||
PIMathMatrixT<Rows, Cols, Type> &swapRows(uint rf, uint rs) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & swapRows(uint rf, uint rs) {
|
||||
PIMM_FOR_C piSwap<Type>(m[rf][i], m[rs][i]);
|
||||
return *this;
|
||||
}
|
||||
@@ -212,7 +217,7 @@ public:
|
||||
//! \param rf номер первого выбранного столбца.
|
||||
//! \param rs номер второго выбранного столбца.
|
||||
//! \return матрица типа \a PIMathMatrixT<Rows, Cols, Type>.
|
||||
PIMathMatrixT<Rows, Cols, Type> &swapCols(uint cf, uint cs) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & swapCols(uint cf, uint cs) {
|
||||
PIMM_FOR_R piSwap<Type>(m[i][cf], m[i][cs]);
|
||||
return *this;
|
||||
}
|
||||
@@ -225,7 +230,7 @@ public:
|
||||
//! \brief Метод, заполняющий матрицу выбранным значением.
|
||||
//! \param v параметр тип и значения, которого выбираются и заносятся в матрицу.
|
||||
//! \return заполненная матрица типа \a PIMathMatrixT<Rows, Cols, Type>.
|
||||
PIMathMatrixT<Rows, Cols, Type> &fill(const Type &v) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & fill(const Type & v) {
|
||||
PIMM_FOR m[r][c] = v;
|
||||
return *this;
|
||||
}
|
||||
@@ -287,7 +292,7 @@ public:
|
||||
//! \param row номер строки матрицы.
|
||||
//! \param col номер столбца матрицы.
|
||||
//! \return элемент матрицы.
|
||||
inline Type & element(uint row, uint col) {return m[row][col];}
|
||||
inline Type & element(uint row, uint col) { return m[row][col]; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Read-only access to element by `row` number and `col` number.
|
||||
@@ -301,7 +306,7 @@ public:
|
||||
//! \param row номер строки матрицы.
|
||||
//! \param col номер столбца матрицы.
|
||||
//! \return копия элемента матрицы.
|
||||
inline const Type & element(uint row, uint col) const {return m[row][col];}
|
||||
inline const Type & element(uint row, uint col) const { return m[row][col]; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Full access to the matrix row pointer.
|
||||
@@ -313,7 +318,7 @@ public:
|
||||
//! \details Если вы введете индекс вне границ матрицы, то поведение не определено ("undefined behavior").
|
||||
//! \param row номер строки матрицы.
|
||||
//! \return указатель на строку матрицы.
|
||||
Type *operator[](uint row) { return m[row]; }
|
||||
Type * operator[](uint row) { return m[row]; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Read-only access to the matrix row pointer.
|
||||
@@ -325,7 +330,7 @@ public:
|
||||
//! \details Если вы введете индекс вне границ матрицы, то поведение не определено ("undefined behavior").
|
||||
//! \param row номер строки матрицы.
|
||||
//! \return указатель на строку матрицы.
|
||||
const Type *operator[](uint row) const {return m[row];}
|
||||
const Type * operator[](uint row) const { return m[row]; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Matrix compare.
|
||||
@@ -335,7 +340,7 @@ public:
|
||||
//! \brief Сравнение матриц.
|
||||
//! \param sm матрица для сравнения.
|
||||
//! \return если матрицы равны true, иначе false.
|
||||
bool operator==(const PIMathMatrixT<Rows, Cols, Type> &sm) const {
|
||||
bool operator==(const PIMathMatrixT<Rows, Cols, Type> & sm) const {
|
||||
PIMM_FOR if (m[r][c] != sm.m[r][c]) return false;
|
||||
return true;
|
||||
}
|
||||
@@ -348,7 +353,7 @@ public:
|
||||
//! \brief Отрицательное сравнение матриц.
|
||||
//! \param sm матрица для сравнения.
|
||||
//! \return если матрицы не равны true, иначе false.
|
||||
bool operator!=(const PIMathMatrixT<Rows, Cols, Type> &sm) const { return !(*this == sm); }
|
||||
bool operator!=(const PIMathMatrixT<Rows, Cols, Type> & sm) const { return !(*this == sm); }
|
||||
|
||||
//! \~english
|
||||
//! \brief Addition assignment with matrix `sm`.
|
||||
@@ -356,7 +361,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Сложение с присваиванием с матрицей `sm`.
|
||||
//! \param sm матрица для сложения с присваиванием.
|
||||
void operator+=(const PIMathMatrixT<Rows, Cols, Type> &sm) {PIMM_FOR m[r][c] += sm.m[r][c];}
|
||||
void operator+=(const PIMathMatrixT<Rows, Cols, Type> & sm) { PIMM_FOR m[r][c] += sm.m[r][c]; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Subtraction assignment with matrix `sm`.
|
||||
@@ -364,7 +369,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Вычитание с присваиванием с матрицей `sm`.
|
||||
//! \param sm матрица для вычитания с присваиванием.
|
||||
void operator-=(const PIMathMatrixT<Rows, Cols, Type> &sm) {PIMM_FOR m[r][c] -= sm.m[r][c];}
|
||||
void operator-=(const PIMathMatrixT<Rows, Cols, Type> & sm) { PIMM_FOR m[r][c] -= sm.m[r][c]; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Multiplication assignment with value `v`.
|
||||
@@ -372,9 +377,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Умножение с присваиванием с матрицей `v`.
|
||||
//! \param sm матрица для умножения с присваиванием.
|
||||
void operator*=(const Type &v) {
|
||||
PIMM_FOR m[r][c] *= v;
|
||||
}
|
||||
void operator*=(const Type & v) { PIMM_FOR m[r][c] *= v; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Division assignment with value `v`.
|
||||
@@ -382,7 +385,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Деление с присваиванием с матрицей `v`.
|
||||
//! \param sm матрица для деления с присваиванием.
|
||||
void operator/=(const Type &v) {
|
||||
void operator/=(const Type & v) {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMM_FOR m[r][c] /= v;
|
||||
}
|
||||
@@ -407,7 +410,7 @@ public:
|
||||
//! \brief Матричное сложение.
|
||||
//! \param sm матричное слагаемое.
|
||||
//! \return результат матричного сложения.
|
||||
PIMathMatrixT<Rows, Cols, Type> operator+(const PIMathMatrixT<Rows, Cols, Type> &sm) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> operator+(const PIMathMatrixT<Rows, Cols, Type> & sm) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>(*this);
|
||||
PIMM_FOR tm.m[r][c] += sm.m[r][c];
|
||||
return tm;
|
||||
@@ -421,7 +424,7 @@ public:
|
||||
//! \brief Матричная разность.
|
||||
//! \param sm матричное вычитаемое.
|
||||
//! \return результат матричной разности.
|
||||
PIMathMatrixT<Rows, Cols, Type> operator-(const PIMathMatrixT<Rows, Cols, Type> &sm) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> operator-(const PIMathMatrixT<Rows, Cols, Type> & sm) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>(*this);
|
||||
PIMM_FOR tm.m[r][c] -= sm.m[r][c];
|
||||
return tm;
|
||||
@@ -435,7 +438,7 @@ public:
|
||||
//! \brief Умножение матрицы на константу.
|
||||
//! \param v множитель.
|
||||
//! \return результат произведения.
|
||||
PIMathMatrixT<Rows, Cols, Type> operator*(const Type &v) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> operator*(const Type & v) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>(*this);
|
||||
PIMM_FOR tm.m[r][c] *= v;
|
||||
return tm;
|
||||
@@ -449,7 +452,7 @@ public:
|
||||
//! \brief Деление матрицы на константу.
|
||||
//! \param v делитель.
|
||||
//! \return результат деления.
|
||||
PIMathMatrixT<Rows, Cols, Type> operator/(const Type &v) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> operator/(const Type & v) const {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMathMatrixT<Rows, Cols, Type> tm = PIMathMatrixT<Rows, Cols, Type>(*this);
|
||||
PIMM_FOR tm.m[r][c] /= v;
|
||||
@@ -457,7 +460,7 @@ public:
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Calculate Determinant of the matrix.
|
||||
//! \brief Calculate Determinant of the matrix.
|
||||
//! \details Works only with square matrix, nonzero matrices and invertible matrix.
|
||||
//! \param ok is a parameter with which we can find out if the method worked correctly.
|
||||
//! \return matrix determinant.
|
||||
@@ -466,7 +469,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return опеределитель матрицы.
|
||||
Type determinant(bool *ok = 0) const {
|
||||
Type determinant(bool * ok = 0) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> m(*this);
|
||||
bool k;
|
||||
Type ret = Type(0);
|
||||
@@ -505,7 +508,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return преобразованная верхнетреугольная матрицы.
|
||||
PIMathMatrixT<Rows, Cols, Type> &toUpperTriangular(bool *ok = 0) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & toUpperTriangular(bool * ok = 0) {
|
||||
static_assert(Rows == Cols, "Works only with square matrix");
|
||||
PIMathMatrixT<Rows, Cols, Type> smat(*this);
|
||||
bool ndet;
|
||||
@@ -513,7 +516,8 @@ public:
|
||||
Type mul;
|
||||
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 (smat.m[i][j] != 0) ndet = false;
|
||||
if (ndet) {
|
||||
if (ok != 0) *ok = false;
|
||||
return *this;
|
||||
@@ -525,7 +529,8 @@ public:
|
||||
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];
|
||||
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)) {
|
||||
@@ -540,7 +545,7 @@ public:
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \details Works only with square matrix, nonzero matrices and invertible matrix.
|
||||
//! \param ok is a parameter with which we can find out if the method worked correctly.
|
||||
//! \return inverted matrix.
|
||||
@@ -549,7 +554,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return обратная матрица.
|
||||
PIMathMatrixT<Rows, Cols, Type> &invert(bool *ok = 0) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & invert(bool * ok = 0) {
|
||||
static_assert(Rows == Cols, "Works only with square matrix");
|
||||
PIMathMatrixT<Rows, Cols, Type> mtmp = PIMathMatrixT<Rows, Cols, Type>::identity(), smat(*this);
|
||||
bool ndet;
|
||||
@@ -557,7 +562,8 @@ 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 (smat.m[i][j] != 0) ndet = false;
|
||||
if (ndet) {
|
||||
if (ok != 0) *ok = false;
|
||||
return *this;
|
||||
@@ -572,8 +578,10 @@ public:
|
||||
}
|
||||
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];
|
||||
for (uint k = 0; k < Cols; ++k) mtmp.m[k][j] -= mul * mtmp.m[k][i];
|
||||
for (uint k = i; k < Cols; ++k)
|
||||
smat.m[k][j] -= mul * smat.m[k][i];
|
||||
for (uint k = 0; k < Cols; ++k)
|
||||
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)) {
|
||||
@@ -582,14 +590,17 @@ public:
|
||||
}
|
||||
}
|
||||
iddiv = smat.m[i][i];
|
||||
for (uint j = i; j < Cols; ++j) smat.m[j][i] /= iddiv;
|
||||
for (uint j = 0; j < Cols; ++j) mtmp.m[j][i] /= iddiv;
|
||||
for (uint j = i; j < Cols; ++j)
|
||||
smat.m[j][i] /= iddiv;
|
||||
for (uint j = 0; j < Cols; ++j)
|
||||
mtmp.m[j][i] /= iddiv;
|
||||
}
|
||||
for (uint i = Cols - 1; i > 0; --i) {
|
||||
for (uint j = 0; j < i; ++j) {
|
||||
mul = smat.m[i][j];
|
||||
smat.m[i][j] -= mul;
|
||||
for (uint k = 0; k < Cols; ++k) mtmp.m[k][j] -= mtmp.m[k][i] * mul;
|
||||
for (uint k = 0; k < Cols; ++k)
|
||||
mtmp.m[k][j] -= mtmp.m[k][i] * mul;
|
||||
}
|
||||
}
|
||||
if (ok != 0) *ok = true;
|
||||
@@ -598,7 +609,7 @@ public:
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \details Works only with square matrix, nonzero matrices and invertible matrix.
|
||||
//! \param ok is a parameter with which we can find out if the method worked correctly.
|
||||
//! \return copy of inverted matrix.
|
||||
@@ -607,14 +618,14 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return копия обратной матрицы.
|
||||
PIMathMatrixT<Rows, Cols, Type> inverted(bool *ok = 0) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> inverted(bool * ok = 0) const {
|
||||
PIMathMatrixT<Rows, Cols, Type> tm(*this);
|
||||
tm.invert(ok);
|
||||
return tm;
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Matrix transposition operation.
|
||||
//! \brief Matrix transposition operation.
|
||||
//! \details Works only with square matrix.
|
||||
//! \return copy of transposed matrix
|
||||
//! \~russian
|
||||
@@ -641,9 +652,9 @@ public:
|
||||
Type s = std::sin(angle);
|
||||
PIMathMatrixT<2u, 2u> tm;
|
||||
tm[0][0] = tm[1][1] = c;
|
||||
tm[0][1] = -s;
|
||||
tm[1][0] = s;
|
||||
*this = *this * tm;
|
||||
tm[0][1] = -s;
|
||||
tm[1][0] = s;
|
||||
*this = *this * tm;
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -655,16 +666,16 @@ public:
|
||||
//! \brief Операция поворота матрицы.
|
||||
//! \details Работает только с матрицами 2x2.
|
||||
//! \return копия повернутой матрицы.
|
||||
PIMathMatrixT<Rows, Cols, Type>& rotated(Type angle) {
|
||||
PIMathMatrixT<Rows, Cols, Type> & rotated(Type angle) {
|
||||
static_assert(Rows == 2 && Cols == 2, "Works only with 2x2 matrix");
|
||||
PIMathMatrixT<Cols, Rows, Type> outm;
|
||||
Type c = std::cos(angle);
|
||||
Type s = std::sin(angle);
|
||||
PIMathMatrixT<2u, 2u> tm;
|
||||
tm[0][0] = tm[1][1] = c;
|
||||
tm[0][1] = -s;
|
||||
tm[1][0] = s;
|
||||
outm = outm * tm;
|
||||
tm[0][1] = -s;
|
||||
tm[1][0] = s;
|
||||
outm = outm * tm;
|
||||
return outm;
|
||||
}
|
||||
|
||||
@@ -677,7 +688,7 @@ private:
|
||||
|
||||
#ifdef PIP_STD_IOSTREAM
|
||||
template<uint Rows, uint Cols, typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT<Rows, Cols, Type> & m) {
|
||||
inline std::ostream & operator<<(std::ostream & s, const PIMathMatrixT<Rows, Cols, Type> & m) {
|
||||
s << "{";
|
||||
for (uint r = 0; r < Rows; ++r) {
|
||||
for (uint c = 0; c < Cols; ++c) {
|
||||
@@ -702,7 +713,7 @@ inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT<Rows, Co
|
||||
//! \param m типа \a PIMathMatrixT.
|
||||
//! \return непечатанная в консоль \a PICout.
|
||||
template<uint Rows, uint Cols, typename Type>
|
||||
inline PICout operator<<(PICout s, const PIMathMatrixT<Rows, Cols, Type> &m) {
|
||||
inline PICout operator<<(PICout s, const PIMathMatrixT<Rows, Cols, Type> & m) {
|
||||
s << "{";
|
||||
for (uint r = 0; r < Rows; ++r) {
|
||||
for (uint c = 0; c < Cols; ++c) {
|
||||
@@ -716,7 +727,7 @@ inline PICout operator<<(PICout s, const PIMathMatrixT<Rows, Cols, Type> &m) {
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Multiplying matrices by each other.
|
||||
//! \brief Multiplying matrices by each other.
|
||||
//! \param fm first matrix multiplier.
|
||||
//! \param sm second matrix multiplier.
|
||||
//! \return matrix that is the result of multiplication.
|
||||
@@ -726,8 +737,7 @@ inline PICout operator<<(PICout s, const PIMathMatrixT<Rows, Cols, Type> &m) {
|
||||
//! \param sm второй множитель-матрица.
|
||||
//! \return матрица, являющаяся результатом умножения.
|
||||
template<uint CR, uint Rows0, uint Cols1, typename Type>
|
||||
inline PIMathMatrixT<Rows0, Cols1, Type> operator*(const PIMathMatrixT<Rows0, CR, Type> &fm,
|
||||
const PIMathMatrixT<CR, Cols1, Type> &sm) {
|
||||
inline PIMathMatrixT<Rows0, Cols1, Type> operator*(const PIMathMatrixT<Rows0, CR, Type> & fm, const PIMathMatrixT<CR, Cols1, Type> & sm) {
|
||||
PIMathMatrixT<Rows0, Cols1, Type> tm;
|
||||
Type t;
|
||||
for (uint j = 0; j < Rows0; ++j) {
|
||||
@@ -752,8 +762,7 @@ inline PIMathMatrixT<Rows0, Cols1, Type> operator*(const PIMathMatrixT<Rows0, CR
|
||||
//! \param sv второй множитель-вектор.
|
||||
//! \return вектор, являющийся результатом умножения.
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
inline PIMathVectorT<Rows, Type> operator*(const PIMathMatrixT<Rows, Cols, Type> &fm,
|
||||
const PIMathVectorT<Cols, Type> &sv) {
|
||||
inline PIMathVectorT<Rows, Type> operator*(const PIMathMatrixT<Rows, Cols, Type> & fm, const PIMathVectorT<Cols, Type> & sv) {
|
||||
PIMathVectorT<Rows, Type> tv;
|
||||
Type t;
|
||||
for (uint j = 0; j < Rows; ++j) {
|
||||
@@ -776,8 +785,7 @@ inline PIMathVectorT<Rows, Type> operator*(const PIMathMatrixT<Rows, Cols, Type>
|
||||
//! \param fm первый множитель-матрица.
|
||||
//! \return вектор, являющийся результатом умножения.
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
inline PIMathVectorT<Cols, Type> operator*(const PIMathVectorT<Rows, Type> &sv,
|
||||
const PIMathMatrixT<Rows, Cols, Type> &fm) {
|
||||
inline PIMathVectorT<Cols, Type> operator*(const PIMathVectorT<Rows, Type> & sv, const PIMathMatrixT<Rows, Cols, Type> & fm) {
|
||||
PIMathVectorT<Cols, Type> tv;
|
||||
Type t;
|
||||
for (uint j = 0; j < Cols; ++j) {
|
||||
@@ -800,7 +808,7 @@ inline PIMathVectorT<Cols, Type> operator*(const PIMathVectorT<Rows, Type> &sv,
|
||||
//! \param v вторая множитель-матрица.
|
||||
//! \return матрица, являющаяся результатом умножения.
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
inline PIMathMatrixT<Rows, Cols, Type> operator*(const Type &x, const PIMathMatrixT<Rows, Cols, Type> &v) {
|
||||
inline PIMathMatrixT<Rows, Cols, Type> operator*(const Type & x, const PIMathMatrixT<Rows, Cols, Type> & v) {
|
||||
return v * x;
|
||||
}
|
||||
|
||||
@@ -821,28 +829,28 @@ class PIMathMatrix;
|
||||
#undef PIMM_FOR_R
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/// Matrix
|
||||
|
||||
#define PIMM_FOR for (uint r = 0; r < _V2D::rows_; ++r) for (uint c = 0; c < _V2D::cols_; ++c)
|
||||
#define PIMM_FOR \
|
||||
for (uint r = 0; r < _V2D::rows_; ++r) \
|
||||
for (uint c = 0; c < _V2D::cols_; ++c)
|
||||
#define PIMM_FOR_A for (uint i = 0; i < _V2D::mat.size(); ++i)
|
||||
#define PIMM_FOR_C for (uint i = 0; i < _V2D::cols_; ++i)
|
||||
#define PIMM_FOR_R for (uint i = 0; i < _V2D::rows_; ++i)
|
||||
|
||||
//! \~english
|
||||
//! \brief A class for dynamic size and fixed type matrix.
|
||||
//! \tparam `Type` There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=, +, -, *, /)
|
||||
//! of the C++ language are implemented.
|
||||
//! \tparam `Type` There are can be basic C++ language data and different classes where the arithmetic operators(=, +=, -=, *=, /=, ==, !=,
|
||||
//! +, -, *, /) of the C++ language are implemented.
|
||||
//! \~russian
|
||||
//! \brief Класс для работы с матрицами динамического размера и фиксированного типа.
|
||||
//! \tparam `Type` Здесь можеть быть базовый тип данных C++ или различные классы,
|
||||
//! где реализованы арифметические операторы(=, +=, -=, *=, /=, ==, !=, +, -, *, /) языка C++.
|
||||
//! где реализованы арифметические операторы(=, +=, -=, *=, /=, ==, !=, +, -, *, /) языка C++.
|
||||
template<typename Type>
|
||||
class PIP_EXPORT PIMathMatrix : public PIVector2D<Type> {
|
||||
class PIP_EXPORT PIMathMatrix: public PIVector2D<Type> {
|
||||
typedef PIVector2D<Type> _V2D;
|
||||
typedef PIMathMatrix<Type> _CMatrix;
|
||||
|
||||
public:
|
||||
//! \~english
|
||||
//! \brief Constructor of class \a PIMathMatrix, which creates a matrix.
|
||||
@@ -854,7 +862,7 @@ public:
|
||||
//! \param cols количество столбов матрицы типа \a uint.
|
||||
//! \param rows количество строк матрицы типа \a uint.
|
||||
//! \param f тип элементов матрицы.
|
||||
PIMathMatrix(const uint cols = 0, const uint rows = 0, const Type &f = Type()) { _V2D::resize(rows, cols, f); }
|
||||
PIMathMatrix(const uint cols = 0, const uint rows = 0, const Type & f = Type()) { _V2D::resize(rows, cols, f); }
|
||||
|
||||
//! \~english
|
||||
//! \brief Constructor of class \a PIMathMatrix, which creates a matrix
|
||||
@@ -866,9 +874,9 @@ public:
|
||||
//! \param cols количество столбов матрицы типа \a uint.
|
||||
//! \param rows количество строк матрицы типа \a uint.
|
||||
//! \param val тип \a PIVector<Type> элементов матрицы.
|
||||
PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> &val) {
|
||||
PIMathMatrix(const uint cols, const uint rows, const PIVector<Type> & val) {
|
||||
_V2D::resize(rows, cols);
|
||||
int i = 0;
|
||||
int i = 0;
|
||||
PIMM_FOR _V2D::element(r, c) = val[i++];
|
||||
}
|
||||
|
||||
@@ -878,7 +886,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Конструктор класса \a PIMathMatrix, который создает матрицу.
|
||||
//! \param val тип \a PIVector<Type>, который создает матрицу.
|
||||
PIMathMatrix(const PIVector<PIVector<Type> > &val) {
|
||||
PIMathMatrix(const PIVector<PIVector<Type>> & val) {
|
||||
if (!val.isEmpty()) {
|
||||
_V2D::resize(val.size(), val[0].size());
|
||||
for (uint r = 0; r < _V2D::rows_; ++r) {
|
||||
@@ -895,7 +903,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Конструктор класса \a PIMathMatrix, который создает матрицу.
|
||||
//! \param val тип \a PIVector2D<Type>, который создает матрицу.
|
||||
PIMathMatrix(const PIVector2D<Type> &val) {
|
||||
PIMathMatrix(const PIVector2D<Type> & val) {
|
||||
if (!val.isEmpty()) {
|
||||
_V2D::resize(val.rows(), val.cols());
|
||||
PIMM_FOR _V2D::element(r, c) = val.element(r, c);
|
||||
@@ -914,7 +922,9 @@ public:
|
||||
//! \return единичная матрица matrix(`cols`, `rows`)
|
||||
static PIMathMatrix<Type> identity(const uint cols, const uint rows) {
|
||||
PIMathMatrix<Type> 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));
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -926,7 +936,7 @@ public:
|
||||
//! \brief Создает матрицу-строку, каждый элемент которой равен каждому элементу вектора
|
||||
//! \param val вектор типа \a PIMathVector
|
||||
//! \return матрица-строка, каждый элемент которой равен каждому элементу вектора
|
||||
static PIMathMatrix<Type> matrixRow(const PIMathVector<Type> &val) {return PIMathMatrix<Type>(val.size(), 1, val.toVector());}
|
||||
static PIMathMatrix<Type> matrixRow(const PIMathVector<Type> & val) { return PIMathMatrix<Type>(val.size(), 1, val.toVector()); }
|
||||
|
||||
//! \~english
|
||||
//! \brief Creates a column matrix of every element that is equal to every element of the vector
|
||||
@@ -936,23 +946,23 @@ public:
|
||||
//! \brief Создает матрицу-столбец, каждый элемент которой равен каждому элементу вектора
|
||||
//! \param val вектор типа \a PIMathVector
|
||||
//! \return матрица-столбец, каждый элемент которой равен каждому элементу вектора
|
||||
static PIMathMatrix<Type> matrixCol(const PIMathVector<Type> &val) {return PIMathMatrix<Type>(1, val.size(), val.toVector());}
|
||||
static PIMathMatrix<Type> matrixCol(const PIMathVector<Type> & val) { return PIMathMatrix<Type>(1, val.size(), val.toVector()); }
|
||||
|
||||
//! \~english
|
||||
//! \brief Set the selected column in matrix.
|
||||
//! \brief Set the selected column in matrix.
|
||||
//! \details If there are more elements of the vector than elements in the column of the matrix
|
||||
//! or index larger than the number of columns otherwise there will be "undefined behavior".
|
||||
//! \param index is the number of the selected column.
|
||||
//! \param v is a vector of the type \a PIMathVector<Type> that needs to fill the column.
|
||||
//! \return matrix type \a PIMathMatrix<Type>
|
||||
//! \~russian
|
||||
//! \brief Определить выбранный столбец матрицы.
|
||||
//! \details Если элементов в векторе больше, чем элементов в столбце матрицы
|
||||
//! \brief Определить выбранный столбец матрицы.
|
||||
//! \details Если элементов в векторе больше, чем элементов в столбце матрицы
|
||||
//! или индекс больше количества стобцов, то поведение не определено ("undefined behavior").
|
||||
//! \param index номер выбранного столбца.
|
||||
//! \param v вектор типа \a PIMathVector<Type>, которым нужно заполнить столбец.
|
||||
//! \return матрица типа \a PIMathMatrix<Type>.
|
||||
PIMathMatrix<Type> &setCol(uint index, const PIMathVector<Type> &v) {
|
||||
PIMathMatrix<Type> & setCol(uint index, const PIMathVector<Type> & v) {
|
||||
assert(_V2D::rows() == v.size());
|
||||
PIMM_FOR_R _V2D::element(i, index) = v[i];
|
||||
return *this;
|
||||
@@ -966,13 +976,13 @@ public:
|
||||
//! \param v is a vector of the type \a PIMathVector<Type> that needs to fill the row.
|
||||
//! \return matrix type \a PIMathMatrix<Type>.
|
||||
//! \~russian
|
||||
//! \brief Определить выбранную строку матрицы.
|
||||
//! \details Если элементов в векторе больше, чем элементов в строке матрицы
|
||||
//! \brief Определить выбранную строку матрицы.
|
||||
//! \details Если элементов в векторе больше, чем элементов в строке матрицы
|
||||
//! или индекс больше количества стобцов, то поведение не определено ("undefined behavior").
|
||||
//! \param index номер выбранной строки.
|
||||
//! \param v вектор типа \a PIMathVector<Type>, которым нужно заполнить строку.
|
||||
//! \return матрица типа \a PIMathMatrix<Type>.
|
||||
PIMathMatrix<Type> &setRow(uint index, const PIMathVector<Type> &v) {
|
||||
PIMathMatrix<Type> & setRow(uint index, const PIMathVector<Type> & v) {
|
||||
assert(_V2D::cols() == v.size());
|
||||
PIMM_FOR_C _V2D::element(index, i) = v[i];
|
||||
return *this;
|
||||
@@ -987,12 +997,12 @@ public:
|
||||
//! \return matrix type \a PIMathMatrix<Type>.
|
||||
//! \~russian
|
||||
//! \brief Метод меняющий местами выбранные строки в матрице.
|
||||
//! \details Вы не можете использовать индекс, который больше количества столбцов,
|
||||
//! \details Вы не можете использовать индекс, который больше количества столбцов,
|
||||
//! иначе будет неопределенное повередение ("undefined behavior").
|
||||
//! \param r0 номер первой выбранного стобца.
|
||||
//! \param r1 номер второй выбранного столбца.
|
||||
//! \return матрица типа \a PIMathMatrix<Type>.
|
||||
PIMathMatrix<Type> &swapCols(uint r0, uint r1) {
|
||||
PIMathMatrix<Type> & swapCols(uint r0, uint r1) {
|
||||
PIMM_FOR_C piSwap<Type>(_V2D::element(i, r0), _V2D::element(i, r1));
|
||||
return *this;
|
||||
}
|
||||
@@ -1006,12 +1016,12 @@ public:
|
||||
//! \return matrix type \a PIMathMatrix<Type>.
|
||||
//! \~russian
|
||||
//! \brief Метод меняющий местами выбранные строки в матрице.
|
||||
//! \details Вы не можете использовать индекс, который больше количества строк,
|
||||
//! \details Вы не можете использовать индекс, который больше количества строк,
|
||||
//! иначе будет неопределенное повередение ("undefined behavior").
|
||||
//! \param с0 номер первой выбранной строки.
|
||||
//! \param с1 номер второй выбранной строки.
|
||||
//! \return матрица типа \a PIMathMatrix<Type>.
|
||||
PIMathMatrix<Type> &swapRows(uint c0, uint c1) {
|
||||
PIMathMatrix<Type> & swapRows(uint c0, uint c1) {
|
||||
PIMM_FOR_R piSwap<Type>(_V2D::element(c0, i), _V2D::element(c1, i));
|
||||
return *this;
|
||||
}
|
||||
@@ -1024,7 +1034,7 @@ public:
|
||||
//! \brief Метод заполняющий матрицу выбранным значением.
|
||||
//! \param v параметр выбранного типа и значения, которым будет заполнена матрица.
|
||||
//! \return заполненная матрица типа \a PIMathMatrix<Type>.
|
||||
PIMathMatrix<Type> &fill(const Type &v) {
|
||||
PIMathMatrix<Type> & fill(const Type & v) {
|
||||
PIMM_FOR_A _V2D::mat[i] = v;
|
||||
return *this;
|
||||
}
|
||||
@@ -1044,7 +1054,7 @@ public:
|
||||
//! \brief Метод, проверяющий содержит ли главная диагональ единицы и все остальные поля нули.
|
||||
//! \return true если матрица единичная, иначе false.
|
||||
bool isIdentity() const {
|
||||
PIMM_FOR if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0))return false;
|
||||
PIMM_FOR if ((c == r) ? _V2D::element(r, c) != Type(1) : _V2D::element(r, c) != Type(0)) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1073,7 +1083,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Сложение с присваиванием с матрицей `sm`.
|
||||
//! \param sm матрица для сложения с присваиванием.
|
||||
void operator+=(const PIMathMatrix<Type> &sm) {
|
||||
void operator+=(const PIMathMatrix<Type> & sm) {
|
||||
assert(_V2D::rows() == sm.rows());
|
||||
assert(_V2D::cols() == sm.cols());
|
||||
PIMM_FOR_A _V2D::mat[i] += sm.mat[i];
|
||||
@@ -1085,7 +1095,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Вычитание с присваиванием с матрицей `sm`.
|
||||
//! \param sm матрица для вычитания с присваиванием.
|
||||
void operator-=(const PIMathMatrix<Type> &sm) {
|
||||
void operator-=(const PIMathMatrix<Type> & sm) {
|
||||
assert(_V2D::rows() == sm.rows());
|
||||
assert(_V2D::cols() == sm.cols());
|
||||
PIMM_FOR_A _V2D::mat[i] -= sm.mat[i];
|
||||
@@ -1097,9 +1107,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Умножение с присваиванием с матрицей `v`.
|
||||
//! \param sm матрица для умножения с присваиванием.
|
||||
void operator*=(const Type &v) {
|
||||
PIMM_FOR_A _V2D::mat[i] *= v;
|
||||
}
|
||||
void operator*=(const Type & v) { PIMM_FOR_A _V2D::mat[i] *= v; }
|
||||
|
||||
//! \~english
|
||||
//! \brief Division assignment with value `v`.
|
||||
@@ -1107,7 +1115,7 @@ public:
|
||||
//! \~russian
|
||||
//! \brief Деление с присваиванием с матрицей `v`.
|
||||
//! \param sm матрица для деления с присваиванием.
|
||||
void operator/=(const Type &v) {
|
||||
void operator/=(const Type & v) {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMM_FOR_A _V2D::mat[i] /= v;
|
||||
}
|
||||
@@ -1132,7 +1140,7 @@ public:
|
||||
//! \brief Матричное сложение.
|
||||
//! \param sm матричное слагаемое.
|
||||
//! \return результат матричного сложения.
|
||||
PIMathMatrix<Type> operator+(const PIMathMatrix<Type> &sm) const {
|
||||
PIMathMatrix<Type> operator+(const PIMathMatrix<Type> & sm) const {
|
||||
PIMathMatrix<Type> tm(*this);
|
||||
assert(tm.rows() == sm.rows());
|
||||
assert(tm.cols() == sm.cols());
|
||||
@@ -1148,7 +1156,7 @@ public:
|
||||
//! \brief Матричная разность.
|
||||
//! \param sm матричное вычитаемое.
|
||||
//! \return результат матричной разности.
|
||||
PIMathMatrix<Type> operator-(const PIMathMatrix<Type> &sm) const {
|
||||
PIMathMatrix<Type> operator-(const PIMathMatrix<Type> & sm) const {
|
||||
PIMathMatrix<Type> tm(*this);
|
||||
assert(tm.rows() == sm.rows());
|
||||
assert(tm.cols() == sm.cols());
|
||||
@@ -1164,7 +1172,7 @@ public:
|
||||
//! \brief Матричное произведение.
|
||||
//! \param v множитель.
|
||||
//! \return результат произведения.
|
||||
PIMathMatrix<Type> operator*(const Type &v) const {
|
||||
PIMathMatrix<Type> operator*(const Type & v) const {
|
||||
PIMathMatrix<Type> tm(*this);
|
||||
PIMM_FOR_A tm.mat[i] *= v;
|
||||
return tm;
|
||||
@@ -1178,7 +1186,7 @@ public:
|
||||
//! \brief Матричное деление.
|
||||
//! \param v делитель.
|
||||
//! \return результат деления.
|
||||
PIMathMatrix<Type> operator/(const Type &v) const {
|
||||
PIMathMatrix<Type> operator/(const Type & v) const {
|
||||
assert(piAbs<Type>(v) > PIMATHVECTOR_ZERO_CMP);
|
||||
PIMathMatrix<Type> tm(*this);
|
||||
PIMM_FOR_A tm.mat[i] /= v;
|
||||
@@ -1186,7 +1194,7 @@ public:
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Calculate Determinant of the matrix.
|
||||
//! \brief Calculate Determinant of the matrix.
|
||||
//! \details Works only with square matrix, nonzero matrices and invertible matrix.
|
||||
//! \param ok is a parameter with which we can find out if the method worked correctly.
|
||||
//! \return matrix determinant.
|
||||
@@ -1195,7 +1203,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return опеределитель матрицы.
|
||||
Type determinant(bool *ok = 0) const {
|
||||
Type determinant(bool * ok = 0) const {
|
||||
PIMathMatrix<Type> m(*this);
|
||||
bool k;
|
||||
m.toUpperTriangular(&k);
|
||||
@@ -1205,8 +1213,7 @@ public:
|
||||
ret = Type(1);
|
||||
for (uint c = 0; c < _V2D::cols_; ++c)
|
||||
for (uint r = 0; r < _V2D::rows_; ++r)
|
||||
if (r == c)
|
||||
ret *= m.element(r, c);
|
||||
if (r == c) ret *= m.element(r, c);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1237,7 +1244,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return копия преобразованной верхнетреугольной матрицы.
|
||||
PIMathMatrix<Type> &toUpperTriangular(bool *ok = 0) {
|
||||
PIMathMatrix<Type> & toUpperTriangular(bool * ok = 0) {
|
||||
assert(isSquare());
|
||||
PIMathMatrix<Type> smat(*this);
|
||||
bool ndet;
|
||||
@@ -1245,7 +1252,8 @@ public:
|
||||
Type mul;
|
||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||
ndet = true;
|
||||
for (uint j = 0; j < _V2D::rows_; ++j) if (smat.element(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;
|
||||
@@ -1257,7 +1265,8 @@ public:
|
||||
smat.swapRows(i, ++crow);
|
||||
for (uint j = i + 1; j < _V2D::rows_; ++j) {
|
||||
mul = smat.element(i, j) / smat.element(i, i);
|
||||
for (uint k = i; k < _V2D::cols_; ++k) smat.element(k, j) -= mul * smat.element(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 (PIMathFloatNullCompare(smat.element(i + 1, i + 1))) {
|
||||
@@ -1272,7 +1281,7 @@ public:
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \details Works only with square matrix, nonzero matrices and invertible matrix.
|
||||
//! \param ok is a parameter with which we can find out if the method worked correctly.
|
||||
//! \return inverted matrix.
|
||||
@@ -1281,7 +1290,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return обратная матрица.
|
||||
PIMathMatrix<Type> &invert(bool *ok = 0, PIMathVector<Type> *sv = 0) {
|
||||
PIMathMatrix<Type> & invert(bool * ok = 0, PIMathVector<Type> * sv = 0) {
|
||||
assert(isSquare());
|
||||
PIMathMatrix<Type> mtmp = PIMathMatrix<Type>::identity(_V2D::cols_, _V2D::rows_), smat(*this);
|
||||
bool ndet;
|
||||
@@ -1289,7 +1298,8 @@ public:
|
||||
Type mul, iddiv;
|
||||
for (uint i = 0; i < _V2D::cols_; ++i) {
|
||||
ndet = true;
|
||||
for (uint j = 0; j < _V2D::rows_; ++j) if (smat.element(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;
|
||||
@@ -1305,8 +1315,10 @@ public:
|
||||
}
|
||||
for (uint j = i + 1; j < _V2D::rows_; ++j) {
|
||||
mul = smat.element(i, j) / smat.element(i, 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.element(k, j) -= mul * mtmp.element(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.element(k, j) -= mul * mtmp.element(k, i);
|
||||
if (sv != 0) (*sv)[j] -= mul * (*sv)[i];
|
||||
}
|
||||
if (i < _V2D::cols_ - 1) {
|
||||
@@ -1316,15 +1328,18 @@ public:
|
||||
}
|
||||
}
|
||||
iddiv = smat.element(i, i);
|
||||
for (uint j = i; j < _V2D::cols_; ++j) smat.element(j, i) /= iddiv;
|
||||
for (uint j = 0; j < _V2D::cols_; ++j) mtmp.element(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.element(j, i) /= iddiv;
|
||||
if (sv != 0) (*sv)[i] /= iddiv;
|
||||
}
|
||||
for (uint i = _V2D::cols_ - 1; i > 0; --i) {
|
||||
for (uint j = 0; j < i; ++j) {
|
||||
mul = smat.element(i, j);
|
||||
smat.element(i, j) -= mul;
|
||||
for (uint k = 0; k < _V2D::cols_; ++k) mtmp.element(k, j) -= mul * mtmp.element(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];
|
||||
}
|
||||
}
|
||||
@@ -1334,7 +1349,7 @@ public:
|
||||
}
|
||||
|
||||
//! \~english
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \brief Matrix inversion operation.
|
||||
//! \details Works only with square matrix, nonzero matrices and invertible matrix.
|
||||
//! \param ok is a parameter with which we can find out if the method worked correctly.
|
||||
//! \return copy of inverted matrix.
|
||||
@@ -1343,7 +1358,7 @@ public:
|
||||
//! \details Работает только с квадратными, ненулевыми и обратимыми матрицами.
|
||||
//! \param ok это параметр, с помощью которого мы можем узнать, правильно ли сработал метод.
|
||||
//! \return копия обратной матрицы.
|
||||
PIMathMatrix<Type> inverted(bool *ok = 0) const {
|
||||
PIMathMatrix<Type> inverted(bool * ok = 0) const {
|
||||
PIMathMatrix<Type> tm(*this);
|
||||
tm.invert(ok);
|
||||
return tm;
|
||||
@@ -1367,7 +1382,18 @@ public:
|
||||
|
||||
#ifdef PIP_STD_IOSTREAM
|
||||
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.element(r, c); 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
|
||||
|
||||
//! \~english
|
||||
@@ -1381,7 +1407,7 @@ inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix<Type> & m
|
||||
//! \param m типа \a PIMathMatrixT.
|
||||
//! \return непечатанная в консоль \a PICout.
|
||||
template<typename Type>
|
||||
inline PICout operator<<(PICout s, const PIMathMatrix<Type> &m) {
|
||||
inline PICout operator<<(PICout s, const PIMathMatrix<Type> & m) {
|
||||
s << "Matrix{";
|
||||
for (uint r = 0; r < m.rows(); ++r) {
|
||||
for (uint c = 0; c < m.cols(); ++c) {
|
||||
@@ -1402,9 +1428,9 @@ inline PICout operator<<(PICout s, const PIMathMatrix<Type> &m) {
|
||||
//! \brief Inline-оператор для сериализации матрицы в \a PIBinaryStream.
|
||||
//! \param s типа \a PIBinaryStream.
|
||||
//! \param v типа \a PIMathMatrix.
|
||||
template <typename P, typename T>
|
||||
inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIMathMatrix<T> & v) {
|
||||
s << (const PIVector2D<T> &) v;
|
||||
template<typename P, typename T>
|
||||
inline PIBinaryStream<P> & operator<<(PIBinaryStream<P> & s, const PIMathMatrix<T> & v) {
|
||||
s << (const PIVector2D<T> &)v;
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -1416,15 +1442,15 @@ inline PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIMathMatrix
|
||||
//! \brief Inline-оператор для сериализации матрицы в \a PIByteArray.
|
||||
//! \param s типа \a PIBinaryStream.
|
||||
//! \param v типа \a PIMathMatrix.
|
||||
template <typename P, typename T>
|
||||
inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIMathMatrix<T> & v) {
|
||||
s >> (PIVector2D<T> &) v;
|
||||
template<typename P, typename T>
|
||||
inline PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIMathMatrix<T> & v) {
|
||||
s >> (PIVector2D<T> &)v;
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
//! \~english
|
||||
//! \brief Multiplying matrices by each other.
|
||||
//! \brief Multiplying matrices by each other.
|
||||
//! \details If you enter an index out of the border of the matrix there will be "undefined behavior".
|
||||
//! \param fm first matrix multiplier.
|
||||
//! \param sm second matrix multiplier.
|
||||
@@ -1436,8 +1462,7 @@ inline PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIMathMatrix<T> &
|
||||
//! \param sm вторая множитель-матрица.
|
||||
//! \return матрица, являющаяся результатом умножения.
|
||||
template<typename Type>
|
||||
inline PIMathMatrix<Type> operator*(const PIMathMatrix<Type> &fm,
|
||||
const PIMathMatrix<Type> &sm) {
|
||||
inline PIMathMatrix<Type> operator*(const PIMathMatrix<Type> & fm, const PIMathMatrix<Type> & sm) {
|
||||
assert(fm.cols() == sm.rows());
|
||||
PIMathMatrix<Type> tm(sm.cols(), fm.rows());
|
||||
Type t;
|
||||
@@ -1465,8 +1490,7 @@ inline PIMathMatrix<Type> operator*(const PIMathMatrix<Type> &fm,
|
||||
//! \param sv второй множитель-вектор.
|
||||
//! \return вектор, являющийся результатом умножения.
|
||||
template<typename Type>
|
||||
inline PIMathVector<Type> operator*(const PIMathMatrix<Type> &fm,
|
||||
const PIMathVector<Type> &sv) {
|
||||
inline PIMathVector<Type> operator*(const PIMathMatrix<Type> & fm, const PIMathVector<Type> & sv) {
|
||||
assert(fm.cols() == sv.size());
|
||||
PIMathVector<Type> tv(fm.rows());
|
||||
Type t;
|
||||
@@ -1492,8 +1516,7 @@ inline PIMathVector<Type> operator*(const PIMathMatrix<Type> &fm,
|
||||
//! \param fm первый множитель-матрица.
|
||||
//! \return вектор, являющийся результатом умножения.
|
||||
template<typename Type>
|
||||
inline PIMathVector<Type> operator*(const PIMathVector<Type> &sv,
|
||||
const PIMathMatrix<Type> &fm) {
|
||||
inline PIMathVector<Type> operator*(const PIMathVector<Type> & sv, const PIMathMatrix<Type> & fm) {
|
||||
PIMathVector<Type> tv(fm.cols());
|
||||
assert(fm.rows() == sv.size());
|
||||
Type t;
|
||||
@@ -1517,7 +1540,7 @@ inline PIMathVector<Type> operator*(const PIMathVector<Type> &sv,
|
||||
//! \param v второй множитель-матрица.
|
||||
//! \return матрица, являющаяся результатом умножения.
|
||||
template<typename Type>
|
||||
inline PIMathMatrix<Type> operator*(const Type &x, const PIMathMatrix<Type> &v) {
|
||||
inline PIMathMatrix<Type> operator*(const Type & x, const PIMathMatrix<Type> & v) {
|
||||
return v * x;
|
||||
}
|
||||
|
||||
@@ -1533,8 +1556,8 @@ typedef PIMathMatrix<double> PIMathMatrixd;
|
||||
//! \param m сопряженная транспонированная матрица.
|
||||
//! \return результат преобразования.
|
||||
template<typename T>
|
||||
PIMathMatrix<complex<T> > hermitian(const PIMathMatrix<complex<T> > &m) {
|
||||
PIMathMatrix<complex<T> > ret(m);
|
||||
PIMathMatrix<complex<T>> hermitian(const PIMathMatrix<complex<T>> & m) {
|
||||
PIMathMatrix<complex<T>> ret(m);
|
||||
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()));
|
||||
|
||||
Reference in New Issue
Block a user