algebramix_doc 0.3
|
00001 00002 /****************************************************************************** 00003 * MODULE : permutation.cpp 00004 * DESCRIPTION: Permutations 00005 * COPYRIGHT : (C) 2009 Joris van der Hoeven 00006 ******************************************************************************* 00007 * This software falls under the GNU general public license and comes WITHOUT 00008 * ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details. 00009 * If you don't have this file, write to the Free Software Foundation, Inc., 00010 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00011 ******************************************************************************/ 00012 00013 #include <algebramix/permutation.hpp> 00014 00016 00017 namespace mmx { 00018 00019 permutation 00020 transposition (nat i, nat j, nat n) { 00021 vector<nat> v= id_vector (n); 00022 v[i]= j; 00023 v[j]= i; 00024 return permutation (v); 00025 } 00026 00027 permutation 00028 cycle (nat n, int plus) { 00029 if (plus >= 0) plus= ((nat) plus) % n; 00030 else plus= (n-1) - (((nat) (-1-plus)) % n); 00031 vector<nat> v= id_vector (n); 00032 for (nat i=0; i<n; i++) 00033 if (i + plus < n) v[i]= i + plus; 00034 else v[i]= i + plus - n; 00035 return permutation (v); 00036 } 00037 00038 nat 00039 nr_transpositions (const permutation& p) { 00040 nat s= 0, n= N(p); 00041 for (nat i=0; i<n; i++) 00042 if (p (i) > i) s += p (i) - i; 00043 return s; 00044 } 00045 00046 permutation 00047 operator * (const permutation& p1, const permutation& p2) { 00048 nat n= N(p1); 00049 ASSERT (N(p2) == n, "sizes do not match"); 00050 vector<nat> v= fill<nat> (n); 00051 for (nat i=0; i<n; i++) v[i]= p1 (p2 (i)); 00052 return permutation (v); 00053 } 00054 00055 permutation 00056 invert (const permutation& p) { 00057 nat n= N(p); 00058 vector<nat> v= fill<nat> (n); 00059 for (nat i=0; i<n; i++) v[p(i)]= i; 00060 return permutation (v); 00061 } 00062 00063 } // namespace mmx