File: matqr.f

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (423 lines) | stat: -rw-r--r-- 11,113 bytes parent folder | download
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
      subroutine matqr
C ================================== ( Inria    ) =============
C     evaluate functions involving qr decomposition (least squares)
C ====================================================================
c     Copyright INRIA
      include '../stack.h'
      integer iadr,sadr
C
      double precision t(1),tol,eps,sr,si,tt
      integer vol
C
C    fin      -2       -1       1       
C             a\a2     a/a2     qr     
C
      iadr(l) = l + l - 1
      sadr(l) = (l/2) + 1
C
      if (ddt .eq. 4) then
        write (buf(1:4),'(i4)') fin
        call basout(io,wte,' matqr '//buf(1:4))
      endif
C
      eps = stk(leps)
C
      il = iadr(lstk(top-rhs+1))
      if (istk(il) .ne. 1) then
         if(fin.eq.1) then
            call putfunnam('qr',top-rhs+1)
            fun=-1
            return
         else
            err = rhs
            call error(53)
            return
         endif
      endif
      m = istk(il+1)
      n = istk(il+2)
      it = istk(il+3)
      l = sadr(il+4)
C
      goto (14,10,99,40) fin+3
C
C     rectangular matrix right division, a/a2
C     call  left division for a2'\a
C
 10   continue
C on interverti l'ordre de a et a2
      l1 = lstk(top-1)
      l2 = lstk(top)
      l3 = lstk(top+1)
      ll = l1 + l3 - l2
      err = ll + l3 - l1 - lstk(bot)
      if (err .gt. 0) then
        call error(17)
        return
      endif
      call unsfdcopy(l3-l1,stk(l1),-1,stk(ll),-1)
      call unsfdcopy(l3-l2,stk(ll+l2-l1),1,stk(l1),1)
      lstk(top) = ll
      lstk(top+1) = ll + l2 - l1
C transposition  a2
      lw = lstk(top+1)
      il1 = iadr(lstk(top))
      m1 = istk(il1+1)
      n1 = istk(il1+2)
      it1 = istk(il1+3)
      mn1 = m1 * n1
      l1 = sadr(il1+4)
      if (mn1.eq.0 .or. istk(il1).eq.0) goto 11
      vol = mn1 * (it1+1)
      ll = lw
      err = ll + vol - lstk(bot)
      if (err .gt. 0) then
        call error(17)
        return
      endif
C
      istk(il1+1) = n1
      istk(il1+2) = m1
C
      call unsfdcopy(vol,stk(l1),1,stk(ll),1)
      call mtran(stk(ll),m1,stk(l1),n1,m1,n1)
      if (it1 .eq. 0) goto 11
      call mtran(stk(ll+mn1),m1,stk(l1+mn1),n1,m1,n1)
      call dscal(mn1,-1.0d+0,stk(l1+mn1),1)
C
C transposition a
 11   continue
      il1 = iadr(lstk(top-1))
      m1 = istk(il1+1)
      n1 = istk(il1+2)
      it1 = istk(il1+3)
      mn1 = m1 * n1
      l1 = sadr(il1+4)
      if (mn1.eq.0 .or. istk(il1).eq.0) goto 12
      vol = mn1 * (it1+1)
      ll = lw
      err = ll + vol - lstk(bot)
      if (err .gt. 0) then
        call error(17)
        return
      endif
C
      istk(il1+1) = n1
      istk(il1+2) = m1
C
      call unsfdcopy(vol,stk(l1),1,stk(ll),1)
      call mtran(stk(ll),m1,stk(l1),n1,m1,n1)
      if (it1 .eq. 0) goto 12
      call mtran(stk(ll+mn1),m1,stk(l1+mn1),n1,m1,n1)
      call dscal(mn1,-1.0d+0,stk(l1+mn1),1)
C
 12   top = top - 1
      il = iadr(lstk(top))
      m = istk(il+1)
      n = istk(il+2)
      it = istk(il+3)
      l = sadr(il+4)
      goto 15
C
C     rectangular matrix left division a backslash a2
C
 14   top = top - 1
 15   il2 = iadr(lstk(top+1))
      m2 = istk(il2+1)
      n2 = istk(il2+2)
      it2 = istk(il2+3)
      l2 = sadr(il2+4)
      if (m2*n2 .gt. 1) goto 16
C     scalar divided by a matrix
      m2 = m
      n2 = m
      err = l2 + m*m*(it2+1) - lstk(bot)
      if (err .gt. 0) then
        call error(17)
        return
      endif
      sr = stk(l2)
      if (it2 .eq. 1) si = stk(l2+1)
      call dset(m*m*(it2+1),0.0d+0,stk(l2),1)
      call dset(m,sr,stk(l2),m+1)
      if (it2 .eq. 1) call dset(m,si,stk(l2+m*m),m+1)
C
 16   if (m2 .ne. m) then
        call error(10-fin)
        return
      endif
      it1 = max(it,it2)
      nn2 = max(m,n) * n2
      l3 = l2 + nn2*(it1+1)
      l4 = l3 + n*(it+1)
      ilb = iadr(l4+n*(it+1))
      err = sadr(ilb+n) - lstk(bot)
      if (err .gt. 0) then
        call error(17)
        return
      endif
      if (m .ge. n) goto 23
C     on plonge a2 dans une matrice ayant des lignes de longueur n
      mn2 = m * n2
      ls = l2 + mn2*(it2+1)
      ll = l2 + nn2*(it2+1)
      mn = n2 * (it2+1)
      do 22 j = 1,mn
        ll = ll - n
        ls = ls - m
        call unsfdcopy(m,stk(ls),-1,stk(ll),-1)
 22   continue
C     factorisation qr
 23   do 24 j = 1,n
        istk(ilb+j-1) = 0
 24   continue
      if (it .eq. 0)
     &  call dqrdc(stk(l),m,m,n,stk(l4),istk(ilb),stk(l3),1)
      if (it .eq. 1)
     &  call wqrdc(stk(l),stk(l+m*n),m,m,n,stk(l4),stk(l4+n),istk(ilb),
     &             stk(l3),stk(l3+n),1)
C     determination du rang
      k = 0
      tt = abs(stk(l))
      if (it .eq. 1) tt = tt + abs(stk(l+m*n))
      tol = dble(max(m,n)) * eps * tt
      mn = min(m,n)
      do 25 j = 1,mn
        ls = l + j - 1 + (j-1)*m
        tt = abs(stk(ls))
        if (it .eq. 1) tt = tt + abs(stk(ls+m*n))
        if (tt .gt. tol) k = j
 25   continue
      if (k .lt. mn) then
        write (buf(1:17),'(i4,1pd13.4)') k, tol
        call basout(io,wte,
     &              '  deficient rank: rank ='//buf(1:4)//' - tol ='//
     &              buf(5:17))
      endif
      mn = max(m,n)
C     resolution
      if (it .eq. 1) goto 28
C a est reelle
      ls = l2
      do 27 j = 1,n2
        call dqrsl(stk(l),m,m,k,stk(l4),stk(ls),t,stk(ls),stk(ls),t,t,
     &             100,info)
        call dset(n-k,0.0d+0,stk(ls+k),1)
        if (it2 .eq. 0) goto 27
        call dqrsl(stk(l),m,m,k,stk(l4),stk(ls+nn2),t,stk(ls+nn2),
     &             stk(ls+nn2),t,t,100,info)
        call dset(n-k,0.0d+0,stk(ls+nn2+k),1)
 27   ls = ls + mn
      goto 30
 28   continue
C cas a complexe
      if (it2 .eq. 0) call dset(nn2,0.0d+0,stk(l2+nn2),1)
      do 29 j = 1,n2
        ls = l2 + (j-1)*mn
        call wqrsl(stk(l),stk(l+m*n),m,m,k,stk(l4),stk(l4+n),stk(ls),
     &             stk(ls+nn2),t,t,stk(ls),stk(ls+nn2),stk(ls),
     &             stk(ls+nn2),t,t,t,t,100,info)
        ll = ls + k
        call dset(n-k,0.0d+0,stk(ll),1)
        call dset(n-k,0.0d+0,stk(ll+nn2),1)
 29   continue
C     permutations
 30   continue
      do 31 j = 1,n
        istk(ilb+j-1) = -istk(ilb+j-1)
 31   continue
      do 35 j = 1,n
        if (istk(ilb+j-1) .gt. 0) goto 35
        k = -istk(ilb+j-1)
        istk(ilb+j-1) = k
 33     continue
        if (k .eq. j) goto 34
        ls = l2 + j - 1
        ll = l2 + k - 1
        call dswap(n2,stk(ls),mn,stk(ll),mn)
        if (it1 .eq. 1) call dswap(n2,stk(ls+nn2),mn,stk(ll+nn2),mn)
        istk(ilb+k-1) = -istk(ilb+k-1)
        k = istk(ilb+k-1)
        goto 33
 34     continue
 35   continue
      do 36 j = 1,n2
        ls = l2 + (j-1)*mn
        ll = l + (j-1)*n
        call unsfdcopy(n,stk(ls),1,stk(ll),1)
 36   continue
      if (it1 .eq. 0) goto 38
      do 37 j = 1,n2
        ls = l2 + (j-1)*mn + mn*n2
        ll = l + n*n2 + (j-1)*n
        call unsfdcopy(n,stk(ls),1,stk(ll),1)
 37   continue
 38   continue
      istk(il+1) = n
      istk(il+2) = n2
      istk(il+3) = it1
      lstk(top+1) = l + n*n2*(it1+1)
      rhs = 1
      if (fin .eq. -1) then
        il1 = iadr(lstk(top))
        m1 = istk(il1+1)
        n1 = istk(il1+2)
        it1 = istk(il1+3)
        mn1 = m1 * n1
        l1 = sadr(il1+4)
        if (mn1.eq.0 .or. istk(il1).eq.0) goto 99
        vol = mn1 * (it1+1)
        ll = lstk(top+1)
        err = ll + vol - lstk(bot)
        if (err .gt. 0) then
          call error(17)
          return
        endif
C
        istk(il1+1) = n1
        istk(il1+2) = m1
C
        call unsfdcopy(vol,stk(l1),1,stk(ll),1)
        call mtran(stk(ll),m1,stk(l1),n1,m1,n1)
        if (it1 .eq. 0) goto 99
        call mtran(stk(ll+mn1),m1,stk(l1+mn1),n1,m1,n1)
        call dscal(mn1,-1.0d+0,stk(l1+mn1),1)
      endif
      goto 99
C
C     qr
 40   if (top+lhs .ge. bot) then
        call error(18)
        return
      endif
      if (rhs .eq. 2) then
        il = iadr(lstk(top))
        tol = stk(sadr(il+4))
        top = top - 1
      endif
C
      if (fin.eq.1 .and. (lhs.lt.2.or.lhs.gt.4.or.
     $    rhs.eq.2.and.lhs.eq.3)) then
        call error(41)
        return
      endif
C
      mn = m * n
      mm = m * m
      job = 0
C     implantation des resultats et tableaux de travail
      ilq = iadr(lstk(top))
      lq = l
      lstk(top+1) = lq + mm*(it+1)
C
      top = top + 1
      ilr = iadr(lstk(top))
      lr = sadr(ilr+4)
      lstk(top+1) = lr + mn*(it+1)
C
      if (lhs .eq. 4) then
        top = top + 1
        ilrk = iadr(lstk(top))
        lrk = sadr(ilrk+4)
        lstk(top+1) = lrk + 1
      endif
C
      if (lhs .ge. 3) then
C     on calcule et on stocke e
        top = top + 1
        nn = n * n
        job = 1
        ile = iadr(lstk(top))
        le = sadr(ile+4)
        lstk(top+1) = le + nn
      endif
C
      laux = lstk(top+1)
      lw = laux + n*(it+1)
      ilb = iadr(lw+n*(it+1))
      err = sadr(ilb+n) - lstk(bot)
      if (err .gt. 0) then
        call error(17)
        return
      endif
C
C     calcul de la dcomposition qr
      call unsfdcopy(mn*(it+1),stk(l),-1,stk(lr),-1)
      do 43 j = 1,n
        istk(ilb+j-1) = 0
 43   continue
      if (it .eq. 0)
     &  call dqrdc(stk(lr),m,m,n,stk(laux),istk(ilb),stk(lw),job)
      if (it .eq. 1)
     &  call wqrdc(stk(lr),stk(lr+mn),m,m,n,stk(laux),stk(laux+n),
     &             istk(ilb),stk(lw),stk(lw+n),job)
C
C     affectation des resultats
C
C     affectation de q
      call dset(mm*(it+1),0.0d+0,stk(lq),1)
      call dset(m,1.0d+0,stk(lq),m+1)
      ll = lq
      do 44 j = 1,m
        if (it .eq. 0)
     &    call dqrsl(stk(lr),m,m,n,stk(laux),stk(ll),stk(ll),t,t,t,t,
     &               10000,info)
        if (it .eq. 1)
     &    call wqrsl(stk(lr),stk(lr+mn),m,m,n,stk(laux),stk(laux+n),
     &               stk(ll),stk(ll+mm),stk(ll),stk(ll+mm),t,t,t,t,t,t,
     &               t,t,10000,info)
        ll = ll + m
 44   continue
      istk(ilq+1) = m
      istk(ilq+2) = m
      m1 = min(m-1,n)
      ll = lr + 1
      do 51 j = 1,m1
        call dset(m-j,0.0d+0,stk(ll),1)
        if (it .eq. 1) call dset(m-j,0.0d+0,stk(ll+mn),1)
        ll = ll + m + 1
 51   continue
      istk(ilr) = 1
      istk(ilr+1) = m
      istk(ilr+2) = n
      istk(ilr+3) = it
C
      if (lhs .eq. 2) goto 99
      if (lhs .eq. 4) then
C     ############# calcul du rang 
        tt = abs(stk(lr))
        if (it .eq. 1) tt = tt + abs(stk(lr+mn))
        if(rhs.eq.1)  tol = dble(max(m,n)) * eps * tt
        k = 0
        ls = lr
        m1 = min(m,n)
        do 450 j = 1,m1
          tt = abs(stk(ls))
          if (it .eq. 1) tt = tt + abs(stk(ls+mn))
          if (tt .le. tol) goto 460
          k = j
          ls = ls + m + 1
 450    continue
 460    istk(ilrk) = 1
        istk(ilrk+1) = 1
        istk(ilrk+2) = 1
        istk(ilrk+3) = 0
        stk(lrk) = dble(k)
      endif
C     #############   affectation de e
      call dset(nn,0.0d+0,stk(le),1)
      ll = le - 1
      do 52 j = 1,n
         stk(ll+istk(ilb+j-1)) = 1.0d+0
         ll = ll + n
 52   continue
      istk(ile) = 1
      istk(ile+1) = n
      istk(ile+2) = n
      istk(ile+3) = 0

      goto 99
C     
 99   return
      end