Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #16

Merged
merged 5 commits into from
Mar 26, 2021
Merged

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions EigenRand/Dists/Basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,52 @@ namespace Eigen
};


/**
* @brief Generator of Bernoulli distribution
*
* @tparam _Scalar
*/
template<typename _Scalar>
class BernoulliGen : public GenBase<BernoulliGen<_Scalar>, _Scalar>
{
uint32_t p;
public:
using Scalar = _Scalar;

BernoulliGen(double _p = 0.5)
{
eigen_assert(0 <= _p && _p <= 1 );
p = (uint32_t)(_p * 0x80000000);
}

BernoulliGen(const BernoulliGen&) = default;
BernoulliGen(BernoulliGen&&) = default;

BernoulliGen& operator=(const BernoulliGen&) = default;
BernoulliGen& operator=(BernoulliGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
using namespace Eigen::internal;
return (((uint32_t)pfirst(std::forward<Rng>(rng)()) & 0x7FFFFFFF) < p) ? 1 : 0;
}

template<typename Packet, typename Rng>
EIGEN_STRONG_INLINE const Packet packetOp(Rng&& rng)
{
using namespace Eigen::internal;
using IPacket = decltype(reinterpret_to_int(std::declval<Packet>()));
using RUtils = RawbitsMaker<IPacket, Rng>;
auto one = pset1<Packet>(1);
auto zero = pset1<Packet>(0);
auto r = RUtils{}.rawbits(std::forward<Rng>(rng));
r = pand(r, pset1<IPacket>(0x7FFFFFFF));
return pblendv(pcmplt(r, pset1<IPacket>(p)), one, zero);
}
};


template<typename Derived, typename Urng>
using RandBitsType = CwiseNullaryOp<internal::scalar_rng_adaptor<RandbitsGen<typename Derived::Scalar>, typename Derived::Scalar, Urng, true>, const Derived>;

Expand Down Expand Up @@ -464,6 +510,48 @@ namespace Eigen
o.rows(), o.cols(), { std::forward<Urng>(urng) }
};
}

template<typename Derived, typename Urng>
using BernoulliType = CwiseNullaryOp<internal::scalar_rng_adaptor<BernoulliGen<typename Derived::Scalar>, typename Derived::Scalar, Urng, true>, const Derived>;

/**
* @brief generates 1 with probability `p` and 0 with probability `1 - p`
*
* @tparam Derived
* @tparam Urng
* @param rows the number of rows being generated
* @param cols the number of columns being generated
* @param urng c++11-style random number generator
* @param p a probability of generating 1
* @return a random matrix expression with a shape (`rows`, `cols`)
*/
template<typename Derived, typename Urng>
inline const BernoulliType<Derived, Urng>
bernoulli(Index rows, Index cols, Urng&& urng, double p = 0.5)
{
return {
rows, cols, { std::forward<Urng>(urng), BernoulliGen<typename Derived::Scalar>{ p } }
};
}

/**
* @brief generates 1 with probability `p` and 0 with probability `1 - p`
*
* @tparam Derived
* @tparam Urng
* @param o an instance of any type of Eigen::DenseBase
* @param urng c++11-style random number generator
* @param p a probability of generating 1
* @return a random matrix expression of the same shape as `o`
*/
template<typename Derived, typename Urng>
inline const BernoulliType<Derived, Urng>
bernoulli(Derived& o, Urng&& urng, double p = 0.5)
{
return {
o.rows(), o.cols(), { std::forward<Urng>(urng), BernoulliGen<typename Derived::Scalar>{ p } }
};
}
}
}

Expand Down
60 changes: 57 additions & 3 deletions EigenRand/Dists/Discrete.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ namespace Eigen
*
* @param _min, _max the range of integers being generated
*/
UniformIntGen(_Scalar _min, _Scalar _max)
UniformIntGen(_Scalar _min = 0, _Scalar _max = 0)
: pmin{ _min }, pdiff{ (size_t)(_max - _min) }
{
if ((pdiff + 1) > pdiff)
Expand All @@ -245,6 +245,9 @@ namespace Eigen
UniformIntGen(const UniformIntGen&) = default;
UniformIntGen(UniformIntGen&&) = default;

UniformIntGen& operator=(const UniformIntGen&) = default;
UniformIntGen& operator=(UniformIntGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -389,6 +392,17 @@ namespace Eigen
{
}

DiscreteGen()
: DiscreteGen({ 1 })
{
}

DiscreteGen(const DiscreteGen&) = default;
DiscreteGen(DiscreteGen&&) = default;

DiscreteGen& operator=(const DiscreteGen&) = default;
DiscreteGen& operator=(DiscreteGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -513,6 +527,17 @@ namespace Eigen
{
}

DiscreteGen()
: DiscreteGen({ 1 })
{
}

DiscreteGen(const DiscreteGen&) = default;
DiscreteGen(DiscreteGen&&) = default;

DiscreteGen& operator=(const DiscreteGen&) = default;
DiscreteGen& operator=(DiscreteGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -619,6 +644,17 @@ namespace Eigen
{
}

DiscreteGen()
: DiscreteGen({ 1 })
{
}

DiscreteGen(const DiscreteGen&) = default;
DiscreteGen(DiscreteGen&&) = default;

DiscreteGen& operator=(const DiscreteGen&) = default;
DiscreteGen& operator=(DiscreteGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -708,14 +744,20 @@ namespace Eigen
*
* @param _mean mean of the distribution
*/
PoissonGen(double _mean)
PoissonGen(double _mean = 1)
: mean{ _mean }, ne_mean{ std::exp(-_mean) }
{
sqrt_tmean = std::sqrt(2 * mean);
log_mean = std::log(mean);
g1 = mean * log_mean - std::lgamma(mean + 1);
}

PoissonGen(const PoissonGen&) = default;
PoissonGen(PoissonGen&&) = default;

PoissonGen& operator=(const PoissonGen&) = default;
PoissonGen& operator=(PoissonGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -835,6 +877,12 @@ namespace Eigen
}
}

BinomialGen(const BinomialGen&) = default;
BinomialGen(BinomialGen&&) = default;

BinomialGen& operator=(const BinomialGen&) = default;
BinomialGen& operator=(BinomialGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -959,11 +1007,17 @@ namespace Eigen
*
* @param _p probability of a trial generating true
*/
GeometricGen(double _p)
GeometricGen(double _p = 0.5)
: p{ _p }, rlog_q{ 1 / std::log(1 - p) }
{
}

GeometricGen(const GeometricGen&) = default;
GeometricGen(GeometricGen&&) = default;

GeometricGen& operator=(const GeometricGen&) = default;
GeometricGen& operator=(GeometricGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down
33 changes: 33 additions & 0 deletions EigenRand/Dists/NormalExp.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ namespace Eigen
NormalGen(const NormalGen&) = default;
NormalGen(NormalGen&&) = default;

NormalGen& operator=(const NormalGen&) = default;
NormalGen& operator=(NormalGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -156,6 +159,9 @@ namespace Eigen
LognormalGen(const LognormalGen&) = default;
LognormalGen(LognormalGen&&) = default;

LognormalGen& operator=(const LognormalGen&) = default;
LognormalGen& operator=(LognormalGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -199,6 +205,9 @@ namespace Eigen
StudentTGen(const StudentTGen&) = default;
StudentTGen(StudentTGen&&) = default;

StudentTGen& operator=(const StudentTGen&) = default;
StudentTGen& operator=(StudentTGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -267,6 +276,9 @@ namespace Eigen
ExponentialGen(const ExponentialGen&) = default;
ExponentialGen(ExponentialGen&&) = default;

ExponentialGen& operator=(const ExponentialGen&) = default;
ExponentialGen& operator=(ExponentialGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -320,6 +332,9 @@ namespace Eigen
GammaGen(const GammaGen&) = default;
GammaGen(GammaGen&&) = default;

GammaGen& operator=(const GammaGen&) = default;
GammaGen& operator=(GammaGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -487,6 +502,9 @@ namespace Eigen
WeibullGen(const WeibullGen&) = default;
WeibullGen(WeibullGen&&) = default;

WeibullGen& operator=(const WeibullGen&) = default;
WeibullGen& operator=(WeibullGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -533,6 +551,9 @@ namespace Eigen
ExtremeValueGen(const ExtremeValueGen&) = default;
ExtremeValueGen(ExtremeValueGen&&) = default;

ExtremeValueGen& operator=(const ExtremeValueGen&) = default;
ExtremeValueGen& operator=(ExtremeValueGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -577,6 +598,9 @@ namespace Eigen
ChiSquaredGen(const ChiSquaredGen&) = default;
ChiSquaredGen(ChiSquaredGen&&) = default;

ChiSquaredGen& operator=(const ChiSquaredGen&) = default;
ChiSquaredGen& operator=(ChiSquaredGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -619,6 +643,9 @@ namespace Eigen
CauchyGen(const CauchyGen&) = default;
CauchyGen(CauchyGen&&) = default;

CauchyGen& operator=(const CauchyGen&) = default;
CauchyGen& operator=(CauchyGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -674,6 +701,9 @@ namespace Eigen
BetaGen(const BetaGen&) = default;
BetaGen(BetaGen&&) = default;

BetaGen& operator=(const BetaGen&) = default;
BetaGen& operator=(BetaGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down Expand Up @@ -752,6 +782,9 @@ namespace Eigen
FisherFGen(const FisherFGen&) = default;
FisherFGen(FisherFGen&&) = default;

FisherFGen& operator=(const FisherFGen&) = default;
FisherFGen& operator=(FisherFGen&&) = default;

template<typename Rng>
EIGEN_STRONG_INLINE const _Scalar operator() (Rng&& rng)
{
Expand Down
2 changes: 1 addition & 1 deletion EigenRand/Macro.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#define EIGENRAND_WORLD_VERSION 0
#define EIGENRAND_MAJOR_VERSION 3
#define EIGENRAND_MINOR_VERSION 0
#define EIGENRAND_MINOR_VERSION 2

#if EIGEN_VERSION_AT_LEAST(3,3,7)
#else
Expand Down
Loading