48 #ifdef HAVE_TEUCHOS_ARPREC
49 #include <arprec/mp_real.h>
52 #ifdef HAVE_TEUCHOS_GNU_MP
58 using namespace Teuchos;
60 #ifdef HAVE_TEUCHOS_ARPREC
61 #define SType1 mp_real
62 #elif defined(HAVE_TEUCHOS_GNU_MP)
63 #define SType1 mpf_class
70 template<
typename TYPE>
73 template<
typename TYPE>
76 template<
typename TYPE1,
typename TYPE2>
79 template<
typename TYPE1,
typename TYPE2>
82 #ifdef HAVE_TEUCHOS_ARPREC
90 #ifdef HAVE_TEUCHOS_GNU_MP
98 template<
typename TYPE>
101 template<
typename TYPE>
102 int Solve(
int, TYPE*, TYPE*, TYPE*);
104 template<
typename TYPE>
107 template<
typename TYPE>
110 #ifdef HAVE_TEUCHOS_ARPREC
118 int main(
int argc,
char *argv[]) {
128 hilbertCLP.
setOption(
"precision", &precision,
"Arbitrary precision");
129 bool verbose =
false;
130 hilbertCLP.
setOption(
"verbose",
"quiet", &verbose,
"Verbosity of example");
133 hilbertCLP.
parse( argc, argv );
135 #ifdef HAVE_TEUCHOS_ARPREC
136 mp::mp_init( precision );
139 #ifdef HAVE_TEUCHOS_GNU_MP
140 mpf_set_default_prec( precision );
141 std::cout<<
"The precision of the GNU MP variable is (in bits) : "<< mpf_get_default_prec() << std::endl;
155 SType1 result1, result2_1;
156 SType2 result2, result1_2;
163 while ( compSType1>0 || compSType2>0 || convSType1>0 || convSType2>0 ) {
165 if (compSType1 > 0) {
171 ConstructHilbertMatrix< SType1 >(H1,
n);
172 ConstructHilbertSumVector< SType1 >(b1,
n);
176 compSType1 =
Solve(n, H1, b1, &result1);
177 if (compSType1 < 0 && verbose)
178 std::cout <<
typeid( result1 ).name() <<
" -- Cholesky factorization failed (negative diagonal) at row "<<-compSType1<< std::endl;
181 delete [] H1; H1 = 0;
182 delete [] b1; b1 = 0;
184 if (compSType2 > 0) {
190 ConstructHilbertMatrix< SType2 >(H2,
n);
191 ConstructHilbertSumVector< SType2 >(b2,
n);
195 compSType2 =
Solve(n, H2, b2, &result2);
196 if (compSType2 < 0 && verbose)
197 std::cout <<
typeid( result2 ).name() <<
" -- Cholesky factorization failed (negative diagonal) at row "<<-compSType2<< std::endl;
200 delete [] H2; H2 = 0;
201 delete [] b2; b2 = 0;
203 if (convSType2 > 0) {
207 if (!H1) H1 =
new SType1[ n*
n ];
212 if (!H2) H2 =
new SType2[ n*
n ];
222 convSType2 =
Solve(n, H2, b2, &result1_2);
223 if (convSType2 < 0 && verbose)
224 std::cout <<
typeid( result1_2 ).name() <<
" (converted) -- Cholesky factorization failed (negative diagonal) at row "<<-convSType2<< std::endl;
228 delete [] H2; H2 = 0;
229 delete [] b2; b2 = 0;
230 delete [] H1; H1 = 0;
231 delete [] b1; b1 = 0;
233 if (convSType1 > 0) {
237 if (!H2) H2 =
new SType2[ n*
n ];
242 if (!H1) H1 =
new SType1[ n*
n ];
252 convSType1 =
Solve(n, H1, b1, &result2_1);
253 if (convSType1 < 0 && verbose)
254 std::cout <<
typeid( result2_1 ).name() <<
" (converted) -- Cholesky factorization failed (negative diagonal) at row "<<-convSType1<< std::endl;
258 delete [] H1; H1 = 0;
259 delete [] b1; b1 = 0;
260 delete [] H2; H2 = 0;
261 delete [] b2; b2 = 0;
263 if (verbose && (compSType1>0 || compSType2>0 || convSType1>0 || convSType2>0) ) {
264 std::cout <<
"***************************************************" << std::endl;
265 std::cout <<
"Dimension of Hilbert Matrix : "<< n << std::endl;
266 std::cout <<
"***************************************************" << std::endl;
267 std::cout <<
"Datatype : Absolute error || x_hat - x ||"<< std::endl;
268 std::cout <<
"---------------------------------------------------" << std::endl;
270 if (compSType1>0 && verbose)
271 std::cout <<
typeid( result1 ).name() <<
"\t : "<< result1 << std::endl;
273 if (convSType1>0 && verbose)
274 std::cout <<
typeid( result2_1 ).name() <<
"(converted) : "<< result2_1 << std::endl;
276 if (convSType2>0 && verbose)
277 std::cout <<
typeid( result1_2 ).name() <<
"(converted) : "<< result2_1 << std::endl;
279 if (compSType2>0 && verbose)
280 std::cout <<
typeid( result2 ).name() <<
"\t : "<< result2 << std::endl;
287 #ifdef HAVE_TEUCHOS_ARPREC
294 template<
typename TYPE>
297 for(
int i = 0; i <
n; i++) {
298 for(
int j = 0; j <
n; j++) {
299 A[i + (j *
n)] = (scal1 / (i + j + scal1));
304 template<
typename TYPE>
308 for(
int i = 0; i <
n; i++) {
310 for(
int j = 0; j <
n; j++) {
311 x[i] += (scal1 / (i + j + scal1));
316 template<
typename TYPE1,
typename TYPE2>
318 for(
int i = 0; i <
n; i++) {
319 for(
int j = 0; j <
n; j++) {
320 B[i + (j *
n)] = A[i + (j * n)];
325 template<
typename TYPE1,
typename TYPE2>
327 for(
int i = 0; i <
n; i++) {
332 #ifdef HAVE_TEUCHOS_ARPREC
335 for(
int i = 0; i <
n; i++) {
336 for(
int j = 0; j <
n; j++) {
337 B[i + (j *
n)] = dble( A[i + (j * n)] );
344 for(
int i = 0; i <
n; i++) {
350 #ifdef HAVE_TEUCHOS_GNU_MP
353 for(
int i = 0; i <
n; i++) {
354 for(
int j = 0; j <
n; j++) {
355 B[i + (j *
n)] = A[i + (j * n)].get_d();
362 for(
int i = 0; i <
n; i++) {
368 template<
typename TYPE>
371 for(
int k = 0; k <
n; k++) {
372 for(
int j = k + 1; j <
n; j++) {
373 TYPE alpha = A[k + (j *
n)] / A[k + (k * n)];
374 for(
int i = j; i <
n; i++) {
375 A[j + (i *
n)] -= (alpha * A[k + (i * n)]);
378 if(A[k + (k * n)] <= scal0) {
382 for(
int i = k; i <
n; i++) {
383 A[k + (i *
n)] /= beta;
389 template<
typename TYPE>
390 int Solve(
int n, TYPE* H, TYPE* b, TYPE* err) {
393 TYPE scalNeg1 = scal0 - scal1;
395 TYPE* x =
new TYPE[
n];
396 for(
int i = 0; i <
n; i++) {
399 int choleskySuccessful =
Cholesky(H, n);
400 if(choleskySuccessful > 0) {
401 blasObj.
TRSM(
Teuchos::LEFT_SIDE,
Teuchos::UPPER_TRI,
Teuchos::TRANS,
Teuchos::NON_UNIT_DIAG, n, 1, scal1, H, n, b, n);
402 blasObj.
TRSM(
Teuchos::LEFT_SIDE,
Teuchos::UPPER_TRI,
Teuchos::NO_TRANS,
Teuchos::NON_UNIT_DIAG, n, 1, scal1, H, n, b, n);
403 blasObj.
AXPY(n, scalNeg1, x, 1, b, 1);
404 *err = blasObj.
NRM2(n, b, 1);
407 return choleskySuccessful;
410 template<
typename TYPE>
413 for(
int i = 0; i <
n; i++) {
414 std::cout <<
" " << x[i];
416 std::cout <<
" ]" << std::endl;
419 template<
typename TYPE>
422 for(
int i = 0; i < m; i++) {
427 for(
int j = 0; j <
n; j++) {
428 std::cout <<
" " << a[i + (j * m)];
432 std::cout << std::endl;
435 std::cout <<
"]" << std::endl;
438 #ifdef HAVE_TEUCHOS_ARPREC
442 for(
int i = 0; i <
n; i++) {
448 std::cout <<
"]" << std::endl;
454 for(
int i = 0; i < m; i++) {
459 for(
int j = 0; j <
n; j++) {
463 std::cout <<
" " << a[i + (j * m)];
467 std::cout << std::endl;
470 std::cout <<
"]" << std::endl;
void TRSM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const OrdinalType &m, const OrdinalType &n, const alpha_type alpha, const A_type *A, const OrdinalType &lda, ScalarType *B, const OrdinalType &ldb) const
Solves the matrix equations: op(A)*X=alpha*B or X*op(A)=alpha*B where X and B are m by n matrices...
void AXPY(const OrdinalType &n, const alpha_type alpha, const x_type *x, const OrdinalType &incx, ScalarType *y, const OrdinalType &incy) const
Perform the operation: y <- y+alpha*x.
void ConstructHilbertSumVector(TYPE *, int)
Templated interface class to BLAS routines.
static T squareroot(T x)
Returns a number of magnitudeType that is the square root of this scalar type x.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
void PrintArrayAsMatrix(TYPE *, int, int)
ScalarTraits< ScalarType >::magnitudeType NRM2(const OrdinalType &n, const ScalarType *x, const OrdinalType &incx) const
Compute the 2-norm of the vector x.
void PrintArrayAsVector(TYPE *, int)
void ConvertHilbertSumVector(TYPE1 *, TYPE2 *, int)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
std::string Teuchos_Version()
int main(int argc, char *argv[])
void ConvertHilbertMatrix(TYPE1 *, TYPE2 *, int)
Basic command line parser for input from (argc,argv[])
void ConstructHilbertMatrix(TYPE *, int)
static T zero()
Returns representation of zero for this scalar type.
int Cholesky(TYPE *, int)
int Solve(int, TYPE *, TYPE *, TYPE *)
static T one()
Returns representation of one for this scalar type.
Class that helps parse command line input arguments from (argc,argv[]) and set options.