-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a NDPseudoRandomNumberGenerator. Refs #5322
Uses a single generator to allow easy construction of an ND point from pseudo-random numbers thereby presenting a cleaner interface inline with the other ND generators.
- Loading branch information
1 parent
c927b81
commit c5998a8
Showing
12 changed files
with
195 additions
and
10 deletions.
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
66 changes: 66 additions & 0 deletions
66
Code/Mantid/Framework/Kernel/inc/MantidKernel/NDPseudoRandomNumberGenerator.h
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,66 @@ | ||
#ifndef MANTID_KERNEL_NDPSEUDORANDOMNUMBERGENERATOR_H_ | ||
#define MANTID_KERNEL_NDPSEUDORANDOMNUMBERGENERATOR_H_ | ||
/** | ||
Copyright © 2012 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory | ||
This file is part of Mantid. | ||
Mantid is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation; either version 3 of the License, or | ||
(at your option) any later version. | ||
Mantid is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid>. | ||
Code Documentation is available at: <http://doxygen.mantidproject.org> | ||
*/ | ||
//------------------------------------------------------------------------------ | ||
// Includes | ||
//------------------------------------------------------------------------------ | ||
#include "MantidKernel/PseudoRandomNumberGenerator.h" | ||
#include "MantidKernel/ClassMacros.h" | ||
#include <boost/shared_ptr.hpp> | ||
|
||
namespace Mantid | ||
{ | ||
namespace Kernel | ||
{ | ||
/** | ||
* | ||
* Defines an ND pseudo-random number generator. This uses a single | ||
* 1D pseudo-random number generator to produce ND random values | ||
*/ | ||
class MANTID_KERNEL_DLL NDPseudoRandomNumberGenerator : public NDRandomNumberGenerator | ||
{ | ||
/// The type for generating a single random number | ||
typedef boost::shared_ptr<PseudoRandomNumberGenerator> SingleValueGenerator; | ||
public: | ||
/// Constructor | ||
NDPseudoRandomNumberGenerator(const unsigned int ndims, SingleValueGenerator singleValueGen); | ||
/// Set the random number seed | ||
void setSeed(const size_t seedValue); | ||
/// Returns the ND point | ||
std::vector<double> nextPoint(); | ||
/// Resets the generator | ||
void restart(); | ||
|
||
private: | ||
DISABLE_DEFAULT_CONSTRUCT(NDPseudoRandomNumberGenerator); | ||
DISABLE_COPY_AND_ASSIGN(NDPseudoRandomNumberGenerator); | ||
|
||
/// The number of dimensions | ||
const unsigned int m_ndims; | ||
/// The single value generator | ||
SingleValueGenerator m_singleValueGen; | ||
}; | ||
} | ||
} | ||
|
||
#endif /* MANTID_KERNEL_NDPSEUDORANDOMNUMBERGENERATOR_H_ */ |
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
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
52 changes: 52 additions & 0 deletions
52
Code/Mantid/Framework/Kernel/src/NDPseudoRandomNumberGenerator.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,52 @@ | ||
//------------------------------------------------------------------- | ||
// Includes | ||
//------------------------------------------------------------------- | ||
#include "MantidKernel/NDPseudoRandomNumberGenerator.h" | ||
|
||
namespace Mantid | ||
{ | ||
namespace Kernel | ||
{ | ||
/** | ||
* Constructor taking the number of dimensions & an existing generator | ||
* @param ndims :: The number of dimensions the point should return | ||
* @param singleValueGen :: An existing generator that is capable of | ||
* producing single random numbers. It is called ndims times for each | ||
* call to nextPoint | ||
*/ | ||
NDPseudoRandomNumberGenerator:: | ||
NDPseudoRandomNumberGenerator(const unsigned int ndims, SingleValueGenerator singleValueGen) | ||
: NDRandomNumberGenerator(), m_ndims(ndims), m_singleValueGen(singleValueGen) | ||
{ | ||
} | ||
|
||
/** | ||
* Set the random number seed | ||
* @param seedValue :: (Re-)seed the generator | ||
*/ | ||
void NDPseudoRandomNumberGenerator::setSeed(const size_t seedValue) | ||
{ | ||
m_singleValueGen->setSeed(seedValue); | ||
} | ||
|
||
/// Returns the ND point | ||
std::vector<double> NDPseudoRandomNumberGenerator::nextPoint() | ||
{ | ||
std::vector<double> point(m_ndims); | ||
for(unsigned int i = 0; i < m_ndims; ++i) | ||
{ | ||
point[i] = m_singleValueGen->nextValue(); | ||
} | ||
return point; | ||
} | ||
|
||
/** | ||
* Resets the underlying generator | ||
*/ | ||
void NDPseudoRandomNumberGenerator::restart() | ||
{ | ||
m_singleValueGen->restart(); | ||
} | ||
} | ||
} | ||
|
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
64 changes: 64 additions & 0 deletions
64
Code/Mantid/Framework/Kernel/test/NDPseudoRandomNumberGeneratorTest.h
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,64 @@ | ||
#ifndef NDPSEUDORANDOMNUMBERGENERATORTEST_H_ | ||
#define NDPSEUDORANDOMNUMBERGENERATORTEST_H_ | ||
|
||
#include <cxxtest/TestSuite.h> | ||
#include "MantidKernel/NDPseudoRandomNumberGenerator.h" | ||
#include "MantidKernel/MersenneTwister.h" | ||
#include <boost/shared_ptr.hpp> | ||
#include <boost/make_shared.hpp> | ||
|
||
using Mantid::Kernel::NDPseudoRandomNumberGenerator; | ||
using Mantid::Kernel::PseudoRandomNumberGenerator; | ||
|
||
class NDPseudoRandomNumberGeneratorTest : public CxxTest::TestSuite | ||
{ | ||
public: | ||
|
||
void test_That_Next_Always_Returns_ND_Size_Array() | ||
{ | ||
boost::shared_ptr<NDPseudoRandomNumberGenerator> ndRand = | ||
createTestGenerator(boost::make_shared<Mantid::Kernel::MersenneTwister>(12345)); | ||
for(int i = 0; i < 20; ++i) | ||
{ | ||
std::vector<double> point = ndRand->nextPoint(); | ||
TS_ASSERT_EQUALS(static_cast<unsigned int>(point.size()), 3); | ||
} | ||
} | ||
|
||
void test_That_Restart_Is_Passed_On_Correctly() | ||
{ | ||
boost::shared_ptr<NDPseudoRandomNumberGenerator> ndRand = | ||
createTestGenerator(boost::make_shared<Mantid::Kernel::MersenneTwister>(12345)); | ||
std::vector<double> firstPoint = ndRand->nextPoint(); | ||
TS_ASSERT_THROWS_NOTHING(ndRand->restart()); | ||
std::vector<double> firstPointAfterReset = ndRand->nextPoint(); | ||
for(size_t i = 0; i < firstPoint.size(); ++i) | ||
{ | ||
TS_ASSERT_EQUALS(firstPoint[i], firstPointAfterReset[i]); | ||
} | ||
} | ||
|
||
void test_That_Range_Of_SingleValue_Generator_Is_Respected() | ||
{ | ||
const double start(2.1), end(3.4); | ||
boost::shared_ptr<NDPseudoRandomNumberGenerator> ndRand = | ||
createTestGenerator(boost::make_shared<Mantid::Kernel::MersenneTwister>(12345,start,end)); | ||
std::vector<double> firstPoint = ndRand->nextPoint(); | ||
for(size_t i = 0; i < firstPoint.size(); ++i) | ||
{ | ||
TS_ASSERT(firstPoint[i] >= start && firstPoint[i] <= end); | ||
} | ||
} | ||
|
||
private: | ||
boost::shared_ptr<NDPseudoRandomNumberGenerator> createTestGenerator(boost::shared_ptr<PseudoRandomNumberGenerator> singleGen) | ||
{ | ||
const unsigned int ndims(3); | ||
return boost::make_shared<NDPseudoRandomNumberGenerator>(ndims, singleGen); | ||
} | ||
|
||
}; | ||
|
||
|
||
|
||
#endif //NDPSEUDORANDOMNUMBERGENERATORTEST_H_ |
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