Octopus
permutations.F90
Go to the documentation of this file.
1!! Copyright (C) 2009 M. J. Verstraete
2!!
3!! This program is free software; you can redistribute it and/or modify
4!! it under the terms of the GNU General Public License as published by
5!! the Free Software Foundation; either version 2, or (at your option)
6!! any later version.
7!!
8!! This program is distributed in the hope that it will be useful,
9!! but WITHOUT ANY WARRANTY; without even the implied warranty of
10!! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11!! GNU General Public License for more details.
12!!
13!! You should have received a copy of the GNU General Public License
14!! along with this program; if not, write to the Free Software
15!! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16!! 02110-1301, USA.
17!!
18
19#include "global.h"
20
22 use debug_oct_m
23 use global_oct_m
24 use math_oct_m
27
28 implicit none
29
30 private
31
32 public :: &
36
38 private
39 integer, public :: npermutations
40 integer :: nn, npairs
41 integer, allocatable, public :: allpermutations(:,:)
42 integer, allocatable, public :: permsign(:)
43 end type permutations_t
44
45contains
46
47 subroutine permutations_init (nn, this)
48 integer, intent(in) :: nn
49 type(permutations_t), intent(inout) :: this
50
51 integer :: i1, order, oldperm, iperm, newpos
52
53 push_sub(permutations_init)
54
55 this%nn = nn
56 this%npermutations = factorial(nn)
57 safe_allocate(this%allpermutations(1:max(1,nn),1:this%npermutations))
58 safe_allocate(this%permsign(1:this%npermutations))
59
60 this%allpermutations(:, :) = -999
61 do i1 = 1, nn
62 this%allpermutations(i1, 1) = i1
63 end do
64 this%permsign(1) = 1
65
66 iperm = 1
67 do order = 2, nn
68 do oldperm = 1, factorial(order-1)
69 do newpos = order-1, 1, -1
70 iperm = iperm + 1
71 this%allpermutations(1:newpos-1, iperm) = this%allpermutations(1:newpos-1, oldperm)
72 this%allpermutations(newpos, iperm) = order
73 this%allpermutations(newpos+1:order, iperm) = this%allpermutations(newpos:order-1, oldperm)
74 this%allpermutations(order+1:nn, iperm) = this%allpermutations(order+1:nn, oldperm)
75
76 this%permsign(iperm) = this%permsign(oldperm) * (-1)**(order-newpos)
77 end do
78 end do
79 end do
80
81 pop_sub(permutations_init)
82 end subroutine permutations_init
83
84 subroutine permutations_end (this)
85 type(permutations_t), intent(inout) :: this
86
87 push_sub(permutations_end)
88
89 safe_deallocate_a(this%allpermutations)
90 safe_deallocate_a(this%permsign)
91
92 pop_sub(permutations_end)
93 end subroutine permutations_end
94
95end module permutations_oct_m
96
97!! Local Variables:
98!! mode: f90
99!! coding: utf-8
100!! End:
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:117
recursive integer function, public factorial(n)
Definition: math.F90:289
subroutine, public permutations_init(nn, this)
subroutine, public permutations_end(this)