Loading...
Searching...
No Matches
dynmatrix.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_DYNMATRIX_HH
#define DUNE_DYNMATRIX_HH
#include <cmath>
#include <cstddef>
#include <iostream>
#include <initializer_list>
#include <dune/common/boundschecking.hh>
#include <dune/common/exceptions.hh>
#include <dune/common/dynvector.hh>
#include <dune/common/densematrix.hh>
#include <dune/common/typetraits.hh>
namespace Dune
{
template< class K > class DynamicMatrix;
template< class K >
struct DenseMatVecTraits< DynamicMatrix<K> >
{
typedef DynamicMatrix<K> derived_type;
typedef DynamicVector<K> row_type;
typedef row_type &row_reference;
typedef const row_type &const_row_reference;
typedef std::vector<K> container_type;
typedef K value_type;
typedef typename container_type::size_type size_type;
};
template< class K >
struct FieldTraits< DynamicMatrix<K> >
{
typedef typename FieldTraits<K>::field_type field_type;
typedef typename FieldTraits<K>::real_type real_type;
};
template<class K>
class DynamicMatrix : public DenseMatrix< DynamicMatrix<K> >
{
std::vector< DynamicVector<K> > _data;
typedef DenseMatrix< DynamicMatrix<K> > Base;
public:
typedef typename Base::size_type size_type;
typedef typename Base::value_type value_type;
typedef typename Base::row_type row_type;
//===== constructors
DynamicMatrix () {}
DynamicMatrix (size_type r, size_type c, value_type v = value_type() ) :
_data(r, row_type(c, v) )
{}
DynamicMatrix (std::initializer_list<DynamicVector<K>> const &ll)
: _data(ll)
{}
template <class T,
typename = std::enable_if_t<!Dune::IsNumber<T>::value && HasDenseMatrixAssigner<DynamicMatrix, T>::value>>
DynamicMatrix(T const& rhs)
{
*this = rhs;
}
//==== resize related methods
void resize (size_type r, size_type c, value_type v = value_type() )
{
_data.resize(0);
_data.resize(r, row_type(c, v) );
}
//===== assignment
// General assignment with resizing
template <typename T,
typename = std::enable_if_t<!Dune::IsNumber<T>::value>>
DynamicMatrix& operator=(T const& rhs) {
_data.resize(rhs.N());
std::fill(_data.begin(), _data.end(), row_type(rhs.M(), K(0)));
Base::operator=(rhs);
return *this;
}
// Specialisation: scalar assignment (no resizing)
template <typename T,
typename = std::enable_if_t<Dune::IsNumber<T>::value>>
DynamicMatrix& operator=(T scalar) {
std::fill(_data.begin(), _data.end(), scalar);
return *this;
}
DynamicMatrix transposed() const
{
DynamicMatrix AT(this->M(), this->N());
for( size_type i = 0; i < this->N(); ++i )
for( size_type j = 0; j < this->M(); ++j )
AT[j][i] = (*this)[i][j];
return AT;
}
// make this thing a matrix
size_type mat_rows() const { return _data.size(); }
size_type mat_cols() const {
assert(this->rows());
return _data.front().size();
}
row_type & mat_access(size_type i) {
DUNE_ASSERT_BOUNDS(i < _data.size());
return _data[i];
}
const row_type & mat_access(size_type i) const {
DUNE_ASSERT_BOUNDS(i < _data.size());
return _data[i];
}
};
} // end namespace
#endif
typetraits.hh
Traits for type conversions and type information.
exceptions.hh
A few common exception classes.
densematrix.hh
Implements a matrix constructed from a given type representing a field and a compile-time given numbe...
boundschecking.hh
Macro for wrapping boundary checks.
dynvector.hh
This file implements a dense vector with a dynamic size.
DUNE_ASSERT_BOUNDS
#define DUNE_ASSERT_BOUNDS(cond)
If DUNE_CHECK_BOUNDS is defined: check if condition cond holds; otherwise, do nothing.
Definition boundschecking.hh:30
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::DenseMatrix< DynamicMatrix< K > >::M
constexpr size_type M() const
Definition densematrix.hh:708
Dune::DenseMatrix< DynamicMatrix< K > >::value_type
Traits::value_type value_type
Definition densematrix.hh:162
Dune::DenseMatrix< DynamicMatrix< K > >::rows
constexpr size_type rows() const
Definition densematrix.hh:714
Dune::DenseMatrix< DynamicMatrix< K > >::row_type
Traits::row_type row_type
Definition densematrix.hh:174
Dune::DenseMatrix< DynamicMatrix< K > >::N
constexpr size_type N() const
Definition densematrix.hh:702
Dune::DenseMatrix< DynamicMatrix< K > >::size_type
Traits::size_type size_type
Definition densematrix.hh:171
Dune::DenseMatrix< DynamicMatrix< K > >::DenseMatrix
friend class DenseMatrix
Definition densematrix.hh:153
Dune::DenseMatrix< DynamicMatrix< K > >::operator=
constexpr derived_type & operator=(const RHS &rhs)
Definition densematrix.hh:284
Dune::DynamicMatrix
Construct a matrix with a dynamic size.
Definition dynmatrix.hh:61
Dune::DynamicMatrix::mat_cols
size_type mat_cols() const
Definition dynmatrix.hh:143
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix(std::initializer_list< DynamicVector< K > > const &ll)
Constructor initializing the matrix from a list of vector.
Definition dynmatrix.hh:80
Dune::DynamicMatrix::row_type
Base::row_type row_type
Definition dynmatrix.hh:67
Dune::DynamicMatrix::transposed
DynamicMatrix transposed() const
Return transposed of the matrix as DynamicMatrix.
Definition dynmatrix.hh:132
Dune::DynamicMatrix::value_type
Base::value_type value_type
Definition dynmatrix.hh:66
Dune::DynamicMatrix::mat_access
row_type & mat_access(size_type i)
Definition dynmatrix.hh:147
Dune::DynamicMatrix::size_type
Base::size_type size_type
Definition dynmatrix.hh:65
Dune::DynamicMatrix::mat_rows
size_type mat_rows() const
Definition dynmatrix.hh:142
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix(T const &rhs)
Definition dynmatrix.hh:87
Dune::DynamicMatrix::mat_access
const row_type & mat_access(size_type i) const
Definition dynmatrix.hh:151
Dune::DynamicMatrix::resize
void resize(size_type r, size_type c, value_type v=value_type())
resize matrix to r × c
Definition dynmatrix.hh:106
Dune::DynamicMatrix::operator=
DynamicMatrix & operator=(T const &rhs)
Definition dynmatrix.hh:116
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix()
Default constructor.
Definition dynmatrix.hh:71
Dune::DynamicMatrix::DynamicMatrix
DynamicMatrix(size_type r, size_type c, value_type v=value_type())
Constructor initializing the whole matrix with a scalar.
Definition dynmatrix.hh:74
Dune::DynamicMatrix::operator=
DynamicMatrix & operator=(T scalar)
Definition dynmatrix.hh:126
Dune::DenseMatVecTraits< DynamicMatrix< K > >::size_type
container_type::size_type size_type
Definition dynmatrix.hh:45
Dune::DenseMatVecTraits< DynamicMatrix< K > >::row_type
DynamicVector< K > row_type
Definition dynmatrix.hh:38
Dune::DenseMatVecTraits< DynamicMatrix< K > >::row_reference
row_type & row_reference
Definition dynmatrix.hh:40
Dune::DenseMatVecTraits< DynamicMatrix< K > >::value_type
K value_type
Definition dynmatrix.hh:44
Dune::DenseMatVecTraits< DynamicMatrix< K > >::derived_type
DynamicMatrix< K > derived_type
Definition dynmatrix.hh:36
Dune::DenseMatVecTraits< DynamicMatrix< K > >::const_row_reference
const row_type & const_row_reference
Definition dynmatrix.hh:41
Dune::DenseMatVecTraits< DynamicMatrix< K > >::container_type
std::vector< K > container_type
Definition dynmatrix.hh:43
Dune::FieldTraits< DynamicMatrix< K > >::real_type
FieldTraits< K >::real_type real_type
Definition dynmatrix.hh:52
Dune::FieldTraits< DynamicMatrix< K > >::field_type
FieldTraits< K >::field_type field_type
Definition dynmatrix.hh:51
Dune::DynamicVector
Construct a vector with a dynamic size.
Definition dynvector.hh:59
Dune::FieldTraits
Definition ftraits.hh:26
Dune::FieldTraits::field_type
T field_type
export the type representing the field
Definition ftraits.hh:28
Dune::FieldTraits::real_type
T real_type
export the type representing the real type of the field
Definition ftraits.hh:30
Dune::DenseMatVecTraits
Definition matvectraits.hh:31