forked from scipy/scipy
/
_decomp_update.pyx.in
2350 lines (2036 loc) · 85.4 KB
/
_decomp_update.pyx.in
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
"""
Routines for updating QR decompositions
.. versionadded: 0.16.0
"""
#
# Copyright (C) 2014 Eric Moore
#
# A few references for Updating QR factorizations:
#
# 1, 2, and 3 cover updates to full decompositons (q is square) and 4 and 5
# cover updates to thin (economic) decompositions (r is square). Reference 3
# additionally covers updating complete orthogonal factorizations and cholesky
# decompositions (i.e. updating R alone).
#
# 1. Golub, G. H. & Van Loan, C. F. Matrix Computations, 3rd Ed.
# (Johns Hopkins University Press, 1996).
#
# 2. Hammarling, S. & Lucas, C. Updating the QR factorization and the least
# squares problem. 1-73 (The University of Manchester, 2008).
# at <http://eprints.ma.man.ac.uk/1192/>
#
# 3. Gill, P. E., Golub, G. H., Murray, W. & Saunders, M. A. Methods for
# modifying matrix factorizations. Math. Comp. 28, 505-535 (1974).
#
# 4. Daniel, J. W., Gragg, W. B., Kaufman, L. & Stewart, G. W.
# Reorthogonalization and stable algorithms for updating the Gram-Schmidt QR
# factorization. Math. Comput. 30, 772-795 (1976).
#
# 5. Reichel, L. & Gragg, W. B. Algorithm 686: FORTRAN Subroutines for
# Updating the QR Decomposition. ACM Trans. Math. Softw. 16, 3693-77 (1990).
#
__all__ = ['qr_delete', 'qr_insert', 'qr_update']
{{py:
TCODES = ['cnp.NPY_FLOAT', 'cnp.NPY_DOUBLE', 'cnp.NPY_CFLOAT', 'cnp.NPY_CDOUBLE']
CNAMES = ['float', 'double', 'float_complex', 'double_complex']
CONDS = ['if', 'elif', 'elif', 'else: #']
PREFIX = ['s', 'd', 'c', 'z']
}}
cimport cython
cimport libc.stdlib
cimport libc.limits
cimport libc.float
from libc.math cimport sqrt, fabs, hypot
from libc.string cimport memset
cimport numpy as cnp
cdef extern from "numpy/npy_math.h":
double NPY_SQRT1_2
from numpy.linalg import LinAlgError
# This is used in place of, e.g., cnp.NPY_C_CONTIGUOUS, to indicate that a C
# F or non contiguous array is acceptable.
DEF ARRAY_ANYORDER = 0
cdef int MEMORY_ERROR = libc.limits.INT_MAX
# These are commented out in the numpy support we cimported above.
# Here I have declared them as taking void* instead of PyArrayDescr
# and object. In this file, only NULL is passed to these parameters.
cdef extern from *:
cnp.ndarray PyArray_CheckFromAny(object, void*, int, int, int, void*)
cnp.ndarray PyArray_FromArray(cnp.ndarray, void*, int)
from . cimport cython_blas as blas_pointers
from . cimport cython_lapack as lapack_pointers
import numpy as np
#------------------------------------------------------------------------------
# These are a set of fused type wrappers around the BLAS and LAPACK calls used.
#------------------------------------------------------------------------------
ctypedef float complex float_complex
ctypedef double complex double_complex
ctypedef fused blas_t:
float
double
float_complex
double_complex
cdef inline blas_t* index2(blas_t* a, int* as, int i, int j) nogil:
return a + i*as[0] + j*as[1]
cdef inline blas_t* index1(blas_t* a, int* as, int i) nogil:
return a + i*as[0]
cdef inline blas_t* row(blas_t* a, int* as, int i) nogil:
return a + i*as[0]
cdef inline blas_t* col(blas_t* a, int* as, int j) nogil:
return a + j*as[1]
cdef inline void copy(int n, blas_t* x, int incx, blas_t* y, int incy) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}copy(&n, x, &incx, y, &incy)
{{endfor}}
cdef inline void swap(int n, blas_t* x, int incx, blas_t* y, int incy) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}swap(&n, x, &incx, y, &incy)
{{endfor}}
cdef inline void scal(int n, blas_t a, blas_t* x, int incx) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}scal(&n, &a, x, &incx)
{{endfor}}
cdef inline void axpy(int n, blas_t a, blas_t* x, int incx,
blas_t* y, int incy) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}axpy(&n, &a, x, &incx, y, &incy)
{{endfor}}
cdef inline blas_t nrm2(int n, blas_t* x, int incx) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, ['s', 'd', 'sc', 'dz'])}}
{{COND}} blas_t is {{CNAME}}:
return blas_pointers.{{C}}nrm2(&n, x, &incx)
{{endfor}}
cdef inline void lartg(blas_t* a, blas_t* b, blas_t* c, blas_t* s) nogil:
cdef blas_t g
if blas_t is float:
lapack_pointers.slartg(a, b, c, s, &g)
elif blas_t is double:
lapack_pointers.dlartg(a, b, c, s, &g)
elif blas_t is float_complex:
c[0] = 0. # init imag
lapack_pointers.clartg(a, b, <float*>c, s, &g)
else:
c[0] = 0. # init imag
lapack_pointers.zlartg(a, b, <double*>c, s, &g)
# make this function more like the BLAS drotg
a[0] = g
b[0] = 0
cdef inline void rot(int n, blas_t* x, int incx, blas_t* y, int incy,
blas_t c, blas_t s) nogil:
if blas_t is float:
blas_pointers.srot(&n, x, &incx, y, &incy, &c, &s)
elif blas_t is double:
blas_pointers.drot(&n, x, &incx, y, &incy, &c, &s)
elif blas_t is float_complex:
lapack_pointers.crot(&n, x, &incx, y, &incy, <float*>&c, &s)
else:
lapack_pointers.zrot(&n, x, &incx, y, &incy, <double*>&c, &s)
cdef inline void larfg(int n, blas_t* alpha, blas_t* x, int incx,
blas_t* tau) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
lapack_pointers.{{C}}larfg(&n, alpha, x, &incx, tau)
{{endfor}}
cdef inline void larf(char* side, int m, int n, blas_t* v, int incv, blas_t tau,
blas_t* c, int ldc, blas_t* work) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
lapack_pointers.{{C}}larf(side, &m, &n, v, &incv, &tau, c, &ldc, work)
{{endfor}}
cdef inline void ger(int m, int n, blas_t alpha, blas_t* x, int incx, blas_t* y,
int incy, blas_t* a, int lda) nogil:
if blas_t is float:
blas_pointers.sger(&m, &n, &alpha, x, &incx, y, &incy, a, &lda)
elif blas_t is double:
blas_pointers.dger(&m, &n, &alpha, x, &incx, y, &incy, a, &lda)
elif blas_t is float_complex:
blas_pointers.cgeru(&m, &n, &alpha, x, &incx, y, &incy, a, &lda)
else:
blas_pointers.zgeru(&m, &n, &alpha, x, &incx, y, &incy, a, &lda)
cdef inline void gemv(char* trans, int m, int n, blas_t alpha, blas_t* a,
int lda, blas_t* x, int incx, blas_t beta, blas_t* y, int incy) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}gemv(trans, &m, &n, &alpha, a, &lda, x, &incx,
&beta, y, &incy)
{{endfor}}
cdef inline void gemm(char* transa, char* transb, int m, int n, int k,
blas_t alpha, blas_t* a, int lda, blas_t* b, int ldb, blas_t beta,
blas_t* c, int ldc) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}gemm(transa, transb, &m, &n, &k, &alpha, a, &lda,
b, &ldb, &beta, c, &ldc)
{{endfor}}
cdef inline void trmm(char* side, char* uplo, char* transa, char* diag, int m,
int n, blas_t alpha, blas_t* a, int lda, blas_t* b, int ldb) nogil:
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
blas_pointers.{{C}}trmm(side, uplo, transa, diag, &m, &n, &alpha, a, &lda,
b, &ldb)
{{endfor}}
cdef inline int geqrf(int m, int n, blas_t* a, int lda, blas_t* tau,
blas_t* work, int lwork) nogil:
cdef int info
{{for COND, CNAME, C in zip(CONDS, CNAMES, PREFIX)}}
{{COND}} blas_t is {{CNAME}}:
lapack_pointers.{{C}}geqrf(&m, &n, a, &lda, tau, work, &lwork, &info)
{{endfor}}
return info
cdef inline int ormqr(char* side, char* trans, int m, int n, int k, blas_t* a,
int lda, blas_t* tau, blas_t* c, int ldc, blas_t* work, int lwork) nogil:
cdef int info = 0
if blas_t is float:
lapack_pointers.sormqr(side, trans, &m, &n, &k, a, &lda, tau, c, &ldc,
work, &lwork, &info)
elif blas_t is double:
lapack_pointers.dormqr(side, trans, &m, &n, &k, a, &lda, tau, c, &ldc,
work, &lwork, &info)
elif blas_t is float_complex:
lapack_pointers.cunmqr(side, trans, &m, &n, &k, a, &lda, tau, c, &ldc,
work, &lwork, &info)
else:
lapack_pointers.zunmqr(side, trans, &m, &n, &k, a, &lda, tau, c, &ldc,
work, &lwork, &info)
return info
#------------------------------------------------------------------------------
# Utility routines
#------------------------------------------------------------------------------
cdef void blas_t_conj(int n, blas_t* x, int* xs) nogil:
cdef int j
if blas_t is float_complex or blas_t is double_complex:
for j in range(n):
index1(x, xs, j)[0] = index1(x, xs, j)[0].conjugate()
cdef void blas_t_2d_conj(int m, int n, blas_t* x, int* xs) nogil:
cdef int i, j
if blas_t is float_complex or blas_t is double_complex:
for i in range(m):
for j in range(n):
index2(x, xs, i, j)[0] = index2(x, xs, i, j)[0].conjugate()
cdef blas_t blas_t_sqrt(blas_t x) nogil:
if blas_t is float:
return sqrt(x)
elif blas_t is double:
return sqrt(x)
elif blas_t is float_complex:
return <float_complex>sqrt(<double>((<float*>&x)[0]))
else:
return sqrt((<double*>&x)[0])
cdef bint blas_t_less_than(blas_t x, blas_t y) nogil:
if blas_t is float or blas_t is double:
return x < y
else:
return x.real < y.real
cdef bint blas_t_less_equal(blas_t x, blas_t y) nogil:
if blas_t is float or blas_t is double:
return x <= y
else:
return x.real <= y.real
cdef int to_lwork(blas_t a, blas_t b) nogil:
cdef int ai, bi
if blas_t is float or blas_t is double:
ai = <int>a
bi = <int>b
elif blas_t is float_complex:
ai = <int>((<float*>&a)[0])
bi = <int>((<float*>&b)[0])
elif blas_t is double_complex:
ai = <int>((<double*>&a)[0])
bi = <int>((<double*>&b)[0])
return max(ai, bi)
#------------------------------------------------------------------------------
# QR update routines start here.
#------------------------------------------------------------------------------
cdef bint reorthx(int m, int n, blas_t* q, int* qs, bint qisF, int j, blas_t* u, blas_t* s) nogil:
# U should be all zeros on entry., and m > 1
cdef blas_t unorm, snorm, wnorm, wpnorm, sigma_max, sigma_min, rc
cdef char* T = 'T'
cdef char* N = 'N'
cdef char* C = 'C'
cdef int ss = 1
cdef blas_t inv_root2 = NPY_SQRT1_2
# u starts out as the jth basis vector.
u[j] = 1
# s = Q.T.dot(u) = jth row of Q.
copy(n, row(q, qs, j), qs[1], s, 1)
blas_t_conj(n, s, &ss)
# make u be the part of u that is not in span(q)
# i.e. u -= q.dot(s)
if qisF:
gemv(N, m, n, -1, q, qs[1], s, 1, 1, u, 1)
else:
gemv(T, n, m, -1, q, n, s, 1, 1, u, 1)
wnorm = nrm2(m, u, 1)
if blas_t_less_than(inv_root2, wnorm):
with cython.cdivision(True):
scal(m, 1/wnorm, u, 1)
s[n] = wnorm
return True
# if the above check failed, try one reorthogonalization
if qisF:
if blas_t is float or blas_t is double:
gemv(T, m, n, 1, q, qs[1], u, 1, 0, s+n, 1)
else:
gemv(C, m, n, 1, q, qs[1], u, 1, 0, s+n, 1)
gemv(N, m, n, -1, q, qs[1], s+n, 1, 1, u, 1)
else:
if blas_t is float or blas_t is double:
gemv(N, n, m, 1, q, n, u, 1, 0, s+n, 1)
else:
blas_t_conj(m, u, &ss)
gemv(N, n, m, 1, q, n, u, 1, 0, s+n, 1)
blas_t_conj(m, u, &ss)
blas_t_conj(n, s+n, &ss)
gemv(T, n, m, -1, q, n, s+n, 1, 1, u, 1)
wpnorm = nrm2(m, u, 1)
if blas_t_less_than(wpnorm, wnorm*inv_root2): # u lies in span(q)
scal(m, 0, u, 1)
axpy(n, 1, s, 1, s+n, 1)
s[n] = 0
return False
scal(m, 1/wpnorm, u, 1)
axpy(n, 1, s, 1, s+n, 1)
s[n] = wpnorm
return True
cdef int thin_qr_row_delete(int m, int n, blas_t* q, int* qs, bint qisF,
blas_t* r, int* rs, int k, int p_eco,
int p_full) nogil:
cdef int i, j, argmin_row_norm
cdef size_t usize = (m + 3*n + 1) * sizeof(blas_t)
cdef blas_t* s
cdef blas_t* u
cdef blas_t* s1
cdef int us[2]
cdef int ss[2]
cdef blas_t c, sn, min_row_norm, row_norm
u = <blas_t*>libc.stdlib.malloc(usize)
if not u:
return MEMORY_ERROR
s = u + m
ss[0] = 1
ss[1] = 0
us[0] = 1
us[1] = 0
for i in range(p_eco):
memset(u, 0, usize)
# permute q such that row k is the last row.
if k != m-1:
for j in range(k, m-1):
swap(n, row(q, qs, j), qs[1], row(q, qs, j+1), qs[1])
if not reorthx(m, n, q, qs, qisF, m-1, u, s):
# if we get here it means that this basis vector lies in span(q).
# we want to use s[:n+1] but we need a vector into null(q)
# find the row of q with the smallest norm and try that. (Daniel, p785)
min_row_norm = nrm2(n, row(q, qs, 0), qs[1])
argmin_row_norm = 0
for j in range(1, m):
row_norm = nrm2(n, row(q, qs, j), qs[1])
if blas_t_less_than(row_norm, min_row_norm):
min_row_norm = row_norm
argmin_row_norm = j
memset(u, 0, m*sizeof(blas_t))
if not reorthx(m, n, q, qs, qisF, argmin_row_norm, u, s):
# failed, quit.
libc.stdlib.free(u)
return 0
s[n] = 0
memset(s+2*n, 0, n*sizeof(blas_t))
# what happens here...
for j in range(n-1, -1, -1):
lartg(index1(s, ss, n), index1(s, ss, j), &c, &sn)
rot(n-j, index1(s+2*n, ss, j), ss[0], index2(r, rs,j, j), rs[1], c, sn)
rot(m-1, u, us[0], col(q, qs, j), qs[0], c, sn.conjugate())
m -= 1
libc.stdlib.free(u)
if p_full:
qr_block_row_delete(m, n, q, qs, r, rs, k, p_full)
return 1
cdef void qr_block_row_delete(int m, int n, blas_t* q, int* qs,
blas_t* r, int* rs, int k, int p) nogil:
cdef int i, j
cdef blas_t c,s
cdef blas_t* W
cdef int* ws
if k != 0:
for j in range(k, 0, -1):
swap(m, row(q, qs, j+p-1), qs[1], row(q, qs, j-1), qs[1])
# W is the block of rows to be removed from q, has shape, (p,m)
W = q
ws = qs
for j in range(p):
blas_t_conj(m, row(W, ws, j), &ws[1])
for i in range(p):
for j in range(m-2, i-1, -1):
lartg(index2(W, ws, i, j), index2(W, ws, i, j+1), &c, &s)
# update W
if i+1 < p:
rot(p-i-1, index2(W, ws, i+1, j), ws[0],
index2(W, ws, i+1, j+1), ws[0], c, s)
# update r if there is a nonzero row.
if j-i < n:
rot(n-j+i, index2(r, rs, j, j-i), rs[1],
index2(r, rs, j+1, j-i), rs[1], c, s)
# update q
rot(m-p, index2(q, qs, p, j), qs[0], index2(q, qs, p, j+1), qs[0],
c, s.conjugate())
cdef void qr_col_delete(int m, int o, int n, blas_t* q, int* qs, blas_t* r,
int* rs, int k) nogil:
"""
Here we support both full and economic decomposition, q is (m,o), and r
is (o, n).
"""
cdef int j
cdef int limit = min(o, n)
for j in range(k, n-1):
copy(limit, col(r, rs, j+1), rs[0], col(r, rs, j), rs[0])
hessenberg_qr(m, n-1, q, qs, r, rs, k)
cdef int qr_block_col_delete(int m, int o, int n, blas_t* q, int* qs,
blas_t* r, int* rs, int k, int p) nogil:
"""
Here we support both full and economic decomposition, q is (m,o), and r
is (o, n).
"""
cdef int j
cdef int limit = min(o, n)
cdef blas_t* work
cdef int worksize = max(m, n)
work = <blas_t*>libc.stdlib.malloc(worksize*sizeof(blas_t))
if not work:
return MEMORY_ERROR
# move the columns to removed to the end
for j in range(k, n-p):
copy(limit, col(r, rs, j+p), rs[0], col(r, rs, j), rs[0])
p_subdiag_qr(m, o, n-p, q, qs, r, rs, k, p, work)
libc.stdlib.free(work)
return 0
cdef void thin_qr_row_insert(int m, int n, blas_t* q, int* qs, blas_t* r,
int* rs, blas_t* u, int* us, int k) nogil:
cdef int j
cdef blas_t c, s
for j in range(n):
lartg(index2(r, rs, j, j), index1(u, us, j), &c, &s)
if j+1 < n:
rot(n-j-1, index2(r, rs, j, j+1), rs[1], index1(u, us, j+1), us[0],
c, s)
rot(m, col(q, qs, j), qs[0], col(q, qs, n), qs[0], c, s.conjugate())
# permute q
for j in range(m-1, k, -1):
swap(n, row(q, qs, j), qs[1], row(q, qs, j-1), qs[1])
cdef void qr_row_insert(int m, int n, blas_t* q, int* qs, blas_t* r, int* rs,
int k) nogil:
cdef int j
cdef blas_t c, s
cdef int limit = min(m-1, n)
for j in range(limit):
lartg(index2(r, rs, j, j), index2(r, rs, m-1, j), &c, &s)
rot(n-j-1, index2(r, rs, j, j+1), rs[1], index2(r, rs, m-1, j+1), rs[1],
c, s)
rot(m, col(q, qs, j), qs[0], col(q, qs, m-1), qs[0], c, s.conjugate())
# permute q
for j in range(m-1, k, -1):
swap(m, row(q, qs, j), qs[1], row(q, qs, j-1), qs[1])
cdef int thin_qr_block_row_insert(int m, int n, blas_t* q, int* qs, blas_t* r,
int* rs, blas_t* u, int* us, int k,
int p) nogil:
# as below this should someday call lapack's xtpqrt.
cdef int j
cdef blas_t rjj, tau
cdef blas_t* work
cdef char* T = 'T'
cdef char* N = 'N'
cdef size_t worksize = m * sizeof(blas_t)
work = <blas_t*>libc.stdlib.malloc(worksize)
if not work:
return MEMORY_ERROR
# possible FIX
# as this is written it requires F order q, r, and u. But thats not
# strictly necessary. C order should also work too with a little fiddling.
for j in range(n):
rjj = index2(r, rs, j, j)[0]
larfg(p+1, &rjj, col(u, us, j), us[0], &tau)
# here we apply the reflector by hand instead of calling larf
# since we need to apply it to a stack of r atop u, and these
# are separate. This also permits the reflector to always be
# p+1 long, rather than having a max of n+p.
if j+1 < n:
copy(n-j-1, index2(r, rs, j, j+1), rs[1], work, 1)
blas_t_conj(p, col(u, us, j), &us[0])
gemv(T, p, n-j-1, 1, index2(u, us, 0, j+1), p, col(u, us, j), us[0],
1, work, 1)
blas_t_conj(p, col(u, us, j), &us[0])
ger(p, n-j-1, -tau.conjugate(), col(u, us, j), us[0], work, 1,
index2(u, us, 0, j+1), p)
axpy(n-j-1, -tau.conjugate(), work, 1, index2(r, rs, j, j+1), rs[1])
index2(r, rs, j, j)[0] = rjj
# now apply this reflector to q
copy(m, col(q, qs, j), qs[0], work, 1)
gemv(N, m, p, 1, index2(q, qs, 0, n), m, col(u, us, j), us[0],
1, work, 1)
blas_t_conj(p, col(u, us, j), &us[0])
ger(m, p, -tau, work, 1, col(u, us, j), us[0],
index2(q, qs, 0, n), m)
axpy(m, -tau, work, 1, col(q, qs, j), qs[0])
# permute the rows of q, work columnwise, since q is fortran order
if k != m-p:
for j in range(n):
copy(m-k, index2(q, qs, k, j), qs[0], work, 1)
copy(p, work+(m-k-p), 1, index2(q, qs, k, j), qs[0])
copy(m-k-p, work, 1, index2(q, qs, k+p, j), qs[0])
libc.stdlib.free(work)
cdef int qr_block_row_insert(int m, int n, blas_t* q, int* qs,
blas_t* r, int* rs, int k, int p) nogil:
# this should someday call lapack's xtpqrt (requires lapack >= 3.4
# released nov 11). RHEL6's atlas doesn't seem to have it.
# On input this looks something like this:
# q = x x x x 0 0 0 r = x x x
# x x x x 0 0 0 0 x x
# x x x x 0 0 0 0 0 x
# x x x x 0 0 0 0 0 0
# 0 0 0 0 1 0 0 * * *
# 0 0 0 0 0 1 0 * * *
# 0 0 0 0 0 0 1 * * *
#
# The method will be to apply a series of reflectors to re triangularize r.
# followed by permuting the rows of q to put the new rows in the requested
# position.
cdef int j, hlen
cdef blas_t rjj, tau
cdef blas_t* work
cdef char* sideL = 'L'
cdef char* sideR = 'R'
# for tall or sqr + rows should be n. for fat + rows should be new m
cdef int limit = min(m, n)
work = <blas_t*>libc.stdlib.malloc(max(m,n)*sizeof(blas_t))
if not work:
return MEMORY_ERROR
for j in range(limit):
rjj = index2(r, rs, j, j)[0]
hlen = m-j
larfg(hlen, &rjj, index2(r, rs, j+1, j), rs[0], &tau)
index2(r, rs, j, j)[0] = 1
if j+1 < n:
larf(sideL, hlen, n-j-1, index2(r, rs, j, j), rs[0],
tau.conjugate(), index2(r, rs, j, j+1), rs[1], work)
larf(sideR, m, hlen, index2(r, rs, j, j), rs[0], tau,
index2(q, qs, 0, j), qs[1], work)
memset(index2(r, rs, j, j), 0, hlen*sizeof(blas_t))
index2(r, rs, j, j)[0] = rjj
# permute the rows., work columnwise, since q is fortran order
if k != m-p:
for j in range(m):
copy(m-k, index2(q, qs, k, j), qs[0], work, 1)
copy(p, work+(m-k-p), 1, index2(q, qs, k, j), qs[0])
copy(m-k-p, work, 1, index2(q, qs, k+p, j), qs[0])
libc.stdlib.free(work)
return 0
cdef int thin_qr_col_insert(int m, int n, blas_t* q, int* qs, blas_t* r,
int* rs, blas_t* u, int* us, int k, int p_eco,
int p_full, blas_t* rcond) nogil:
# here q and r will always be fortran ordered since we have to allocate them
cdef int i, j, info
cdef blas_t c, sn
cdef blas_t rc0, rc;
cdef blas_t* s
cdef char* N = 'N'
cdef char* T = 'T'
cdef char* C = 'C'
cdef char* TC
if blas_t is float or blas_t is double:
TC = T
rc0 = rcond[0]
elif blas_t is float_complex:
TC = C
rc0 = (<float*>rcond)[0]
else:
TC = C
rc0 = (<double*>rcond)[0]
# on entry, Q and R have both been increased in size, Q via the appending
# columns of zeros, and R by the addition of both columns and rows of
# zeros. In R, the new columns are located from column k to k + p. and
# the new rows are at the bottom. m and n refer to the size of the
# original system, not the new system.
s = <blas_t*>libc.stdlib.malloc(2*(n+p_eco)*sizeof(blas_t))
if not s:
return MEMORY_ERROR
for j in range(p_eco):
rc = rcond[0]
info = reorth(m, n+j, q, qs, True, col(u, us, j), us, s, &rc)
if info == 2:
if blas_t is float or blas_t is double:
rcond[0] = rc;
elif blas_t is float_complex:
rcond[0] = (<float*>&rc)[0]
else:
rcond[0] = (<double*>&rc)[0]
libc.stdlib.free(s)
return info
copy(m, col(u, us, j), us[0], col(q, qs, n+j), qs[0])
copy(n+j+1, s, 1, col(r, rs, k+j), rs[0])
for i in range(n-2+1, k-1, -1):
lartg(index2(r, rs, i+j, k+j), index2(r, rs, i+j+1, k+j), &c, &sn)
rot(n-i, index2(r, rs, i+j, i+p_eco+p_full), rs[1],
index2(r, rs, i+j+1, i+p_eco+p_full), rs[1], c, sn)
rot(m, col(q, qs, i+j), qs[0], col(q, qs, i+j+1), qs[0],
c, sn.conjugate())
libc.stdlib.free(s)
if p_full > 0:
# if this is true, we have ensured the u is also F contiguous.
gemm(TC, N, m, p_full, m, 1, q, m, col(u, us, p_eco), m, 0,
col(r, rs, k+p_eco), m)
qr_block_col_insert(m, n+p_eco+p_full, q, qs, r, rs, k+p_eco, p_full)
return 0
cdef void qr_col_insert(int m, int n, blas_t* q, int* qs, blas_t* r, int* rs,
int k) nogil:
cdef int j
cdef blas_t c, s, temp, tau
cdef blas_t* work
for j in range(m-2, k-1, -1):
lartg(index2(r, rs, j, k), index2(r, rs, j+1, k), &c, &s)
# update r if j is a nonzero row
if j+1 < n:
rot(n-j-1, index2(r, rs, j, j+1), rs[1],
index2(r, rs, j+1, j+1), rs[1], c, s)
# update the columns of q
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0], c, s.conjugate())
cdef int qr_block_col_insert(int m, int n, blas_t* q, int* qs,
blas_t* r, int* rs, int k, int p) nogil:
cdef int i, j
cdef blas_t c, s
cdef blas_t* tau = NULL
cdef blas_t* work = NULL
cdef int info, lwork
cdef char* side = 'R'
cdef char* trans = 'N'
if m >= n:
# if m > n, r looks like this.
# x x x x x x x x x x
# x x x x x x x x x
# x x x x x x x x
# x x x x x x x
# x x x x x x
# x x x x x
# x x x x
# x x x
# x x x
# x x x
# x x x
# x x x
#
# First zero the lower part of the new columns using a qr.
# query the workspace,
# set tau to point at something to keep new MKL working.
tau = &c
info = geqrf(m-n+p, p, index2(r, rs, n-p, k), m, tau, &c, -1)
if info < 0:
return libc.stdlib.abs(info)
info = ormqr(side, trans, m, m-(n-p), p, index2(r, rs, n-p, k), m,
tau, index2(q, qs, 0, n-p), m, &s, -1)
if info < 0:
return info
# we're only doing one allocation, so use the larger
lwork = to_lwork(c, s)
# allocate the workspace + tau
work = <blas_t*>libc.stdlib.malloc((lwork+min(m-n+p, p))*sizeof(blas_t))
if not work:
return MEMORY_ERROR
tau = work + lwork
# qr
info = geqrf(m-n+p, p, index2(r, rs, n-p, k), m, tau, work, lwork)
if info < 0:
return libc.stdlib.abs(info)
# apply the Q from this small qr to the last (m-(n-p)) columns of q.
info = ormqr(side, trans, m, m-(n-p), p, index2(r, rs, n-p, k), m,
tau, index2(q, qs, 0, n-p), m, work, lwork)
if info < 0:
return info
libc.stdlib.free(work)
# zero the reflectors since we're done with them
# memset can be used here, since r is always fortran order
for j in range(p):
memset(index2(r, rs, n-p+1+j, k+j), 0, (m-(n-p+1+j))*sizeof(blas_t))
# now we have something that looks like
# x x x x x x x x x x
# x x x x x x x x x
# x x x x x x x x
# x x x x x x x
# x x x x x x
# x x x x x
# x x x x
# x x x
# 0 x x
# 0 0 x
# 0 0 0
# 0 0 0
#
# and the rest of the columns need to be eliminated using rotations.
for i in range(p):
for j in range(n-p+i-1, k+i-1, -1):
lartg(index2(r, rs, j, k+i), index2(r, rs, j+1, k+i), &c, &s)
if j+1 < n:
rot(n-k-i-1, index2(r, rs, j, k+i+1), rs[1],
index2(r, rs, j+1, k+i+1), rs[1], c, s)
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0],
c, s.conjugate())
else:
# this case we can only uses givens rotations.
for i in range(p):
for j in range(m-2, k+i-1, -1):
lartg(index2(r, rs, j, k+i), index2(r, rs, j+1, k+i), &c, &s)
if j+1 < n:
rot(n-k-i-1, index2(r, rs, j, k+i+1), rs[1],
index2(r, rs, j+1, k+i+1), rs[1], c, s)
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0],
c, s.conjugate())
return 0
cdef void thin_qr_rank_1_update(int m, int n, blas_t* q, int* qs, bint qisF,
blas_t* r, int* rs, blas_t* u, int* us, blas_t* v, int* vs, blas_t* s,
int* ss) nogil:
"""Assume that q is (M,N) and either C or F contiguous, r is (N,N), u is M,
and V is N. s is a 2*n work array.
"""
cdef int j
cdef blas_t c, sn, rlast, t, rcond = 0.0
reorth(m, n, q, qs, qisF, u, us, s, &rcond)
# reduce s with givens, using u as the n+1 column of q
# do the first one since the rots will be different.
lartg(index1(s, ss, n-1), index1(s, ss, n), &c, &sn)
t = index2(r, rs, n-1, n-1)[0]
rlast = -t * sn.conjugate()
index2(r, rs, n-1, n-1)[0] = t * c
rot(m, col(q, qs, n-1), qs[0], u, us[0], c, sn.conjugate())
for j in range(n-2, -1, -1):
lartg(index1(s, ss, j), index1(s, ss, j+1), &c, &sn)
rot(n-j, index2(r, rs, j, j), rs[1],
index2(r, rs, j+1, j), rs[1], c, sn)
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0], c, sn.conjugate())
# add v to the first row of r
blas_t_conj(n, v, vs)
axpy(n, s[0], v, vs[0], row(r, rs, 0), rs[1])
# now r is upper hessenberg with the only value in the last row stored in
# rlast (This is very similar to hessenberg_qr below, but this loop ends
# at n-1 instead of n)
for j in range(n-1):
lartg(index2(r, rs, j, j), index2(r, rs, j+1, j), &c, &sn)
rot(n-j-1, index2(r, rs, j, j+1), rs[1],
index2(r, rs, j+1, j+1), rs[1], c, sn)
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0], c, sn.conjugate())
# handle the extra value in rlast
lartg(index2(r, rs, n-1, n-1), &rlast, &c, &sn)
rot(m, col(q, qs, n-1), qs[0], u, us[0], c, sn.conjugate())
cdef void thin_qr_rank_p_update(int m, int n, int p, blas_t* q, int* qs,
bint qisF, blas_t* r, int* rs, blas_t* u, int* us, blas_t* v, int* vs,
blas_t* s, int* ss) nogil:
"""Assume that q is (M,N) and either C or F contiguous, r is (N,N), u is
(M,p) and V is (N,p). s is a 2*n work array.
"""
cdef int j
for j in range(p):
thin_qr_rank_1_update(m, n, q, qs, qisF, r, rs, col(u, us, j), us,
col(v, vs, j), vs, s, ss)
cdef void qr_rank_1_update(int m, int n, blas_t* q, int* qs, blas_t* r, int* rs,
blas_t* u, int* us, blas_t* v, int* vs) nogil:
""" here we will assume that the u = Q.T.dot(u) and not the bare u.
if A is MxN then q is MxM, r is MxN, u is M and v is N.
e.g. currently assuming full matrices.
"""
cdef int j
cdef blas_t c, s
# The technique here is to reduce u to a series of givens rotations followed
# by a scalar e.g. [u1,u2,u3] --> [u,0,0]. Applying these rotations to r as
# we go. Then we will have the update be adding v scaled by the remainder
# of u to the first row of r, which will be upper hessenberg due to the
# givens applied to reduce u. We then reduce the upper hessenberg r to upper
# triangular.
for j in range(m-2, -1, -1):
lartg(index1(u, us, j), index1(u, us, j+1), &c, &s)
# update jth and (j+1)th rows of r.
if n-j > 0:
rot(n-j, index2(r, rs, j, j), rs[1], index2(r, rs, j+1, j), rs[1], c, s)
# update jth and (j+1)th cols of q.
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0], c, s.conjugate())
# add v to the first row
blas_t_conj(n, v, vs)
axpy(n, u[0], v, vs[0], row(r, rs, 0), rs[1])
# return to q, r form
hessenberg_qr(m, n, q, qs, r, rs, 0)
# no return, return q, r from python driver.
cdef int qr_rank_p_update(int m, int n, int p, blas_t* q, int* qs, blas_t* r,
int* rs, blas_t* u, int* us, blas_t* v, int* vs) nogil:
cdef int i, j
cdef blas_t c, s
cdef blas_t* tau = NULL
cdef blas_t* work = NULL
cdef int info, lwork
cdef char* sideR = 'R'
cdef char* sideL = 'L'
cdef char* uplo = 'U'
cdef char* trans = 'N'
cdef char* diag = 'N'
if m > n:
# query the workspace
# below p_subdiag_qr will need workspace of size m, which is the
# minimum, ormqr will also require.
# set tau to point at something, to keep new MKL working.
tau = &c
info = geqrf(m-n, p, index2(u, us, n, 0), m, tau, &c, -1)
if info < 0:
return libc.stdlib.abs(info)
info = ormqr(sideR, trans, m, m-n, p, index2(u, us, n, 0), m, tau,
index2(q, qs, 0, n), m, &s, -1)
if info < 0:
return info
# we're only doing one allocation, so use the larger
lwork = to_lwork(c, s)
# allocate the workspace + tau
work = <blas_t*>libc.stdlib.malloc((lwork+min(m-n, p))*sizeof(blas_t))
if not work:
return MEMORY_ERROR
tau = work + lwork
# qr
info = geqrf(m-n, p, index2(u, us, n, 0), m, tau, work, lwork)
if info < 0:
libc.stdlib.free(work)
return libc.stdlib.abs(info)
# apply the Q from this small qr to the last (m-n) columns of q.
info = ormqr(sideR, trans, m, m-n, p, index2(u, us, n, 0), m, tau,
index2(q, qs, 0, n), m, work, lwork)
if info < 0:
libc.stdlib.free(work)
return info
# reduce u the rest of the way to upper triangular using givens.
for i in range(p):
for j in range(n+i-1, i-1, -1):
lartg(index2(u, us, j, i), index2(u, us, j+1, i), &c, &s)
if p-i-1:
rot(p-i-1, index2(u, us, j, i+1), us[1],
index2(u, us, j+1, i+1), us[1], c, s)
rot(n, row(r, rs, j), rs[1], row(r, rs, j+1), rs[1], c, s)
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0],
c, s.conjugate())
else: # m == n or m < n
# reduce u to upper triangular using givens.
for i in range(p):
for j in range(m-2, i-1, -1):
lartg(index2(u, us, j, i), index2(u, us, j+1, i), &c, &s)
if p-i-1:
rot(p-i-1, index2(u, us, j, i+1), us[1],
index2(u, us, j+1, i+1), us[1], c, s)
rot(n, row(r, rs, j), rs[1], row(r, rs, j+1), rs[1], c, s)
rot(m, col(q, qs, j), qs[0], col(q, qs, j+1), qs[0],
c, s.conjugate())
# allocate workspace
work = <blas_t*>libc.stdlib.malloc(n*sizeof(blas_t))
if not work:
return MEMORY_ERROR
# now form UV**H and add it to R.
# This won't fill in any more of R than we have already.
blas_t_2d_conj(p, n, v, vs)
trmm(sideL, uplo, trans, diag, p, n, 1, u, m, v, p)
# (should this be n, p length adds instead since these are fortran contig?)
for j in range(p):
axpy(n, 1, row(v, vs, j), vs[1], row(r, rs, j), rs[1])
# now r has p subdiagonals, eliminate them with reflectors.
p_subdiag_qr(m, m, n, q, qs, r, rs, 0, p, work)
libc.stdlib.free(work)
return 0
cdef void hessenberg_qr(int m, int n, blas_t* q, int* qs, blas_t* r, int* rs,
int k) nogil:
"""Reduce an upper hessenberg matrix r, to upper triangular, starting in
row j. Apply these transformation to q as well. Both full and economic
decompositions are supported here.
"""
cdef int j
cdef blas_t c, s
cdef int limit = min(m-1, n)
for j in range(k, limit):
lartg(index2(r, rs, j, j), index2(r, rs, j+1, j), &c, &s)
# update the rest of r
if j+1 < m: