diff --git a/src/Picasso_BatchedLinearAlgebra.hpp b/src/Picasso_BatchedLinearAlgebra.hpp index 7741f34..a18aa7c 100644 --- a/src/Picasso_BatchedLinearAlgebra.hpp +++ b/src/Picasso_BatchedLinearAlgebra.hpp @@ -12,6 +12,8 @@ #ifndef PICASSO_BATCHEDLINEARALGEBRA_HPP #define PICASSO_BATCHEDLINEARALGEBRA_HPP +#include + #include #include @@ -1604,6 +1606,8 @@ struct Vector #pragma unroll #endif auto norm2 = Kokkos::sqrt( ~( *this ) * ( *this ) ); + assert( norm2 != 0.0 ); + for ( int i = 0; i < N; ++i ) ( *this )( i ) /= norm2; return *this; @@ -1735,6 +1739,16 @@ struct Vector return *this; } + // Normalization + KOKKOS_INLINE_FUNCTION + Vector& normalize() + { + assert( ( *this )( 0 ) != 0.0 ); + + ( *this )( 0 ) = ( *this )( 0 ) > 0 ? 1.0 : -1.0; + return *this; + } + // Strides. KOKKOS_INLINE_FUNCTION int stride_0() const { return 1; } @@ -1898,6 +1912,8 @@ struct VectorView #pragma unroll #endif auto norm2 = Kokkos::sqrt( ~( *this ) * ( *this ) ); + assert( norm2 != 0.0 ); + for ( int i = 0; i < N; ++i ) ( *this )( i ) /= norm2; return *this; diff --git a/unit_test/tstBatchedLinearAlgebra.hpp b/unit_test/tstBatchedLinearAlgebra.hpp index e7277b0..73fd273 100644 --- a/unit_test/tstBatchedLinearAlgebra.hpp +++ b/unit_test/tstBatchedLinearAlgebra.hpp @@ -1128,6 +1128,11 @@ void vectorTest() for ( int i = 0; i < 3; ++i ) EXPECT_DOUBLE_EQ( w( i ), 32.3 ); + // Size 1 vector test + LinearAlgebra::Vector vec1 = { 4.7 }; + vec1.normalize(); + EXPECT_DOUBLE_EQ( vec1, 1.0 ); + // Size 1 vector test. // FIXME: construction of length 1 vector fails with NVCC. /*