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 :: &
38
40 private
41 integer, public :: npermutations
42 integer :: nn, npairs
43 integer, allocatable, public :: allpermutations(:,:)
44 integer, allocatable, public :: permsign(:)
45 end type permutations_t
46
47contains
48
49 subroutine permutations_init (nn, this)
50 integer, intent(in) :: nn
51 type(permutations_t), intent(inout) :: this
52
53 integer :: i1, order, oldperm, iperm, newpos
54
55 push_sub(permutations_init)
56
57 this%nn = nn
58 this%npermutations = factorial(nn)
59 safe_allocate(this%allpermutations(1:max(1,nn),1:this%npermutations))
60 safe_allocate(this%permsign(1:this%npermutations))
61
62 this%allpermutations(:, :) = -999
63 do i1 = 1, nn
64 this%allpermutations(i1, 1) = i1
65 end do
66 this%permsign(1) = 1
67
68 iperm = 1
69 do order = 2, nn
70 do oldperm = 1, factorial(order-1)
71 do newpos = order-1, 1, -1
72 iperm = iperm + 1
73 this%allpermutations(1:newpos-1, iperm) = this%allpermutations(1:newpos-1, oldperm)
74 this%allpermutations(newpos, iperm) = order
75 this%allpermutations(newpos+1:order, iperm) = this%allpermutations(newpos:order-1, oldperm)
76 this%allpermutations(order+1:nn, iperm) = this%allpermutations(order+1:nn, oldperm)
77
78 this%permsign(iperm) = this%permsign(oldperm) * (-1)**(order-newpos)
79 end do
80 end do
81 end do
82
83 pop_sub(permutations_init)
84 end subroutine permutations_init
85
86 subroutine permutations_write (this)
87 type(permutations_t), intent(inout) :: this
88
89 integer :: iperm
90
91 push_sub(permutations_write)
92
93 do iperm = 1, this%npermutations
94 write (message(1), '(a,I7,a,I7,a,10I7)') 'permutation ', iperm, &
95 ' sign ', this%permsign(iperm), '= ', this%allpermutations(:,iperm)
96 call messages_info(1)
97 end do
98
99 pop_sub(permutations_write)
100 end subroutine permutations_write
101
102 subroutine permutations_copy (perm_in, perm_out)
103 type(permutations_t), intent(inout) :: perm_in, perm_out
104
105 push_sub(permutations_copy)
106
107 perm_out%nn = perm_in%nn
108 perm_out%npermutations = perm_in%npermutations
109 perm_out%npairs = perm_in%npairs
110
111 safe_allocate_source_a(perm_out%allpermutations,perm_in%allpermutations)
112 safe_allocate_source_a(perm_out%permsign,perm_in%permsign)
113
115 end subroutine permutations_copy
116
117 subroutine permutations_end (this)
118 type(permutations_t), intent(inout) :: this
119
120 push_sub(permutations_end)
121
122 safe_deallocate_a(this%allpermutations)
123 safe_deallocate_a(this%permsign)
124
125 pop_sub(permutations_end)
126 end subroutine permutations_end
127
128end module permutations_oct_m
129
130!! Local Variables:
131!! mode: f90
132!! coding: utf-8
133!! End:
This module is intended to contain "only mathematical" functions and procedures.
Definition: math.F90:115
recursive integer function, public factorial(n)
Definition: math.F90:285
subroutine, public messages_info(no_lines, iunit, verbose_limit, stress, all_nodes, namespace)
Definition: messages.F90:624
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
Definition: messages.F90:160
subroutine, public permutations_copy(perm_in, perm_out)
subroutine, public permutations_init(nn, this)
subroutine, public permutations_end(this)
subroutine, public permutations_write(this)