diff --git a/src/gnu/CMakeLists.txt b/src/gnu/CMakeLists.txt index c8b5f59cdb..02cf145960 100644 --- a/src/gnu/CMakeLists.txt +++ b/src/gnu/CMakeLists.txt @@ -2,23 +2,16 @@ add_library( nrngnu STATIC Binomial.cpp DiscUnif.cpp - Erlang.cpp Geom.cpp HypGeom.cpp - LogNorm.cpp MCellRan4RNG.cpp mcran4.cpp - NegExp.cpp - Normal.cpp NrnRandom123RNG.cpp nrnran123.cpp Poisson.cpp Rand.cpp Random.cpp - RndInt.cpp - RNG.cpp - Uniform.cpp - Weibull.cpp) + RNG.cpp) set_property(TARGET nrngnu PROPERTY POSITION_INDEPENDENT_CODE ON) target_include_directories(nrngnu SYSTEM PRIVATE "${PROJECT_SOURCE_DIR}/external/Random123/include") target_compile_definitions(nrngnu PRIVATE ${NRN_R123_COMPILE_DEFS}) diff --git a/src/gnu/Erlang.cpp b/src/gnu/Erlang.cpp deleted file mode 100755 index ce03930f35..0000000000 --- a/src/gnu/Erlang.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "Random.h" -#include "Erlang.h" - -double Erlang::operator()() -{ - double prod = 1.0; - - for (int i = 0; i < k; i++) { - prod *= pGenerator -> asDouble(); - } - return(-log(prod)/a); -} diff --git a/src/gnu/Erlang.h b/src/gnu/Erlang.h deleted file mode 100755 index 98774310f0..0000000000 --- a/src/gnu/Erlang.h +++ /dev/null @@ -1,62 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#pragma once - -#include "Random.h" - -class Erlang: public Random { -protected: - double pMean; - double pVariance; - int k; - double a; - void setState(); -public: - Erlang(double mean, double variance, RNG *gen); - - double mean(); - double mean(double x); - double variance(); - double variance(double x); - - virtual double operator()(); - -}; - - -inline void Erlang::setState() { - k = int( (pMean * pMean ) / pVariance + 0.5 ); - k = (k > 0) ? k : 1; - a = k / pMean; -} - -inline Erlang::Erlang(double mean, double variance, RNG *gen) : Random(gen) -{ - pMean = mean; pVariance = variance; - setState(); -} - -inline double Erlang::mean() { return pMean; } -inline double Erlang::mean(double x) { - double tmp = pMean; pMean = x; setState(); return tmp; -}; - -inline double Erlang::variance() { return pVariance; } -inline double Erlang::variance(double x) { - double tmp = pVariance; pVariance = x; setState(); return tmp; -} diff --git a/src/gnu/LogNorm.cpp b/src/gnu/LogNorm.cpp deleted file mode 100755 index 89d457a7e9..0000000000 --- a/src/gnu/LogNorm.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "Random.h" -#include "Normal.h" - -#include "LogNorm.h" - -#ifndef M_E -#define M_E 2.71828182845904523536 -#endif - -// -// See Simulation, Modelling & Analysis by Law & Kelton, pp260 -// -// - -double LogNormal::operator()() -{ - return( pow(M_E, this->Normal::operator()() ) ); -} - - diff --git a/src/gnu/LogNorm.h b/src/gnu/LogNorm.h deleted file mode 100755 index 5404c0e3f6..0000000000 --- a/src/gnu/LogNorm.h +++ /dev/null @@ -1,72 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#pragma once - -#include "Normal.h" - -class LogNormal: public Normal { -protected: - double logMean; - double logVariance; - void setState(); -public: - LogNormal(double mean, double variance, RNG *gen); - double mean(); - double mean(double x); - double variance(); - double variance(double x); - virtual double operator()(); -}; - - -inline void LogNormal::setState() -{ - double m2 = logMean * logMean; - pMean = log(m2 / sqrt(logVariance + m2) ); -// from ch@heike.informatik.uni-dortmund.de: -// (was pVariance = log((sqrt(logVariance + m2)/m2 )); ) - pStdDev = sqrt(log((logVariance + m2)/m2 )); -} - -inline LogNormal::LogNormal(double mean, double variance, RNG *gen) - : Normal(mean, variance, gen) -{ - logMean = mean; - logVariance = variance; - setState(); -} - -inline double LogNormal::mean() { - return logMean; -} - -inline double LogNormal::mean(double x) -{ - double t=logMean; logMean = x; setState(); - return t; -} - -inline double LogNormal::variance() { - return logVariance; -} - -inline double LogNormal::variance(double x) -{ - double t=logVariance; logVariance = x; setState(); - return t; -} diff --git a/src/gnu/NegExp.cpp b/src/gnu/NegExp.cpp deleted file mode 100755 index 99ad65cde0..0000000000 --- a/src/gnu/NegExp.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "Random.h" -#include "NegExp.h" - -double NegativeExpntl::operator()() -{ - return(-pMean * log(pGenerator -> asDouble())); -} - diff --git a/src/gnu/NegExp.h b/src/gnu/NegExp.h deleted file mode 100755 index 57ef63d316..0000000000 --- a/src/gnu/NegExp.h +++ /dev/null @@ -1,49 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#pragma once - - -// -// Negative Exponential Random Numbers -// -// - -#include "Random.h" - -class NegativeExpntl: public Random { -protected: - double pMean; -public: - NegativeExpntl(double xmean, RNG *gen); - double mean(); - double mean(double x); - - virtual double operator()(); -}; - - -inline NegativeExpntl::NegativeExpntl(double xmean, RNG *gen) -: Random(gen) { - pMean = xmean; -} - -inline double NegativeExpntl::mean() { return pMean; } -inline double NegativeExpntl::mean(double x) { - double t = pMean; pMean = x; - return t; -} diff --git a/src/gnu/Normal.cpp b/src/gnu/Normal.cpp deleted file mode 100755 index 11ef2e21be..0000000000 --- a/src/gnu/Normal.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "Random.h" -#include "Normal.h" -// -// See Simulation, Modelling & Analysis by Law & Kelton, pp259 -// -// This is the ``polar'' method. -// - -double Normal::operator()() -{ - - if (haveCachedNormal == 1) { - haveCachedNormal = 0; - return(cachedNormal * pStdDev + pMean ); - } else { - - for(;;) { - double u1 = pGenerator -> asDouble(); - double u2 = pGenerator -> asDouble(); - double v1 = 2 * u1 - 1; - double v2 = 2 * u2 - 1; - double w = (v1 * v1) + (v2 * v2); - -// -// We actually generate two IID normal distribution variables. -// We cache the one & return the other. -// - if (w <= 1) { - double y = sqrt( (-2 * log(w)) / w); - double x1 = v1 * y; - double x2 = v2 * y; - - haveCachedNormal = 1; - cachedNormal = x2; - return(x1 * pStdDev + pMean); - } - } - } -} - diff --git a/src/gnu/Normal.h b/src/gnu/Normal.h deleted file mode 100755 index 1f2bff87db..0000000000 --- a/src/gnu/Normal.h +++ /dev/null @@ -1,59 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#pragma once -#include "Random.h" - -class Normal: public Random { - char haveCachedNormal; - double cachedNormal; - -protected: - double pMean; - double pVariance; - double pStdDev; - -public: - Normal(double xmean, double xvariance, RNG *gen); - double mean(); - double mean(double x); - double variance(); - double variance(double x); - virtual double operator()(); -}; - - -inline Normal::Normal(double xmean, double xvariance, RNG *gen) -: Random(gen) { - pMean = xmean; - pVariance = xvariance; - pStdDev = sqrt(pVariance); - haveCachedNormal = 0; -} - -inline double Normal::mean() { return pMean; }; -inline double Normal::mean(double x) { - double t=pMean; pMean = x; - return t; -} - -inline double Normal::variance() { return pVariance; } -inline double Normal::variance(double x) { - double t=pVariance; pVariance = x; - pStdDev = sqrt(pVariance); - return t; -}; diff --git a/src/gnu/RNG.h b/src/gnu/RNG.h index 56202c1cec..ed83ffe47a 100755 --- a/src/gnu/RNG.h +++ b/src/gnu/RNG.h @@ -23,6 +23,7 @@ Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. #include #include #include +#include union PrivateRNGSingleType { // used to access floats as unsigneds float s; @@ -41,16 +42,28 @@ class RNG { static PrivateRNGSingleType singleMantissa; // mantissa bit vector static PrivateRNGDoubleType doubleMantissa; // mantissa bit vector public: + using result_type = std::uint32_t; + RNG(); virtual ~RNG(); // // Return a long-words word of random bits // - virtual uint32_t asLong() = 0; + virtual result_type asLong() = 0; virtual void reset() = 0; // // Return random bits converted to either a float or a double // virtual float asFloat(); virtual double asDouble(); + + static constexpr result_type min() { + return std::numeric_limits::min(); + } + static constexpr result_type max() { + return std::numeric_limits::max(); + } + result_type operator()() { + return asLong(); + } }; diff --git a/src/gnu/Rand.cpp b/src/gnu/Rand.cpp index 09f8fe64d6..5637c27935 100644 --- a/src/gnu/Rand.cpp +++ b/src/gnu/Rand.cpp @@ -1,7 +1,7 @@ #include "Rand.hpp" #include "NrnRandom123RNG.hpp" -#include "Normal.h" +#include "distributions.hpp" Rand::Rand(unsigned long seed, int size, Object* obj) { // printf("Rand\n"); diff --git a/src/gnu/RndInt.cpp b/src/gnu/RndInt.cpp deleted file mode 100755 index 9bdb39be72..0000000000 --- a/src/gnu/RndInt.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "RndInt.h" diff --git a/src/gnu/RndInt.h b/src/gnu/RndInt.h deleted file mode 100755 index 3a46e9d9a5..0000000000 --- a/src/gnu/RndInt.h +++ /dev/null @@ -1,168 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1990 Free Software Foundation - adapted from a submission from John Reidl - - -GNU CC is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY. No author or distributor -accepts responsibility to anyone for the consequences of using it -or for whether it serves any particular purpose or works at all, -unless he says so in writing. Refer to the GNU CC General Public -License for full details. - -Everyone is granted permission to copy, modify and redistribute -GNU CC, but only under the conditions described in the -GNU CC General Public License. A copy of this license is -supposed to have been given to you along with GNU CC so you -can know your rights and responsibilities. It should be in a -file named COPYING. Among other things, the copyright notice -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#pragma once -// RandomInteger uses a random number generator to generate an integer -// in a specified range. By default the range is 0..1. Since in my -// experience random numbers are often needed for a wide variety of -// ranges in the same program, this generator accepts a new low or high value -// as an argument to the asLong and operator() methods to temporarily -// override stored values - -#include -#include "RNG.h" - -class RandomInteger -{ - protected: - RNG *pGenerator; - long pLow; - long pHigh; - - long _asLong(long, long); - - public: - - RandomInteger(long low, long high, RNG *gen); - RandomInteger(long high, RNG *gen); - RandomInteger(RNG *gen); - -// read params - - long low() const; - long high() const; - RNG* generator() const; - -// change params - - long low(long x); - long high(long x); - RNG* generator(RNG *gen); - -// get a random number - - long asLong(); - long operator()(); // synonym for asLong - int asInt(); // (possibly) truncate as int - -// override params for one shot - - long asLong(long high); - long asLong(long low, long high); - - long operator () (long high); // synonyms - long operator () (long low, long high); - -}; - - -inline RandomInteger::RandomInteger(long low, long high, RNG *gen) - : pGenerator(gen), - pLow((low < high) ? low : high), - pHigh((low < high) ? high : low) -{} - -inline RandomInteger::RandomInteger(long high, RNG *gen) - : pGenerator(gen), - pLow((0 < high) ? 0 : high), - pHigh((0 < high) ? high : 0) -{} - - -inline RandomInteger::RandomInteger(RNG *gen) - : pGenerator(gen), - pLow(0), - pHigh(1) -{} - -inline RNG* RandomInteger::generator() const { return pGenerator;} -inline long RandomInteger::low() const { return pLow; } -inline long RandomInteger::high() const { return pHigh; } - -inline RNG* RandomInteger::generator(RNG *gen) -{ - RNG *tmp = pGenerator; pGenerator = gen; return tmp; -} - -inline long RandomInteger::low(long x) -{ - long tmp = pLow; pLow = x; return tmp; -} - -inline long RandomInteger:: high(long x) -{ - long tmp = pHigh; pHigh = x; return tmp; -} - -inline long RandomInteger:: _asLong(long low, long high) -{ - return (pGenerator->asLong() % (high-low+1)) + low; -} - - -inline long RandomInteger:: asLong() -{ - return _asLong(pLow, pHigh); -} - -inline long RandomInteger:: asLong(long high) -{ - return _asLong(pLow, high); -} - -inline long RandomInteger:: asLong(long low, long high) -{ - return _asLong(low, high); -} - -inline long RandomInteger:: operator () () -{ - return _asLong(pLow, pHigh); -} - -inline long RandomInteger:: operator () (long high) -{ - return _asLong(pLow, high); -} - -inline long RandomInteger:: operator () (long low, long high) -{ - return _asLong(low, high); -} - - - - -inline int RandomInteger:: asInt() -{ - return int(asLong()); -} diff --git a/src/gnu/Uniform.cpp b/src/gnu/Uniform.cpp deleted file mode 100755 index ea41b9610d..0000000000 --- a/src/gnu/Uniform.cpp +++ /dev/null @@ -1,26 +0,0 @@ -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "Random.h" -#include "Uniform.h" - -double Uniform::operator()() -{ - return( pLow + delta * pGenerator -> asDouble() ); -} diff --git a/src/gnu/Uniform.h b/src/gnu/Uniform.h deleted file mode 100755 index e2f0d48d62..0000000000 --- a/src/gnu/Uniform.h +++ /dev/null @@ -1,65 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#pragma once - -#include "Random.h" - -// -// The interval [lo..hi] -// - -class Uniform: public Random { - double pLow; - double pHigh; - double delta; -public: - Uniform(double low, double high, RNG *gen); - - double low(); - double low(double x); - double high(); - double high(double x); - - virtual double operator()(); -}; - - -inline Uniform::Uniform(double low, double high, RNG *gen) : Random(gen) -{ - pLow = (low < high) ? low : high; - pHigh = (low < high) ? high : low; - delta = pHigh - pLow; -} - -inline double Uniform::low() { return pLow; } - -inline double Uniform::low(double x) { - double tmp = pLow; - pLow = x; - delta = pHigh - pLow; - return tmp; -} - -inline double Uniform::high() { return pHigh; } - -inline double Uniform::high(double x) { - double tmp = pHigh; - pHigh = x; - delta = pHigh - pLow; - return tmp; -} diff --git a/src/gnu/Weibull.cpp b/src/gnu/Weibull.cpp deleted file mode 100755 index 1c408ac64d..0000000000 --- a/src/gnu/Weibull.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#ifdef __GNUG__ -#pragma implementation -#endif -#include "Random.h" -#include "Weibull.h" - -// -// See Simulation, Modelling & Analysis by Law & Kelton, pp259 -// -// This is the ``polar'' method. -// - -double Weibull::operator()() -{ - return( pow(pBeta * ( - log(1 - pGenerator -> asDouble()) ), pInvAlpha) ); -} diff --git a/src/gnu/Weibull.h b/src/gnu/Weibull.h deleted file mode 100755 index 6586b059f3..0000000000 --- a/src/gnu/Weibull.h +++ /dev/null @@ -1,68 +0,0 @@ -// This may look like C code, but it is really -*- C++ -*- -/* -Copyright (C) 1988 Free Software Foundation - written by Dirk Grunwald (grunwald@cs.uiuc.edu) - -This file is part of the GNU C++ Library. This library is free -software; you can redistribute it and/or modify it under the terms of -the GNU Library General Public License as published by the Free -Software Foundation; either version 2 of the License, or (at your -option) any later version. This library 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 Library General Public License for more details. -You should have received a copy of the GNU Library General Public -License along with this library; if not, write to the Free Software -Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. -*/ -#pragma once - -#include "Random.h" - -class Weibull: public Random { -protected: - double pAlpha; - double pInvAlpha; - double pBeta; - - void setState(); - -public: - Weibull(double alpha, double beta, RNG *gen); - - double alpha(); - double alpha(double x); - - double beta(); - double beta(double x); - - virtual double operator()(); -}; - - -inline void Weibull::setState() { - pInvAlpha = 1.0 / pAlpha; -} - -inline Weibull::Weibull(double alpha, double beta, RNG *gen) : Random(gen) -{ - pAlpha = alpha; - pBeta = beta; - setState(); -} - -inline double Weibull::alpha() { return pAlpha; } - -inline double Weibull::alpha(double x) { - double tmp = pAlpha; - pAlpha = x; - setState(); - return tmp; -} - -inline double Weibull::beta() { return pBeta; }; -inline double Weibull::beta(double x) { - double tmp = pBeta; - pBeta = x; - return tmp; -}; diff --git a/src/gnu/distributions.hpp b/src/gnu/distributions.hpp new file mode 100644 index 0000000000..5cc92a7ad4 --- /dev/null +++ b/src/gnu/distributions.hpp @@ -0,0 +1,92 @@ +#pragma once +#include +#include "Random.h" + +class Erlang: public Random { +public: + Erlang(double mean, double variance, RNG *gen) + : Random(gen) + , d(mean * mean / variance, variance / mean) + {} + + double operator()() override { + return d(*generator()); + } + +private: + std::gamma_distribution<> d; +}; + +class LogNormal: public Random { +public: + LogNormal(double mean, double variance, RNG *gen) + : Random(gen) + , d(std::log(mean * mean / std::sqrt(variance + (mean * mean))), std::sqrt(std::log(variance/(mean * mean) + 1))) + {} + + double operator()() override { + return d(*generator()); + } +private: + std::lognormal_distribution<> d; +}; + +class NegativeExpntl: public Random { +public: + NegativeExpntl(double mean, RNG *gen) + : Random(gen) + , d(1 / mean) + {} + + double operator()() override { + return d(*generator()); + } +private: + std::exponential_distribution<> d; +}; + +class Normal: public Random { +public: + Normal(double mean, double variance, RNG *gen) + : Random(gen) + , d(mean, std::sqrt(variance)) + {} + + double operator()() override { + return d(*generator()); + } + +private: + std::normal_distribution<> d; +}; + +class Uniform: public Random { +public: + Uniform(double low, double high, RNG *gen) + : Random(gen) + , d(low, high) + {} + + double operator()() override { + return d(*generator()); + } + +private: + std::uniform_real_distribution<> d; +}; + +class Weibull: public Random { +public: + Weibull(double alpha, double beta, RNG *gen) + : Random(gen) + , d(alpha, std::pow(beta, 1 / alpha)) + {} + + double operator()() override { + return d(*generator()); + } + +private: + std::weibull_distribution<> d; +}; + diff --git a/src/ivoc/ivocrand.cpp b/src/ivoc/ivocrand.cpp index 2b6fd3b5fc..aaede102cf 100644 --- a/src/ivoc/ivocrand.cpp +++ b/src/ivoc/ivocrand.cpp @@ -18,17 +18,11 @@ #include #include -#include -#include #include #include -#include #include -#include -#include -#include #include -#include +#include #include #include diff --git a/src/ivoc/ivocvect.cpp b/src/ivoc/ivocvect.cpp index 9681a6b270..f257c46ecc 100644 --- a/src/ivoc/ivocvect.cpp +++ b/src/ivoc/ivocvect.cpp @@ -97,7 +97,7 @@ static double dmaxint_ = 9007199254740992; // definition of random numer generator #include "Rand.hpp" -#include +#include "distributions.hpp" #if HAVE_IV #include "utility.h" diff --git a/test/pytest_coreneuron/test_zptrlist.py b/test/pytest_coreneuron/test_zptrlist.py index 3f5b11b4cf..83db2575d3 100644 --- a/test/pytest_coreneuron/test_zptrlist.py +++ b/test/pytest_coreneuron/test_zptrlist.py @@ -23,7 +23,7 @@ def test_random_play(): # for coverage of ptrlist changes #1815 for _ in range(3): h.fadvance() std = h.Vector( - [0.0, 0.00046940111168614074, 0.004571699024450762, 0.01086603263672303] + [0.0, 0.006769657533393409, 0.01017024899523811, 0.016085586243023933] ) assert v_vec.c().sub(std).abs().sum() < 1e-15