1 #ifndef _MATRIX_HPP_INC_
2 #define _MATRIX_HPP_INC_
12 template<
typename T,
size_t N = 1>
14 N *
sizeof(T) < 20000, std::array<T, N>, std::vector<T> >::type;
18 operator>(
const std::complex<T> &
a,
const std::complex<T> &
b) {
19 return std::norm(
a) > std::norm(
b);
24 operator<(
const std::complex<T> &
a,
const std::complex<T> &
b) {
25 return std::norm(
a) < std::norm(
b);
31 return std::norm(
a) >= std::norm(
b);
36 operator<=(
const std::complex<T> &
a,
const std::complex<T> &
b) {
37 return std::norm(
a) <= std::norm(
b);
57 template<
typename T,
size_t N,
typename ST = storageType<T, N> >
61 static constexpr
size_t sizeN = N;
64 if constexpr (std::is_same<ST, std::vector<T> >::value) {
70 if constexpr (std::is_same<ST, std::vector<T> >::value) {
97 template<
typename T,
size_t M>
106 template<
typename T,
size_t M,
size_t N = M,
112 if constexpr (std::is_same<ST, std::vector<
StaticRow<T, N> > >::value) {
118 if constexpr (std::is_same<ST, std::vector<T> >::value) {
146 for (
auto & row :
rows) {
159 void rowAddition(
size_t destinationRow,
size_t sourceRow, T scalingFactor) {
160 assert(0 <= destinationRow && destinationRow <= N);
161 assert(0 <= sourceRow && sourceRow <= M);
162 for (
size_t i = 0;
i < N;
i++) {
163 rows[destinationRow][
i] += scalingFactor *
rows[sourceRow][
i];
173 for (
size_t m = 0;
m < M;
m++) {
174 for (
size_t n = 0; n < N; n++) {
201 for (
size_t m = 0;
m < M;
m++) {
202 for (
size_t k = 0;
k < N;
k++) {
203 for (
size_t n = 0; n2 < N2; n2++) {
217 for (
size_t m = 0;
m < M;
m++) {
218 for (
size_t n = 0; n < N; n++) {
219 dest[
m][n] =
rows[
m][n] + rhs[
m][n];
232 for (
size_t m = 0;
m < M;
m++) {
233 for (
size_t n = 0; n < N; n++) {
234 dest[
m][n] =
rows[
m][n] - dest[
m][n]
240 std::stringstream
toRet;
241 for (
const auto & row :
rows) {
242 for (
const auto & val : row.columns) {
243 toRet << std::setw(5) << std::setprecision(2) << val <<
" ";
253 static_assert(N == M,
"StaticMatrix must be a square");
261 static_assert(N == M,
"StaticMatrix must be a square");
265 for (
size_t n = 0; n < N; n++) {
270 for (
size_t r = 0;
r < M - 1;
r++) {
272 size_t largestRow =
r;
273 auto maxV =
abs(dest.
u[
r][
r]);
274 for (
size_t r2 =
r + 1; r2 < M; r2++) {
275 if (
abs(dest.
u[r2][
r]) > maxV) {
276 maxV =
abs(dest.
u[r2][
r]);
283 std::swap(dest.
p[
r], dest.
p[largestRow]);
285 for (
size_t n = 0; n <
r; n++) {
286 std::swap(dest.
l[
r][n], dest.
l[largestRow][n]);
290 for (
size_t m =
r + 1;
m < M;
m++) {
294 T multiplier = dest.
u[
m][
r] / dest.
u[
r][
r];
296 dest.
l[
m][
r] = multiplier;
313 for (
size_t i = 0;
i < M;
i++) {
314 dest[
i] = rhs[lu.
p[
i]];
318 for (
size_t m = 0;
m < M;
m++) {
320 for (
size_t n = 0; n <
m; n++) {
321 val -= scratchSpace[n][0] * lu.
l[
m][n];
323 scratchSpace[
m][0] = val / lu.
l[
m][
m];
327 for (
size_t m = 0;
m < M;
m++) {
328 T val = scratchSpace[M -
m - 1][0];
329 for (
size_t n = 0; n <
m; n++) {
330 val -= dest[M - n - 1][0] * lu.
u[M -
m - 1][N - n - 1];
332 dest[M -
m - 1][0] = val / lu.
u[M -
m - 1][M -
m - 1];
341 template<
typename T,
size_t M>
345 std::array<size_t, M>
p;
348 std::stringstream
toRet;
352 for (
size_t i = 0;
i <
p.size();
i++) {
353 toRet << std::setw(5) <<
p[
i] <<
" ";
std::complex< double > operator>(const std::complex< T > &a, const std::complex< T > &b)
std::complex< double > operator<(const std::complex< T > &a, const std::complex< T > &b)
typename std::conditional< N *sizeof(T)< 20000, std::array< T, N >, std::vector< T > >::type storageType
std::complex< double > operator>=(const std::complex< T > &a, const std::complex< T > &b)
std::complex< double > operator<=(const std::complex< T > &a, const std::complex< T > &b)
Find a complex rational model for freq domain data for a
end if abs(real(dotprod))>rstoerst rstoerst
requires(std::is_arithmetic< ValType >::value &&NumVars >=0) struct DiffVar
A compile-time sized L U and pivot grouping.
StaticMatrix< T, M, M > u
std::array< size_t, M > p
StaticMatrix< T, M, M > l
A compile-time sized matrix.
static constexpr size_t sizeM
void add(const StaticMatrix< T, M, N > &rhs, StaticMatrix< T, M, N > &dest) const
void leftDivide(const StaticMatrix< T, M, 1 > &rhs, const StaticLUPair< T, M > &lu, StaticMatrix< T, M, 1 > &scratchSpace, StaticMatrix< T, M, 1 > &dest) const
void rowAddition(size_t destinationRow, size_t sourceRow, T scalingFactor)
StaticMatrix< T, M, N2 > multiply(const StaticMatrix< T, N, N2 > &rhs) const
void subtract(const StaticMatrix< T, N, N > &rhs, StaticMatrix< T, M, N > &dest) const
StaticLUPair< T, M > luPair() const
std::string toString() const
static constexpr size_t sizeN
StaticMatrix< T, M, N > add(const StaticMatrix< T, M, N > &rhs) const
void swapRows(size_t row1, size_t row2)
void luPair(StaticLUPair< T, M > &dest) const
StaticMatrix< T, N, M > transpose()
StaticMatrix< T, M, 1 > leftDivide(const StaticMatrix< T, M, 1 > &rhs) const
StaticRow< T, N > operator[](size_t index) const
StaticMatrix< T, M, N > subtract(const StaticMatrix< T, N, N > &rhs) const
StaticRow< T, N > & operator[](size_t index)
void multiply(const StaticMatrix< T, N, N2 > &rhs, StaticMatrix< T, M, N2 > &dest) const
StaticMatrix(T initialValue)
A compile-time sized matrix row.
T dot(StaticRow< T, N > other)
static constexpr size_t sizeN
const T & operator[](size_t index) const
StaticRow(T initialValue)
T & operator[](size_t index)