-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionLU.cpp
More file actions
45 lines (40 loc) · 1.06 KB
/
Copy pathExpressionLU.cpp
File metadata and controls
45 lines (40 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "ExpressionLU.h"
#include <iostream>
ExpressionLU::ExpressionLU(const ExpressionMatrix& a)
: mL(a.dimensions().first, a.dimensions().second),
mU(a)
{
decompose();
}
void ExpressionLU::decompose()
{
const auto [rows, cols] = mU.dimensions();
mL = ExpressionMatrix(Matrix::identity(rows));
for (size_t k = 0; k < rows - 1; k++)
{
const std::string pivot = mU(k, k);
if (pivot == "0.0")
{
std::cout << "Zero pivot, aborting.\n";
break;
}
// These updating of rows can be parallelized
for (size_t j = k + 1; j < rows; j++)
{
const std::string factor = "(("+ mU(j, k) + ") / (" + pivot + "))";
mL(j, k) = factor;
for (size_t i = 0; i < cols; i++)
mU(j, i) = "(" + mU(j, i) + " - ((" + mU(k, i) + ") * " + factor + "))";
}
}
}
std::string ExpressionLU::determinant() const
{
std::cout << "LU\n " << mU << std::endl;
// det(A) = det(L)det(U), det(L) = 1
std::string determinant = "1";
const size_t size = mU.dimensions().first;
for (size_t i = 0; i < size; i++)
determinant += " * (" + mU(i, i) + ")";
return determinant;
}