L-BFGS-B  3.0
Large-scale Bound-constrained Optimization
hpsolb.f
Go to the documentation of this file.
1 c> \file hpsolb.f
2 
3 c> This subroutine sorts out the least element of t, and puts the
4 c> remaining elements of t in a heap.
5 c>
6 c> @param n On entry n is the dimension of the arrays t and iorder.<br/>
7 c> On exit n is unchanged.
8 c>
9 c> @param t On entry t stores the elements to be sorted.<br/>
10 c> On exit t(n) stores the least elements of t, and t(1) to t(n-1)
11 c> stores the remaining elements in the form of a heap.
12 c>
13 c> @param iorder On entry iorder(i) is the index of t(i).<br/>
14 c> On exit iorder(i) is still the index of t(i), but iorder may be
15 c> permuted in accordance with t.
16 c>
17 c> @param iheap On entry iheap should be set as follows:<ul>
18 c> <li>iheap .eq. 0 if t(1) to t(n) is not in the form of a heap,</li>
19 c> <li>iheap .ne. 0 if otherwise.</li></ul>
20 c> On exit iheap is unchanged.
21  subroutine hpsolb(n, t, iorder, iheap)
22  integer iheap, n, iorder(n)
23  double precision t(n)
24 c
25 c References:
26 c Algorithm 232 of CACM (J. W. J. Williams): HEAPSORT.
27 c
28 c * * *
29 c
30 c NEOS, November 1994. (Latest revision June 1996.)
31 c Optimization Technology Center.
32 c Argonne National Laboratory and Northwestern University.
33 c Written by
34 c Ciyou Zhu
35 c in collaboration with R.H. Byrd, P. Lu-Chen and J. Nocedal.
36 c
37 c ************
38 
39  integer i,j,k,indxin,indxou
40  double precision ddum,out
41 
42  if (iheap .eq. 0) then
43 
44 c Rearrange the elements t(1) to t(n) to form a heap.
45 
46  do 20 k = 2, n
47  ddum = t(k)
48  indxin = iorder(k)
49 
50 c Add ddum to the heap.
51  i = k
52  10 continue
53  if (i.gt.1) then
54  j = i/2
55  if (ddum .lt. t(j)) then
56  t(i) = t(j)
57  iorder(i) = iorder(j)
58  i = j
59  goto 10
60  endif
61  endif
62  t(i) = ddum
63  iorder(i) = indxin
64  20 continue
65  endif
66 
67 c Assign to 'out' the value of t(1), the least member of the heap,
68 c and rearrange the remaining members to form a heap as
69 c elements 1 to n-1 of t.
70 
71  if (n .gt. 1) then
72  i = 1
73  out = t(1)
74  indxou = iorder(1)
75  ddum = t(n)
76  indxin = iorder(n)
77 
78 c Restore the heap
79  30 continue
80  j = i+i
81  if (j .le. n-1) then
82  if (t(j+1) .lt. t(j)) j = j+1
83  if (t(j) .lt. ddum ) then
84  t(i) = t(j)
85  iorder(i) = iorder(j)
86  i = j
87  goto 30
88  endif
89  endif
90  t(i) = ddum
91  iorder(i) = indxin
92 
93 c Put the least member in t(n).
94 
95  t(n) = out
96  iorder(n) = indxou
97  endif
98 
99  return
100 
101  end
subroutine hpsolb(n, t, iorder, iheap)
This subroutine sorts out the least element of t, and puts the remaining elements of t in a heap.
Definition: hpsolb.f:22