On Tue, 25 May 2021 at 20:15, Gilles Sadowski <[email protected]> wrote:
> Hi.
>
> I wonder if/how we would introduce the following functionality:
> ---CUT---
> /**
> * @param rng Generator that will be shared in the returned sampler.
> * @param list Samplers whose underlying generators will be discarded
> in
> * the returned instance.
> * @return a sampler sharing the given provider.
> */
> public static ObjectSampler<double[]>
> withUniformRandomProvider(final UniformRandomProvider rng, final
> SharedStateContinuousSampler... list) {
> final SharedStateContinuousSampler[] samplers = new
> SharedStateContinuousSampler[list.length];
> for (int i = 0; i < list.length; i++) {
> samplers[i] = list[i].withUniformRandomProvider(rng);
> }
>
> return new ObjectSampler<double[]>() {
> /** {@inheritDoc} */
> @Override
> public double[] sample() {
> final double[] out = new double[list.length];
> for (int i = 0; i < list.length; i++) {
> out[i] = samplers[i].sample();
> }
> return out;
> }
> };
> }
> ---CUT---
>
Note it can return SharedStateObjectSampler<double[]>. The implementation
is to create a new instance using the same method:
static SharedStateObjectSampler<double[]> of(UniformRandomProvider rng,
SharedStateContinuousSampler... list) {
final SharedStateContinuousSampler[] samplers = new
SharedStateContinuousSampler[list.length];
for (int i = 0; i < list.length; i++) {
samplers[i] = list[i].withUniformRandomProvider(rng);
}
return new SharedStateObjectSampler<double[]>() {
@Override
public double[] sample() {
final double[] out = new double[samplers.length];
for (int i = 0; i < samplers.length; i++) {
out[i] = samplers[i].sample();
}
return out;
}
@Override
public SharedStateObjectSampler<double[]>
withUniformRandomProvider(UniformRandomProvider rng) {
return of(rng, samplers);
}
};
}
In this case though the anonymous inner class retains a reference to the
enclosing class. So chaining the withUniformRandomProvider calls on
returned objects would have a memory overhead. It would be cleaner to
return a static class with the same functionality.
Out of interest, do you have a use case?
Possible name:
CompoundSampler
This is a variation on the CompositeSampler I suggested in another thread.
A composite sampler (name TBD) is composed of 2 or more samplers. A sampler
is chosen using a weighted distribution and a single sample returned from 1
sampler. Here a compound sampler (name TBD) is composed of 2 or more
samplers. The return sample is a single sample from each sampler in the
compound.
I will open a ticket and PR with the static CompositeSamplers factory class
I created. The idea of a compound sampler could be added to the same class.
Alex