-
Notifications
You must be signed in to change notification settings - Fork 1
/
r82vec_permute.m
104 lines (87 loc) · 2.33 KB
/
r82vec_permute.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function a = r82vec_permute ( n, a, p )
%*****************************************************************************80
%
%% R82VEC_PERMUTE permutes an R82VEC in place.
%
% Discussion:
%
% This routine permutes an array of real "objects", but the same
% logic can be used to permute an array of objects of any arithmetic
% type, or an array of objects of any complexity. The only temporary
% storage required is enough to store a single object. The number
% of data movements made is N + the number of cycles of order 2 or more,
% which is never more than N + N/2.
%
% Example:
%
% Input:
%
% N = 5
% P = ( 2, 4, 5, 1, 3 )
% A = ( 1.0, 2.0, 3.0, 4.0, 5.0 )
% (11.0, 22.0, 33.0, 44.0, 55.0 )
%
% Output:
%
% A = ( 2.0, 4.0, 5.0, 1.0, 3.0 )
% ( 22.0, 44.0, 55.0, 11.0, 33.0 ).
%
% Licensing:
%
% This code is distributed under the GNU LGPL license.
%
%
% Parameters:
%
% Input, integer N, the number of objects.
%
% Input, real A(2,N), the array to be permuted.
%
% Input, integer P(N), the permutation. P(I) = J means
% that the I-th element of the output array should be the J-th
% element of the input array. P must be a legal permutation
% of the integers from 1 to N, otherwise the algorithm will
% fail catastrophically.
%
% Output, real A(2,N), the permuted array.
%
%
% Search for the next element of the permutation that has not been used.
%
for istart = 1 : n
if ( p(istart) < 0 )
continue
elseif ( p(istart) == istart )
p(istart) = - p(istart);
continue
else
a_temp(1:2) = a(1:2,istart);
iget = istart;
%
% Copy the new value into the vacated entry.
%
while ( true )
iput = iget;
iget = p(iget);
p(iput) = - p(iput);
if ( iget < 1 | n < iget )
fprintf ( 1, '\n' );
fprintf ( 1, 'R82VEC_PERMUTE - Fatal error!\n' );
fprintf ( 1, ' A permutation index is out of range.\n' );
fprintf ( 1, ' P(%d) = %d\n', iput, iget );
error ( 'R82VEC_PERMUTE - Fatal error!' );
end
if ( iget == istart )
a(1:2,iput) = a_temp(1:2)';
break
end
a(1:2,iput) = a(1:2,iget);
end
end
end
%
% Restore the signs of the entries.
%
p(1:n) = -p(1:n);
return
end