do not use Lapack anymore
[profile/ivi/opencv.git] / 3rdparty / lapack / slasq3.c
1 /* slasq3.f -- translated by f2c (version 20061008).
2    You must link the resulting object file with libf2c:
3         on Microsoft Windows system, link with libf2c.lib;
4         on Linux or Unix systems, link with .../path/to/libf2c.a -lm
5         or, if you install libf2c.a in a standard place, with -lf2c -lm
6         -- in that order, at the end of the command line, as in
7                 cc *.o -lf2c -lm
8         Source for libf2c is in /netlib/f2c/libf2c.zip, e.g.,
9
10                 http://www.netlib.org/f2c/libf2c.zip
11 */
12
13 #include "clapack.h"
14
15
16 /* Subroutine */ int slasq3_(integer *i0, integer *n0, real *z__, integer *pp, 
17          real *dmin__, real *sigma, real *desig, real *qmax, integer *nfail, 
18         integer *iter, integer *ndiv, logical *ieee, integer *ttype, real *
19         dmin1, real *dmin2, real *dn, real *dn1, real *dn2, real *g, real *
20         tau)
21 {
22     /* System generated locals */
23     integer i__1;
24     real r__1, r__2;
25
26     /* Builtin functions */
27     double sqrt(doublereal);
28
29     /* Local variables */
30     real s, t;
31     integer j4, nn;
32     real eps, tol;
33     integer n0in, ipn4;
34     real tol2, temp;
35     extern /* Subroutine */ int slasq4_(integer *, integer *, real *, integer 
36             *, integer *, real *, real *, real *, real *, real *, real *, 
37             real *, integer *, real *), slasq5_(integer *, integer *, real *, 
38             integer *, real *, real *, real *, real *, real *, real *, real *, 
39              logical *), slasq6_(integer *, integer *, real *, integer *, 
40             real *, real *, real *, real *, real *, real *);
41     extern doublereal slamch_(char *);
42     extern logical sisnan_(real *);
43
44
45 /*  -- LAPACK routine (version 3.2)                                    -- */
46
47 /*  -- Contributed by Osni Marques of the Lawrence Berkeley National   -- */
48 /*  -- Laboratory and Beresford Parlett of the Univ. of California at  -- */
49 /*  -- Berkeley                                                        -- */
50 /*  -- November 2008                                                   -- */
51
52 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
53 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
54
55 /*     .. Scalar Arguments .. */
56 /*     .. */
57 /*     .. Array Arguments .. */
58 /*     .. */
59
60 /*  Purpose */
61 /*  ======= */
62
63 /*  SLASQ3 checks for deflation, computes a shift (TAU) and calls dqds. */
64 /*  In case of failure it changes shifts, and tries again until output */
65 /*  is positive. */
66
67 /*  Arguments */
68 /*  ========= */
69
70 /*  I0     (input) INTEGER */
71 /*         First index. */
72
73 /*  N0     (input) INTEGER */
74 /*         Last index. */
75
76 /*  Z      (input) REAL array, dimension ( 4*N ) */
77 /*         Z holds the qd array. */
78
79 /*  PP     (input/output) INTEGER */
80 /*         PP=0 for ping, PP=1 for pong. */
81 /*         PP=2 indicates that flipping was applied to the Z array */
82 /*         and that the initial tests for deflation should not be */
83 /*         performed. */
84
85 /*  DMIN   (output) REAL */
86 /*         Minimum value of d. */
87
88 /*  SIGMA  (output) REAL */
89 /*         Sum of shifts used in current segment. */
90
91 /*  DESIG  (input/output) REAL */
92 /*         Lower order part of SIGMA */
93
94 /*  QMAX   (input) REAL */
95 /*         Maximum value of q. */
96
97 /*  NFAIL  (output) INTEGER */
98 /*         Number of times shift was too big. */
99
100 /*  ITER   (output) INTEGER */
101 /*         Number of iterations. */
102
103 /*  NDIV   (output) INTEGER */
104 /*         Number of divisions. */
105
106 /*  IEEE   (input) LOGICAL */
107 /*         Flag for IEEE or non IEEE arithmetic (passed to SLASQ5). */
108
109 /*  TTYPE  (input/output) INTEGER */
110 /*         Shift type. */
111
112 /*  DMIN1, DMIN2, DN, DN1, DN2, G, TAU (input/output) REAL */
113 /*         These are passed as arguments in order to save their values */
114 /*         between calls to SLASQ3. */
115
116 /*  ===================================================================== */
117
118 /*     .. Parameters .. */
119 /*     .. */
120 /*     .. Local Scalars .. */
121 /*     .. */
122 /*     .. External Subroutines .. */
123 /*     .. */
124 /*     .. External Function .. */
125 /*     .. */
126 /*     .. Intrinsic Functions .. */
127 /*     .. */
128 /*     .. Executable Statements .. */
129
130     /* Parameter adjustments */
131     --z__;
132
133     /* Function Body */
134     n0in = *n0;
135     eps = slamch_("Precision");
136     tol = eps * 100.f;
137 /* Computing 2nd power */
138     r__1 = tol;
139     tol2 = r__1 * r__1;
140
141 /*     Check for deflation. */
142
143 L10:
144
145     if (*n0 < *i0) {
146         return 0;
147     }
148     if (*n0 == *i0) {
149         goto L20;
150     }
151     nn = (*n0 << 2) + *pp;
152     if (*n0 == *i0 + 1) {
153         goto L40;
154     }
155
156 /*     Check whether E(N0-1) is negligible, 1 eigenvalue. */
157
158     if (z__[nn - 5] > tol2 * (*sigma + z__[nn - 3]) && z__[nn - (*pp << 1) - 
159             4] > tol2 * z__[nn - 7]) {
160         goto L30;
161     }
162
163 L20:
164
165     z__[(*n0 << 2) - 3] = z__[(*n0 << 2) + *pp - 3] + *sigma;
166     --(*n0);
167     goto L10;
168
169 /*     Check  whether E(N0-2) is negligible, 2 eigenvalues. */
170
171 L30:
172
173     if (z__[nn - 9] > tol2 * *sigma && z__[nn - (*pp << 1) - 8] > tol2 * z__[
174             nn - 11]) {
175         goto L50;
176     }
177
178 L40:
179
180     if (z__[nn - 3] > z__[nn - 7]) {
181         s = z__[nn - 3];
182         z__[nn - 3] = z__[nn - 7];
183         z__[nn - 7] = s;
184     }
185     if (z__[nn - 5] > z__[nn - 3] * tol2) {
186         t = (z__[nn - 7] - z__[nn - 3] + z__[nn - 5]) * .5f;
187         s = z__[nn - 3] * (z__[nn - 5] / t);
188         if (s <= t) {
189             s = z__[nn - 3] * (z__[nn - 5] / (t * (sqrt(s / t + 1.f) + 1.f)));
190         } else {
191             s = z__[nn - 3] * (z__[nn - 5] / (t + sqrt(t) * sqrt(t + s)));
192         }
193         t = z__[nn - 7] + (s + z__[nn - 5]);
194         z__[nn - 3] *= z__[nn - 7] / t;
195         z__[nn - 7] = t;
196     }
197     z__[(*n0 << 2) - 7] = z__[nn - 7] + *sigma;
198     z__[(*n0 << 2) - 3] = z__[nn - 3] + *sigma;
199     *n0 += -2;
200     goto L10;
201
202 L50:
203     if (*pp == 2) {
204         *pp = 0;
205     }
206
207 /*     Reverse the qd-array, if warranted. */
208
209     if (*dmin__ <= 0.f || *n0 < n0in) {
210         if (z__[(*i0 << 2) + *pp - 3] * 1.5f < z__[(*n0 << 2) + *pp - 3]) {
211             ipn4 = *i0 + *n0 << 2;
212             i__1 = *i0 + *n0 - 1 << 1;
213             for (j4 = *i0 << 2; j4 <= i__1; j4 += 4) {
214                 temp = z__[j4 - 3];
215                 z__[j4 - 3] = z__[ipn4 - j4 - 3];
216                 z__[ipn4 - j4 - 3] = temp;
217                 temp = z__[j4 - 2];
218                 z__[j4 - 2] = z__[ipn4 - j4 - 2];
219                 z__[ipn4 - j4 - 2] = temp;
220                 temp = z__[j4 - 1];
221                 z__[j4 - 1] = z__[ipn4 - j4 - 5];
222                 z__[ipn4 - j4 - 5] = temp;
223                 temp = z__[j4];
224                 z__[j4] = z__[ipn4 - j4 - 4];
225                 z__[ipn4 - j4 - 4] = temp;
226 /* L60: */
227             }
228             if (*n0 - *i0 <= 4) {
229                 z__[(*n0 << 2) + *pp - 1] = z__[(*i0 << 2) + *pp - 1];
230                 z__[(*n0 << 2) - *pp] = z__[(*i0 << 2) - *pp];
231             }
232 /* Computing MIN */
233             r__1 = *dmin2, r__2 = z__[(*n0 << 2) + *pp - 1];
234             *dmin2 = dmin(r__1,r__2);
235 /* Computing MIN */
236             r__1 = z__[(*n0 << 2) + *pp - 1], r__2 = z__[(*i0 << 2) + *pp - 1]
237                     , r__1 = min(r__1,r__2), r__2 = z__[(*i0 << 2) + *pp + 3];
238             z__[(*n0 << 2) + *pp - 1] = dmin(r__1,r__2);
239 /* Computing MIN */
240             r__1 = z__[(*n0 << 2) - *pp], r__2 = z__[(*i0 << 2) - *pp], r__1 =
241                      min(r__1,r__2), r__2 = z__[(*i0 << 2) - *pp + 4];
242             z__[(*n0 << 2) - *pp] = dmin(r__1,r__2);
243 /* Computing MAX */
244             r__1 = *qmax, r__2 = z__[(*i0 << 2) + *pp - 3], r__1 = max(r__1,
245                     r__2), r__2 = z__[(*i0 << 2) + *pp + 1];
246             *qmax = dmax(r__1,r__2);
247             *dmin__ = -0.f;
248         }
249     }
250
251 /*     Choose a shift. */
252
253     slasq4_(i0, n0, &z__[1], pp, &n0in, dmin__, dmin1, dmin2, dn, dn1, dn2, 
254             tau, ttype, g);
255
256 /*     Call dqds until DMIN > 0. */
257
258 L70:
259
260     slasq5_(i0, n0, &z__[1], pp, tau, dmin__, dmin1, dmin2, dn, dn1, dn2, 
261             ieee);
262
263     *ndiv += *n0 - *i0 + 2;
264     ++(*iter);
265
266 /*     Check status. */
267
268     if (*dmin__ >= 0.f && *dmin1 > 0.f) {
269
270 /*        Success. */
271
272         goto L90;
273
274     } else if (*dmin__ < 0.f && *dmin1 > 0.f && z__[(*n0 - 1 << 2) - *pp] < 
275             tol * (*sigma + *dn1) && dabs(*dn) < tol * *sigma) {
276
277 /*        Convergence hidden by negative DN. */
278
279         z__[(*n0 - 1 << 2) - *pp + 2] = 0.f;
280         *dmin__ = 0.f;
281         goto L90;
282     } else if (*dmin__ < 0.f) {
283
284 /*        TAU too big. Select new TAU and try again. */
285
286         ++(*nfail);
287         if (*ttype < -22) {
288
289 /*           Failed twice. Play it safe. */
290
291             *tau = 0.f;
292         } else if (*dmin1 > 0.f) {
293
294 /*           Late failure. Gives excellent shift. */
295
296             *tau = (*tau + *dmin__) * (1.f - eps * 2.f);
297             *ttype += -11;
298         } else {
299
300 /*           Early failure. Divide by 4. */
301
302             *tau *= .25f;
303             *ttype += -12;
304         }
305         goto L70;
306     } else if (sisnan_(dmin__)) {
307
308 /*        NaN. */
309
310         if (*tau == 0.f) {
311             goto L80;
312         } else {
313             *tau = 0.f;
314             goto L70;
315         }
316     } else {
317
318 /*        Possible underflow. Play it safe. */
319
320         goto L80;
321     }
322
323 /*     Risk of underflow. */
324
325 L80:
326     slasq6_(i0, n0, &z__[1], pp, dmin__, dmin1, dmin2, dn, dn1, dn2);
327     *ndiv += *n0 - *i0 + 2;
328     ++(*iter);
329     *tau = 0.f;
330
331 L90:
332     if (*tau < *sigma) {
333         *desig += *tau;
334         t = *sigma + *desig;
335         *desig -= t - *sigma;
336     } else {
337         t = *sigma + *tau;
338         *desig = *sigma - (t - *tau) + *desig;
339     }
340     *sigma = t;
341
342     return 0;
343
344 /*     End of SLASQ3 */
345
346 } /* slasq3_ */