Skip to content
Snippets Groups Projects
Commit eab33714 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

New singleton class IPrecomputed. Provides factorial[k].

parent fd6a282a
No related branches found
No related tags found
No related merge requests found
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Tools/src/Precomputed.cpp
//! @brief Implements class Precomputed, providing precomputed constants
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2016
//! @authors Scientific Computing Group at MLZ Garching
//! @authors J. Fisher, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#include <cmath>
#include "Precomputed.h"
//! Precompute things upon class instantiation.
Precomputed::Precomputed()
{
// Precompute the factorial
double fac = 1;
for( int k=1; std::isfinite(fac); ++k ){
factorial.push_back( fac );
fac *= k;
}
}
// ************************************************************************** //
//
// BornAgain: simulate and fit scattering at grazing incidence
//
//! @file Tools/inc/Precomputed.h
//! @brief Defines classes Precomputed, IPrecomputed, providing precomputed constants
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2016
//! @authors Scientific Computing Group at MLZ Garching
//! @authors J. Fisher, M. Ganeva, G. Pospelov, W. Van Herck, J. Wuttke
//
// ************************************************************************** //
#include <vector>
#include "ISingleton.h"
class Precomputed
{
public:
Precomputed();
std::vector<double> factorial;
};
class IPrecomputed: public ISingleton<Precomputed>
{
};
#ifndef PRECOMPUTEDTEST_H
#define PRECOMPUTEDTEST_H
#include "Precomputed.h"
#include "gtest/gtest.h"
#include <memory>
class PrecomputedTest : public ::testing::Test
{
public:
};
TEST_F(PrecomputedTest, Factorial)
{
const double eps = 2.3e-16; // about the machine precision
auto& precomputed = IPrecomputed::instance();
EXPECT_TRUE(precomputed.factorial.size()>150);
EXPECT_DOUBLE_EQ(precomputed.factorial[0], 1.);
EXPECT_DOUBLE_EQ(precomputed.factorial[1], 1.);
EXPECT_DOUBLE_EQ(precomputed.factorial[2], 2.);
EXPECT_DOUBLE_EQ(precomputed.factorial[3], 6.);
for( size_t k=4; k<precomputed.factorial.size(); ++k )
EXPECT_NEAR(precomputed.factorial[k], tgamma(k+1.), 4*eps*tgamma(k+1.) );
}
#endif
......@@ -56,6 +56,7 @@
#include "Shape2DTest.h"
#include "RectangularDetectorTest.h"
#include "SpecialFunctionsTest.h"
#include "PrecomputedTest.h"
struct ErrorStreamRedirect {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment