-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding the first moment of a histogram panel
- Loading branch information
Showing
5 changed files
with
114 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef NJOY_SCION_INTEGRATION_FIRSTMOMENTHISTOGRAM | ||
#define NJOY_SCION_INTEGRATION_FIRSTMOMENTHISTOGRAM | ||
|
||
// system includes | ||
|
||
// other includes | ||
#include "scion/integration/IntegratorBase.hpp" | ||
|
||
namespace njoy { | ||
namespace scion { | ||
namespace integration { | ||
|
||
/** | ||
* @class | ||
* @brief First raw moment of a histogram panel (y is constant in x) | ||
* | ||
* The first raw moment or mean is defined as the integral of x * f(x) | ||
*/ | ||
class FirstMomentHistogram : public IntegratorBase< FirstMomentHistogram > { | ||
|
||
/* friend declarations */ | ||
friend class IntegratorBase< FirstMomentHistogram >; | ||
|
||
/* interface implementation functions */ | ||
|
||
/** | ||
* @brief Perform first raw moment integration of a histogram panel (y is constant in x) | ||
* | ||
* @param[in] xLeft the left value on the x interval | ||
* @param[in] xRight the right value on the x interval | ||
* @param[in] yLeft the left value on the y interval | ||
* @param[in] yRight the right value on the y interval | ||
*/ | ||
template < typename X, typename Y, | ||
typename I = decltype( std::declval< X >() * std::declval< X >() * std::declval< Y >() ) > | ||
I integrate( const X& xLeft, const X& xRight, | ||
const Y& yLeft, const Y& ) const noexcept { | ||
|
||
return 0.5 * yLeft * ( xRight - xLeft ) * ( xRight + xLeft ); | ||
} | ||
|
||
public: | ||
|
||
using IntegratorBase::operator(); | ||
}; | ||
|
||
// integration function | ||
static constexpr FirstMomentHistogram firstMomentHistogram; | ||
|
||
} // integration namespace | ||
} // scion namespace | ||
} // njoy namespace | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_cpp_test( integration.FirstMomentHistogram FirstMomentHistogram.test.cpp ) |
57 changes: 57 additions & 0 deletions
57
src/scion/integration/FirstMomentHistogram/test/FirstMomentHistogram.test.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// include Catch2 | ||
#include <catch2/catch_test_macros.hpp> | ||
#include <catch2/matchers/catch_matchers_floating_point.hpp> | ||
using Catch::Matchers::WithinRel; | ||
|
||
// what we are testing | ||
#include "scion/integration/FirstMomentHistogram.hpp" | ||
|
||
// other includes | ||
|
||
// convenience typedefs | ||
using namespace njoy::scion; | ||
|
||
SCENARIO( "FirstMomentHistogram" ) { | ||
|
||
GIVEN( "FirstMomentHistogram integration object" ) { | ||
|
||
WHEN( "integrating an interval" ) { | ||
|
||
integration::FirstMomentHistogram integrator{}; | ||
|
||
THEN( "the integration is performed correctly" ) { | ||
|
||
double xLeft = 1.0; | ||
double xRight = 2.0; | ||
double yLeft = 1.0; | ||
double yRight = 4.0; | ||
|
||
// both y values are the same | ||
CHECK_THAT( 1.5, WithinRel( integrator( xLeft, xRight, yLeft, yLeft ) ) ); | ||
|
||
// the y values are different | ||
CHECK_THAT( 1.5, WithinRel( integrator( xLeft, xRight, yLeft, yRight ) ) ); | ||
} // THEN | ||
} // WHEN | ||
} // GIVEN | ||
|
||
GIVEN( "linlin integration function" ) { | ||
|
||
WHEN( "integrating an interval" ) { | ||
|
||
THEN( "the integration is performed correctly" ) { | ||
|
||
double xLeft = 1.0; | ||
double xRight = 2.0; | ||
double yLeft = 1.0; | ||
double yRight = 4.0; | ||
|
||
// both y values are the same | ||
CHECK_THAT( 1.5, WithinRel( integration::firstMomentHistogram( xLeft, xRight, yLeft, yLeft ) ) ); | ||
|
||
// the y values are different | ||
CHECK_THAT( 1.5, WithinRel( integration::firstMomentHistogram( xLeft, xRight, yLeft, yRight ) ) ); | ||
} // THEN | ||
} // WHEN | ||
} // GIVEN | ||
} // SCENARIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters