-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
lapack.jl
6228 lines (5845 loc) · 264 KB
/
lapack.jl
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
module LAPACK
@doc """
Interfaces to LAPACK subroutines.
""" LAPACK
const liblapack = Base.liblapack_name
import ..LinearAlgebra.BLAS.@blasfunc
import ..LinearAlgebra: BlasFloat, BlasInt, LAPACKException,
DimensionMismatch, SingularException, PosDefException, chkstride1, checksquare
using ..LinearAlgebra: triu, tril, dot
using Base: iszero, has_offset_axes
#Generic LAPACK error handlers
"""
Handle only negative LAPACK error codes
*NOTE* use only if the positive error code is useful.
"""
function chkargsok(ret::BlasInt)
if ret < 0
throw(ArgumentError("invalid argument #$(-ret) to LAPACK call"))
end
end
"Handle all nonzero info codes"
function chklapackerror(ret::BlasInt)
if ret == 0
return
elseif ret < 0
throw(ArgumentError("invalid argument #$(-ret) to LAPACK call"))
else # ret > 0
throw(LAPACKException(ret))
end
end
function chknonsingular(ret::BlasInt)
if ret > 0
throw(SingularException(ret))
end
end
function chkposdef(ret::BlasInt)
if ret > 0
throw(PosDefException(ret))
end
end
"Check that upper/lower (for special matrices) is correctly specified"
function chkuplo(uplo::AbstractChar)
if !(uplo == 'U' || uplo == 'L')
throw(ArgumentError("uplo argument must be 'U' (upper) or 'L' (lower), got $uplo"))
end
uplo
end
"Check that {c}transpose is correctly specified"
function chktrans(trans::AbstractChar)
if !(trans == 'N' || trans == 'C' || trans == 'T')
throw(ArgumentError("trans argument must be 'N' (no transpose), 'T' (transpose), or 'C' (conjugate transpose), got $trans"))
end
trans
end
"Check that left/right hand side multiply is correctly specified"
function chkside(side::AbstractChar)
if !(side == 'L' || side == 'R')
throw(ArgumentError("side argument must be 'L' (left hand multiply) or 'R' (right hand multiply), got $side"))
end
side
end
"Check that unit diagonal flag is correctly specified"
function chkdiag(diag::AbstractChar)
if !(diag == 'U' || diag =='N')
throw(ArgumentError("diag argument must be 'U' (unit diagonal) or 'N' (non-unit diagonal), got $diag"))
end
diag
end
subsetrows(X::AbstractVector, Y::AbstractArray, k) = Y[1:k]
subsetrows(X::AbstractMatrix, Y::AbstractArray, k) = Y[1:k, :]
function chkfinite(A::AbstractMatrix)
for a in A
if !isfinite(a)
throw(ArgumentError("matrix contains Infs or NaNs"))
end
end
return true
end
# LAPACK version number
function version()
major = Ref{BlasInt}(0)
minor = Ref{BlasInt}(0)
patch = Ref{BlasInt}(0)
ccall((@blasfunc(ilaver_), liblapack), Cvoid,
(Ptr{BlasInt}, Ptr{BlasInt}, Ptr{BlasInt}),
major, minor, patch)
return VersionNumber(major[], minor[], patch[])
end
# (GB) general banded matrices, LU decomposition and solver
for (gbtrf, gbtrs, elty) in
((:dgbtrf_,:dgbtrs_,:Float64),
(:sgbtrf_,:sgbtrs_,:Float32),
(:zgbtrf_,:zgbtrs_,:ComplexF64),
(:cgbtrf_,:cgbtrs_,:ComplexF32))
@eval begin
# SUBROUTINE DGBTRF( M, N, KL, KU, AB, LDAB, IPIV, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, KL, KU, LDAB, M, N
# * .. Array Arguments ..
# INTEGER IPIV( * )
# DOUBLE PRECISION AB( LDAB, * )
function gbtrf!(kl::Integer, ku::Integer, m::Integer, AB::AbstractMatrix{$elty})
@assert !has_offset_axes(AB)
chkstride1(AB)
n = size(AB, 2)
mnmn = min(m, n)
ipiv = similar(AB, BlasInt, mnmn)
info = Ref{BlasInt}()
ccall((@blasfunc($gbtrf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}, Ptr{BlasInt}),
m, n, kl, ku, AB, max(1,stride(AB,2)), ipiv, info)
chklapackerror(info[])
AB, ipiv
end
# SUBROUTINE DGBTRS( TRANS, N, KL, KU, NRHS, AB, LDAB, IPIV, B, LDB, INFO)
# * .. Scalar Arguments ..
# CHARACTER TRANS
# INTEGER INFO, KL, KU, LDAB, LDB, N, NRHS
# * .. Array Arguments ..
# INTEGER IPIV( * )
# DOUBLE PRECISION AB( LDAB, * ), B( LDB, * )
function gbtrs!(trans::AbstractChar, kl::Integer, ku::Integer, m::Integer,
AB::AbstractMatrix{$elty}, ipiv::AbstractVector{BlasInt},
B::AbstractVecOrMat{$elty})
@assert !has_offset_axes(AB, B)
chkstride1(AB, B, ipiv)
chktrans(trans)
info = Ref{BlasInt}()
n = size(AB,2)
if m != n || m != size(B,1)
throw(DimensionMismatch("matrix AB has dimensions $(size(AB)), but right hand side matrix B has dimensions $(size(B))"))
end
ccall((@blasfunc($gbtrs), liblapack), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{BlasInt}),
trans, n, kl, ku, size(B,2), AB, max(1,stride(AB,2)), ipiv,
B, max(1,stride(B,2)), info)
chklapackerror(info[])
B
end
end
end
"""
gbtrf!(kl, ku, m, AB) -> (AB, ipiv)
Compute the LU factorization of a banded matrix `AB`. `kl` is the first
subdiagonal containing a nonzero band, `ku` is the last superdiagonal
containing one, and `m` is the first dimension of the matrix `AB`. Returns
the LU factorization in-place and `ipiv`, the vector of pivots used.
"""
gbtrf!(kl::Integer, ku::Integer, m::Integer, AB::AbstractMatrix)
"""
gbtrs!(trans, kl, ku, m, AB, ipiv, B)
Solve the equation `AB * X = B`. `trans` determines the orientation of `AB`. It may
be `N` (no transpose), `T` (transpose), or `C` (conjugate transpose). `kl` is the
first subdiagonal containing a nonzero band, `ku` is the last superdiagonal
containing one, and `m` is the first dimension of the matrix `AB`. `ipiv` is the vector
of pivots returned from `gbtrf!`. Returns the vector or matrix `X`, overwriting `B` in-place.
"""
gbtrs!(trans::AbstractChar, kl::Integer, ku::Integer, m::Integer, AB::AbstractMatrix, ipiv::AbstractVector{BlasInt}, B::AbstractVecOrMat)
## (GE) general matrices: balancing and back-transforming
for (gebal, gebak, elty, relty) in
((:dgebal_, :dgebak_, :Float64, :Float64),
(:sgebal_, :sgebak_, :Float32, :Float32),
(:zgebal_, :zgebak_, :ComplexF64, :Float64),
(:cgebal_, :cgebak_, :ComplexF32, :Float32))
@eval begin
# SUBROUTINE DGEBAL( JOB, N, A, LDA, ILO, IHI, SCALE, INFO )
#* .. Scalar Arguments ..
# CHARACTER JOB
# INTEGER IHI, ILP, INFO, LDA, N
# .. Array Arguments ..
# DOUBLE PRECISION A( LDA, * ), SCALE( * )
function gebal!(job::AbstractChar, A::AbstractMatrix{$elty})
chkstride1(A)
n = checksquare(A)
chkfinite(A) # balancing routines don't support NaNs and Infs
ihi = Ref{BlasInt}()
ilo = Ref{BlasInt}()
scale = similar(A, $relty, n)
info = Ref{BlasInt}()
ccall((@blasfunc($gebal), liblapack), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{BlasInt}, Ptr{BlasInt}, Ptr{$relty}, Ptr{BlasInt}),
job, n, A, max(1,stride(A,2)), ilo, ihi, scale, info)
chklapackerror(info[])
ilo[], ihi[], scale
end
# SUBROUTINE DGEBAK( JOB, SIDE, N, ILO, IHI, SCALE, M, V, LDV, INFO )
#* .. Scalar Arguments ..
# CHARACTER JOB, SIDE
# INTEGER IHI, ILP, INFO, LDV, M, N
# .. Array Arguments ..
# DOUBLE PRECISION SCALE( * ), V( LDV, * )
function gebak!(job::AbstractChar, side::AbstractChar,
ilo::BlasInt, ihi::BlasInt, scale::AbstractVector{$relty},
V::AbstractMatrix{$elty})
@assert !has_offset_axes(scale, V)
chkstride1(scale, V)
chkside(side)
chkfinite(V) # balancing routines don't support NaNs and Infs
n = checksquare(V)
info = Ref{BlasInt}()
ccall((@blasfunc($gebak), liblapack), Cvoid,
(Ref{UInt8}, Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt},
Ptr{$relty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
job, side, size(V,1), ilo, ihi, scale, n, V, max(1,stride(V,2)), info)
chklapackerror(info[])
V
end
end
end
"""
gebal!(job, A) -> (ilo, ihi, scale)
Balance the matrix `A` before computing its eigensystem or Schur factorization.
`job` can be one of `N` (`A` will not be permuted or scaled), `P` (`A` will only
be permuted), `S` (`A` will only be scaled), or `B` (`A` will be both permuted
and scaled). Modifies `A` in-place and returns `ilo`, `ihi`, and `scale`. If
permuting was turned on, `A[i,j] = 0` if `j > i` and `1 < j < ilo` or `j > ihi`.
`scale` contains information about the scaling/permutations performed.
"""
gebal!(job::AbstractChar, A::AbstractMatrix)
"""
gebak!(job, side, ilo, ihi, scale, V)
Transform the eigenvectors `V` of a matrix balanced using `gebal!` to
the unscaled/unpermuted eigenvectors of the original matrix. Modifies `V`
in-place. `side` can be `L` (left eigenvectors are transformed) or `R`
(right eigenvectors are transformed).
"""
gebak!(job::AbstractChar, side::AbstractChar, ilo::BlasInt, ihi::BlasInt, scale::AbstractVector, V::AbstractMatrix)
# (GE) general matrices, direct decompositions
#
# These mutating functions take as arguments all the values they
# return, even if the value of the function does not depend on them
# (e.g. the tau argument). This is so that a factorization can be
# updated in place. The condensed mutating functions, usually a
# function of A only, are defined after this block.
for (gebrd, gelqf, geqlf, geqrf, geqp3, geqrt, geqrt3, gerqf, getrf, elty, relty) in
((:dgebrd_,:dgelqf_,:dgeqlf_,:dgeqrf_,:dgeqp3_,:dgeqrt_,:dgeqrt3_,:dgerqf_,:dgetrf_,:Float64,:Float64),
(:sgebrd_,:sgelqf_,:sgeqlf_,:sgeqrf_,:sgeqp3_,:sgeqrt_,:sgeqrt3_,:sgerqf_,:sgetrf_,:Float32,:Float32),
(:zgebrd_,:zgelqf_,:zgeqlf_,:zgeqrf_,:zgeqp3_,:zgeqrt_,:zgeqrt3_,:zgerqf_,:zgetrf_,:ComplexF64,:Float64),
(:cgebrd_,:cgelqf_,:cgeqlf_,:cgeqrf_,:cgeqp3_,:cgeqrt_,:cgeqrt3_,:cgerqf_,:cgetrf_,:ComplexF32,:Float32))
@eval begin
# SUBROUTINE DGEBRD( M, N, A, LDA, D, E, TAUQ, TAUP, WORK, LWORK,
# INFO )
# .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# .. Array Arguments ..
# DOUBLE PRECISION A( LDA, * ), D( * ), E( * ), TAUP( * ),
# TAUQ( * ), WORK( * )
function gebrd!(A::AbstractMatrix{$elty})
@assert !has_offset_axes(A)
chkstride1(A)
m, n = size(A)
k = min(m, n)
d = similar(A, $relty, k)
e = similar(A, $relty, k)
tauq = similar(A, $elty, k)
taup = similar(A, $elty, k)
work = Vector{$elty}(undef, 1)
lwork = BlasInt(-1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($gebrd), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$relty}, Ptr{$relty}, Ptr{$elty}, Ptr{$elty},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, max(1,stride(A,2)),
d, e, tauq, taup,
work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A, d, e, tauq, taup
end
# SUBROUTINE DGELQF( M, N, A, LDA, TAU, WORK, LWORK, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# * .. Array Arguments ..
# DOUBLE PRECISION A( LDA, * ), TAU( * ), WORK( * )
function gelqf!(A::AbstractMatrix{$elty}, tau::AbstractVector{$elty})
@assert !has_offset_axes(A, tau)
chkstride1(A,tau)
m = BlasInt(size(A, 1))
n = BlasInt(size(A, 2))
lda = BlasInt(max(1,stride(A, 2)))
if length(tau) != min(m,n)
throw(DimensionMismatch("tau has length $(length(tau)), but needs length $(min(m,n))"))
end
lwork = BlasInt(-1)
work = Vector{$elty}(undef, 1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($gelqf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, lda, tau, work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A, tau
end
# SUBROUTINE DGEQLF( M, N, A, LDA, TAU, WORK, LWORK, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# * .. Array Arguments ..
# DOUBLE PRECISION A( LDA, * ), TAU( * ), WORK( * )
function geqlf!(A::AbstractMatrix{$elty}, tau::AbstractVector{$elty})
@assert !has_offset_axes(A, tau)
chkstride1(A,tau)
m = BlasInt(size(A, 1))
n = BlasInt(size(A, 2))
lda = BlasInt(max(1,stride(A, 2)))
if length(tau) != min(m,n)
throw(DimensionMismatch("tau has length $(length(tau)), but needs length $(min(m,n))"))
end
lwork = BlasInt(-1)
work = Vector{$elty}(undef, 1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($geqlf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, lda, tau, work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A, tau
end
# SUBROUTINE DGEQP3( M, N, A, LDA, JPVT, TAU, WORK, LWORK, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# * .. Array Arguments ..
# INTEGER JPVT( * )
# DOUBLE PRECISION A( LDA, * ), TAU( * ), WORK( * )
function geqp3!(A::AbstractMatrix{$elty}, jpvt::AbstractVector{BlasInt}, tau::AbstractVector{$elty})
@assert !has_offset_axes(A, jpvt, tau)
chkstride1(A,jpvt,tau)
m,n = size(A)
if length(tau) != min(m,n)
throw(DimensionMismatch("tau has length $(length(tau)), but needs length $(min(m,n))"))
end
if length(jpvt) != n
throw(DimensionMismatch("jpvt has length $(length(jpvt)), but needs length $n"))
end
lda = stride(A,2)
if lda == 0
return A, tau, jpvt
end # Early exit
work = Vector{$elty}(undef, 1)
lwork = BlasInt(-1)
cmplx = eltype(A)<:Complex
if cmplx
rwork = Vector{$relty}(undef, 2n)
end
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
if cmplx
ccall((@blasfunc($geqp3), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ref{BlasInt},
Ptr{$relty}, Ptr{BlasInt}),
m, n, A, lda,
jpvt, tau, work, lwork,
rwork, info)
else
ccall((@blasfunc($geqp3), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ref{BlasInt},
Ptr{BlasInt}),
m, n, A, lda,
jpvt, tau, work,
lwork, info)
end
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
return A, tau, jpvt
end
function geqrt!(A::AbstractMatrix{$elty}, T::AbstractMatrix{$elty})
@assert !has_offset_axes(A, T)
chkstride1(A)
m, n = size(A)
minmn = min(m, n)
nb = size(T, 1)
if nb > minmn
throw(ArgumentError("block size $nb > $minmn too large"))
end
lda = max(1, stride(A,2))
work = Vector{$elty}(undef, nb*n)
if n > 0
info = Ref{BlasInt}()
ccall((@blasfunc($geqrt), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty},
Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty},
Ptr{BlasInt}),
m, n, nb, A,
lda, T, max(1,stride(T,2)), work,
info)
chklapackerror(info[])
end
A, T
end
function geqrt3!(A::AbstractMatrix{$elty}, T::AbstractMatrix{$elty})
@assert !has_offset_axes(A, T)
chkstride1(A)
chkstride1(T)
m, n = size(A)
p, q = size(T)
if m < n
throw(DimensionMismatch("input matrix A has dimensions ($m,$n), but should have more rows than columns"))
end
if p != n || q != n
throw(DimensionMismatch("block reflector T has dimensions ($p,$q), but should have dimensions ($n,$n)"))
end
if n > 0
info = Ref{BlasInt}()
ccall((@blasfunc($geqrt3), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, max(1, stride(A, 2)),
T, max(1,stride(T,2)), info)
chklapackerror(info[])
end
A, T
end
## geqrfp! - positive elements on diagonal of R - not defined yet
# SUBROUTINE DGEQRFP( M, N, A, LDA, TAU, WORK, LWORK, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# * .. Array Arguments ..
# DOUBLE PRECISION A( LDA, * ), TAU( * ), WORK( * )
function geqrf!(A::AbstractMatrix{$elty}, tau::AbstractVector{$elty})
@assert !has_offset_axes(A, tau)
chkstride1(A,tau)
m, n = size(A)
if length(tau) != min(m,n)
throw(DimensionMismatch("tau has length $(length(tau)), but needs length $(min(m,n))"))
end
work = Vector{$elty}(undef, 1)
lwork = BlasInt(-1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($geqrf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, max(1,stride(A,2)), tau, work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A, tau
end
# SUBROUTINE DGERQF( M, N, A, LDA, TAU, WORK, LWORK, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# * .. Array Arguments ..
# DOUBLE PRECISION A( LDA, * ), TAU( * ), WORK( * )
function gerqf!(A::AbstractMatrix{$elty},tau::AbstractVector{$elty})
@assert !has_offset_axes(A, tau)
chkstride1(A,tau)
m, n = size(A)
if length(tau) != min(m,n)
throw(DimensionMismatch("tau has length $(length(tau)), but needs length $(min(m,n))"))
end
lwork = BlasInt(-1)
work = Vector{$elty}(undef, 1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($gerqf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, max(1,stride(A,2)), tau, work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A, tau
end
# SUBROUTINE DGETRF( M, N, A, LDA, IPIV, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, M, N
# * .. Array Arguments ..
# INTEGER IPIV( * )
# DOUBLE PRECISION A( LDA, * )
function getrf!(A::AbstractMatrix{$elty})
@assert !has_offset_axes(A)
chkstride1(A)
m, n = size(A)
lda = max(1,stride(A, 2))
ipiv = similar(A, BlasInt, min(m,n))
info = Ref{BlasInt}()
ccall((@blasfunc($getrf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty},
Ref{BlasInt}, Ptr{BlasInt}, Ptr{BlasInt}),
m, n, A, lda, ipiv, info)
chkargsok(info[])
A, ipiv, info[] #Error code is stored in LU factorization type
end
end
end
"""
gebrd!(A) -> (A, d, e, tauq, taup)
Reduce `A` in-place to bidiagonal form `A = QBP'`. Returns `A`, containing the
bidiagonal matrix `B`; `d`, containing the diagonal elements of `B`; `e`,
containing the off-diagonal elements of `B`; `tauq`, containing the
elementary reflectors representing `Q`; and `taup`, containing the
elementary reflectors representing `P`.
"""
gebrd!(A::AbstractMatrix)
"""
gelqf!(A, tau)
Compute the `LQ` factorization of `A`, `A = LQ`. `tau` contains scalars
which parameterize the elementary reflectors of the factorization. `tau`
must have length greater than or equal to the smallest dimension of `A`.
Returns
`A` and `tau` modified in-place.
"""
gelqf!(A::AbstractMatrix, tau::AbstractVector)
"""
geqlf!(A, tau)
Compute the `QL` factorization of `A`, `A = QL`. `tau` contains scalars
which parameterize the elementary reflectors of the factorization. `tau`
must have length greater than or equal to the smallest dimension of `A`.
Returns `A` and `tau` modified in-place.
"""
geqlf!(A::AbstractMatrix, tau::AbstractVector)
"""
geqp3!(A, jpvt, tau)
Compute the pivoted `QR` factorization of `A`, `AP = QR` using BLAS level 3.
`P` is a pivoting matrix, represented by `jpvt`. `tau` stores the elementary
reflectors. `jpvt` must have length length greater than or equal to `n` if `A`
is an `(m x n)` matrix. `tau` must have length greater than or equal to the
smallest dimension of `A`.
`A`, `jpvt`, and `tau` are modified in-place.
"""
geqp3!(A::AbstractMatrix, jpvt::AbstractVector{BlasInt}, tau::AbstractVector)
"""
geqrt!(A, T)
Compute the blocked `QR` factorization of `A`, `A = QR`. `T` contains upper
triangular block reflectors which parameterize the elementary reflectors of
the factorization. The first dimension of `T` sets the block size and it must
be between 1 and `n`. The second dimension of `T` must equal the smallest
dimension of `A`.
Returns `A` and `T` modified in-place.
"""
geqrt!(A::AbstractMatrix, T::AbstractMatrix)
"""
geqrt3!(A, T)
Recursively computes the blocked `QR` factorization of `A`, `A = QR`. `T`
contains upper triangular block reflectors which parameterize the
elementary reflectors of the factorization. The first dimension of `T` sets the
block size and it must be between 1 and `n`. The second dimension of `T` must
equal the smallest dimension of `A`.
Returns `A` and `T` modified in-place.
"""
geqrt3!(A::AbstractMatrix, T::AbstractMatrix)
"""
geqrf!(A, tau)
Compute the `QR` factorization of `A`, `A = QR`. `tau` contains scalars
which parameterize the elementary reflectors of the factorization. `tau`
must have length greater than or equal to the smallest dimension of `A`.
Returns `A` and `tau` modified in-place.
"""
geqrf!(A::AbstractMatrix, tau::AbstractVector)
"""
gerqf!(A, tau)
Compute the `RQ` factorization of `A`, `A = RQ`. `tau` contains scalars
which parameterize the elementary reflectors of the factorization. `tau`
must have length greater than or equal to the smallest dimension of `A`.
Returns `A` and `tau` modified in-place.
"""
gerqf!(A::AbstractMatrix, tau::AbstractVector)
"""
getrf!(A) -> (A, ipiv, info)
Compute the pivoted `LU` factorization of `A`, `A = LU`.
Returns `A`, modified in-place, `ipiv`, the pivoting information, and an `info`
code which indicates success (`info = 0`), a singular value in `U`
(`info = i`, in which case `U[i,i]` is singular), or an error code (`info < 0`).
"""
getrf!(A::AbstractMatrix, tau::AbstractVector)
"""
gelqf!(A) -> (A, tau)
Compute the `LQ` factorization of `A`, `A = LQ`.
Returns `A`, modified in-place, and `tau`, which contains scalars
which parameterize the elementary reflectors of the factorization.
"""
gelqf!(A::AbstractMatrix{<:BlasFloat}) = ((m,n) = size(A); gelqf!(A, similar(A, min(m, n))))
"""
geqlf!(A) -> (A, tau)
Compute the `QL` factorization of `A`, `A = QL`.
Returns `A`, modified in-place, and `tau`, which contains scalars
which parameterize the elementary reflectors of the factorization.
"""
geqlf!(A::AbstractMatrix{<:BlasFloat}) = ((m,n) = size(A); geqlf!(A, similar(A, min(m, n))))
"""
geqrt!(A, nb) -> (A, T)
Compute the blocked `QR` factorization of `A`, `A = QR`. `nb` sets the block size
and it must be between 1 and `n`, the second dimension of `A`.
Returns `A`, modified in-place, and `T`, which contains upper
triangular block reflectors which parameterize the elementary reflectors of
the factorization.
"""
geqrt!(A::AbstractMatrix{<:BlasFloat}, nb::Integer) = geqrt!(A, similar(A, nb, minimum(size(A))))
"""
geqrt3!(A) -> (A, T)
Recursively computes the blocked `QR` factorization of `A`, `A = QR`.
Returns `A`, modified in-place, and `T`, which contains upper triangular block
reflectors which parameterize the elementary reflectors of the factorization.
"""
geqrt3!(A::AbstractMatrix{<:BlasFloat}) = (n = size(A, 2); geqrt3!(A, similar(A, n, n)))
"""
geqrf!(A) -> (A, tau)
Compute the `QR` factorization of `A`, `A = QR`.
Returns `A`, modified in-place, and `tau`, which contains scalars
which parameterize the elementary reflectors of the factorization.
"""
geqrf!(A::AbstractMatrix{<:BlasFloat}) = ((m,n) = size(A); geqrf!(A, similar(A, min(m, n))))
"""
gerqf!(A) -> (A, tau)
Compute the `RQ` factorization of `A`, `A = RQ`.
Returns `A`, modified in-place, and `tau`, which contains scalars
which parameterize the elementary reflectors of the factorization.
"""
gerqf!(A::AbstractMatrix{<:BlasFloat}) = ((m,n) = size(A); gerqf!(A, similar(A, min(m, n))))
"""
geqp3!(A, jpvt) -> (A, jpvt, tau)
Compute the pivoted `QR` factorization of `A`, `AP = QR` using BLAS level 3.
`P` is a pivoting matrix, represented by `jpvt`. `jpvt` must have length
greater than or equal to `n` if `A` is an `(m x n)` matrix.
Returns `A` and `jpvt`, modified in-place, and `tau`, which stores the elementary
reflectors.
"""
function geqp3!(A::AbstractMatrix{<:BlasFloat}, jpvt::AbstractVector{BlasInt})
m, n = size(A)
geqp3!(A, jpvt, similar(A, min(m, n)))
end
"""
geqp3!(A) -> (A, jpvt, tau)
Compute the pivoted `QR` factorization of `A`, `AP = QR` using BLAS level 3.
Returns `A`, modified in-place, `jpvt`, which represents the pivoting matrix `P`,
and `tau`, which stores the elementary reflectors.
"""
function geqp3!(A::AbstractMatrix{<:BlasFloat})
m, n = size(A)
geqp3!(A, zeros(BlasInt, n), similar(A, min(m, n)))
end
## Complete orthogonaliztion tools
for (tzrzf, ormrz, elty) in
((:dtzrzf_,:dormrz_,:Float64),
(:stzrzf_,:sormrz_,:Float32),
(:ztzrzf_,:zunmrz_,:ComplexF64),
(:ctzrzf_,:cunmrz_,:ComplexF32))
@eval begin
# SUBROUTINE ZTZRZF( M, N, A, LDA, TAU, WORK, LWORK, INFO )
#
# .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, M, N
# ..
# .. Array Arguments ..
# COMPLEX*16 A( LDA, * ), TAU( * ), WORK( * )
function tzrzf!(A::AbstractMatrix{$elty})
@assert !has_offset_axes(A)
chkstride1(A)
m, n = size(A)
if n < m
throw(DimensionMismatch("input matrix A has dimensions ($m,$n), but cannot have fewer columns than rows"))
end
lda = max(1, stride(A,2))
tau = similar(A, $elty, m)
work = Vector{$elty}(undef, 1)
lwork = BlasInt(-1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($tzrzf), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
m, n, A, lda,
tau, work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A, tau
end
# SUBROUTINE ZUNMRZ( SIDE, TRANS, M, N, K, L, A, LDA, TAU, C, LDC,
# WORK, LWORK, INFO )
#
# .. Scalar Arguments ..
# CHARACTER SIDE, TRANS
# INTEGER INFO, K, L, LDA, LDC, LWORK, M, N
# ..
# .. Array Arguments ..
# COMPLEX*16 A( LDA, * ), C( LDC, * ), TAU( * ), WORK( * )
function ormrz!(side::AbstractChar, trans::AbstractChar, A::AbstractMatrix{$elty},
tau::AbstractVector{$elty}, C::AbstractMatrix{$elty})
@assert !has_offset_axes(A, tau, C)
chktrans(trans)
chkside(side)
chkstride1(A, tau, C)
m, n = size(C)
k = length(tau)
l = size(A, 2) - size(A, 1)
lda = max(1, stride(A,2))
ldc = max(1, stride(C,2))
work = Vector{$elty}(undef, 1)
lwork = BlasInt(-1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($ormrz), liblapack), Cvoid,
(Ref{UInt8}, Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt},
Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ptr{$elty}, Ref{BlasInt}, Ptr{$elty},
Ref{BlasInt}, Ptr{BlasInt}),
side, trans, m, n,
k, l, A, lda,
tau, C, ldc, work,
lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
C
end
end
end
"""
ormrz!(side, trans, A, tau, C)
Multiplies the matrix `C` by `Q` from the transformation supplied by
`tzrzf!`. Depending on `side` or `trans` the multiplication can be
left-sided (`side = L, Q*C`) or right-sided (`side = R, C*Q`) and `Q`
can be unmodified (`trans = N`), transposed (`trans = T`), or conjugate
transposed (`trans = C`). Returns matrix `C` which is modified in-place
with the result of the multiplication.
"""
ormrz!(side::AbstractChar, trans::AbstractChar, A::AbstractMatrix, tau::AbstractVector, C::AbstractMatrix)
"""
tzrzf!(A) -> (A, tau)
Transforms the upper trapezoidal matrix `A` to upper triangular form in-place.
Returns `A` and `tau`, the scalar parameters for the elementary reflectors
of the transformation.
"""
tzrzf!(A::AbstractMatrix)
## (GE) general matrices, solvers with factorization, solver and inverse
for (gels, gesv, getrs, getri, elty) in
((:dgels_,:dgesv_,:dgetrs_,:dgetri_,:Float64),
(:sgels_,:sgesv_,:sgetrs_,:sgetri_,:Float32),
(:zgels_,:zgesv_,:zgetrs_,:zgetri_,:ComplexF64),
(:cgels_,:cgesv_,:cgetrs_,:cgetri_,:ComplexF32))
@eval begin
# SUBROUTINE DGELS( TRANS, M, N, NRHS, A, LDA, B, LDB, WORK, LWORK,INFO)
# * .. Scalar Arguments ..
# CHARACTER TRANS
# INTEGER INFO, LDA, LDB, LWORK, M, N, NRHS
function gels!(trans::AbstractChar, A::AbstractMatrix{$elty}, B::AbstractVecOrMat{$elty})
@assert !has_offset_axes(A, B)
chktrans(trans)
chkstride1(A, B)
btrn = trans == 'T'
m, n = size(A)
if size(B,1) != (btrn ? n : m)
throw(DimensionMismatch("matrix A has dimensions ($m,$n), transposed: $btrn, but leading dimension of B is $(size(B,1))"))
end
info = Ref{BlasInt}()
work = Vector{$elty}(undef, 1)
lwork = BlasInt(-1)
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($gels), liblapack), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
(btrn ? 'T' : 'N'), m, n, size(B,2), A, max(1,stride(A,2)),
B, max(1,stride(B,2)), work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
k = min(m, n)
F = m < n ? tril(A[1:k, 1:k]) : triu(A[1:k, 1:k])
ssr = Vector{$elty}(undef, size(B, 2))
for i = 1:size(B,2)
x = zero($elty)
for j = k+1:size(B,1)
x += abs2(B[j,i])
end
ssr[i] = x
end
F, subsetrows(B, B, k), ssr
end
# SUBROUTINE DGESV( N, NRHS, A, LDA, IPIV, B, LDB, INFO )
# * .. Scalar Arguments ..
# INTEGER INFO, LDA, LDB, N, NRHS
# * ..
# * .. Array Arguments ..
# INTEGER IPIV( * )
# DOUBLE PRECISION A( LDA, * ), B( LDB, * )
function gesv!(A::AbstractMatrix{$elty}, B::AbstractVecOrMat{$elty})
@assert !has_offset_axes(A, B)
chkstride1(A, B)
n = checksquare(A)
if size(B,1) != n
throw(DimensionMismatch("B has leading dimension $(size(B,1)), but needs $n"))
end
ipiv = similar(A, BlasInt, n)
info = Ref{BlasInt}()
ccall((@blasfunc($gesv), liblapack), Cvoid,
(Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
n, size(B,2), A, max(1,stride(A,2)), ipiv, B, max(1,stride(B,2)), info)
chklapackerror(info[])
B, A, ipiv
end
# SUBROUTINE DGETRS( TRANS, N, NRHS, A, LDA, IPIV, B, LDB, INFO )
#* .. Scalar Arguments ..
# CHARACTER TRANS
# INTEGER INFO, LDA, LDB, N, NRHS
# .. Array Arguments ..
# INTEGER IPIV( * )
# DOUBLE PRECISION A( LDA, * ), B( LDB, * )
function getrs!(trans::AbstractChar, A::AbstractMatrix{$elty}, ipiv::AbstractVector{BlasInt}, B::AbstractVecOrMat{$elty})
@assert !has_offset_axes(A, ipiv, B)
chktrans(trans)
chkstride1(A, B, ipiv)
n = checksquare(A)
if n != size(B, 1)
throw(DimensionMismatch("B has leading dimension $(size(B,1)), but needs $n"))
end
nrhs = size(B, 2)
info = Ref{BlasInt}()
ccall((@blasfunc($getrs), liblapack), Cvoid,
(Ref{UInt8}, Ref{BlasInt}, Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt},
Ptr{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
trans, n, size(B,2), A, max(1,stride(A,2)), ipiv, B, max(1,stride(B,2)), info)
chklapackerror(info[])
B
end
# SUBROUTINE DGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )
#* .. Scalar Arguments ..
# INTEGER INFO, LDA, LWORK, N
#* .. Array Arguments ..
# INTEGER IPIV( * )
# DOUBLE PRECISION A( LDA, * ), WORK( * )
function getri!(A::AbstractMatrix{$elty}, ipiv::AbstractVector{BlasInt})
@assert !has_offset_axes(A, ipiv)
chkstride1(A, ipiv)
n = checksquare(A)
if n != length(ipiv)
throw(DimensionMismatch("ipiv has length $(length(ipiv)), but needs $n"))
end
lda = max(1,stride(A, 2))
lwork = BlasInt(-1)
work = Vector{$elty}(undef, 1)
info = Ref{BlasInt}()
for i = 1:2 # first call returns lwork as work[1]
ccall((@blasfunc($getri), liblapack), Cvoid,
(Ref{BlasInt}, Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt},
Ptr{$elty}, Ref{BlasInt}, Ptr{BlasInt}),
n, A, lda, ipiv, work, lwork, info)
chklapackerror(info[])
if i == 1
lwork = BlasInt(real(work[1]))
resize!(work, lwork)
end
end
A
end
end
end
"""
gels!(trans, A, B) -> (F, B, ssr)
Solves the linear equation `A * X = B`, `transpose(A) * X = B`, or `adjoint(A) * X = B` using
a QR or LQ factorization. Modifies the matrix/vector `B` in place with the
solution. `A` is overwritten with its `QR` or `LQ` factorization. `trans`
may be one of `N` (no modification), `T` (transpose), or `C` (conjugate
transpose). `gels!` searches for the minimum norm/least squares solution.