1 /* permutation/permutation.c
3 * Copyright (C) 2001, 2002 Nicolas Darnis
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Modified for GSL by Brian Gough.
21 * Use in-place algorithms, no need for workspace
22 * Use conventions for canonical form given in Knuth (opposite of Sedgewick)
26 #include <gsl/gsl_errno.h>
27 #include <gsl/gsl_permutation.h>
30 gsl_permutation_linear_to_canonical (gsl_permutation * q,
31 const gsl_permutation * p)
33 const size_t n = p->size;
37 const size_t *const pp = p->data;
38 size_t *const qq = q->data;
40 if (q->size != p->size)
42 GSL_ERROR ("size of q does not match size of p", GSL_EINVAL);
45 for (i = 0; i < n; i++)
60 /* Now have k == i, i.e the least in its cycle, and s == cycle length */
84 gsl_permutation_canonical_to_linear (gsl_permutation * p,
85 const gsl_permutation * q)
87 size_t i, k, kk, first;
88 const size_t n = p->size;
90 size_t *const pp = p->data;
91 const size_t *const qq = q->data;
93 if (q->size != p->size)
95 GSL_ERROR ("size of q does not match size of p", GSL_EINVAL);
98 for (i = 0; i < n; i++)
106 for (i = 1; i < n; i++)
130 gsl_permutation_inversions (const gsl_permutation * p)
134 const size_t size = p->size;
136 for (i = 0; i < size - 1; i++)
138 for (j = i + 1; j < size; j++)
140 if (p->data[i] > p->data[j])
151 gsl_permutation_linear_cycles (const gsl_permutation * p)
155 const size_t size = p->size;
157 for (i = 0; i < size; i++)
177 gsl_permutation_canonical_cycles (const gsl_permutation * p)
181 size_t min = p->data[0];
183 for (i = 0; i < p->size; i++)
185 if (p->data[i] < min)