-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_iterative_ref_mpreal.cpp
More file actions
153 lines (124 loc) · 5.18 KB
/
test_iterative_ref_mpreal.cpp
File metadata and controls
153 lines (124 loc) · 5.18 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
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
145
146
147
148
149
150
151
152
153
//******************************************************************************
// test_iterative_ref_mpreal.cpp : Interative refinement method based
// on direct method (double, dd_real and qd_real + MPFR)
// Copyright (C) 2019 Tomonori Kouya
//
// 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 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.
//
// 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/>.
//
//******************************************************************************
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
// Multiple precision with QD
#define QD_INLINE
#include "qd/qd_real.h"
#include "qd/fpu.h"
// mpreal
#include "mpreal.h"
// Template linear compucation with double, QD, MPFR/GMP
#include "template_linear.h"
// Time routines
#include "get_secv.h"
/* Simple Estimation of Condition Number */
using namespace std;
using namespace mpfr;
int main(int argc, char *argv[])
{
unsigned long prec;
int i, j, dimension, *pivot, itimes;
mpreal *matrix, *true_x, *b, *x;
mpreal rtol, atol;
double start_time, end_time;
unsigned int old_cw;
if(argc <= 2)
{
cerr << "USAGE: " << argv[0] << " [dimension] [prec]" << endl;
return EXIT_SUCCESS;
}
prec = atoi(argv[2]);
if(prec <= 1)
{
cerr << "ERROR: prec = " << prec << " is illegal!" << endl;
return EXIT_FAILURE;
}
mpreal::set_default_prec(prec);
dimension = atoi(argv[1]);
if(dimension <= 1)
{
cerr << "ERROR: dimension = " << dimension << " is illegal!" << endl;
return EXIT_FAILURE;
}
fpu_fix_start(&old_cw);
// initialize
matrix = new mpreal[dimension * dimension];
true_x = new mpreal[dimension];
x = new mpreal[dimension];
b = new mpreal[dimension];
pivot = new int[dimension];
set_test_linear_eq<mpreal>(matrix, true_x, b, dimension);
// run LU decomposion
start_time = get_secv();
LU<mpreal>(matrix, dimension, pivot);
// backward & forward substitution
solve_LU_linear_eq<mpreal>(x, matrix, b, dimension, pivot);
end_time = get_secv() - start_time;
cout << "comp.time(second): " << end_time << ", relerr = " << get_relerr_norm2(x, true_x, dimension) << endl;
// double-MPFR
// set test problem
set_test_linear_eq<mpreal>(matrix, true_x, b, dimension);
// rtol = "1.0e-300"; atol = "0.0";
rtol = "1.0e-1000"; atol = "0.0";
// rtol = "1.0e-50"; atol = "0.0";
start_time = get_secv();
itimes = iterative_refinement<mpreal, double>(x, matrix, b, rtol, atol, dimension, dimension * 10);
end_time = get_secv() - start_time;
cout << "Iterative Times: " << itimes << ", Dimension: " << dimension << endl;
cout << "comp.time(second): " << end_time << ", relerr = " << get_relerr_norm2(x, true_x, dimension) << endl;
// print solution
// for(i = 0; i < dimension; i++)
// cout << setw(3) << i << " " << scientific << setprecision(mpfr::bits2digits(mpreal::get_default_prec())) << x[i] << " " << setprecision(3) << get_relerr(x[i], true_x[i]) << endl;
// DD-MPFR
// set test problem
start_time = get_secv();
set_test_linear_eq<mpreal>(matrix, true_x, b, dimension);
// rtol = "1.0e-100"; atol = "0.0";
itimes = iterative_refinement<mpreal, dd_real>(x, matrix, b, rtol, atol, dimension, dimension * 10);
end_time = get_secv() - start_time;
cout << "Iterative Times: " << itimes << ", Dimension: " << dimension << endl;
cout << "comp.time(second): " << end_time << ", relerr = " << get_relerr_norm2(x, true_x, dimension) << endl;
// print solution
// for(i = 0; i < dimension; i++)
// cout << setw(3) << i << " " << scientific << setprecision(mpfr::bits2digits(mpreal::get_default_prec())) << x[i] << " " << setprecision(3) << get_relerr(x[i], true_x[i]) << endl;
// QD-MPFR
// set test problem
start_time = get_secv();
set_test_linear_eq<mpreal>(matrix, true_x, b, dimension);
// rtol = "1.0e-200"; atol = "0.0";
itimes = iterative_refinement<mpreal, qd_real>(x, matrix, b, rtol, atol, dimension, dimension * 10);
end_time = get_secv() - start_time;
cout << "Iterative Times: " << itimes << ", Dimension: " << dimension << endl;
cout << "comp.time(second): " << end_time << ", relerr = " << get_relerr_norm2(x, true_x, dimension) << endl;
// print solution
// for(i = 0; i < dimension; i++)
// cout << setw(3) << i << " " << scientific << setprecision(mpfr::bits2digits(mpreal::get_default_prec())) << x[i] << " " << setprecision(3) << get_relerr(x[i], true_x[i]) << endl;
fpu_fix_end(&old_cw);
// free
delete_array<mpreal>(matrix, dimension * dimension);
delete_array<mpreal>(true_x, dimension);
delete_array<mpreal>(x, dimension);
delete_array<mpreal>(b, dimension);
delete pivot;
return EXIT_SUCCESS;
}