Reputation: 367
I am using the Armadillo linear algebra library to diagonalize matrices. I need to increase the number of digits displayed/written to a file at the end. According to Armadillo's reference, "arma::mat" will create a double matrix. So, I tried using std::setprecision from "iomanip", but it did not quite work. Here is a minimal code that captures the problem:
#include<iostream>
#include<armadillo>
#include<iomanip>
int main()
{
double Trace_A = 0.;
arma::mat A;
A = :arma::randu<arma::mat>(5,5);
Trace = arma::trace(A);
// Normal output
std::cout << "A = \n" <<A ;
std::cout << "Trace(A) = " << Trace_A << std::endl;
std::cout << "---------------------------------------------" << std::endl;
// Displaying more digits
std::cout << std::fixed << std::setprecision(15);
std::cout << "A = \n" << A;
std::cout << "Trace(A) = " << Trace_A << std::endl;
}
And, here is what I get:
A =
0.8402 0.1976 0.4774 0.9162 0.0163
0.3944 0.3352 0.6289 0.6357 0.2429
0.7831 0.7682 0.3648 0.7173 0.1372
0.7984 0.2778 0.5134 0.1416 0.8042
0.9116 0.5540 0.9522 0.6070 0.1567
Trace(A) = 1.83848
---------------------------------------------
A =
0.8402 0.1976 0.4774 0.9162 0.0163
0.3944 0.3352 0.6289 0.6357 0.2429
0.7831 0.7682 0.3648 0.7173 0.1372
0.7984 0.2778 0.5134 0.1416 0.8042
0.9116 0.5540 0.9522 0.6070 0.1567
Trace(A) = 1.838476590271330
Curiously, it works for the trace which is passed on to the double variable "Trace_A" but not for the matrix itself. Any idea what I am doing wrong here?
Upvotes: 4
Views: 2223
Reputation: 335
Following is one of the ways to set precision before using raw_print
arma::mat A = arma::randu<arma::mat>(5,5);
cout.precision(11);
cout.setf(ios::fixed);
A.raw_print(cout, "A:");
Normal of setting precision also works
arma::mat A = arma::randu<arma::mat>(5,5);
cout << fixed << setprecision(11);
A.raw_print(cout, "A:");
Upvotes: 1
Reputation: 367
I think I figured out how to do this. It can be done using "raw_print":
A.raw_print(std::cout);
This gives:
0.8402 0.1976 0.4774 0.9162 0.0163
0.3944 0.3352 0.6289 0.6357 0.2429
0.7831 0.7682 0.3648 0.7173 0.1372
0.7984 0.2778 0.5134 0.1416 0.8042
0.9116 0.5540 0.9522 0.6070 0.1567
0.84018771715 0.19755136929 0.47739705186 0.91619506800 0.01630057162
0.39438292682 0.33522275571 0.62887092476 0.63571172796 0.24288677063
0.78309922376 0.76822959481 0.36478447279 0.71729692943 0.13723157679
0.79844003348 0.27777471080 0.51340091020 0.14160255536 0.80417675423
0.91164735794 0.55396995580 0.95222972517 0.60696887626 0.15667908925
Upvotes: 6