-
Notifications
You must be signed in to change notification settings - Fork 258
/
Copy pathmatmul.cpp
51 lines (40 loc) · 1.2 KB
/
matmul.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
//#include <cstdlib>
#include <Eigen/Dense>
#include <iostream>
#include <libnotify.h>
using namespace std;
using namespace Eigen;
#ifdef __clang__
static constexpr auto COMPILER = "clang++";
#else
static constexpr auto COMPILER = "g++";
#endif
// NOTE: Eigen requires explicit variable types for the proper
// compile-time template processing
MatrixXd build_matrix(int n, double seed) {
ArrayXXd i_idxs = ArrayXXd::Zero(n, n);
i_idxs.colwise() = ArrayXd::LinSpaced(n, 0, n - 1);
ArrayXXd j_idxs = ArrayXXd::Zero(n, n);
j_idxs.rowwise() = ArrayXd::LinSpaced(n, 0, n - 1).transpose();
ArrayXXd result = (i_idxs - j_idxs) * (i_idxs + j_idxs) * (seed / n / n);
return result.matrix();
}
double calc(int n) {
n = n / 2 * 2;
MatrixXd a = build_matrix(n, 1.0);
MatrixXd b = build_matrix(n, 2.0);
MatrixXd d = a * b;
return d(n / 2, n / 2);
}
int main(int argc, char **argv) {
auto n = argc > 1 ? atoi(argv[1]) : 100;
auto left = calc(101);
auto right = -18.67;
if (fabs(left - right) > 0.1) {
cerr << left << " != " << right << endl;
exit(EXIT_FAILURE);
}
auto results =
notifying_invoke([&]() { return calc(n); }, "C++/{} (Eigen)", COMPILER);
cout << results << endl;
}