COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-eigen.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Test module for Eigen"
9 
10 #include <boost/test/unit_test.hpp>
11 
12 #include <iostream>
13 
14 #include <Eigen/Dense>
15 
17 
18 using namespace std;
19 using namespace Eigen;
20 
23 {
24 
26  {
27  va.resize(5);
28  va << 1.0, 2.0, 3.0, 5.0, 7.0;
29 
30  vb = VectorXd::LinSpaced(5, 2.0, 10.0);
31 
32  vc = VectorXd::Constant(5, 7.0);
33 
34  ma.resize(5,5);
35  ma << 1.0 , 2.5 , 3.0 , 4.5 , 5.0 ,
36  3.0 , 4.5 , 5.0 , 6.5 , 7.0 ,
37  5.0 , 6.5 , 7.0 , 8.5 , 5.0 ,
38  7.0 , 8.5 , 9.0 ,10.5 , 5.0 ,
39  9.0 ,10.5 ,11.0 ,12.5 ,13.0 ;
40 
41  mb = MatrixXd::Identity(5,5);
42 
43  mc = MatrixXd::Zero(5,5);
44  }
45 
46  // functions
47 
54  template <typename Derived>
55  void modify(Eigen::MatrixBase<Derived> const& m)
56  {
57  const_cast< MatrixBase<Derived>& >(m).setConstant(2);
58  }
59 
60  // data
61 
62  VectorXd va;
63  VectorXd vb;
64  VectorXd vc;
65 
66  MatrixXd ma;
67  MatrixXd mb;
68  MatrixXd mc;
69 
70 };
71 
73 
74 BOOST_FIXTURE_TEST_SUITE( TestEigen, EigenFixture )
75 
76 
78 // BOOST_CHECK( bool );
79 // BOOST_CHECK_EQUAL(str, f.m_buffer);
80 // BOOST_CHECK_CLOSE( v1, v2, 0.0001 );
81 
82 BOOST_AUTO_TEST_CASE( VectorCoeffWiseOps )
83 {
84  vc = va.array() * vb.array();
85 // cout << vc << endl;
86  vc = va.array() / vb.array();
87 // cout << vc << endl;
88 }
89 
90 BOOST_AUTO_TEST_CASE( VectorVectorOps )
91 {
92  vc = va + vb;
93 // cout << vc << endl;
94  vc += vb;
95 // cout << vc << endl;
96 }
97 
98 BOOST_AUTO_TEST_CASE( MatrixMatrixOps )
99 {
100  mc = ma * mb;
101 // cout << mc << endl;
102  mc += mb;
103 // cout << mc << endl;
104 }
105 
107 {
108 
109  // Create an array
110  std::vector<int> v(9);
111  for (int i=0; i<9; ++i)
112  v[i]=i;
113 
114  typedef Eigen::Matrix<int,3,3,Eigen::RowMajor> Matrix3x3_t;
115  typedef Eigen::Matrix<int,1,9,Eigen::RowMajor> Matrix1x9_t;
116 
117  // Map the array to a 3x3 matrix
118  Eigen::Map< Matrix3x3_t > em1(&v.front());
119  // or equivalent:
120  Matrix3x3_t::MapType em2(&v.front());
121 
122  // Map the array to a 1x9 matrix
123  Eigen::Map< Matrix1x9_t > ev1(&v.front());
124  // or equivalent
125  Matrix1x9_t::MapType ev2(&v.front());
126 
127 
128  // We can now modify the original array as if they were Eigen structures
129 
130  // set all elements of row1 to 2
131  modify(em1.row(1));
132 
133  // Multiply entire matrix with 2
134  ev1 *= 2;
135 
136 // cout << ev2 << endl << endl;
137 
138  int nb_pts = 5;
139  int nb_vars = 3;
140  std::vector<int> array(nb_pts*nb_vars);
141  for(int i = 0; i < array.size(); ++i) array[i] = i;
142 
143  int var_size = 2;
144  int var_idx = 1;
145 
146  typedef Map< Matrix<int,Dynamic,Dynamic,Eigen::RowMajor> , Unaligned, Stride<Dynamic,Dynamic> > VariableMapT;
147  VariableMapT var (&array.front()+var_idx, nb_pts, var_size, Stride<Dynamic,Dynamic>(nb_vars, var_size-1));
148  cout << var << endl;
149  cout << endl;
150  cout << var.transpose()*var << endl;
151  cout << endl;
152  var.array() = var.array()*var.array();
153  cout << var << endl;
154 
155 
156  Array<double,2,2,RowMajor> test;
157  test.setOnes();
158  test += 1.;
159  std::stringstream ss; ss << test;
160  cout << ss.str() << endl;
161 }
162 
163 
164 
166 
167 BOOST_AUTO_TEST_SUITE_END()
168 
169 
VectorXd vc
Definition: utest-eigen.cpp:64
void modify(Eigen::MatrixBase< Derived > const &m)
Definition: utest-eigen.cpp:55
STL namespace.
MatrixXd ma
Definition: utest-eigen.cpp:66
MatrixXd mc
Definition: utest-eigen.cpp:68
fixture for each test of Eigen
Definition: utest-eigen.cpp:22
VectorXd vb
Definition: utest-eigen.cpp:63
BOOST_AUTO_TEST_CASE(VectorCoeffWiseOps)
Definition: utest-eigen.cpp:82
VectorXd va
Definition: utest-eigen.cpp:62
MatrixXd mb
Definition: utest-eigen.cpp:67
Send comments to:
COOLFluiD Web Admin