LA_library/permutation.h

152 lines
6.0 KiB
C
Raw Normal View History

2008-02-26 14:55:23 +01:00
/*
LA: linear algebra C++ interface library
Copyright (C) 2021 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
2008-02-26 14:55:23 +01:00
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2009-11-12 22:01:19 +01:00
#ifndef _PERMUTATION_H
#define _PERMUTATION_H
#include "la_traits.h"
#include "vec.h"
2021-05-21 09:07:45 +02:00
typedef unsigned long long PERM_RANK_TYPE;
//permutations are always numbered from 1; offset is employed when applied to vectors and matrices
2009-11-12 22:01:19 +01:00
namespace LA {
//forward declaration
template <typename T> class NRVec_from1;
2021-05-14 17:39:22 +02:00
template <typename T> class CyclePerm;
template <typename T> class Partition;
2021-05-21 14:09:19 +02:00
template <typename T> class CompressedPartition;
template <typename T>
class NRPerm : public NRVec_from1<T> {
public:
//basic constructors
NRPerm(): NRVec_from1<T>() {};
NRPerm(const int n) : NRVec_from1<T>(n) {};
NRPerm(const NRVec_from1<T> &rhs): NRVec_from1<T>(rhs) {};
NRPerm(const T *a, const int n): NRVec_from1<T>(a, n) {};
2021-05-19 22:29:47 +02:00
explicit NRPerm(const CyclePerm<T> &rhs, const int n=0);
//specific operations
void identity();
bool is_valid() const; //is it really a permutation
bool is_identity() const;
NRPerm inverse() const;
NRPerm operator*(const NRPerm q) const; //q is rhs and applied first, this applied second
NRPerm conjugate_by(const NRPerm q) const; //q^-1 p q
int parity() const;
2021-05-19 22:29:47 +02:00
void randomize(void); //uniformly random by Fisher-Yates shuffle
2021-05-21 09:07:45 +02:00
bool next(); //generate next permutation in lex order
PERM_RANK_TYPE generate_all(void (*callback)(const NRPerm<T>&), int parity_select=0); //Algorithm from Knuth's vol.4, efficient but not in lex order!
PERM_RANK_TYPE generate_all2(void (*callback)(const NRPerm<T>&)); //recursive method, also not lexicographic
PERM_RANK_TYPE generate_all_lex(void (*callback)(const NRPerm<T>&)); //generate in lex order using next()
PERM_RANK_TYPE rank() const; //counted from 0 to n!-1
NRVec_from1<T> inversions(const int type, PERM_RANK_TYPE *prank=NULL) const; //inversion tables
explicit NRPerm(const int type, const NRVec_from1<T> &inversions); //compute permutation from inversions
explicit NRPerm(const int n, const PERM_RANK_TYPE rank); //compute permutation from its rank
};
2021-05-21 09:07:45 +02:00
extern PERM_RANK_TYPE factorial(const int n);
2021-05-21 14:09:19 +02:00
extern PERM_RANK_TYPE ipow(PERM_RANK_TYPE x, int i);
2021-05-14 17:39:22 +02:00
//permutations represented in the cycle format
template <typename T>
class CyclePerm : public NRVec_from1<NRVec_from1<T> > {
public:
CyclePerm() : NRVec_from1<NRVec_from1<T> >() {};
2021-05-19 22:29:47 +02:00
explicit CyclePerm(const NRPerm<T> &rhs);
2021-05-14 17:39:22 +02:00
bool is_valid() const; //is it really a permutation
bool is_identity() const; //no cycles of length > 1
2021-05-21 09:07:45 +02:00
void identity() {this->resize(0);};
2021-05-14 17:39:22 +02:00
CyclePerm inverse() const; //reverse all cycles
int parity() const; //negative if having odd number of even-length cycles
2021-05-19 22:29:47 +02:00
T max() const {T m=0; for(int i=1; i<=this->size(); ++i) {T mm= (*this)[i].max(); if(mm>m) m=mm;} return m;}
2021-05-21 14:09:19 +02:00
CompressedPartition<T> cycles(const T n) const;
2021-05-19 22:29:47 +02:00
void readfrom(const std::string &line);
CyclePerm operator*(const CyclePerm q) const; //q is rhs and applied first, this applied second
2021-05-21 14:09:19 +02:00
//@@@order = lcm of cycle lengths
2021-05-14 17:39:22 +02:00
};
2021-05-19 22:29:47 +02:00
template <typename T>
std::istream & operator>>(std::istream &s, CyclePerm<T> &x);
template <typename T>
std::ostream & operator<<(std::ostream &s, const CyclePerm<T> &x);
2021-05-21 14:09:19 +02:00
//compressed partitions stored as #of 1s, #of 2s, etc.
2021-05-14 17:39:22 +02:00
template <typename T>
2021-05-21 14:09:19 +02:00
class CompressedPartition : public NRVec_from1<T> {
2021-05-14 17:39:22 +02:00
public:
2021-05-21 14:09:19 +02:00
CompressedPartition(): NRVec_from1<T>() {};
CompressedPartition(const int n) : NRVec_from1<T>(n) {};
T sum() const {T s=0; for(int i=1; i<=this->size(); ++i) s += i*(*this)[i]; return s;}
T nparts() const {T s=0; for(int i=1; i<=this->size(); ++i) s += (*this)[i]; return s;}
T nclasses() const {T s=0; for(int i=1; i<=this->size(); ++i) if((*this)[i]) ++s; return s;}
2021-05-14 17:39:22 +02:00
bool is_valid() const {return this->size() == this->sum();}
2021-05-21 14:09:19 +02:00
explicit CompressedPartition(const Partition<T> &rhs) : NRVec_from1<T>(rhs.size()) {this->clear(); for(int i=1; i<=rhs.size(); ++i) if(!rhs[i]) break; else (*this)[rhs[i]]++; }
PERM_RANK_TYPE Sn_class_size() const;
2021-05-14 17:39:22 +02:00
2021-05-21 14:09:19 +02:00
};
2021-05-23 10:28:50 +02:00
template <typename T>
std::ostream & operator<<(std::ostream &s, const CompressedPartition<T> &x);
2021-05-21 14:09:19 +02:00
template <typename T>
class Partition : public NRVec_from1<T> {
public:
Partition(): NRVec_from1<T>() {};
Partition(const int n) : NRVec_from1<T>(n) {};
T nparts() const {T s=0; for(int i=1; i<=this->size(); ++i) if((*this)[i]) ++s; return s;}
bool is_valid() const {if(this->size() != this->sum()) return false; for(int i=2; i<=this->size(); ++i) if((*this)[i]>(*this)[i-1]) return false; return true; }
explicit Partition(const CompressedPartition<T> &rhs) : NRVec_from1<T>(rhs.size()) {this->clear(); int ithru=0; for(int i=rhs.size(); i>=1; --i) for(int j=0; j<rhs[i]; ++j) (*this)[++ithru]=i; }
2021-05-23 10:28:50 +02:00
Partition adjoint() const; //also called conjugate partition
2021-05-21 14:09:19 +02:00
PERM_RANK_TYPE Sn_irrep_dim() const;
PERM_RANK_TYPE generate_all(void (*callback)(const Partition<T>&), int nparts=0); //nparts <0 means at most to -nparts
//@@@yamanouchi symbol
2021-05-14 17:39:22 +02:00
};
2021-05-21 14:09:19 +02:00
template <typename T>
class YoungTableaux : public NRVec_from1<NRVec_from1<T> > {
public:
YoungTableaux() : NRVec_from1<NRVec_from1<T> >() {};
explicit YoungTableaux(const Partition<T> &frame);
bool is_valid() const; //@@@shape forms a partition
2021-05-23 10:28:50 +02:00
bool filled_correctly() const; //is it filled correctly@@@
2021-05-21 14:09:19 +02:00
};
//@@@enumerator of partitions of n to r parts and total from expr
//@@@Sn character table computation from young - young frame filling
2009-11-12 22:01:19 +01:00
}//namespace
#endif