1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
%perlcode %{
use strict;
use warnings;
use Carp qw/croak/;
use Math::GSL::Errno qw/$GSL_SUCCESS/;
our @EXPORT_OK = qw($gsl_qrng_niederreiter_2 $gsl_qrng_sobol
$gsl_qrng_halton $gsl_qrng_reversehalton
gsl_qrng_alloc gsl_qrng_memcpy gsl_qrng_clone
gsl_qrng_free gsl_qrng_init gsl_qrng_name
gsl_qrng_size gsl_qrng_state gsl_qrng_get
);
our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
=encoding utf8
=head1 NAME
Math::GSL::QRNG - Quasi-random number generators
=head1 SYNOPSIS
# use OO approach
use Math::GSL::QRNG;
my $QRNG = Math::GSL::QRNG::Sobol->new(2);
my @samples = $QRNG->get();
# use GSL interface
use Math::GSL::QRNG qw/:all/;
=head1 DESCRIPTION
This module interfaces with GNU Scientific Library quasi-random number generators (QRNG).
=head1 OO Interface
The OO Interface described in this documentation is available to all
different subclasses, namely:
=over
=item Math::GSL::QRNG::Sobol
=item Math::GSL::QRNG::Niederreiter2
=item Math::GSL::QRNG::Halton
=item Math::GSL::QRNG::ReverseHalton
=back
=cut
sub _init {
my ($self, $qrng, $dimension) = @_;
$self->{qrng} = gsl_qrng_alloc($qrng, $dimension);
return $self;
}
=head2 reinit
Reinitializes the generator to its starting point. Note that quasi-random
sequences do not use a seed and always produce the same set of values.
$qrng->reinit();
=cut
sub reinit {
my $self = shift;
gsl_qrng_init($self->{qrng});
}
=head2 get
Retrieves the next point from the sequence generator.
Returns C<undef> on error.
my @points = $qrng->get();
=cut
sub get {
my $self = shift;
my ($status, @values) = gsl_qrng_get($self->{qrng});
return $status == $GSL_SUCCESS ? @values : undef;
}
=head2 name
Retrieves the QRNG name.
my $name = $qrng->name();
=cut
sub name {
my $self = shift;
return gsl_qrng_name($self->{qrng});
}
=head2 state_size
Returns the size of the QRNG state.
=cut
sub state_size {
my $self = shift;
return gsl_qrng_size($self->{qrng});
}
=head2 clone
Returns an exact copy of the current QRNG.
=cut
sub clone {
my $self = shift;
my $new = { qrng => gsl_qrng_clone($self->{qrng})};
return bless $new, ref($self);
}
### gsl_qrng_state => TODO
### gsl_qrng_memcpy => ignore?
=head1 GSL API
Here is a list of all the functions included in this module :
=over
=item C<gsl_qrng_alloc($T, $n)> - This function returns a pointer to a newly-created instance of a quasi-random sequence generator of type $T and dimension $d. The type $T must be one of the constants included in this module.
=item C<gsl_qrng_clone($q)> - This function returns a pointer to a newly created generator which is an exact copy of the generator $q.
=item C<gsl_qrng_memcpy($dest, $src)> - This function copies the quasi-random sequence generator $src into the pre-existing generator $dest, making $dest into an exact copy of $src. The two generators must be of the same type.
=item C<gsl_qrng_free($q)> - This function frees all the memory associated with the generator $q.
Don't call this function explicitly. It will be called automatically in DESTROY.
=item C<gsl_qrng_init($q)> - This function reinitializes the generator $q to its starting point. Note that quasi-random sequences do not use a seed and always produce the same set of values.
=item C<gsl_qrng_name($q)> - This function returns a pointer to the name of the generator $q.
=item C<gsl_qrng_size($q)> - This function returns the size of the state of generator r from the generator $q. You can use this information to access the state directly.
=item C<gsl_qrng_state($q)> - This function returns a pointer to the state of generator r from the generator $q. You can use this information to access the state directly.
=item C<gsl_qrng_get>
=back
This module also contains the following constants :
=over
=item C<$gsl_qrng_niederreiter_2>
=item C<$gsl_qrng_sobol>
=item C<$gsl_qrng_halton>
=item C<$gsl_qrng_reversehalton>
=back
For more information on the functions, we refer you to the GSL official documentation: L<http://www.gnu.org/software/gsl/manual/html_node/>
=head1 EXAMPLES
=head1 AUTHORS
Jonathan "Duke" Leto <jonathan@leto.net>
Thierry Moisan <thierry.moisan@gmail.com>
Alberto Simões <ambs@cpan.org>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2008-2024 Jonathan "Duke" Leto and Thierry Moisan
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
package Math::GSL::QRNG::Sobol;
use parent 'Math::GSL::QRNG';
sub new {
my ($class, $dimension) = @_;
croak (__PACKAGE__.'::new($d) - supported dimensions are positive up to 40.')
if $dimension < 1 or $dimension > 40;
my $self = bless {}, $class;
$self->SUPER::_init($Math::GSL::QRNG::gsl_qrng_sobol, $dimension);
return $self;
}
# gsl_qrng_halton
package Math::GSL::QRNG::Halton;
use parent 'Math::GSL::QRNG';
sub new {
my ($class, $dimension) = @_;
croak (__PACKAGE__.'::new($d) - supported dimensions are positive up to 1229.')
if $dimension < 1 or $dimension > 1229;
my $self = bless {}, $class;
$self->SUPER::_init($Math::GSL::QRNG::gsl_qrng_halton, $dimension);
return $self;
}
# gsl_qrng_reversehalton
package Math::GSL::QRNG::ReverseHalton;
use parent 'Math::GSL::QRNG';
sub new {
my ($class, $dimension) = @_;
croak (__PACKAGE__.'::new($d) - supported dimensions are positive up to 1229.')
if $dimension < 1 or $dimension > 1229;
my $self = bless {}, $class;
$self->SUPER::_init($Math::GSL::QRNG::gsl_qrng_reversehalton, $dimension);
return $self;
}
# gsl_qrng_niederreiter_2
package Math::GSL::QRNG::Niederreiter2;
use parent 'Math::GSL::QRNG';
sub new {
my ($class, $dimension) = @_;
croak (__PACKAGE__.'::new($d) - supported dimensions are positive up to 12.')
if $dimension < 1 or $dimension > 12;
my $self = bless {}, $class;
$self->SUPER::_init($Math::GSL::QRNG::gsl_qrng_niederreiter_2, $dimension);
return $self;
}
__END__
%}
|