-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculations.cpp
More file actions
54 lines (45 loc) · 1.24 KB
/
calculations.cpp
File metadata and controls
54 lines (45 loc) · 1.24 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
#include <iostream>
#include <Eigen/Dense>
#include <fstream>
using namespace Eigen;
using namespace std;
// Function to write a matrix to a CSV file
void writeMatrixToCSV(const string& filename, const MatrixXd& matrix) {
ofstream file(filename);
if (file.is_open()) {
for (int i = 0; i < matrix.rows(); ++i) {
for (int j = 0; j < matrix.cols(); ++j) {
file << matrix(i, j);
if (j < matrix.cols() - 1) file << ","; // Separate values by commas
}
file << "\n"; // Newline after each row
}
file.close();
cout << "Matrix saved to " << filename << endl;
} else {
cerr << "Error opening file " << filename << endl;
}
}
int main() {
// Define matrices A and B
MatrixXd A(3, 3);
MatrixXd B(3, 3);
A << 1, 2, 3,
4, 5, 6,
7, 8, 9;
B << 9, 8, 7,
6, 5, 4,
3, 2, 1;
#ifdef TESTING
// Write matrices A and B to CSV files
writeMatrixToCSV("A_cpp.dat", A);
writeMatrixToCSV("B_cpp.dat", B);
#endif
// Compute the product of A and B
MatrixXd C = A * B;
#ifdef TESTING
// Write the resulting matrix C to a CSV file
writeMatrixToCSV("C_cpp.dat", C);
#endif
return 0;
}