Commit 02b75636 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added overloads of l2Norm and lpNorm for 1D vectors to avoid unnecessary sqrt

Also removed unnecessary getVectorLength function from getEntityMesure.h

Fixes #71
parent 2d2fa9c2
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -749,7 +749,8 @@ l1Norm( const ET1& a )
}

template< typename ET1,
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 > >
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 >,
          std::enable_if_t< (ET1::getSize() > 1), bool > = true >
__cuda_callable__
auto
l2Norm( const ET1& a )
@@ -758,9 +759,21 @@ l2Norm( const ET1& a )
   return sqrt( sum( a * a ) );
}

template< typename ET1,
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 >,
          std::enable_if_t< ET1::getSize() == 1, bool > = true >
__cuda_callable__
auto
l2Norm( const ET1& a )
{
   // avoid sqrt for 1D vectors (l1 and l2 norms are identical in 1D)
   return l1Norm( a );
}

template< typename ET1,
          typename Real,
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 > >
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 >,
          std::enable_if_t< (ET1::getSize() > 1), bool > = true >
__cuda_callable__
auto
lpNorm( const ET1& a, const Real& p )
@@ -776,6 +789,18 @@ lpNorm( const ET1& a, const Real& p )
   return pow( sum( pow( abs( a ), p ) ), 1.0 / p );
}

template< typename ET1,
          typename Real,
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 >,
          std::enable_if_t< ET1::getSize() == 1, bool > = true >
__cuda_callable__
auto
lpNorm( const ET1& a, const Real& p )
{
   // avoid sqrt and pow for 1D vectors (all lp norms are identical in 1D)
   return l1Norm( a );
}

template< typename ET1,
          typename..., typename = EnableIfStaticUnaryExpression_t< ET1 > >
__cuda_callable__
+1 −18
Original line number Diff line number Diff line
@@ -50,23 +50,6 @@ getEntityMeasure( const Mesh< MeshConfig, Device > & mesh,
    return 1.0;
}

// TODO: move to StaticVector::norm
template< typename Real >
__cuda_callable__
Real
getVectorLength( const TNL::Containers::StaticVector< 1, Real > & vector )
{
    return TNL::abs( vector[ 0 ] );
}

template< typename VectorExpression >
__cuda_callable__
typename VectorExpression::RealType
getVectorLength( const VectorExpression& expr )
{
    return TNL::sqrt( TNL::dot( expr, expr ) );
}

// Edge
template< typename MeshConfig, typename Device >
__cuda_callable__
@@ -76,7 +59,7 @@ getEntityMeasure( const Mesh< MeshConfig, Device > & mesh,
{
    const auto& v0 = mesh.getPoint( entity.template getSubentityIndex< 0 >( 0 ) );
    const auto& v1 = mesh.getPoint( entity.template getSubentityIndex< 0 >( 1 ) );
    return getVectorLength( v1 - v0 );
    return l2Norm( v1 - v0 );
}

// Triangle