42 #ifndef O2SCL_PERMUTATION_H 43 #define O2SCL_PERMUTATION_H 56 #include <boost/numeric/ublas/vector.hpp> 58 #include <o2scl/vector.h> 60 #ifndef DOXYGEN_NO_O2NS 75 boost::numeric::ublas::unbounded_array<size_t> data;
82 if (dim>0) data.resize(dim);
89 if (size_>0) data.resize(size_);
90 for(
size_t i=0;i<size_;i++) {
98 if (
this==&v)
return *
this;
101 if (size_>0) data.resize(size_);
102 for(
size_t i=0;i<size_;i++) {
115 #if !O2SCL_NO_RANGE_CHECK 118 +
" in permutation::operator[]. Size: "+
120 " (index should be less than size).").c_str(),
exc_eindex);
130 #if !O2SCL_NO_RANGE_CHECK 133 +
" in permutation::operator[]. Size: "+
135 " (index should be less than size).").c_str(),
exc_eindex);
145 #if !O2SCL_NO_RANGE_CHECK 148 +
" in permutation::operator(). Size: "+
150 " (index should be less than size).").c_str(),
exc_eindex);
160 #if !O2SCL_NO_RANGE_CHECK 163 +
" in permutation::operator(). Size: "+
165 " (index should be less than size).").c_str(),
exc_eindex);
173 size_t get(
size_t i)
const {
174 #if !O2SCL_NO_RANGE_CHECK 176 O2SCL_ERR((((std::string)
"Permutation index ")+
177 szttos(i)+
" out of bounds"+
178 " in permutation::get(). Size: "+
180 " (index should be less than size).").c_str(),
exc_eindex);
188 int set(
size_t i,
size_t val) {
189 #if !O2SCL_NO_RANGE_CHECK 192 " out of bounds"+
" in permutation::set(). Size: "+
194 " (index should be less than size).").c_str(),
204 for(
size_t i=0;i<size_;i++) data[i]=i;
220 if (size_!=dim && size_>0)
free();
249 int swap(
const size_t i,
const size_t j) {
258 for(
size_t i=0;i<size_;i++) {
259 if (data[i]>size_)
return false;
260 for(
size_t j=0;j<i;j++) {
261 if (data[i]==data[j])
return false;
270 for (i = 0; i < (size_ / 2); i++){
271 size_t j = size_ - i - 1;
273 size_t tmp = this->data[i] ;
274 this->data[i] = this->data[j] ;
275 this->data[j] = tmp ;
283 for(
size_t i=0;i<size_;i++) {
290 template<
class vec_t>
int apply(vec_t &v)
const {
292 for(i=0;i<size_;i++) {
294 while (k>i) k=data[k];
317 for(i=0;i<size_;i++) {
319 while (k>i) k=data[k];
353 #ifndef DOXYGEN_NO_O2NS The main O<span style='position: relative; top: 0.3em; font-size: 0.8em'>2</span>scl O$_2$scl names...
void resize(size_t dim)
Resize.
int reverse()
Reverse the permutation.
permutation inverse() const
Compute the inverse of a permutation.
A class for representing permutations.
std::ostream & operator<<(std::ostream &os, const permutation &p)
Output operator for permutations.
permutation(size_t dim=0)
Create a permutation of size dim.
int free()
Free the memory.
int apply_inverse(vec_t &v) const
Apply the inverse permutation to a vector.
size_t & operator[](size_t i)
Array-like indexing.
size_t size() const
Return permutation size.
const size_t & operator()(size_t i) const
Array-like indexing.
#define O2SCL_ERR(d, n)
Set an error with message d and code n.
const size_t & operator[](size_t i) const
Array-like indexing.
size_t & operator()(size_t i)
Array-like indexing.
int init()
Initialize permutation to the identity.
Invalid index for array or matrix.
int swap(const size_t i, const size_t j)
Swap two elements of a permutation.
int allocate(size_t dim)
Allocate memory for a permutation of size dim.
bool valid() const
Check to see that a permutation is valid.
std::string szttos(size_t x)
Convert a size_t to a string.
int apply(vec_t &v) const
Apply the permutation to a vector.