Permalink
Browse files

Multi set (#136)

Multisets by @ambs 

* More tests and docs

* Enough for now

* update changes

* Properly generate things based on GSL version

* Revert Leto and fix manifest

* ok, fixed manifestpr

* Handle correctly v1.15
  • Loading branch information...
1 parent 64622c8 commit 18ff216a9305e5fb33403b33d2c9ef04c4b16771 @ambs ambs committed with Nov 9, 2016
Showing with 378 additions and 1 deletion.
  1. +2 −0 Changes
  2. +19 −0 MANIFEST
  3. +4 −1 inc/GSLBuilder.pm
  4. +238 −0 pod/Multiset.pod
  5. +16 −0 swig/Multiset.i
  6. +99 −0 t/Multiset.t
View
@@ -1,5 +1,7 @@
=head1 Math::GSL Changes
+ - added Math::GSL::Multiset (standard gsl API and Perl OO)
+
=head1 v0.40 - ??? 2016
- gsl_linalg_givens now works
- gsl_vector_const_subvector is now exported properly
View
@@ -62,6 +62,7 @@ lib/Math/GSL/Multifit.pm
lib/Math/GSL/Multilarge.pm
lib/Math/GSL/Multimin.pm
lib/Math/GSL/Multiroots.pm
+lib/Math/GSL/Multiset.pm
lib/Math/GSL/NTuple.pm
lib/Math/GSL/ODEIV.pm
lib/Math/GSL/Permutation.pm
@@ -269,6 +270,12 @@ pm/Math/GSL/Multiroots.pm.2.0
pm/Math/GSL/Multiroots.pm.2.1
pm/Math/GSL/Multiroots.pm.2.2
pm/Math/GSL/Multiroots.pm.2.2.1
+pm/Math/GSL/Multiset.pm.1.15
+pm/Math/GSL/Multiset.pm.1.16
+pm/Math/GSL/Multiset.pm.2.0
+pm/Math/GSL/Multiset.pm.2.1
+pm/Math/GSL/Multiset.pm.2.2
+pm/Math/GSL/Multiset.pm.2.2.1
pm/Math/GSL/NTuple.pm.1.15
pm/Math/GSL/NTuple.pm.1.16
pm/Math/GSL/NTuple.pm.2.0
@@ -434,6 +441,7 @@ pod/Multifit.pod
pod/Multilarge.pod
pod/Multimin.pod
pod/Multiroots.pod
+pod/Multiset.pod
pod/NTuple.pod
pod/ODEIV.pod
pod/Permutation.pod
@@ -491,6 +499,7 @@ swig/Multifit.i
swig/Multilarge.i
swig/Multimin.i
swig/Multiroots.i
+swig/Multiset.i
swig/NTuple.i
swig/ODEIV.i
swig/Permutation.i
@@ -546,6 +555,7 @@ t/Multifit.t
t/Multilarge.t
t/Multimin.t
t/Multiroots.t
+t/Multiset.t
t/NTuple.t
t/ODEIV.t
t/Permutation.t
@@ -749,6 +759,12 @@ xs/Multiroots_wrap.2.0.c
xs/Multiroots_wrap.2.1.c
xs/Multiroots_wrap.2.2.1.c
xs/Multiroots_wrap.2.2.c
+xs/Multiset_wrap.1.15.c
+xs/Multiset_wrap.1.16.c
+xs/Multiset_wrap.2.0.c
+xs/Multiset_wrap.2.1.c
+xs/Multiset_wrap.2.2.1.c
+xs/Multiset_wrap.2.2.c
xs/NTuple_wrap.1.15.c
xs/NTuple_wrap.1.16.c
xs/NTuple_wrap.2.0.c
@@ -885,3 +901,6 @@ xs/Wavelet_wrap.2.2.1.c
xs/Wavelet_wrap.2.2.c
xt/01-pod.t
xt/style-trailing-space.t
+
+
+META.json
View
@@ -12,6 +12,9 @@ use base 'Module::Build';
sub is_release {
return -e '.git' ? 0 : 1;
}
+
+# Only subsystems that existed in GSL 1.15 are listed here
+# update inc/GSLBuilder.pm and Build.PL to add others
sub subsystems {
sort qw/
Diff Machine Statistics BLAS
@@ -27,7 +30,7 @@ sub subsystems {
NTuple Integration Sort Test
DHT Interp ODEIV SF
Deriv Linalg Permutation Spline
- Version
+ Version Multiset
/;
}
View
@@ -0,0 +1,238 @@
+%perlcode %{
+
+use Math::GSL qw/gsl_version/;
+
+@EXPORT_OK = qw/
+ gsl_multiset_calloc gsl_multiset_alloc
+ gsl_multiset_init_first gsl_multiset_init_last
+ gsl_multiset_free gsl_multiset_memcpy
+ gsl_multiset_get gsl_multiset_n gsl_multiset_k
+ gsl_multiset_data
+ gsl_multiset_valid
+ gsl_multiset_next gsl_multiset_prev
+ gsl_multiset_fwrite
+ gsl_multiset_fread
+ gsl_multiset_fprintf
+ gsl_multiset_fscanf
+/;
+
+
+%EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
+
+sub new {
+ my ($class, $n, $k) = @_;
+
+ if (gsl_version() < v1.16 && $k > $n) {
+ die __PACKAGE__.": This version of GSL doesn't support k > n"
+ }
+
+ my $self = { k => $k, n => $n, multiset => gsl_multiset_calloc($n, $k)};
+
+ return bless $self, $class;
+}
+
+sub init_first {
+ my ($self) = @_;
+ gsl_multiset_init_first($self->{multiset});
+}
+
+sub init_last {
+ my ($self) = @_;
+ gsl_multiset_init_last($self->{multiset});
+}
+
+
+sub get {
+ my ($self, $i) = @_;
+ die __PACKAGE__.": element $i is out of range (0 <= i < $self->{k})"
+ unless $i >= 0 && $i < $self->{k};
+ return gsl_multiset_get($self->{multiset}, $i);
+}
+
+sub k { $_[0]->{k} }
+sub n { $_[0]->{n} }
+
+sub clone {
+ my $self = shift;
+ my $other = ref($self)->new($self->n, $self->k);
+ gsl_multiset_memcpy($other->{multiset}, $self->{multiset});
+ return $other;
+}
+
+sub next {
+ my $self=shift;
+ return gsl_multiset_next($self->{multiset});
+}
+
+sub prev {
+ my $self=shift;
+ return gsl_multiset_prev($self->{multiset});
+}
+
+sub to_list {
+ my $self = shift;
+ return map { $self->get($_) } (0..$self->k-1);
+}
+
+sub DESTROY {
+ my $self = shift;
+ gsl_multiset_free($self->{multiset});
+}
+
+__END__
+
+=encoding utf8
+
+=head1 NAME
+
+Math::GSL::Multiset - Multisets manipulation
+
+=head1 SYNOPSIS
+
+ use Math::GSL::Multiset qw/:all/;
+
+ my $ms = Math::GSL::Multiset->($n, $k);
+
+ my $value = $ms->get(2);
+
+ # compute next multiset
+ $ms->next;
+ # compute the previous multiset
+
+
+ # clone a multiset
+ my $other = $ms->clone();
+
+=head1 DESCRIPTION
+
+A multiset c is represented by an array of k integers
+in the range 0 to n-1, where each value c_i may occur more
+than once. The multiset c corresponds to indices of k elements
+chosen from an n element vector with replacement. In mathematical
+terms, n is the cardinality of the multiset while k is the maximum
+multiplicity of any value.
+
+=head2 Object Oriented API
+
+Handy Perl-style OO API for Multisets.
+
+=over 4
+
+=item C<new>
+
+Creates a new multiset with parameters n, k and initializes it to the
+lexicographically first multiset element, i.e., 0 repeated k times.
+
+ my $ms = Math::GSL::Multiset->($n, $k);
+
+=item C<init_first>
+
+Initializes the multiset to the lexicographically first multiset element, i.e.
+0 repeated k times.
+
+ $ms->init_first;
+
+=item C<init_last>
+
+Initializes the multiset c to the lexicographically last multiset element, i.e.
+n-1 repeated k times.
+
+ $ms->init_last;
+
+=item C<get>
+
+Returns the value of the i-th element of the multiset. If i lies outside
+the allowed range of 0 to k-1 then the error handler is invoked and 0 is returned.
+
+ my $val = $ms->get($k-1);
+
+=item C<next>
+
+Advances the multiset to the next multiset element in lexicographic order and returns
+GSL_SUCCESS. If no further multisets elements are available it returns GSL_FAILURE and
+leaves the multiset unmodified. Starting with the first multiset and repeatedly applying
+this function will iterate through all possible multisets of a given order.
+
+ $ms->next();
+
+=item C<prev>
+
+ $ms->prev();
+
+Steps backwards from the multiset to the previous multiset element in lexicographic
+order, returning GSL_SUCCESS. If no previous multiset is available it returns
+GSL_FAILURE and leaves the multiset unmodified.
+
+=item C<to_list>
+
+Creates a Perl list of integers with the values from the multiset, starting at
+index 0 and ending at index $k-1.
+
+ @data = $ms->to_list;
+
+=item C<clone>
+
+Creates a new multiset with the same size, and same values.
+
+ my $new = $ms->clone;
+
+=back
+
+=head2 GSL API
+
+For reference on these methds, please consult the GSL documentation.
+
+=over 4
+
+=item C<gsl_multiset_calloc>
+
+=item C<gsl_multiset_alloc>
+
+=item C<gsl_multiset_init_first>
+
+=item C<gsl_multiset_init_last>
+
+=item C<gsl_multiset_free>
+
+=item C<gsl_multiset_memcpy>
+
+=item C<gsl_multiset_get>
+
+=item C<gsl_multiset_n>
+
+=item C<gsl_multiset_k>
+
+=item C<gsl_multiset_data>
+
+=item C<gsl_multiset_valid>
+
+=item C<gsl_multiset_next>
+
+=item C<gsl_multiset_prev>
+
+=item C<gsl_multiset_fwrite>
+
+=item C<gsl_multiset_fread>
+
+=item C<gsl_multiset_fprintf>
+
+=item C<gsl_multiset_fscanf>
+
+
+=back
+
+=head1 AUTHORS
+
+Jonathan "Duke" Leto <jonathan@leto.net> and Thierry Moisan <thierry.moisan@gmail.com>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2008-2016 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
+%}
+
+
View
@@ -0,0 +1,16 @@
+%module "Math::GSL::Multiset"
+
+%include "typemaps.i"
+%include "gsl_typemaps.i"
+%include "renames.i"
+
+%{
+ #include "gsl/gsl_types.h"
+ #include "gsl/gsl_inline.h"
+ #include "gsl/gsl_multiset.h"
+%}
+
+%include "gsl/gsl_types.h"
+%include "gsl/gsl_inline.h"
+%include "gsl/gsl_multiset.h"
+%include "../pod/Multiset.pod"
Oops, something went wrong.

0 comments on commit 18ff216

Please sign in to comment.