Skip to content
Snippets Groups Projects
Commit aad0eac6 authored by Christian Engwer's avatar Christian Engwer
Browse files

Add constructor via initialiser list to FieldMatrix and FieldVector

kudos to Elias Pipping, for details see (FS #1166)
as the new Dune requirements are >= g++ 4.4 we are sure that initializer_list is available.
parent 0421884a
No related branches found
No related tags found
No related merge requests found
......@@ -7,6 +7,7 @@
#include <cmath>
#include <cstddef>
#include <iostream>
#include <initializer_list>
#include <dune/common/exceptions.hh>
#include <dune/common/fvector.hh>
......@@ -96,6 +97,15 @@ namespace Dune
DenseMatrixAssigner< FieldMatrix< K, ROWS, COLS >, Other >::apply( *this, other );
}
FieldMatrix (std::initializer_list<std::initializer_list<K> > const &ll)
{
assert(ll.size() == rows);
size_t i = 0;
for (typename std::initializer_list<std::initializer_list<K> >::
const_iterator lit = ll.begin(); lit != ll.end(); ++lit)
_data[i++] = *lit;
}
//===== assignment
using Base::operator=;
......
......@@ -10,6 +10,7 @@
#include <complex>
#include <cstring>
#include <utility>
#include<initializer_list>
#include <dune/common/std/constexpr.hh>
......@@ -129,6 +130,15 @@ namespace Dune {
FieldVector (const FieldVector & x) : _data(x._data)
{}
FieldVector (std::initializer_list<K> const &l)
{
assert(l.size() == dimension);
size_t i = 0;
for (typename std::initializer_list<K>::const_iterator it = l.begin();
it != l.end(); ++it)
_data[i++] = *it;
}
/**
* \brief Copy constructor from a second vector of possibly different type
*
......
......@@ -553,11 +553,24 @@ void test_interface()
checkMatrixInterface< FMatrix, Traits >( m );
}
void test_initialisation()
{
Dune::FieldMatrix<int, 2, 2> const A = {
{ 1, 2 },
{ 3, 4 }
};
assert(A[0][0] == 1);
assert(A[0][1] == 2);
assert(A[1][0] == 3);
assert(A[1][1] == 4);
}
int main()
{
try {
test_nan();
test_infinity_norms();
test_initialisation();
// test 1 x 1 matrices
test_interface<float, 1, 1>();
......
......@@ -342,6 +342,15 @@ test_infinity_norms()
assert(std::abs(v.infinity_norm_real()-14.0) < 1e-10); // max(7,14)
}
void
test_initialisation()
{
Dune::FieldVector<int, 2> const b = { 1, 2 };
assert(b[0] == 1);
assert(b[1] == 2);
}
int main()
{
try {
......@@ -351,6 +360,7 @@ int main()
test_nan();
test_infinity_norms();
test_initialisation();
} catch (Dune::Exception& e) {
std::cerr << e << std::endl;
return 1;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment