-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpf.ijs
2298 lines (2237 loc) · 78.6 KB
/
pf.ijs
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
NB. Orthogonal factorization with pivoting
NB.
NB. gelpf LQ factorization with row pivoting of a general
NB. matrix
NB. geplf QL factorization with column pivoting of a
NB. general matrix
NB. geprf QR factorization with column pivoting of a
NB. general matrix
NB. gerpf RQ factorization with row pivoting of a general
NB. matrix
NB.
NB. testgepf Test gexxf by general matrix
NB. testpf Adv. to make verb to test gexxf by matrix of
NB. generator and shape given
NB.
NB. Copyright 2010,2011,2013,2017,2018,2020,2021,2023,2024,
NB. 2025 Igor Zhuravlov
NB.
NB. This file is part of mt
NB.
NB. mt is free software: you can redistribute it and/or
NB. modify it under the terms of the GNU Lesser General
NB. Public License as published by the Free Software
NB. Foundation, either version 3 of the License, or (at your
NB. option) any later version.
NB.
NB. mt is distributed in the hope that it will be useful, but
NB. WITHOUT ANY WARRANTY; without even the implied warranty
NB. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
NB. See the GNU Lesser General Public License for more
NB. details.
NB.
NB. You should have received a copy of the GNU Lesser General
NB. Public License along with mt. If not, see
NB. <http://www.gnu.org/licenses/>.
NB. =========================================================
NB. Concepts
NB.
NB. TODO:
NB. - geplf: Q * L * P = A
NB. - gerpf: P * R * Q = A
NB.
NB. References:
NB. [1] C. H. Bischof, G. Quintana-Ortí. Computing Rank-
NB. Revealing QR Factorizations of Dense Matrices. ACM
NB. Transactions on Mathematical Software, June 1998,
NB. Vol. 24, No. 2, pp. 226-253.
NB. [2] C. H. Bischof, G. Quintana-Ortí. Algorithm 782: Codes
NB. for Rank-Revealing QR Factorizations of Dense
NB. Matrices. ACM Transactions on Mathematical Software,
NB. June 1998, Vol. 24, No. 2, pp. 254-257.
NB. =========================================================
NB. Configuration
coclass 'mt'
NB. =========================================================
NB. Local definitions
NB. ---------------------------------------------------------
NB. Constants
PFFP=: 0.9
PFSF=: 100 NB. Safe Factor, a security factor to avoid
NB. arithmetic exceptions
NB. ---------------------------------------------------------
NB. lauc1f
NB.
NB. Description:
NB. Apply incremental condition estimation to determine
NB. whether the k-th column of A, stored in vector w, would
NB. be acceptable as a pivot column with respect to the
NB. threshold thresh
NB.
NB. Syntax:
NB. out=. lauc1f ismin;iv;w;gamma;thresh
NB. where
NB. ismin - an estimate for the smallest singular value of
NB. R(0:k-1,0:k-1)
NB. iv - k-vector, an approximate smallest left
NB. singular vector of R(0:k-1,0:k-1)
NB. w - k-vector of elements R(0:k-1,k)
NB. gamma - k-th diagonal element R(k,k)
NB. thresh - if osmin is smaller than thresh, the k-th
NB. column is rejected
NB. out - is either 0 if k-th column of R is rejected,
NB. or
NB. out -: osmin;ov
NB. if the k-th column of R is found acceptable
NB. osmin - an estimate for the smallest singular value of
NB. R(0:k,0:k)
NB. ov - (k+1)-vector, an approximate smallest left
NB. singular vector of R(0:k,0:k)
NB. R - m×n-matrix, the upper trapezoidal
NB.
NB. Notes:
NB. - models RRQR's xLAUC1 [1, 2]
lauc1f=: 3 : 0
'smin v w gamma thresh'=. y
if. thresh > | gamma do.
0
else.
'smin cs'=. laic12 smin ; gamma , w mp + v
if. thresh > smin do.
0
else.
smin ; v ((* {:) , 0 { ]) cs
end.
end.
)
NB. ---------------------------------------------------------
NB. gelpc
NB.
NB. Description:
NB. Continue a partial LQ factorization on
NB. A(offset:offset+dsrd-1,:). If A(0:offset-1,:) has been
NB. reduced to lower trapezoidal form, then gelpc applies
NB. the traditional row pivoting strategy to identify dsrd
NB. more independent rows of A with the restriction that
NB. the condition number of the leading lower triangle of A
NB. would not be larger than 1/rcond.
NB. If lacptd (≤ dsrd) such rows are found, then the
NB. condition number of lower triangle in
NB. A(0:offset+lacptd-1,0:offset+lacptd-1) is less than
NB. 1/rcond. If lacptd < dsrd, then the LQ factorization
NB. of A is completed, otherwise only dsrd new steps were
NB. performed.
NB.
NB. Syntax:
NB. 'dLQf oA10 oA11 op ov osvlues omxnm'=. gelpc iA10;iA11;dsrd;rcond;ip;iv;isvlues;imxnm
NB. where
NB. iA10 - (m-offset)×offset-matrix, left part of not yet
NB. factorized rows
NB. iA11 - (m-offset)×(n+1-offset)-matrix, right part of
NB. not yet factorized rows, augmented by
NB. trash vector
NB. dsrd > 0, number of independent rows to extract
NB. rcond > 0, 1/rcond specifies an upper bound of the
NB. condition number
NB. ip - (m-offset)-vector, rows inversed permutation
NB. of (iA10 ,. iA11)
NB. iv - offset-vector, an approximate smallest right
NB. singular vector of iL
NB. isvlues - 2-vector, estimates of the singular values:
NB. sigma_max(A) , sigma_min(iL)
NB. imxnm - ≥ 0, the norm of largest row in iL
NB. dLQf - lacptd×(n+1)-matrix, identified more
NB. independent rows of A,
NB. (offset+lacptd)×(n+1)-matrix (LQf , dLQf)
NB. contains oQ in factorized form and oL:
NB. oL -: trl }:"1 LQf , dLQf
NB. oQ -: unglq LQf , dLQf
NB. oA10 - (m-offset-lacptd)×(offset+lacptd)-matrix,
NB. left part of still not factorized rows
NB. oA11 - (m-offset-lacptd)×(n+1-offset-lacptd)-matrix,
NB. right part of still not factorized rows
NB. op - (m-offset)-vector, rows inversed
NB. permutation of (dLQf , oA10 ,. oA11)
NB. ov - (offset+lacptd)-vector, an approximate
NB. smallest right singular vector of oL
NB. osvlues - 4-vector, estimates of the singular values:
NB. sigma_max(A) , sigma_r(oL) , sigma_q(oL) , sigma_min(A)
NB. where
NB. r -: offset+lacptd
NB. q -: </ m , n , r+1
NB. omxnm - ≥ 0, the norm of largest row in oL
NB. LQf - offset×(n+1)-matrix, already factorized
NB. rows of A, contains iQ in factorized form
NB. and iL:
NB. iL -: trl }:"1 LQf
NB. iQ -: unglq LQf
NB. iL -:trl (2 # offset) {. LQf
NB. oL -:trl (2 # offset+lacptd) {. LQf , dLQf
NB. iQ - offset×(n+1)-matrix with orthonormal rows,
NB. which is defined as the product of (offset)
NB. elementary reflectors
NB. oQ - (offset+lacptd)×(n+1)-matrix with orthonormal
NB. rows, which is defined as the product of
NB. (offset+lacptd) elementary reflectors
NB. L - m×k-matrix, the lower trapezoidal
NB. A - m×n-matrix to factorize
NB. k = min(m,n)
NB.
NB. Storage layout:
NB. input: output:
NB. ( LQf LQf ) offset ( LQf LQf ) offset
NB. ( iA10 iA11 ) m-offset ( dLQf dLQf ) lacptd
NB. offset n+1-offset ( oA10 oA11 ) m-offset-lacptd
NB. offset+lacptd n+1-offset-lacptd
NB.
NB. Notes:
NB. - mxnm is updated only when offset = 0
gelpc=: 3 : 0
'A10 A11 dsrd rcond p v svlues mxnm'=. y
n=. <: n1=. A10 +&c A11
'm offset'=. (+/\.) $ A10
k=. m <. n
dLQf=. (0,n1)$0
if. offset do.
'smax smin'=. 0 1{svlues
end.
NB. Initialize partial row norms. The first item of
NB. rnorms store the exact row norms
rnorms=. ,:~ normsr }:"1 A11
NB. Compute factorization
i=. >: offset
lasti=. k <. offset + dsrd
while. i <: lasti do.
NB. Determine ith pivot row and swap if necessary
io=. liofmax {. rnorms
if. io do.
dip=. < 0 , io
A10=. dip C. A10
A11=. dip C. A11
rnorms=. dip C."1 rnorms
p=. ((# dLQf)&+&.:> dip) C. p
end.
rnorms=. (}."1) rnorms
w=. {. A10
NB. Generate elementary reflector H(i)
gamma=. {. z=. larfgfc {. A11
NB. Apply elementary reflector H(I) to the corresponding
NB. block of matrix A
y=. ((< (<0) ; 0)) { A11=. (1 (0)} z) larfrnfr A11
NB. Update partial row norms
if. i < lasti do.
'temp temp2'=. 2 (%/\) (| y) , rnorms
temp=. 0 >. 1 - *: temp
temp2=. >: 20 %~ temp * *: temp2
rnorms2=. }. normsr (}:"1) A11
rnorms3=. temp (((* %:)~ {.) 0} ]) rnorms
rnorms2=. (,:~ 1 = temp2)} rnorms3 ,: rnorms2
rnorms=. (,:~ 0 = {. rnorms)} rnorms2 ,: rnorms
end.
NB. Check new row for independence
if. 1 = i do.
mxnm=. smin=. smax=. | gamma
v=. 1
if. 0 = mxnm do.
svlues=. smin 2} svlues
A11=. z ((< 0 ; <<0))} A11
break.
end.
else.
smaxpr=. mxnm * 3 %: i
out=. lauc1f smin ; v ; w ; gamma ; smaxpr * rcond
if. 0 = L. out do.
NB. Row rejected
A11=. z 0} A11
break.
end.
NB. Row accepted
'smin v'=. out
smax=. smaxpr
end.
dLQf=. dLQf , w , z
A10=. (}. A10) ,. y
A11=. 1 1 }. A11
i=. >: i
end.
svlues=. (smax,smin) 0 1} svlues
if. dsrd = # dLQf do.
NB. DSRD independent rows have been found
svlues=. smin 2 3} svlues
else.
NB. All remaining rows rejected
if. (<./) $ A11 do.
NB. Factor remaining columns
A11=. gelqf (}:"1) A11 NB. FIXME TWICE excessive re-shaping
end.
eAsfx=. A10 ,. A11
NB. Use incremental condition estimation to get an
NB. estimate of the smallest singular value
j=. 0 = c A10
jsupr=. k - c A10
while. j < jsupr do.
'w gamma'=. (}: ; {:) (< j ; (i. >: # v)) { eAsfx
'smin cs'=. laic12 smin ; gamma , w mp + v
v=. v ((* {:) , 0 { ]) cs
if. 0 = j do.
svlues=. smin 2} svlues
end.
j=. >: j
end.
svlues=. smin 3} svlues
end.
dLQf ; A10 ; A11 ; p ; v ; svlues ; mxnm
)
NB. ---------------------------------------------------------
NB. geprc
NB.
NB. Description:
NB. Continue a partial QR factorization on
NB. A(:,offset:offset+dsrd-1). If A(:,0:offset-1) has been
NB. reduced to upper trapezoidal form, then geprc applies
NB. the traditional column pivoting strategy to identify
NB. dsrd more independent columns of A with the restriction
NB. that the condition number of the leading upper triangle
NB. of A would not be larger than 1/rcond.
NB. If lacptd (≤ dsrd) such columns are found, then the
NB. condition number of upper triangle in
NB. A(0:offset+lacptd-1,0:offset+lacptd-1) is less than
NB. 1/rcond. If lacptd < dsrd, then the QR factorization
NB. of A is completed, otherwise only dsrd new steps were
NB. performed.
NB.
NB. Syntax:
NB. 'dQfR oA01 oA11 op ov osvlues omxnm'=. geprc iA01;iA11;dsrd;rcond;ip;iv;isvlues;imxnm
NB. where
NB. iA01 - offset×(n-offset)-matrix, top part of not yet
NB. factorized columns
NB. iA11 - (m+1-offset)×(n-offset)-matrix, bottom part
NB. of not yet factorized columns, augmented by
NB. trash vector
NB. dsrd > 0, number of independent columns to extract
NB. rcond > 0, 1/rcond specifies an upper bound of the
NB. condition number
NB. ip - (n-offset)-vector, columns inversed
NB. permutation of (iA01 , iA11)
NB. iv - offset-vector, an approximate smallest left
NB. singular vector of iR
NB. isvlues - 2-vector, estimates of the singular values:
NB. sigma_max(A) , sigma_min(iR)
NB. imxnm - ≥ 0, the norm of largest column in iR
NB. dQfR - (m+1)×lacptd-matrix, identified more
NB. independent columns of A,
NB. (m+1)×(offset+lacptd)-matrix (QfR ,. dQfR)
NB. contains oQ in factorized form and oR:
NB. oR -: tru }: QfR ,. dQfR
NB. oQ -: ungqr QfR ,. dQfR
NB. oA01 - (offset+lacptd)×(n-offset-lacptd)-matrix, top
NB. part of still not factorized columns
NB. oA11 - (m+1-offset-lacptd)×(n-offset-lacptd)-matrix,
NB. bottom part of still not factorized columns
NB. op - (n-offset)-vector, columns inversed
NB. permutation of (dQfR ,. oA01 , oA11)
NB. ov - (offset+lacptd)-vector, an approximate
NB. smallest left singular vector of oR
NB. osvlues - 4-vector, estimates of the singular values:
NB. sigma_max(A) , sigma_r(oR) , sigma_q(oR) , sigma_min(A)
NB. where
NB. r -: offset+lacptd
NB. q -: </ m , n , r+1
NB. omxnm - ≥ 0, the norm of largest column in oR
NB. QfR - (m+1)×offset-matrix, already factorized
NB. columns of A, contains iQ in factorized form
NB. and iR:
NB. iR -: tru }: QfR
NB. iQ -: ungqr QfR
NB. iQ - (m+1)×offset-matrix with orthonormal columns,
NB. which is defined as the product of (offset)
NB. elementary reflectors
NB. oQ - (m+1)×(offset+lacptd)-matrix with orthonormal
NB. columns, which is defined as the product of
NB. (offset+lacptd) elementary reflectors
NB. iR -:tru (2 # offset) {. QfR
NB. oR -:tru (2 # offset+lacptd) {. QfR ,. dQfR
NB. R - k×n-matrix, the upper trapezoidal
NB. A - m×n-matrix to factorize
NB. k = min(m,n)
NB.
NB. Storage layout:
NB. input: output:
NB. ( QfR iA01 ) offset ( QfR dQfR oA01 ) offset+lacptd
NB. ( QfR iA11 ) m+1-offset ( QfR dQfR oA11 ) m+1-offset-lacptd
NB. offset n-offset offset lacptd n-offset-lacptd
NB.
NB. Notes:
NB. - models RRQR's xGEQPC [1, 2] with following differences:
NB. - Q returned is represented as the product of
NB. elementary reflectors, and is merged with R
NB. - mxnm is updated only when offset = 0
geprc=: 3 : 0
'A01 A11 dsrd rcond p v svlues mxnm'=. y
m=. <: m1=. A01 (+&#) A11
'offset n'=. (+/\) $ A01
k=. m <. n
dQfR=. (m1,0)$0
if. offset do.
'smax smin'=. 0 1{svlues
end.
NB. Initialize partial column norms. The first item of
NB. cnorms store the exact column norms
cnorms=. ,:~ normsc }: A11
NB. Compute factorization
i=. >: offset
lasti=. k <. offset + dsrd
while. i <: lasti do.
NB. Determine ith pivot column and swap if necessary
io=. liofmax {. cnorms
if. io do.
dip=. < 0 , io
A01=. dip (C."1) A01
A11=. dip (C."1) A11
cnorms=. dip (C."1) cnorms
p=. ((c dQfR)&+&.:> dip) C. p
end.
cnorms=. (}."1) cnorms
w=. ({."1) A01
NB. Generate elementary reflector H(i)
gamma=. {. z=. larfgf ({."1) A11
NB. Apply elementary reflector H(I) to the corresponding
NB. block of matrix A
y=. ((< 0 ; <<0)) { A11=. (1 (0)} z) larflcfc A11
NB. Update partial column norms
if. i < lasti do.
'temp temp2'=. 2 (%/\) (| y) , cnorms
temp=. 0 >. 1 - *: temp
temp2=. >: 20 %~ temp * *: temp2
cnorms2=. }. normsc }: A11
cnorms3=. temp (((* %:)~ {.) 0} ]) cnorms
cnorms2=. (,:~ 1 = temp2)} cnorms3 ,: cnorms2
cnorms=. (,:~ 0 = {. cnorms)} cnorms2 ,: cnorms
end.
NB. Check new column for independence
if. 1 = i do.
mxnm=. smin=. smax=. | gamma
v=. 1
if. 0 = mxnm do.
svlues=. smin 2} svlues
A11=. z ((< (<0) ; 0))} A11
break.
end.
else.
smaxpr=. mxnm * 3 %: i
out=. lauc1f smin ; v ; w ; gamma ; smaxpr * rcond
if. 0 = L. out do.
NB. Column rejected
A11=. z ((< a: ; 0))} A11
break.
end.
NB. Column accepted
'smin v'=. out
smax=. smaxpr
end.
dQfR=. dQfR ,. w , z
A01=. ((}."1) A01) , y
A11=. 1 1 }. A11
i=. >: i
end.
svlues=. (smax,smin) 0 1} svlues
if. dsrd = c dQfR do.
NB. DSRD independent columns have been found
svlues=. smin 2 3} svlues
else.
NB. All remaining columns rejected
if. (<./) $ A11 do.
NB. Factor remaining columns
A11=. geqrf }: A11 NB. FIXME TWICE excessive re-shaping
end.
eAsfx=. A01 , A11
NB. Use incremental condition estimation to get an
NB. estimate of the smallest singular value
j=. 0 = # A01
jsupr=. k - # A01
while. j < jsupr do.
'w gamma'=. (}: ; {:) (< (i. >: # v) ; j) { eAsfx
'smin cs'=. laic12 smin ; gamma , w mp + v
v=. v ((* {:) , 0 { ]) cs
if. 0 = j do.
svlues=. smin 2} svlues
end.
j=. >: j
end.
svlues=. smin 3} svlues
end.
dQfR ; A01 ; A11 ; p ; v ; svlues ; mxnm
)
NB. ---------------------------------------------------------
NB. gelpw
NB.
NB. Description:
NB. Apply one block step of the Householder LQ
NB. factorization algorithm with restricted pivoting. It is
NB. called by gelpb to factorize a window of the matrix.
NB. Let A be the partial LQ factorization of
NB. (offset+lwsize)×n-matrix C, i.e. we have computed an
NB. orthogonal matrix iQ and a permutation matrix iP such
NB. that
NB. iP * iL * iQ = C
NB. Then in addition let iv be an approximate smallest
NB. right singular vector of iL in the sense that
NB. sigma_min(iL) ~ twonorm(iL * iv) = smin
NB. and
NB. sigma_max(iL) ~ (offset ^ 1r3) * mxnm = smax
NB. with
NB. cond_no(iL) ~ smax/smin ≤ 1/rcond.
NB. Then gelpw tries to identify nb rows in (iA10,.iA11)
NB. such that
NB. cond_no(oL) < 1/rcond.
NB. On exit,
NB. oP * oL * oQ = C
NB. is again a partial LQ factorization of C, but last
NB. lacptd rows of oL have been reduced via a series of
NB. elementary reflectors. Further
NB. sigma_min(oL) ~ twonorm(oL * ov) = smin
NB. and
NB. sigma_max(oL) ~ ((offset+lacptd) ^ 1r3) * mxnm = smax
NB. with
NB. cond_no(oL) ~ smax/smin ≤ 1/rcond.
NB. In the ideal case, lacptd = nb, that is, we found nb
NB. independent rows in the window consisting of the
NB. first lwsize rows of A.
NB.
NB. Syntax:
NB. 'dLQf oA10 oA11 op osmin ov'=. gelpw iA10;iA11;nb;rcond;ip;mxnm;ismin;iv
NB. where
NB. iA10 - lwsize×offset-matrix, left part of not yet
NB. factorized part of eA
NB. iA11 - lwsize×(n+1-offset)-matrix, right part of not
NB. yet factorized part of eA
NB. nb > 0, the number of independent rows to identify.
NB. This equals the desired blocksize in gelpb
NB. rcond > 0, 1/rcond specifies an upper bound on the
NB. condition number
NB. ip - lwsize-vector, rows inversed permutation of
NB. A, represents permutation m×m-matrix
NB. mxnm ≥ 0, the norm of the largest rown in iL
NB. ismin ≥ 0, an estimate of smallest singular value of
NB. iL
NB. iv - offset-vector, an approximate right
NB. null-vector of iL
NB. dLQf - lacptd×(n+1)-matrix, factorized rows,
NB. (offset+lacptd)×(n+1)-matrix (LQf , dLQf)
NB. contains oQ in factorized form and oL:
NB. oL -: trl }:"1 LQf , dLQf
NB. oQ -: unglq LQf , dLQf
NB. oA10 - (lwsize-lacptd)×(offset+lacptd)-matrix, left
NB. part of not yet factorized or rejected rows
NB. oA11 - (lwsize-lacptd)×(n+1-offset-lacptd)-matrix,
NB. right part of not yet factorized or rejected
NB. rows, augmented by trash vector
NB. op - lwsize-vector, rows inversed permutation of
NB. A, represents permutation m×m-matrix
NB. osmin ≥ 0, an estimate of smallest singular value of
NB. oL
NB. ov - (offset+lacptd)-vector, an approximate right
NB. null-vector of oL
NB. LQf - offset×(n+1)-matrix, already factorized
NB. rows of A, contains iQ in factorized form
NB. and iL:
NB. iL -: trl }:"1 LQf
NB. iQ -: unglq LQf
NB. iL -:trl (2 # offset) {. LQf
NB. oL -:trl (2 # offset+lacptd) {. LQf , dLQf
NB. iQ - offset×(n+1)-matrix with orthonormal rows,
NB. which is defined as the product of (offset)
NB. elementary reflectors
NB. oQ - (offset+lacptd)×(n+1)-matrix with orthonormal
NB. rows, which is defined as the product of
NB. (offset+lacptd) elementary reflectors
NB. lacptd ≤ nb
NB. lwsize ≤ n-offset, the size of pivot window
NB. eA - m×(n+1)-matrix, being A augmented by zero
NB. vector:
NB. eA -: A ,. 0
NB.
NB. Storage layout:
NB. input: output:
NB. ( LQf LQf ) offset ( LQf LQf ) offset
NB. ( iA10 iA11 ) lwsize ( dLQf dLQf ) lacptd
NB. offset n+1-offset ( oA10 0A11 ) lwsize-lacptd
NB. offset+lacptd n+1-offset-lacptd
gelpw=: 3 : 0
'A10 A11 nb rcond p mxnm smin v'=. y
offset=. c A10
n=. <: n1=. offset + c A11
dLQf=. (0,n1)$0
NB. Initialize partial row norms (stored in the first item
NB. of rnorms) and exact row norms (stored in the second
NB. item of rnorms) for the first batch of rows
rnorms=. ,:~ normsr (}:"1) A11
NB. Main loop
lastk=. <: (n - offset) <. # p
while. nb > # dLQf do.
NB. Determine pivot candidate
io=. liofmax {. rnorms
if. io do.
dip=. < 0 , io
A10=. dip C. A10
A11=. dip C. A11
rnorms=. dip (C."1) rnorms
p=. ((# dLQf)&+&.:> dip) C. p
end.
NB. Determine (offset+lacptd)st diagonal element gamma
NB. of matrix A would elementary reflector be applied
gamma=. rnorms (negpos~ 9&o.) & (0&({,)) A11
NB. Update estimate for largest singular value
smax=. mxnm * 3 %: >: c A10
u=. ({."1) A11
w=. {. A10
NB. Is candidate pivot row acceptable ?
out=. lauc1f smin ; v ; w ; gamma ; smax * rcond
if. 0 = L. out do. break. end.
NB. Pivot candidate was accepted
'smin v'=. out
NB. Generate Householder vector
z=. larfgfc {. A11
dLQf=. dLQf , w , z
NB. Apply Householder reflection to A11
y=. ((< (<0) ; 0)) { A11=. (1 (0)} z) larfrnfr A11
A10=. (}. A10) ,. y
A11=. 1 1 }. A11
rnorms=. (}."1) rnorms
NB. Update partial column norms
if. lastk > # dLQf do.
'temp temp2'=. 2 (%/\) (| y) , rnorms
temp=. 0 >. 1 - *: temp
temp2=. >: 20 %~ temp * *: temp2
rnorms2=. }. normsr (}:"1) A11
rnorms3=. temp (((* %:)~ {.) 0} ]) rnorms
rnorms2=. (,:~ 1 = temp2)} rnorms3 ,: rnorms2
rnorms=. (,:~ 0 = {. rnorms)} rnorms2 ,: rnorms
end.
end.
NB. Reject all remaining rows in pivot window
dLQf ; A10 ; A11 ; p ; smin ; v
)
NB. ---------------------------------------------------------
NB. geprw
NB.
NB. Description:
NB. Apply one block step of the Householder QR
NB. factorization algorithm with restricted pivoting. It is
NB. called by geprb to factorize a window of the matrix.
NB. Let A be the partial QR factorization of
NB. m×(offset+lwsize)-matrix C, i.e. we have computed an
NB. orthogonal matrix iQ and a permutation matrix iP such
NB. that
NB. iQ * iR * iP = C
NB. Then in addition let iv be an approximate smallest left
NB. singular vector of iR in the sense that
NB. sigma_min(iR) ~ twonorm(iR' * iv) = smin
NB. and
NB. sigma_max(iR) ~ (offset ^ 1r3) * mxnm = smax
NB. with
NB. cond_no(iR) ~ smax/smin ≤ 1/rcond.
NB. Then geprw tries to identify nb columns in (iA01,iA11)
NB. such that
NB. cond_no(oR) < 1/rcond.
NB. On exit,
NB. oQ * oR * oP = C
NB. is again a partial QR factorization of C, but last
NB. lacptd columns of oR have been reduced via a series of
NB. elementary reflectors. Further
NB. sigma_min(oR) ~ twonorm(oR' * ov) = smin
NB. and
NB. sigma_max(oR) ~ ((offset+lacptd) ^ 1r3) * mxnm = smax
NB. with
NB. cond_no(oR) ~ smax/smin ≤ 1/rcond.
NB. In the ideal case, lacptd = nb, that is, we found nb
NB. independent columns in the window consisting of the
NB. first lwsize columns of A.
NB.
NB. Syntax:
NB. 'dQfR oA01 oA11 op osmin ov'=. geprw iA01;iA11;nb;rcond;ip;mxnm;ismin;iv
NB. where
NB. iA01 - offset×lwsize-matrix, top part of not yet
NB. factorized part of eA
NB. iA11 - (m+1-offset)×lwsize-matrix, bottom part of not
NB. yet factorized part of eA
NB. nb > 0, the number of independent columns to
NB. identify. This equals the desired blocksize in
NB. geprb
NB. rcond > 0, 1/rcond specifies an upper bound on the
NB. condition number
NB. ip - lwsize-vector, columns inversed permutation of
NB. A, represents permutation n×n-matrix
NB. mxnm ≥ 0, the norm of the largest column in iR
NB. ismin ≥ 0, an estimate of smallest singular value of
NB. iR
NB. iv - offset-vector, an approximate left null-vector
NB. of iR
NB. dQfR - (m+1)×lacptd-matrix, factorized columns,
NB. (m+1)×(offset+lacptd)-matrix (QfR ,. dQfR)
NB. contains oQ in factorized form and oR:
NB. oR -: tru }: QfR ,. dQfR
NB. oQ -: ungqr QfR ,. dQfR
NB. oA01 - (offset+lacptd)×(lwsize-lacptd)-matrix, top
NB. part of not yet factorized or rejected columns
NB. oA11 - (m+1-offset-lacptd)×(lwsize-lacptd)-matrix,
NB. bottom part of not yet factorized or rejected
NB. columns, augmented by trash vector
NB. op - lwsize-vector, columns inversed permutation of
NB. A, represents permutation n×n-matrix
NB. osmin ≥ 0, an estimate of smallest singular value of
NB. oR
NB. ov - (offset+lacptd)-vector, an approximate left
NB. null-vector of oR
NB. QfR - (m+1)×offset-matrix, already factorized
NB. columns of A, contains iQ in factorized form
NB. and iR:
NB. iR -: tru }: QfR
NB. iQ -: ungqr QfR
NB. iQ - (m+1)×offset-matrix with orthonormal columns,
NB. which is defined as the product of (offset)
NB. elementary reflectors
NB. oQ - (m+1)×(offset+lacptd)-matrix with orthonormal
NB. columns, which is defined as the product of
NB. (offset+lacptd) elementary reflectors
NB. iR -:tru (2 # offset) {. QfR
NB. oR -:tru (2 # offset+lacptd) {. QfR ,. dQfR
NB. lacptd ≤ nb
NB. lwsize ≤ n-offset, the size of pivot window
NB. eA - (m+1)×n-matrix, being A augmented by zero
NB. vector:
NB. eA -: A , 0
NB.
NB. Storage layout:
NB. input: output:
NB. ( QfR iA01 ) offset ( QfR dQfR oA01 ) offset+lacptd
NB. ( QfR iA11 ) m+1-offset ( QfR dQfR oA11 ) m+1-offset-lacptd
NB. offset lwsize offset lacptd lwsize-lacptd
NB.
NB. Notes:
NB. - models RRQR's xGEQPW [1, 2]
geprw=: 3 : 0
'A01 A11 nb rcond p mxnm smin v'=. y
offset=. # A01
m=. <: m1=. offset + # A11
dQfR=. (m1,0)$0
NB. Initialize partial column norms (stored in the first
NB. item of cnorms) and exact column norms (stored in the
NB. second item of cnorms) for the first batch of columns
cnorms=. ,:~ normsc }: A11
NB. Main loop
lastk=. <: (m - offset) <. # p
while. nb > c dQfR do.
NB. Determine pivot candidate
io=. liofmax {. cnorms
if. io do.
dip=. < 0 , io
A01=. dip (C."1) A01
A11=. dip (C."1) A11
cnorms=. dip (C."1) cnorms
p=. ((c dQfR)&+&.:> dip) C. p
end.
NB. Determine (offset+lacptd)st diagonal element gamma
NB. of matrix A would elementary reflector be applied
gamma=. cnorms (negpos~ 9&o.) & (0&({,)) A11
NB. Update estimate for largest singular value
smax=. mxnm * 3 %: >: # A01
u=. {. A11
w=. ({."1) A01
NB. Is candidate pivot column acceptable ?
out=. lauc1f smin ; v ; w ; gamma ; smax * rcond
if. 0 = L. out do. break. end.
NB. Pivot candidate was accepted
'smin v'=. out
NB. Generate Householder vector
z=. larfgf ({."1) A11
dQfR=. dQfR ,. w , z
NB. Apply Householder reflection to A11
y=. ((< 0 ; <<0)) { A11=. (1 (0)} z) larflcfc A11
A01=. ((}."1) A01) , y
A11=. 1 1 }. A11
cnorms=. (}."1) cnorms
NB. Update partial column norms
if. lastk > c dQfR do.
'temp temp2'=. 2 (%/\) (| y) , cnorms
temp=. 0 >. 1 - *: temp
temp2=. >: 20 %~ temp * *: temp2
cnorms2=. }. normsc }: A11
cnorms3=. temp (((* %:)~ {.) 0} ]) cnorms
cnorms2=. (,:~ 1 = temp2)} cnorms3 ,: cnorms2
cnorms=. (,:~ 0 = {. cnorms)} cnorms2 ,: cnorms
end.
end.
NB. Reject all remaining columns in pivot window
dQfR ; A01 ; A11 ; p ; smin ; v
)
NB. ---------------------------------------------------------
NB. gelpb
NB.
NB. Description:
NB. Compute a LQ factorization:
NB. P * ( L00 0 ) * Q = A
NB. ( L10 L11 )
NB. of matrix A. The permutation P is chosen with the goal
NB. to reveal the rank of A by a suitably dimensioned
NB. trailing submatrix L11 with norm(L11) being small.
NB.
NB. Syntax:
NB. 'ip L Qn orcond rank svlues'=. ircond gelpb A
NB. where
NB. A - m×n-matrix, the input to factorize
NB. ircond > 0, 1/ircond specifies an upper bound on the
NB. condition number of L00
NB. ip - m-vector, rows inversed permutation of A,
NB. represents permutation m×m-matrix P
NB. L - m×k-matrix, the lower trapezoidal
NB. Qn - n×n-matrix, the unitary (orthogonal), embeds
NB. Q:
NB. Q -: k {. Qn
NB. orcond > 0, 1/orcond is an estimate for the condition
NB. number of L00
NB. rank ≥ 0, an estimate for the numerical rank of A
NB. with respect to the threshold 1/ircond in the
NB. sense that
NB. rank = arg_max(cond_no(L(0:r-1,0:r-1))<1/ircond)
NB. This may be an underestimate of the rank if
NB. the leading rows were not well-conditioned
NB. svlues - 4-vector, estimates of the singular values,
NB. see gelpf
NB. Q - k×n-matrix with orthonormal rows, which is
NB. defined as the first k rows of the product of
NB. k elementary reflectors
NB. k = min(m,n)
NB.
NB. Storage layout:
NB. L = ( L00 0 ) rank
NB. ( L10 L11 ) m-rank
NB. rank n-rank
gelpb=: 4 : 0
k=. <./ 'm n'=. $ y
p=. i. m
if. 0 = k do.
p ; ((m,0)$0) ; ((2#n)$0) ; ((0 ; 0 ; 4$0))
return.
end.
y=. y ,. 0
NB. Move row with largest residual norm left front
'y A10 A11 p v svlues mxnm'=. gelpc ((m,0)$0);y;1;x;p;(((i.0);(4$_.);_.))
if. # y do.
if. 1 = k do.
p ; (trl (}:"1) y) ; (n unglq y) ; (%/ 1 0 { svlues) ; 1 ; svlues
return.
else.
smin=. 1 { svlues
end.
else.
p ; (trl (}:"1) A11) ; (n unglq A11) ; ((0 ; 0 ; 4$0))
return.
end.
NB. Factor remaining rows using blocked code with
NB. restricted pivoting strategy
nb=. QFNB <. k
NB. The size of the pivot window is chosen to be nb+nllity
nllity=. k <. 10 >. <. 0.5 0.05 mp nb , m
norej=. m
while. (# y) < k <. norej do.
NB. Invariant: the 1st row of A1x is the 1st row
NB. in currently considered block row
kb=. <./ nb , (k , norej) - # y
NB. The goal now is to find kb independent rows
NB. among the remaining norej not yet rejected rows
lwsize=. (norej - # y) <. kb + nllity
'A00 A10'=. lwsize ({. ; }.) A10
'A01 A11'=. lwsize ({. ; }.) A11
pwi=. (i. lwsize) + # y
'dLQf A00 A01 pw smin v'=. gelpw A00;A01;kb;x;(pwi{p);mxnm;smin;v
p=. pw pwi} p
if. # dLQf do.
NB. Accumulate Householder vectors in a block reflector
if. # A11 do.
NB. Apply block reflector to A11
A11=. ((# y) tru1 dLQf) larfbrnfr A11
end.
y=. y , dLQf
end.
NB. Move rejected rows to the end if there is space
if. kb > # dLQf do.
kklwsz=. y (+&#) A01
if. norej > kklwsz do.
'ppfx psfx'=. (# y) ({. ; }.) p
p=. ppfx , (# A01) |. psfx
A10=. (A10 ,. (# dLQf) ({."1) A11) , A00
A11=. ((# dLQf) (}."1) A11) , A01
norej=. norej - # A01
else.
A10=. A00 , A10 ,. (# dLQf) ({."1) A11
A11=. A01 , (# dLQf) (}."1) A11
break.
end.
else.
A10=. A00 , A10 ,. (# dLQf) ({."1) A11
A11=. A01 , (# dLQf) (}."1) A11
end.
end.
svlues=. (smin , mxnm * 3 %: # y) 1 0} svlues
if. k > # y do.
NB. Process rejected rows
'ppfx psfx'=. (# y) ({. ; }.) p
'eApfx A10 A11 psfx v svlues mxnm'=. gelpc A10;A11;(# A11);x;psfx;v;svlues;mxnm
p=. ppfx , psfx
y=. y , eApfx
else.
svlues=. smin 2 3} svlues
end.
rank=. # y
y=. y , A10 ,. A11
p ; (trl (}:"1) y) ; (n unglq y) ; (%/ 1 0 { svlues) ; rank ; svlues
)
NB. ---------------------------------------------------------
NB. geprb
NB.
NB. Description:
NB. Compute a QR factorization:
NB. Q * ( R00 R01 ) * P = A
NB. ( 0 R11 )
NB. of matrix A. The permutation P is chosen with the goal
NB. to reveal the rank of A by a suitably dimensioned
NB. trailing submatrix R11 with norm(R11) being small.
NB.
NB. Syntax:
NB. 'Qm R ip orcond rank svlues'=. ircond geprb A
NB. where
NB. A - m×n-matrix, the input to factorize
NB. ircond > 0, 1/ircond specifies an upper bound on the
NB. condition number of R00
NB. Qm - m×m-matrix, the unitary (orthogonal), embeds
NB. Q:
NB. Q -: k {."1 Qm
NB. R - k×n-matrix, the upper trapezoidal
NB. ip - n-vector, columns inversed permutation of A,
NB. represents permutation n×n-matrix P
NB. orcond > 0, 1/orcond is an estimate for the condition
NB. number of R00
NB. rank ≥ 0, an estimate for the numerical rank of A
NB. with respect to the threshold 1/ircond in the
NB. sense that
NB. rank = arg_max(cond_no(R(0:r-1,0:r-1))<1/ircond)
NB. This may be an underestimate of the rank if
NB. the leading columns were not well-conditioned
NB. svlues - 4-vector, estimates of the singular values,
NB. see geprf
NB. Q - m×k-matrix with orthonormal columns, which is
NB. defined as the first k columns of the product
NB. of k elementary reflectors
NB. k = min(m,n)
NB.
NB. Storage layout:
NB. R = ( R00 R01 ) rank
NB. ( 0 R11 ) m-rank
NB. rank n-rank
NB.
NB. Notes:
NB. - models RRQR's xGEQPB(3) [1, 2] with following
NB. differences:
NB. - matrix C is an identity matrix
NB. - rejected columns are moved to R11 not from the left
NB. side, but from the right
NB. - rejected columns are moved to R11 in non-reversed
NB. order
NB. - ircond get its default value in caller geprf
geprb=: 4 : 0
k=. <./ 'm n'=. $ y
p=. i. n
if. 0 = k do.
((2#m)$0) ; ((0,n)$0) ; p ; ((0 ; 0 ; 4$0))
return.
end.
y=. y , 0
NB. Move column with largest residual norm up front
'y A01 A11 p v svlues mxnm'=. geprc ((0,n)$0);y;1;x;p;(((i.0);(4$_.);_.))
if. c y do.
if. 1 = k do.
(m ungqr y) ; (tru }: y) ; p ; (%/ 1 0 { svlues) ; 1 ; svlues
return.
else.
smin=. 1 { svlues
end.
else.
(m ungqr A11) ; (tru }: A11) ; p ; ((0 ; 0 ; 4$0))
return.
end.
NB. Factor remaining columns using blocked code with
NB. restricted pivoting strategy
nb=. QFNB <. k
NB. The size of the pivot window is chosen to be nb+nllity
nllity=. k <. 10 >. <. 0.5 0.05 mp nb , n
norej=. n
while. (c y) < k <. norej do.
NB. Invariant: the 1st column of Ax1 is the 1st column
NB. in currently considered block column
kb=. <./ nb , (k , norej) - c y
NB. The goal now is to find kb independent columns
NB. among the remaining norej not yet rejected columns
lwsize=. (norej - c y) <. kb + nllity
'A00 A01'=. lwsize ({."1 ; }."1) A01
'A10 A11'=. lwsize ({."1 ; }."1) A11
pwi=. (i. lwsize) + c y
'dQfR A00 A10 pw smin v'=. geprw A00;A10;kb;x;(pwi{p);mxnm;smin;v