aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc/examples
diff options
context:
space:
mode:
authorGravatar Gael Guennebaud <g.gael@free.fr>2009-07-05 11:33:55 +0200
committerGravatar Gael Guennebaud <g.gael@free.fr>2009-07-05 11:33:55 +0200
commitc6f610093bff10c5a6e4fac94bfed422ba54b39a (patch)
treeda30a412eb2c03edbccebfd9a4d0170f6dcf74f4 /doc/examples
parenteec334c604d3fbd0ab7fcb0d9380f87c943afc90 (diff)
add a VectorBlock expr as a specialization of Block
Diffstat (limited to 'doc/examples')
-rw-r--r--doc/examples/class_FixedVectorBlock.cpp26
-rw-r--r--doc/examples/class_VectorBlock.cpp26
2 files changed, 52 insertions, 0 deletions
diff --git a/doc/examples/class_FixedVectorBlock.cpp b/doc/examples/class_FixedVectorBlock.cpp
new file mode 100644
index 000000000..c176be495
--- /dev/null
+++ b/doc/examples/class_FixedVectorBlock.cpp
@@ -0,0 +1,26 @@
+#include <Eigen/Core>
+USING_PART_OF_NAMESPACE_EIGEN
+using namespace std;
+
+template<typename Derived>
+Eigen::VectorBlock<Derived, 2>
+firstTwo(MatrixBase<Derived>& v)
+{
+ return Eigen::VectorBlock<Derived, 2>(v.derived(), 0);
+}
+
+template<typename Derived>
+const Eigen::VectorBlock<Derived, 2>
+firstTwo(const MatrixBase<Derived>& v)
+{
+ return Eigen::VectorBlock<Derived, 2>(v.derived(), 0);
+}
+
+int main(int, char**)
+{
+ Matrix<int,1,6> v; v << 1,2,3,4,5,6;
+ cout << firstTwo(4*v) << endl; // calls the const version
+ firstTwo(v) *= 2; // calls the non-const version
+ cout << "Now the vector v is:" << endl << v << endl;
+ return 0;
+}
diff --git a/doc/examples/class_VectorBlock.cpp b/doc/examples/class_VectorBlock.cpp
new file mode 100644
index 000000000..d979f973a
--- /dev/null
+++ b/doc/examples/class_VectorBlock.cpp
@@ -0,0 +1,26 @@
+#include <Eigen/Core>
+USING_PART_OF_NAMESPACE_EIGEN
+using namespace std;
+
+template<typename Derived>
+Eigen::VectorBlock<Derived>
+segmentFromRange(MatrixBase<Derived>& v, int start, int end)
+{
+ return Eigen::VectorBlock<Derived>(v.derived(), start, end-start);
+}
+
+template<typename Derived>
+const Eigen::VectorBlock<Derived>
+segmentFromRange(const MatrixBase<Derived>& v, int start, int end)
+{
+ return Eigen::VectorBlock<Derived>(v.derived(), start, end-start);
+}
+
+int main(int, char**)
+{
+ Matrix<int,1,6> v; v << 1,2,3,4,5,6;
+ cout << segmentFromRange(2*v, 2, 4) << endl; // calls the const version
+ segmentFromRange(v, 1, 3) *= 5; // calls the non-const version
+ cout << "Now the vector v is:" << endl << v << endl;
+ return 0;
+}