mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add a tutorial page on the Map class, and add a section to FunctionsTakingEigenTypes about multiple-argument functions and the pitfalls when using Map/Expression types.
(transplanted from 44b19b432c
)
This commit is contained in:
21
doc/snippets/Tutorial_Map_using.cpp
Normal file
21
doc/snippets/Tutorial_Map_using.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
typedef Matrix<float,1,Dynamic> MatrixType;
|
||||
typedef Map<MatrixType> MapType;
|
||||
typedef Map<const MatrixType> MapTypeConst; // a read-only map
|
||||
const int n_dims = 5;
|
||||
|
||||
MatrixType m1(n_dims), m2(n_dims);
|
||||
m1.setRandom();
|
||||
m2.setRandom();
|
||||
float *p = &m2(0); // get the address storing the data for m2
|
||||
MapType m2map(p,m2.size()); // m2map shares data with m2
|
||||
MapTypeConst m2mapconst(p,m2.size()); // a read-only accessor for m2
|
||||
|
||||
cout << "m1: " << m1 << endl;
|
||||
cout << "m2: " << m2 << endl;
|
||||
cout << "Squared euclidean distance: " << (m1-m2).squaredNorm() << endl;
|
||||
cout << "Squared euclidean distance, using map: " <<
|
||||
(m1-m2map).squaredNorm() << endl;
|
||||
m2map(3) = 7; // this will change m2, since they share the same array
|
||||
cout << "Updated m2: " << m2 << endl;
|
||||
cout << "m2 coefficient 2, constant accessor: " << m2mapconst(2) << endl;
|
||||
/* m2mapconst(2) = 5; */ // this yields a compile-time error
|
||||
Reference in New Issue
Block a user