From 38e64917a1af82243d9765d79724ce51a857745e Mon Sep 17 00:00:00 2001 From: David Coeurjolly Date: Tue, 27 Nov 2018 09:10:43 +0100 Subject: [PATCH 1/4] sqnorm in pointvector --- ChangeLog.md | 10 +++++++--- src/DGtal/kernel/PointVector.h | 11 +++++++++++ src/DGtal/kernel/PointVector.ih | 16 ++++++++++++++++ tests/kernel/testPointVector-catch.cpp | 1 + 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 936a77764a..ad8d6b253f 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -58,15 +58,19 @@ - *DEC* - Adding missing headers in some files of DEC. (Roland Denis, [#1349](https://github.com/DGtal-team/DGtal/pull/1349)) - + - *Math* - - Fix possible division by zero in the MultiStatistics class. - (Kacper Pluta, [#1358](https://github.com/DGtal-team/DGtal/pull/1358)) + - Fix possible division by zero in the MultiStatistics class. + (Kacper Pluta, [#1358](https://github.com/DGtal-team/DGtal/pull/1358)) - *Image* - Fix bug in ImageLinearCellEmbedder. (Jacques-Olivier Lachaud, [#1356](https://github.com/DGtal-team/DGtal/pull/1356)) +- *Kernel* + - Adding square norm method to Point/Vector class. (David Coeurjolly, + [#1365](https://github.com/DGtal-team/DGtal/pull/1365)) + # DGtal 0.9.4.1 ## Bug Fixes diff --git a/src/DGtal/kernel/PointVector.h b/src/DGtal/kernel/PointVector.h index bfe363fc37..d692026b04 100644 --- a/src/DGtal/kernel/PointVector.h +++ b/src/DGtal/kernel/PointVector.h @@ -730,6 +730,17 @@ namespace DGtal */ double norm( const NormType type = L_2 ) const; + /** + * Computes the square L2 norm of a point/vector. + * \warning This method performs a conversion + * from the type T to double for each components to compute the + * norms. + * + * @return the square norm of the point/vector as a double. + */ + double sqnorm( ) const; + + /** * Computes the 1-norm of a vector. * diff --git a/src/DGtal/kernel/PointVector.ih b/src/DGtal/kernel/PointVector.ih index 9161c4d63c..bc3fc85160 100644 --- a/src/DGtal/kernel/PointVector.ih +++ b/src/DGtal/kernel/PointVector.ih @@ -665,6 +665,22 @@ negate() template inline double +DGtal::PointVector::sqnorm () const +{ + double tmp = 0.0; + ASSERT ( dim > 0 ); + double v; + for ( DGtal::Dimension i = 0; i < dimension; i++ ) + { + v =NumberTraits::castToDouble(myArray[ i ]); + tmp += v*v; + } + return tmp; +} +//------------------------------------------------------------------------------ +template +inline +double DGtal::PointVector::norm (const typename Self::NormType aType ) const { diff --git a/tests/kernel/testPointVector-catch.cpp b/tests/kernel/testPointVector-catch.cpp index 00486521a2..fa75d82180 100644 --- a/tests/kernel/testPointVector-catch.cpp +++ b/tests/kernel/testPointVector-catch.cpp @@ -87,6 +87,7 @@ TEST_CASE( "Point Vector Unit tests" ) CAPTURE( normalized ); REQUIRE( aPoint.norm ( Point::L_1 ) == 6 ); REQUIRE( aPoint.norm ( Point::L_infty ) == 3 ); + REQUIRE( aPoint.sqnorm() == Approx(aPoint.norm()*aPoint.norm()) ); REQUIRE( normalized[0] == Approx( 0.801784) ); REQUIRE( normalized[1] == Approx( -0.267261) ); REQUIRE( normalized[2] == Approx( 0.534522) ); From acbe3766ae26e39d736b9aa1fcc475c0a680aef2 Mon Sep 17 00:00:00 2001 From: David Coeurjolly Date: Tue, 27 Nov 2018 10:49:22 +0100 Subject: [PATCH 2/4] std::pow --- src/DGtal/kernel/PointVector.ih | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/DGtal/kernel/PointVector.ih b/src/DGtal/kernel/PointVector.ih index bc3fc85160..8064447b60 100644 --- a/src/DGtal/kernel/PointVector.ih +++ b/src/DGtal/kernel/PointVector.ih @@ -667,14 +667,11 @@ inline double DGtal::PointVector::sqnorm () const { - double tmp = 0.0; ASSERT ( dim > 0 ); + double tmp = 0.0; double v; for ( DGtal::Dimension i = 0; i < dimension; i++ ) - { - v =NumberTraits::castToDouble(myArray[ i ]); - tmp += v*v; - } + tmp += std::pow(NumberTraits::castToDouble(myArray[ i ]), 2); return tmp; } //------------------------------------------------------------------------------ From 2ea2f8a5431fd7c4316484ab22a36114804fd3d9 Mon Sep 17 00:00:00 2001 From: David Coeurjolly Date: Tue, 27 Nov 2018 10:52:58 +0100 Subject: [PATCH 3/4] std::pow --- src/DGtal/kernel/PointVector.ih | 1 - 1 file changed, 1 deletion(-) diff --git a/src/DGtal/kernel/PointVector.ih b/src/DGtal/kernel/PointVector.ih index 8064447b60..86cf344912 100644 --- a/src/DGtal/kernel/PointVector.ih +++ b/src/DGtal/kernel/PointVector.ih @@ -669,7 +669,6 @@ DGtal::PointVector::sqnorm () const { ASSERT ( dim > 0 ); double tmp = 0.0; - double v; for ( DGtal::Dimension i = 0; i < dimension; i++ ) tmp += std::pow(NumberTraits::castToDouble(myArray[ i ]), 2); return tmp; From d87f0262bb68aa6116aba247fd6e92bafc524e43 Mon Sep 17 00:00:00 2001 From: David Coeurjolly Date: Fri, 30 Nov 2018 10:33:19 +0100 Subject: [PATCH 4/4] squaredNorm instead of sqnorm --- src/DGtal/kernel/PointVector.h | 4 ++-- src/DGtal/kernel/PointVector.ih | 2 +- tests/kernel/testPointVector-catch.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/DGtal/kernel/PointVector.h b/src/DGtal/kernel/PointVector.h index d692026b04..f79a82c3f3 100644 --- a/src/DGtal/kernel/PointVector.h +++ b/src/DGtal/kernel/PointVector.h @@ -736,9 +736,9 @@ namespace DGtal * from the type T to double for each components to compute the * norms. * - * @return the square norm of the point/vector as a double. + * @return the squared norm of the point/vector as a double. */ - double sqnorm( ) const; + double squaredNorm( ) const; /** diff --git a/src/DGtal/kernel/PointVector.ih b/src/DGtal/kernel/PointVector.ih index 86cf344912..3cbf7d556c 100644 --- a/src/DGtal/kernel/PointVector.ih +++ b/src/DGtal/kernel/PointVector.ih @@ -665,7 +665,7 @@ negate() template inline double -DGtal::PointVector::sqnorm () const +DGtal::PointVector::squaredNorm () const { ASSERT ( dim > 0 ); double tmp = 0.0; diff --git a/tests/kernel/testPointVector-catch.cpp b/tests/kernel/testPointVector-catch.cpp index fa75d82180..d1202a295b 100644 --- a/tests/kernel/testPointVector-catch.cpp +++ b/tests/kernel/testPointVector-catch.cpp @@ -87,7 +87,7 @@ TEST_CASE( "Point Vector Unit tests" ) CAPTURE( normalized ); REQUIRE( aPoint.norm ( Point::L_1 ) == 6 ); REQUIRE( aPoint.norm ( Point::L_infty ) == 3 ); - REQUIRE( aPoint.sqnorm() == Approx(aPoint.norm()*aPoint.norm()) ); + REQUIRE( aPoint.squaredNorm() == Approx(aPoint.norm()*aPoint.norm()) ); REQUIRE( normalized[0] == Approx( 0.801784) ); REQUIRE( normalized[1] == Approx( -0.267261) ); REQUIRE( normalized[2] == Approx( 0.534522) );