-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlu_example_mpreal.cpp
144 lines (119 loc) · 3.45 KB
/
lu_example_mpreal.cpp
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//
// A minor modification of the lu example to use mpreal instead of double
//
#include <tlapack/plugins/legacyArray.hpp>
#include <tlapack/plugins/mpreal.hpp>
#include <tlapack/lapack/getrf.hpp>
#include <tlapack/lapack/lu_mult.hpp>
#include <tlapack/lapack/lange.hpp>
#include <tlapack/lapack/lacpy.hpp>
#include <mpreal.h>
#include <vector>
#include <iostream>
#include <iomanip>
using namespace tlapack;
int main()
{
using T = double;
using idx_t = size_t;
idx_t n = 10;
// const int digits = 50;
// // // Setup default precision for all subsequent computations
// // // MPFR accepts precision in bits - so we do the conversion
// mpfr::mpreal::set_default_prec(mpfr::digits2bits(digits));
// Create a matrix and vector
// Note: these are all managed as objects
// no need to deallocate them manually
std::vector<T> A_(n * n);
tlapack::LegacyMatrix<T, idx_t> A(n, n, A_.data(), n);
std::vector<T> A_copy_(n * n);
tlapack::LegacyMatrix<T, idx_t> A_copy(n, n, A_copy_.data(), n);
std::vector<idx_t> ipiv(n);
// Initialize the matrix
for (idx_t i = 0; i < n; i++)
{
for (idx_t j = 0; j < n; j++)
{
A(i, j) = rand() % 100;
}
}
// Store A in A_copy for later testing
lacpy(Uplo::General, A, A_copy);
std::cout << "Matrix A:" << std::endl;
for (idx_t i = 0; i < n; i++)
{
for (idx_t j = 0; j < n; j++)
{
std::cout << std::setw(9) << A(i, j) << " ";
}
std::cout << std::endl;
}
// Perform LU factorization
int info = tlapack::getrf(A, ipiv);
std::cout << "Info:" << info << std::endl;
std::cout << "Matrix L:" << std::endl;
for (idx_t i = 0; i < n; i++)
{
for (idx_t j = 0; j < n; j++)
{
if (i > j)
{
std::cout << std::setw(9) << A(i, j) << " ";
}
else if (i == j)
{
std::cout << std::setw(9) << 1 << " ";
}
else
{
std::cout << std::setw(9) << 0 << " ";
}
}
std::cout << std::endl;
}
std::cout << "Matrix U:" << std::endl;
for (idx_t i = 0; i < n; i++)
{
for (idx_t j = 0; j < n; j++)
{
if (i <= j)
{
std::cout << std::setw(9) << A(i, j) << " ";
}
else
{
std::cout << std::setw(9) << 0 << " ";
}
}
std::cout << std::endl;
}
std::cout << "Pivot vector:" << std::endl;
for (idx_t i = 0; i < n; i++)
{
std::cout << ipiv[i] << " ";
}
std::cout << std::endl;
tlapack::lu_mult(A);
for (idx_t j = n - 1; j != idx_t(-1); j--)
{
auto vect1 = tlapack::row(A, j);
auto vect2 = tlapack::row(A, ipiv[j]);
tlapack::swap(vect1, vect2);
}
std::cout << "inv(P) * L*U:" << std::endl;
for (idx_t i = 0; i < n; i++)
{
for (idx_t j = 0; j < n; j++)
{
std::cout << std::setw(9) << A(i, j) << " ";
}
std::cout << std::endl;
}
// Calculate A - inv(P) * L*U (i.e. check the result of lu and lu_mult)
for (idx_t i = 0; i < n; i++)
for (idx_t j = 0; j < n; j++)
A_copy(i, j) -= A(i, j);
auto error_norm = tlapack::lange(tlapack::Norm::Fro, A_copy);
std::cout << "Error norm: " << error_norm << std::endl;
return 0;
}