28 using Eigen::Matrix3f;
29 using Eigen::MatrixXf;
35 int main(
int argc,
char **argv) {
37 const unsigned int repeats = 5;
46 MatrixXf A = MatrixXf::Random(100,100);
47 MatrixXf b = MatrixXf::Identity(100,100);
51 for (
unsigned int i = 0; i < repeats; ++i ) {
52 x = A.colPivHouseholderQr().solve(b);
56 for (
unsigned int i = 0; i < repeats; ++i ) {
57 A = MatrixXf::Random(100,100);
59 x = A.colPivHouseholderQr().solve(b);
60 times[0] += stopwatch.split();
63 for (
unsigned int i = 0; i < repeats; ++i ) {
64 A = MatrixXf::Random(100,100);
66 x = A.fullPivHouseholderQr().solve(b);
67 times[1] += stopwatch.split();
70 for (
unsigned int i = 0; i < repeats; ++i ) {
71 A = MatrixXf::Random(100,100);
73 x = A.partialPivLu().solve(b);
74 times[2] += stopwatch.split();
77 for (
unsigned int i = 0; i < repeats; ++i ) {
78 A = MatrixXf::Random(100,100);
80 x = A.fullPivLu().solve(b);
81 times[3] += stopwatch.split();
85 std::cout << std::endl;
86 std::cout <<
"************** Eigen3 Decompositions ***************" << std::endl;
87 std::cout << std::endl;
88 std::cout <<
"Ax=b where A = Rand(100,100) and b = Identity(100x100)" << std::endl;
89 std::cout <<
"i.e. solving the inverse (results averaged over " << repeats <<
" runs)" << std::endl;
90 std::cout << std::endl;
91 std::cout <<
"ColPivHouseHolder : " << format(
static_cast<double>(times[0])/
static_cast<double>(repeats)) << std::endl;
92 std::cout <<
"FullPivHouseHolder : " << format(
static_cast<double>(times[1])/
static_cast<double>(repeats)) << std::endl;
93 std::cout <<
"PartialPivLu : " << format(
static_cast<double>(times[2])/
static_cast<double>(repeats)) << std::endl;
94 std::cout <<
"FullPivLu : " << format(
static_cast<double>(times[3])/
static_cast<double>(repeats)) << std::endl;
95 std::cout << std::endl;
97 b = MatrixXf::Random(100,1);
99 for (
unsigned int i = 0; i < repeats; ++i ) {
100 A = MatrixXf::Random(100,100);
102 x = A.colPivHouseholderQr().solve(b);
103 times[4] += stopwatch.split();
106 for (
unsigned int i = 0; i < repeats; ++i ) {
107 A = MatrixXf::Random(100,100);
109 x = A.fullPivHouseholderQr().solve(b);
110 times[5] += stopwatch.split();
113 for (
unsigned int i = 0; i < repeats; ++i ) {
114 A = MatrixXf::Random(100,100);
116 x = A.partialPivLu().solve(b);
117 times[6] += stopwatch.split();
120 for (
unsigned int i = 0; i < repeats; ++i ) {
121 A = MatrixXf::Random(100,100);
123 x = A.fullPivLu().solve(b);
124 times[7] += stopwatch.split();
127 std::cout <<
"Ax=b where A = Rand(100,100) and b = Random(100x1)" << std::endl;
128 std::cout <<
"i.e. solving linear equations (results averaged over " << repeats <<
" runs)" << std::endl;
129 std::cout << std::endl;
130 std::cout <<
"ColPivHouseHolder : " << format(
static_cast<double>(times[4])/
static_cast<double>(repeats)) << std::endl;
131 std::cout <<
"FullPivHouseHolder : " << format(
static_cast<double>(times[5])/
static_cast<double>(repeats)) << std::endl;
132 std::cout <<
"PartialPivLu : " << format(
static_cast<double>(times[6])/
static_cast<double>(repeats)) << std::endl;
133 std::cout <<
"FullPivLu : " << format(
static_cast<double>(times[7])/
static_cast<double>(repeats)) << std::endl;
134 std::cout << std::endl;
136 b = MatrixXf::Identity(100,100);
138 for (
unsigned int i = 0; i < repeats; ++i ) {
139 MatrixXf D = MatrixXf::Random(100,100);
142 x = A.colPivHouseholderQr().solve(b);
143 times[8] += stopwatch.split();
146 for (
unsigned int i = 0; i < repeats; ++i ) {
147 MatrixXf D = MatrixXf::Random(100,100);
150 x = A.fullPivHouseholderQr().solve(b);
151 times[9] += stopwatch.split();
153 times[10].stamp(0,0);
154 for (
unsigned int i = 0; i < repeats; ++i ) {
155 MatrixXf D = MatrixXf::Random(100,100);
158 x = A.partialPivLu().solve(b);
159 times[10] += stopwatch.split();
161 times[11].stamp(0,0);
162 for (
unsigned int i = 0; i < repeats; ++i ) {
163 MatrixXf D = MatrixXf::Random(100,100);
166 x = A.fullPivLu().solve(b);
167 times[11] += stopwatch.split();
169 times[12].stamp(0,0);
170 for (
unsigned int i = 0; i < repeats; ++i ) {
171 MatrixXf D = MatrixXf::Random(100,100);
174 x = A.llt().solve(b);
176 times[12] += stopwatch.split();
178 times[13].stamp(0,0);
179 for (
unsigned int i = 0; i < repeats; ++i ) {
180 MatrixXf D = MatrixXf::Random(100,100);
183 x = A.ldlt().solve(b);
185 times[13] += stopwatch.split();
188 std::cout <<
"Ax=b where A = RandPosDef(100,100) and b = Identity(100x100)" << std::endl;
189 std::cout <<
"(results averaged over " << repeats <<
" runs)" << std::endl;
190 std::cout << std::endl;
191 std::cout <<
"ColPivHouseHolder : " << format(
static_cast<double>(times[8])/
static_cast<double>(repeats)) << std::endl;
192 std::cout <<
"FullPivHouseHolder : " << format(
static_cast<double>(times[9])/
static_cast<double>(repeats)) << std::endl;
193 std::cout <<
"PartialPivLu : " << format(
static_cast<double>(times[10])/
static_cast<double>(repeats)) << std::endl;
194 std::cout <<
"FullPivLu : " << format(
static_cast<double>(times[11])/
static_cast<double>(repeats)) << std::endl;
195 std::cout <<
"llt : " << format(
static_cast<double>(times[12])/
static_cast<double>(repeats)) << std::endl;
196 std::cout <<
"ldlt : " << format(
static_cast<double>(times[13])/
static_cast<double>(repeats)) << std::endl;
197 std::cout << std::endl;