[Dune-devel] [Dune-Commit] [Commit] dune-common - 64df627: Implement read-only IdentityMatrix

Christoph_Gersbacher gersbach at mathematik.uni-freiburg.de
Tue Dec 3 11:59:44 CET 2013


Hi Oliver,

there is of course no real need for such a class: you can always use a 
FieldMatrix to represent an identity matrix. But isn't it tedious to 
initialize a dense matrix? With the new 
IdentityMatrix/DenseMatrixAssigner functionality you can now write:

    FieldMatrix< double, 2, 2 > Id = IdentityMatrix< double, 2 >();

Then, IdentityMatrix may be used as the return type of the methods 
jacobianTransposed() and jacobianInverseTransposed(); for some grids, 
the LocalGeometry's jacobians reduce to an identity matrix. On the 
developer meeting in Aachen we agreed that jacobians may be implemented 
as slim "const" dense matrices that only cast into a field matrix. The 
IdentityMatrix simply implements this interface. As long as you don't 
need access to the entries of an identity matrix (do you ever need 
that?) you should prefer IdentityMatrix over another dense matrix as it 
holds no data at all.

Cheers,
Christoph


On 12/03/2013 11:32 AM, Oliver Sander wrote:
> Hi Christoph,
> out of curiosity: what do you need such a matrix for?
> Cheers,
> Oliver
>
>
> Am 03.12.2013 11:26, schrieb Christoph Gersbacher:
>> New commit, appeared at Tue Dec  3 11:26:03 2013 +0100
>> as part of the following ref changes:
>>
>>      branch refs/heads/master    updated from 79e8fa8 -> d58db46
>>
>> Browsable version: http://cgit.dune-project.org/repositories/dune-common/commit/?id=64df62745d93fbe6c380a039eebb5a3ac8f0da0c
>>
>> ======================================================================
>>
>> commit 64df62745d93fbe6c380a039eebb5a3ac8f0da0c
>> Author: Christoph Gersbacher <christoph.gersbacher at mathematik.uni-freiburg.de>
>> Date:   Wed Oct 9 11:34:01 2013 +0200
>>
>>      Implement read-only IdentityMatrix
>>      
>>      This implementation of an IdentityMatrix does not hold any data. It
>>      implements a reduced version of the DenseMatrix interface including a
>>      standard C++ cast operator to a Dune::FieldMatrix. This code illustrates
>>      the new forward capabilities of DenseMatrixAssigner.
>>
>>   dune/common/CMakeLists.txt    |   1 +
>>   dune/common/Makefile.am       |   1 +
>>   dune/common/identitymatrix.hh | 160 ++++++++++++++++++++++++++++++++++++++++++
>>   3 files changed, 162 insertions(+)
>>   create mode 100644 dune/common/identitymatrix.hh
>>
>>
>>
>> diff --git a/dune/common/CMakeLists.txt b/dune/common/CMakeLists.txt
>> index 0075584..91ebd9e 100644
>> --- a/dune/common/CMakeLists.txt
>> +++ b/dune/common/CMakeLists.txt
>> @@ -58,6 +58,7 @@ install(FILES
>>           genericiterator.hh
>>           gmpfield.hh
>>           hash.hh
>> +        identitymatrix.hh
>>           indent.hh
>>           interfaces.hh
>>           ios_state.hh
>> diff --git a/dune/common/Makefile.am b/dune/common/Makefile.am
>> index 189757a..4212515 100644
>> --- a/dune/common/Makefile.am
>> +++ b/dune/common/Makefile.am
>> @@ -54,6 +54,7 @@ commoninclude_HEADERS = 			\
>>   	genericiterator.hh			\
>>   	gmpfield.hh				\
>>   	hash.hh					\
>> +	identitymatrix.hh                       \
>>   	indent.hh				\
>>   	interfaces.hh				\
>>   	ios_state.hh				\
>> diff --git a/dune/common/identitymatrix.hh b/dune/common/identitymatrix.hh
>> new file mode 100644
>> index 0000000..646e398
>> --- /dev/null
>> +++ b/dune/common/identitymatrix.hh
>> @@ -0,0 +1,160 @@
>> +#ifndef DUNE_COMMON_IDENTITYMATRIX_HH
>> +#define DUNE_COMMON_IDENTITYMATRIX_HH
>> +
>> +#include <dune/common/fmatrix.hh>
>> +#include <dune/common/ftraits.hh>
>> +#include <dune/common/math.hh>
>> +#include <dune/common/std/constexpr.hh>
>> +
>> +/**
>> + * \file
>> + * \ingroup DenseMatVec
>> + * \brief Implementation of an identity matrix that does not store
>> + *        any data.
>> + * \author Christoph Gersbacher
>> + */
>> +
>> +namespace Dune
>> +{
>> +
>> +  // IdentityMatrix
>> +  // --------------
>> +
>> +  /** \class IdentityMatrix
>> +   *
>> +   *  \ingroup DenseMatVec
>> +   *
>> +   *  \brief Read-only identity matrix.
>> +   *
>> +   *  Implementation of an identity matrix that does not store any data.
>> +   *
>> +   *  \tparam  K  field type
>> +   *  \tparam  N  dimension
>> +   */
>> +  template< class K, int N >
>> +  struct IdentityMatrix
>> +  {
>> +    /** \brief field type */
>> +    typedef K field_type;
>> +    /** \brief size type */
>> +    typedef std::size_t size_type;
>> +
>> +    /** \brief return number of rows */
>> +    DUNE_CONSTEXPR size_type rows () const { return N; }
>> +    /** \brief return number of columns */
>> +    DUNE_CONSTEXPR size_type cols () const { return N; }
>> +
>> +    /** \copydoc Dune::DenseMatrix::mv */
>> +    template< class X, class Y >
>> +    void mv ( const X &x, Y &y ) const
>> +    {
>> +      y = x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::mtv */
>> +    template< class X, class Y >
>> +    void mtv ( const X &x, Y &y ) const
>> +    {
>> +      y = x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::umv */
>> +    template< class X, class Y >
>> +    void umv ( const X &x, Y &y ) const
>> +    {
>> +      y += x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::umtv */
>> +    template< class X, class Y >
>> +    void umtv ( const X &x, Y &y ) const
>> +    {
>> +      y += x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::umhv */
>> +    template< class X, class Y >
>> +    void umhv ( const X &x, Y &y ) const
>> +    {
>> +      y += x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::mmv */
>> +    template< class X, class Y >
>> +    void mmv ( const X &x, Y &y ) const
>> +    {
>> +      y -= x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::mmtv */
>> +    template< class X, class Y >
>> +    void mmtv ( const X &x, Y &y ) const
>> +    {
>> +      y -= x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::mmhv */
>> +    template< class X, class Y >
>> +    void mmhv ( const X &x, Y &y ) const
>> +    {
>> +      y -= x;
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::usmv */
>> +    template< class X, class Y >
>> +    void usmv ( const field_type &alpha, const X &x, Y &y ) const
>> +    {
>> +      y.axpy( alpha, x );
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::usmtv */
>> +    template< class X, class Y >
>> +    void usmtv ( const field_type &alpha, const X &x, Y &y ) const
>> +    {
>> +      y.axpy( alpha, x );
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::usmhv */
>> +    template< class X, class Y >
>> +    void usmhv ( const field_type &alpha, const X &x, Y &y ) const
>> +    {
>> +      y.axpy( alpha, x );
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::frobenius_norm */
>> +    typename FieldTraits< field_type >::real_type frobenius_norm () const
>> +    {
>> +      return std::sqrt( frobenius_norm2() );
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::frobenius_norm2 */
>> +    typename FieldTraits< field_type >::real_type frobenius_norm2 () const
>> +    {
>> +      return FieldTraits< field_type >::real_type( N );
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::infinity_norm */
>> +    typename FieldTraits< field_type >::real_type infinity_norm () const
>> +    {
>> +      return FieldTraits< field_type >::real_type( 1 );
>> +    }
>> +
>> +    /** \copydoc Dune::DenseMatrix::infinity_norm_real */
>> +    typename FieldTraits< field_type >::real_type infinity_norm_real () const
>> +    {
>> +      return FieldTraits< field_type >::real_type( 1 );
>> +    }
>> +
>> +    /** \brief cast to FieldMatrix */
>> +    operator FieldMatrix< field_type, N, N > () const
>> +    {
>> +      FieldMatrix< field_type, N, N > fieldMatrix( 0 );
>> +      for( int i = 0; i < N; ++i )
>> +        fieldMatrix[ i ][ i ] = field_type( 1 );
>> +      return fieldMatrix;
>> +    }
>> +  };
>> +
>> +} // namespace Dune
>> +
>> +#endif // #ifndef DUNE_COMMON_IDENTITYMATRIX_HH
>>
>> _______________________________________________
>> Dune-Commit mailing list
>> Dune-Commit at dune-project.org
>> http://lists.dune-project.org/mailman/listinfo/dune-commit
>>
>
>
>
> _______________________________________________
> Dune-devel mailing list
> Dune-devel at dune-project.org
> http://lists.dune-project.org/mailman/listinfo/dune-devel


-- 
Christoph Gersbacher<christoph.gersbacher at mathematik.uni-freiburg.de> 
<mailto:christoph.gersbacher at mathematik.uni-freiburg.de>
Abteilung für Angewandte Mathematik
Albert-Ludwigs-Universität Freiburg
Hermann-Herder-Str. 10
D-79104 Freiburg im Breisgau
Tel.: +49 (0)761 / 203 5645
Fax: +49 (0)761 / 203 5632
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.dune-project.org/pipermail/dune-devel/attachments/20131203/54259a71/attachment.htm>


More information about the Dune-devel mailing list