is Eigen make a deep copy of matrix?


void testSparseMatrix()
{ 
using namespace Eigen; 
using namespace std;
 typedef Triplet Trip;
 std::vector trp, tmp;
 // I subtracted 1 from the indices so that the output matches your question trp.push_back(Trip(1 - 1, 1 - 1, 3));
 trp.push_back(Trip(1 - 1, 3 - 1, 4)); 
trp.push_back(Trip(2 - 1, 3 - 1, 1)); 
trp.push_back(Trip(3 - 1, 2 - 1, 2));
 trp.push_back(Trip(3 - 1, 4 - 1, 5));
 trp.push_back(Trip(4 - 1, 1 - 1, 4)); 
int rows, cols; 
rows = cols = 4; 
SparseMatrix A(rows, cols);
 A.setFromTriplets(trp.begin(), trp.end()); 
cout << "Matrix from triplets:" << endl;
 cout << A << endl; cout << endl << "Triplets:" << endl;
 cout << "Row\tCol\tVal" << endl; 
for (int k = 0; k < A.outerSize(); ++k) 
{ 
for (SparseMatrix::InnerIterator it(A, k); it; ++it) 
{ 
cout << 1 + it.row() << "\t"; // row index 
cout << 1 + it.col() << "\t"; // col index (here it is equal to k)
 cout << it.value() << endl;
 } 
} 
////////////////////////// 
SparseMatrix B; B = A; 
std::cout << "A\n" << Eigen::MatrixXi(A) << std::endl;
 std::cout << "B\n" << Eigen::MatrixXi(B) << std::endl;
 B.coeffRef(0, 0) = 98; 
std::cout << "A\n" << Eigen::MatrixXi(A) << std::endl;
 std::cout << "B\n" << Eigen::MatrixXi(B) << std::endl;
 std::cin.get();
}

output:

Matrix from triplets:
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0

 

Triplets:
Row Col Val
1 1 3
4 1 4
3 2 2
1 3 4
2 3 1
3 4 5
A
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
B
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
A
3 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0
B
98 0 4 0
0 0 1 0
0 2 0 5
4 0 0 0


 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s