3 * Copyright (C) 2000, 2007 Brian Gough
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.
24 #include <gsl/gsl_permutation.h>
25 #include <gsl/gsl_permute_double.h>
26 #include <gsl/gsl_test.h>
27 #include <gsl/gsl_ieee_utils.h>
29 unsigned int p5[120][5] = {
30 {0, 1, 2, 3, 4}, {0, 1, 2, 4, 3}, {0, 1, 3, 2, 4}, {0, 1, 3, 4, 2},
31 {0, 1, 4, 2, 3}, {0, 1, 4, 3, 2}, {0, 2, 1, 3, 4}, {0, 2, 1, 4, 3},
32 {0, 2, 3, 1, 4}, {0, 2, 3, 4, 1}, {0, 2, 4, 1, 3}, {0, 2, 4, 3, 1},
33 {0, 3, 1, 2, 4}, {0, 3, 1, 4, 2}, {0, 3, 2, 1, 4}, {0, 3, 2, 4, 1},
34 {0, 3, 4, 1, 2}, {0, 3, 4, 2, 1}, {0, 4, 1, 2, 3}, {0, 4, 1, 3, 2},
35 {0, 4, 2, 1, 3}, {0, 4, 2, 3, 1}, {0, 4, 3, 1, 2}, {0, 4, 3, 2, 1},
36 {1, 0, 2, 3, 4}, {1, 0, 2, 4, 3}, {1, 0, 3, 2, 4}, {1, 0, 3, 4, 2},
37 {1, 0, 4, 2, 3}, {1, 0, 4, 3, 2}, {1, 2, 0, 3, 4}, {1, 2, 0, 4, 3},
38 {1, 2, 3, 0, 4}, {1, 2, 3, 4, 0}, {1, 2, 4, 0, 3}, {1, 2, 4, 3, 0},
39 {1, 3, 0, 2, 4}, {1, 3, 0, 4, 2},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, 6, 7, 7, 8, 6, 7,
122 7, 8, 8, 9, 7, 8, 8, 9, 9, 10
128 gsl_ieee_env_setup ();
131 int i = 0, j, status = 0;
133 gsl_permutation * p ;
135 p = gsl_permutation_alloc (5);
137 gsl_permutation_init (p);
141 for (j = 0; j < 5; j++)
143 status |= (p->data[j] != p5[i][j]);
148 while (gsl_permutation_next(p) == GSL_SUCCESS);
150 gsl_test(status, "gsl_permutation_next, 5-th order permutation, 120 steps");
156 for (j = 0; j < 5; j++)
158 status |= (p->data[j] != p5[i][j]);
161 while (gsl_permutation_prev(p) == GSL_SUCCESS);
163 gsl_test(status, "gsl_permutation_prev, 5-th order permutation, 120 steps");
165 gsl_permutation_free (p);
173 gsl_permutation * p1 = gsl_permutation_alloc (5);
174 gsl_permutation * p2 = gsl_permutation_alloc (5);
175 gsl_permutation * p = gsl_permutation_alloc (5);
177 double v[5] = { 100.0, 101.0, 102.0, 103.0, 104.0 } ;
179 gsl_permutation_init (p1);
183 gsl_permutation_init (p2);
189 /* Compute x= p1 p2 v */
190 memcpy (x, v, 5 * sizeof(double));
191 gsl_permute (p2->data, x, 1, 5);
192 gsl_permute (p1->data, x, 1, 5);
194 /* Compute P= p1 p2, y = P v */
195 gsl_permutation_mul (p, p1, p2);
196 memcpy (y, v, 5 * sizeof(double));
197 gsl_permute (p->data, y, 1, 5);
199 for (i = 0; i < 5; i++)
208 while (gsl_permutation_next(p2) == GSL_SUCCESS);
213 while (gsl_permutation_next(p1) == GSL_SUCCESS);
215 gsl_permutation_free (p1);
216 gsl_permutation_free (p2);
217 gsl_permutation_free (p);
219 gsl_test(status, "gsl_permutation_mul, all 5-th order combinations");
223 /* testing cycles representations */
225 int i = 0, j, status = 0;
227 gsl_permutation * p = gsl_permutation_alloc (5);
229 gsl_permutation * plin = gsl_permutation_alloc (5);
230 gsl_permutation * pcan = gsl_permutation_alloc (5);
232 gsl_permutation_init (p);
236 gsl_permutation_memcpy (plin, p);
238 for (j = 0; j < 5; j++)
243 gsl_permutation_linear_to_canonical (pcan, plin);
245 for (j = 0; j < 5; j++)
247 status |= (pcan->data[j] != c5[i][j]);
250 status |= (gsl_permutation_canonical_cycles (pcan) != cycles[i]);
252 status |= (gsl_permutation_linear_cycles (plin) != cycles[i]);
254 for (j = 0; j < 5; j++)
259 gsl_permutation_canonical_to_linear (plin, pcan);
261 for (j = 0; j < 5; j++)
263 status |= (plin->data[j] != p5[i][j]);
268 while (gsl_permutation_next(p) == GSL_SUCCESS);
270 gsl_permutation_free (p);
271 gsl_permutation_free (plin);
272 gsl_permutation_free (pcan);
274 gsl_test (status, "gsl_permutation canonical conversion, 5-th order permutation, 120 steps");
277 /* testing number of inversions */
279 int i = 0, status = 0;
281 gsl_permutation * p = gsl_permutation_alloc (5);
283 gsl_permutation_init (p);
287 status |= gsl_permutation_inversions (p) != inversions[i];
290 while (gsl_permutation_next(p) == GSL_SUCCESS);
292 gsl_permutation_free (p);
294 gsl_test (status, "gsl_permutation_inversions, 5-th order permutation, 120 steps");
298 exit (gsl_test_summary());