-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
triangular.jl
1840 lines (1716 loc) · 61.5 KB
/
triangular.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: http://julialang.org/license
## Triangular
abstract AbstractTriangular{T,S<:AbstractMatrix} <: AbstractMatrix{T} # could be renamed to Triangular when than name has been fully deprecated
# First loop through all methods that don't need special care for upper/lower and unit diagonal
for t in (:LowerTriangular, :UnitLowerTriangular, :UpperTriangular,
:UnitUpperTriangular)
@eval begin
immutable $t{T,S<:AbstractMatrix} <: AbstractTriangular{T,S}
data::S
end
$t(A::$t) = A
function $t(A::AbstractMatrix)
Base.LinAlg.checksquare(A)
return $t{eltype(A), typeof(A)}(A)
end
size(A::$t, d) = size(A.data, d)
size(A::$t) = size(A.data)
convert{T,S}(::Type{$t{T}}, A::$t{T,S}) = A
convert{Tnew,Told,S}(::Type{$t{Tnew}}, A::$t{Told,S}) = (Anew = convert(AbstractMatrix{Tnew}, A.data); $t(Anew))
convert{Tnew,Told,S}(::Type{AbstractMatrix{Tnew}}, A::$t{Told,S}) = convert($t{Tnew}, A)
convert{T,S}(::Type{Matrix}, A::$t{T,S}) = convert(Matrix{T}, A)
function similar{T,S,Tnew}(A::$t{T,S}, ::Type{Tnew})
B = similar(A.data, Tnew)
return $t(B)
end
copy(A::$t) = $t(copy(A.data))
big(A::$t) = $t(big(A.data))
real{T<:Real}(A::$t{T}) = A
real{T<:Complex}(A::$t{T}) = (B = real(A.data); $t(B))
abs(A::$t) = $t(abs(A.data))
end
end
LowerTriangular(U::UpperTriangular) = throw(ArgumentError("cannot create a LowerTriangular matrix from an UpperTriangular input"))
UpperTriangular(U::LowerTriangular) = throw(ArgumentError("cannot create an UpperTriangular matrix from a LowerTriangular input"))
imag(A::UpperTriangular) = UpperTriangular(imag(A.data))
imag(A::LowerTriangular) = LowerTriangular(imag(A.data))
imag(A::UnitLowerTriangular) = LowerTriangular(tril!(imag(A.data),-1))
imag(A::UnitUpperTriangular) = UpperTriangular(triu!(imag(A.data),1))
full(A::AbstractTriangular) = convert(Matrix, A)
parent(A::AbstractTriangular) = A.data
# then handle all methods that requires specific handling of upper/lower and unit diagonal
function convert{Tret,T,S}(::Type{Matrix{Tret}}, A::LowerTriangular{T,S})
B = Array{Tret}(size(A, 1), size(A, 1))
copy!(B, A.data)
tril!(B)
B
end
function convert{Tret,T,S}(::Type{Matrix{Tret}}, A::UnitLowerTriangular{T,S})
B = Array{Tret}(size(A, 1), size(A, 1))
copy!(B, A.data)
tril!(B)
for i = 1:size(B,1)
B[i,i] = 1
end
B
end
function convert{Tret,T,S}(::Type{Matrix{Tret}}, A::UpperTriangular{T,S})
B = Array{Tret}(size(A, 1), size(A, 1))
copy!(B, A.data)
triu!(B)
B
end
function convert{Tret,T,S}(::Type{Matrix{Tret}}, A::UnitUpperTriangular{T,S})
B = Array{Tret}(size(A, 1), size(A, 1))
copy!(B, A.data)
triu!(B)
for i = 1:size(B,1)
B[i,i] = 1
end
B
end
function full!{T,S}(A::LowerTriangular{T,S})
B = A.data
tril!(B)
B
end
function full!{T,S}(A::UnitLowerTriangular{T,S})
B = A.data
tril!(B)
for i = 1:size(A,1)
B[i,i] = 1
end
B
end
function full!{T,S}(A::UpperTriangular{T,S})
B = A.data
triu!(B)
B
end
function full!{T,S}(A::UnitUpperTriangular{T,S})
B = A.data
triu!(B)
for i = 1:size(A,1)
B[i,i] = 1
end
B
end
getindex{T,S}(A::UnitLowerTriangular{T,S}, i::Integer, j::Integer) = i > j ? A.data[i,j] : ifelse(i == j, one(T), zero(T))
getindex{T,S}(A::LowerTriangular{T,S}, i::Integer, j::Integer) = i >= j ? A.data[i,j] : zero(A.data[j,i])
getindex{T,S}(A::UnitUpperTriangular{T,S}, i::Integer, j::Integer) = i < j ? A.data[i,j] : ifelse(i == j, one(T), zero(T))
getindex{T,S}(A::UpperTriangular{T,S}, i::Integer, j::Integer) = i <= j ? A.data[i,j] : zero(A.data[j,i])
function setindex!(A::UpperTriangular, x, i::Integer, j::Integer)
if i > j
x == 0 || throw(ArgumentError("cannot set index in the lower triangular part ($i, $j) of an UpperTriangular matrix to a nonzero value ($x)"))
else
A.data[i,j] = x
end
return A
end
function setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer)
if i > j
x == 0 || throw(ArgumentError("cannot set index in the lower triangular part ($i, $j) of a UnitUpperTriangular matrix to a nonzero value ($x)"))
elseif i == j
x == 1 || throw(ArgumentError("cannot set index on the diagonal ($i, $j) of a UnitUpperTriangular matrix to a non-unit value ($x)"))
else
A.data[i,j] = x
end
return A
end
function setindex!(A::LowerTriangular, x, i::Integer, j::Integer)
if i < j
x == 0 || throw(ArgumentError("cannot set index in the upper triangular part ($i, $j) of a LowerTriangular matrix to a nonzero value ($x)"))
else
A.data[i,j] = x
end
return A
end
function setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer)
if i < j
x == 0 || throw(ArgumentError("cannot set index in the upper triangular part ($i, $j) of a UnitLowerTriangular matrix to a nonzero value ($x)"))
elseif i == j
x == 1 || throw(ArgumentError("cannot set diagonal index ($i, $j) of a UnitLowerTriangular matrix to a non-unit value ($x)"))
else
A.data[i,j] = x
end
return A
end
## structured matrix methods ##
function Base.replace_in_print_matrix(A::UpperTriangular,i::Integer,j::Integer,s::AbstractString)
i<=j ? s : Base.replace_with_centered_mark(s)
end
function Base.replace_in_print_matrix(A::LowerTriangular,i::Integer,j::Integer,s::AbstractString)
i>=j ? s : Base.replace_with_centered_mark(s)
end
istril(A::LowerTriangular) = true
istril(A::UnitLowerTriangular) = true
istriu(A::UpperTriangular) = true
istriu(A::UnitUpperTriangular) = true
function tril!(A::UpperTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k < 0
fill!(A.data,0)
return A
elseif k == 0
for j in 1:n, i in 1:j-1
A.data[i,j] = 0
end
return A
else
return UpperTriangular(tril!(A.data,k))
end
end
triu!(A::UpperTriangular,k::Integer=0) = UpperTriangular(triu!(A.data,k))
function tril!(A::UnitUpperTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k < 0
fill!(A.data,0)
return UpperTriangular(A.data)
elseif k == 0
fill!(A.data,0)
for i in diagind(A)
A.data[i] = one(eltype(A))
end
return UpperTriangular(A.data)
else
for i in diagind(A)
A.data[i] = one(eltype(A))
end
return UpperTriangular(tril!(A.data,k))
end
end
function triu!(A::UnitUpperTriangular,k::Integer=0)
for i in diagind(A)
A.data[i] = one(eltype(A))
end
return triu!(UpperTriangular(A.data),k)
end
function triu!(A::LowerTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k > 0
fill!(A.data,0)
return A
elseif k == 0
for j in 1:n, i in j+1:n
A.data[i,j] = 0
end
return A
else
return LowerTriangular(triu!(A.data,k))
end
end
tril!(A::LowerTriangular,k::Integer=0) = LowerTriangular(tril!(A.data,k))
function triu!(A::UnitLowerTriangular,k::Integer=0)
n = size(A,1)
if abs(k) > n
throw(ArgumentError("requested diagonal, $k, out of bounds in matrix of size ($n,$n)"))
elseif k > 0
fill!(A.data,0)
return LowerTriangular(A.data)
elseif k == 0
fill!(A.data,0)
for i in diagind(A)
A.data[i] = one(eltype(A))
end
return LowerTriangular(A.data)
else
for i in diagind(A)
A.data[i] = one(eltype(A))
end
return LowerTriangular(triu!(A.data,k))
end
end
function tril!(A::UnitLowerTriangular,k::Integer=0)
for i in diagind(A)
A.data[i] = one(eltype(A))
end
return tril!(LowerTriangular(A.data),k)
end
transpose(A::LowerTriangular) = UpperTriangular(transpose(A.data))
transpose(A::UnitLowerTriangular) = UnitUpperTriangular(transpose(A.data))
transpose(A::UpperTriangular) = LowerTriangular(transpose(A.data))
transpose(A::UnitUpperTriangular) = UnitLowerTriangular(transpose(A.data))
ctranspose(A::LowerTriangular) = UpperTriangular(ctranspose(A.data))
ctranspose(A::UnitLowerTriangular) = UnitUpperTriangular(ctranspose(A.data))
ctranspose(A::UpperTriangular) = LowerTriangular(ctranspose(A.data))
ctranspose(A::UnitUpperTriangular) = UnitLowerTriangular(ctranspose(A.data))
transpose!(A::LowerTriangular) = UpperTriangular(copytri!(A.data, 'L'))
transpose!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L'))
transpose!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U'))
transpose!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U'))
ctranspose!(A::LowerTriangular) = UpperTriangular(copytri!(A.data, 'L' , true))
ctranspose!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L' , true))
ctranspose!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U' , true))
ctranspose!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U' , true))
diag(A::LowerTriangular) = diag(A.data)
diag(A::UnitLowerTriangular) = ones(eltype(A), size(A,1))
diag(A::UpperTriangular) = diag(A.data)
diag(A::UnitUpperTriangular) = ones(eltype(A), size(A,1))
# Unary operations
-(A::LowerTriangular) = LowerTriangular(-A.data)
-(A::UpperTriangular) = UpperTriangular(-A.data)
function -(A::UnitLowerTriangular)
Anew = -A.data
for i = 1:size(A, 1)
Anew[i, i] = -1
end
LowerTriangular(Anew)
end
function -(A::UnitUpperTriangular)
Anew = -A.data
for i = 1:size(A, 1)
Anew[i, i] = -1
end
UpperTriangular(Anew)
end
# copy and scale
function copy!{T<:Union{UpperTriangular, UnitUpperTriangular}}(A::T, B::T)
n = size(B,1)
for j = 1:n
for i = 1:(isa(B, UnitUpperTriangular)?j-1:j)
@inbounds A[i,j] = B[i,j]
end
end
return A
end
function copy!{T<:Union{LowerTriangular, UnitLowerTriangular}}(A::T, B::T)
n = size(B,1)
for j = 1:n
for i = (isa(B, UnitLowerTriangular)?j+1:j):n
@inbounds A[i,j] = B[i,j]
end
end
return A
end
function scale!{T<:Union{UpperTriangular, UnitUpperTriangular}}(A::UpperTriangular, B::T, c::Number)
n = checksquare(B)
for j = 1:n
if isa(B, UnitUpperTriangular)
@inbounds A[j,j] = c
end
for i = 1:(isa(B, UnitUpperTriangular)?j-1:j)
@inbounds A[i,j] = c * B[i,j]
end
end
return A
end
function scale!{T<:Union{LowerTriangular, UnitLowerTriangular}}(A::LowerTriangular, B::T, c::Number)
n = checksquare(B)
for j = 1:n
if isa(B, UnitLowerTriangular)
@inbounds A[j,j] = c
end
for i = (isa(B, UnitLowerTriangular)?j+1:j):n
@inbounds A[i,j] = c * B[i,j]
end
end
return A
end
scale!(A::Union{UpperTriangular,LowerTriangular},c::Number) = scale!(A,A,c)
scale!(c::Number, A::Union{UpperTriangular,LowerTriangular}) = scale!(A,c)
# Binary operations
+(A::UpperTriangular, B::UpperTriangular) = UpperTriangular(A.data + B.data)
+(A::LowerTriangular, B::LowerTriangular) = LowerTriangular(A.data + B.data)
+(A::UpperTriangular, B::UnitUpperTriangular) = UpperTriangular(A.data + triu(B.data, 1) + I)
+(A::LowerTriangular, B::UnitLowerTriangular) = LowerTriangular(A.data + tril(B.data, -1) + I)
+(A::UnitUpperTriangular, B::UpperTriangular) = UpperTriangular(triu(A.data, 1) + B.data + I)
+(A::UnitLowerTriangular, B::LowerTriangular) = LowerTriangular(tril(A.data, -1) + B.data + I)
+(A::UnitUpperTriangular, B::UnitUpperTriangular) = UpperTriangular(triu(A.data, 1) + triu(B.data, 1) + 2I)
+(A::UnitLowerTriangular, B::UnitLowerTriangular) = LowerTriangular(tril(A.data, -1) + tril(B.data, -1) + 2I)
+(A::AbstractTriangular, B::AbstractTriangular) = full(A) + full(B)
-(A::UpperTriangular, B::UpperTriangular) = UpperTriangular(A.data - B.data)
-(A::LowerTriangular, B::LowerTriangular) = LowerTriangular(A.data - B.data)
-(A::UpperTriangular, B::UnitUpperTriangular) = UpperTriangular(A.data - triu(B.data, 1) - I)
-(A::LowerTriangular, B::UnitLowerTriangular) = LowerTriangular(A.data - tril(B.data, -1) - I)
-(A::UnitUpperTriangular, B::UpperTriangular) = UpperTriangular(triu(A.data, 1) - B.data + I)
-(A::UnitLowerTriangular, B::LowerTriangular) = LowerTriangular(tril(A.data, -1) - B.data + I)
-(A::UnitUpperTriangular, B::UnitUpperTriangular) = UpperTriangular(triu(A.data, 1) - triu(B.data, 1))
-(A::UnitLowerTriangular, B::UnitLowerTriangular) = LowerTriangular(tril(A.data, -1) - tril(B.data, -1))
-(A::AbstractTriangular, B::AbstractTriangular) = full(A) - full(B)
######################
# BlasFloat routines #
######################
A_mul_B!(A::Tridiagonal, B::AbstractTriangular) = A*full!(B)
A_mul_B!(C::AbstractMatrix, A::AbstractTriangular, B::Tridiagonal) = A_mul_B!(C, full(A), B)
A_mul_B!(C::AbstractMatrix, A::Tridiagonal, B::AbstractTriangular) = A_mul_B!(C, A, full(B))
A_mul_B!(C::AbstractVector, A::AbstractTriangular, B::AbstractVector) = A_mul_B!(A, copy!(C, B))
A_mul_B!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractVecOrMat) = A_mul_B!(A, copy!(C, B))
A_mul_B!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVecOrMat) = A_mul_B!(A, copy!(C, B))
A_mul_Bt!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVecOrMat) = A_mul_B!(A, transpose!(C, B))
A_mul_Bc!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractVecOrMat) = A_mul_B!(A, ctranspose!(C, B))
A_mul_Bc!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVecOrMat) = A_mul_B!(A, ctranspose!(C, B))
for (t, uploc, isunitc) in ((:LowerTriangular, 'L', 'N'),
(:UnitLowerTriangular, 'L', 'U'),
(:UpperTriangular, 'U', 'N'),
(:UnitUpperTriangular, 'U', 'U'))
@eval begin
# Vector multiplication
A_mul_B!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, b::StridedVector{T}) = BLAS.trmv!($uploc, 'N', $isunitc, A.data, b)
At_mul_B!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, b::StridedVector{T}) = BLAS.trmv!($uploc, 'T', $isunitc, A.data, b)
Ac_mul_B!{T<:BlasReal,S<:StridedMatrix}(A::$t{T,S}, b::StridedVector{T}) = BLAS.trmv!($uploc, 'T', $isunitc, A.data, b)
Ac_mul_B!{T<:BlasComplex,S<:StridedMatrix}(A::$t{T,S}, b::StridedVector{T}) = BLAS.trmv!($uploc, 'C', $isunitc, A.data, b)
# Matrix multiplication
A_mul_B!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, B::StridedMatrix{T}) = BLAS.trmm!('L', $uploc, 'N', $isunitc, one(T), A.data, B)
A_mul_B!{T<:BlasFloat,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trmm!('R', $uploc, 'N', $isunitc, one(T), B.data, A)
At_mul_B!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, B::StridedMatrix{T}) = BLAS.trmm!('L', $uploc, 'T', $isunitc, one(T), A.data, B)
Ac_mul_B!{T<:BlasComplex,S<:StridedMatrix}(A::$t{T,S}, B::StridedMatrix{T}) = BLAS.trmm!('L', $uploc, 'C', $isunitc, one(T), A.data, B)
Ac_mul_B!{T<:BlasReal,S<:StridedMatrix}(A::$t{T,S}, B::StridedMatrix{T}) = BLAS.trmm!('L', $uploc, 'T', $isunitc, one(T), A.data, B)
A_mul_Bt!{T<:BlasFloat,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trmm!('R', $uploc, 'T', $isunitc, one(T), B.data, A)
A_mul_Bc!{T<:BlasComplex,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trmm!('R', $uploc, 'C', $isunitc, one(T), B.data, A)
A_mul_Bc!{T<:BlasReal,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trmm!('R', $uploc, 'T', $isunitc, one(T), B.data, A)
# Left division
A_ldiv_B!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, B::StridedVecOrMat{T}) = LAPACK.trtrs!($uploc, 'N', $isunitc, A.data, B)
At_ldiv_B!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, B::StridedVecOrMat{T}) = LAPACK.trtrs!($uploc, 'T', $isunitc, A.data, B)
Ac_ldiv_B!{T<:BlasReal,S<:StridedMatrix}(A::$t{T,S}, B::StridedVecOrMat{T}) = LAPACK.trtrs!($uploc, 'T', $isunitc, A.data, B)
Ac_ldiv_B!{T<:BlasComplex,S<:StridedMatrix}(A::$t{T,S}, B::StridedVecOrMat{T}) = LAPACK.trtrs!($uploc, 'C', $isunitc, A.data, B)
# Right division
A_rdiv_B!{T<:BlasFloat,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trsm!('R', $uploc, 'N', $isunitc, one(T), B.data, A)
A_rdiv_Bt!{T<:BlasFloat,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trsm!('R', $uploc, 'T', $isunitc, one(T), B.data, A)
A_rdiv_Bc!{T<:BlasReal,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trsm!('R', $uploc, 'T', $isunitc, one(T), B.data, A)
A_rdiv_Bc!{T<:BlasComplex,S<:StridedMatrix}(A::StridedMatrix{T}, B::$t{T,S}) = BLAS.trsm!('R', $uploc, 'C', $isunitc, one(T), B.data, A)
# Matrix inverse
inv!{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}) = $t{T,S}(LAPACK.trtri!($uploc, $isunitc, A.data))
# Error bounds for triangular solve
errorbounds{T<:BlasFloat,S<:StridedMatrix}(A::$t{T,S}, X::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = LAPACK.trrfs!($uploc, 'N', $isunitc, A.data, B, X)
# Condition numbers
function cond{T<:BlasFloat,S}(A::$t{T,S}, p::Real=2)
checksquare(A)
if p == 1
return inv(LAPACK.trcon!('O', $uploc, $isunitc, A.data))
elseif p == Inf
return inv(LAPACK.trcon!('I', $uploc, $isunitc, A.data))
else #use fallback
return cond(full(A), p)
end
end
end
end
function inv{T}(A::LowerTriangular{T})
S = typeof((zero(T)*one(T) + zero(T))/one(T))
LowerTriangular(A_ldiv_B!(convert(AbstractArray{S}, A), eye(S, size(A, 1))))
end
function inv{T}(A::UpperTriangular{T})
S = typeof((zero(T)*one(T) + zero(T))/one(T))
UpperTriangular(A_ldiv_B!(convert(AbstractArray{S}, A), eye(S, size(A, 1))))
end
inv{T}(A::UnitUpperTriangular{T}) = UnitUpperTriangular(A_ldiv_B!(A, eye(T, size(A, 1))))
inv{T}(A::UnitLowerTriangular{T}) = UnitLowerTriangular(A_ldiv_B!(A, eye(T, size(A, 1))))
errorbounds{T<:Union{BigFloat, Complex{BigFloat}},S<:StridedMatrix}(A::AbstractTriangular{T,S}, X::StridedVecOrMat{T}, B::StridedVecOrMat{T}) = error("not implemented yet! Please submit a pull request.")
function errorbounds{TA<:Number,S<:StridedMatrix,TX<:Number,TB<:Number}(A::AbstractTriangular{TA,S}, X::StridedVecOrMat{TX}, B::StridedVecOrMat{TB})
TAXB = promote_type(TA, TB, TX, Float32)
errorbounds(convert(AbstractMatrix{TAXB}, A), convert(AbstractArray{TAXB}, X), convert(AbstractArray{TAXB}, B))
end
# Eigensystems
## Notice that trecv works for quasi-triangular matrices and therefore the lower sub diagonal must be zeroed before calling the subroutine
eigvecs{T<:BlasFloat,S<:StridedMatrix}(A::UpperTriangular{T,S}) = LAPACK.trevc!('R', 'A', BlasInt[], triu!(A.data))
eigvecs{T<:BlasFloat,S<:StridedMatrix}(A::UnitUpperTriangular{T,S}) = (for i = 1:size(A, 1); A.data[i,i] = 1;end;LAPACK.trevc!('R', 'A', BlasInt[], triu!(A.data)))
eigvecs{T<:BlasFloat,S<:StridedMatrix}(A::LowerTriangular{T,S}) = LAPACK.trevc!('L', 'A', BlasInt[], tril!(A.data)')
eigvecs{T<:BlasFloat,S<:StridedMatrix}(A::UnitLowerTriangular{T,S}) = (for i = 1:size(A, 1); A.data[i,i] = 1;end;LAPACK.trevc!('L', 'A', BlasInt[], tril!(A.data)'))
####################
# Generic routines #
####################
for (t, unitt) in ((UpperTriangular, UnitUpperTriangular),
(LowerTriangular, UnitLowerTriangular))
@eval begin
(*)(A::$t, x::Number) = $t(A.data*x)
function (*)(A::$unitt, x::Number)
B = A.data*x
for i = 1:size(A, 1)
B[i,i] = x
end
$t(B)
end
(*)(x::Number, A::$t) = $t(x*A.data)
function (*)(x::Number, A::$unitt)
B = x*A.data
for i = 1:size(A, 1)
B[i,i] = x
end
$t(B)
end
(/)(A::$t, x::Number) = $t(A.data/x)
function (/)(A::$unitt, x::Number)
B = A.data/x
invx = inv(x)
for i = 1:size(A, 1)
B[i,i] = invx
end
$t(B)
end
(\)(x::Number, A::$t) = $t(x\A.data)
function (\)(x::Number, A::$unitt)
B = x\A.data
invx = inv(x)
for i = 1:size(A, 1)
B[i,i] = invx
end
$t(B)
end
end
end
## Generic triangular multiplication
function A_mul_B!(A::UpperTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = 1:m
Bij = A.data[i,i]*B[i,j]
for k = i + 1:m
Bij += A.data[i,k]*B[k,j]
end
B[i,j] = Bij
end
end
B
end
function A_mul_B!(A::UnitUpperTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = 1:m
Bij = B[i,j]
for k = i + 1:m
Bij += A.data[i,k]*B[k,j]
end
B[i,j] = Bij
end
end
B
end
function A_mul_B!(A::LowerTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = m:-1:1
Bij = A.data[i,i]*B[i,j]
for k = 1:i - 1
Bij += A.data[i,k]*B[k,j]
end
B[i,j] = Bij
end
end
B
end
function A_mul_B!(A::UnitLowerTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = m:-1:1
Bij = B[i,j]
for k = 1:i - 1
Bij += A.data[i,k]*B[k,j]
end
B[i,j] = Bij
end
end
B
end
function Ac_mul_B!(A::UpperTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = m:-1:1
Bij = A.data[i,i]'B[i,j]
for k = 1:i - 1
Bij += A.data[k,i]'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function Ac_mul_B!(A::UnitUpperTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = m:-1:1
Bij = B[i,j]
for k = 1:i - 1
Bij += A.data[k,i]'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function Ac_mul_B!(A::LowerTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = 1:m
Bij = A.data[i,i]'B[i,j]
for k = i + 1:m
Bij += A.data[k,i]'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function Ac_mul_B!(A::UnitLowerTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = 1:m
Bij = B[i,j]
for k = i + 1:m
Bij += A.data[k,i]'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function At_mul_B!(A::UpperTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = m:-1:1
Bij = A.data[i,i].'B[i,j]
for k = 1:i - 1
Bij += A.data[k,i].'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function At_mul_B!(A::UnitUpperTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = m:-1:1
Bij = B[i,j]
for k = 1:i - 1
Bij += A.data[k,i].'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function At_mul_B!(A::LowerTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = 1:m
Bij = A.data[i,i].'B[i,j]
for k = i + 1:m
Bij += A.data[k,i].'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function At_mul_B!(A::UnitLowerTriangular, B::StridedVecOrMat)
m, n = size(B, 1), size(B, 2)
if m != size(A, 1)
throw(DimensionMismatch("right hand side B needs first dimension of size $(size(A,1)), has size $m"))
end
for j = 1:n
for i = 1:m
Bij = B[i,j]
for k = i + 1:m
Bij += A.data[k,i].'B[k,j]
end
B[i,j] = Bij
end
end
B
end
function A_mul_B!(A::StridedMatrix, B::UpperTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = n:-1:1
Aij = A[i,j]*B[j,j]
for k = 1:j - 1
Aij += A[i,k]*B.data[k,j]
end
A[i,j] = Aij
end
end
A
end
function A_mul_B!(A::StridedMatrix, B::UnitUpperTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = n:-1:1
Aij = A[i,j]
for k = 1:j - 1
Aij += A[i,k]*B.data[k,j]
end
A[i,j] = Aij
end
end
A
end
function A_mul_B!(A::StridedMatrix, B::LowerTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = 1:n
Aij = A[i,j]*B[j,j]
for k = j + 1:n
Aij += A[i,k]*B.data[k,j]
end
A[i,j] = Aij
end
end
A
end
function A_mul_B!(A::StridedMatrix, B::UnitLowerTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = 1:n
Aij = A[i,j]
for k = j + 1:n
Aij += A[i,k]*B.data[k,j]
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bc!(A::StridedMatrix, B::UpperTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = 1:n
Aij = A[i,j]*B.data[j,j]'
for k = j + 1:n
Aij += A[i,k]*B.data[j,k]'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bc!(A::StridedMatrix, B::UnitUpperTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = 1:n
Aij = A[i,j]
for k = j + 1:n
Aij += A[i,k]*B.data[j,k]'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bc!(A::StridedMatrix, B::LowerTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = n:-1:1
Aij = A[i,j]*B.data[j,j]'
for k = 1:j - 1
Aij += A[i,k]*B.data[j,k]'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bc!(A::StridedMatrix, B::UnitLowerTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = n:-1:1
Aij = A[i,j]
for k = 1:j - 1
Aij += A[i,k]*B.data[j,k]'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bt!(A::StridedMatrix, B::UpperTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = 1:n
Aij = A[i,j]*B.data[j,j].'
for k = j + 1:n
Aij += A[i,k]*B.data[j,k].'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bt!(A::StridedMatrix, B::UnitUpperTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = 1:n
Aij = A[i,j]
for k = j + 1:n
Aij += A[i,k]*B.data[j,k].'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bt!(A::StridedMatrix, B::LowerTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = n:-1:1
Aij = A[i,j]*B.data[j,j].'
for k = 1:j - 1
Aij += A[i,k]*B.data[j,k].'
end
A[i,j] = Aij
end
end
A
end
function A_mul_Bt!(A::StridedMatrix, B::UnitLowerTriangular)
m, n = size(A)
if size(B, 1) != n
throw(DimensionMismatch("right hand side B needs first dimension of size $n, has size $(size(B,1))"))
end
for i = 1:m
for j = n:-1:1
Aij = A[i,j]
for k = 1:j - 1
Aij += A[i,k]*B.data[j,k].'
end
A[i,j] = Aij
end
end
A
end
#Generic solver using naive substitution
# manually hoisting x[j] significantly improves performance as of Dec 2015
# manually eliding bounds checking significantly improves performance as of Dec 2015
# directly indexing A.data rather than A significantly improves performance as of Dec 2015
# replacing repeated references to A.data with [Adata = A.data and references to Adata] does not significantly impact performance as of Dec 2015
# replacing repeated references to A.data[j,j] with [Ajj = A.data[j,j] and references to Ajj] does not significantly impact performance as of Dec 2015
function naivesub!(A::UpperTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
@inbounds for j in n:-1:1
A.data[j,j] == zero(A.data[j,j]) && throw(SingularException(j))
xj = x[j] = A.data[j,j] \ b[j]
for i in j-1:-1:1 # counterintuitively 1:j-1 performs slightly better
b[i] -= A.data[i,j] * xj
end
end
x
end
function naivesub!(A::UnitUpperTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
@inbounds for j in n:-1:1
xj = x[j] = b[j]
for i in j-1:-1:1 # counterintuitively 1:j-1 performs slightly better
b[i] -= A.data[i,j] * xj
end
end
x
end
function naivesub!(A::LowerTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
@inbounds for j in 1:n
A.data[j,j] == zero(A.data[j,j]) && throw(SingularException(j))
xj = x[j] = A.data[j,j] \ b[j]
for i in j+1:n
b[i] -= A.data[i,j] * xj
end
end
x
end
function naivesub!(A::UnitLowerTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 2)
if !(n == length(b) == length(x))
throw(DimensionMismatch("second dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
@inbounds for j in 1:n
xj = x[j] = b[j]
for i in j+1:n
b[i] -= A.data[i,j] * xj
end
end
x
end
# in the following transpose and conjugate transpose naive substitution variants,
# accumulating in z rather than b[j] significantly improves performance as of Dec 2015
function At_ldiv_B!(A::LowerTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 1)
if !(n == length(b) == length(x))
throw(DimensionMismatch("first dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
@inbounds for j in n:-1:1
z = b[j]
for i in n:-1:j+1
z -= A.data[i,j] * x[i]
end
A.data[j,j] == zero(A.data[j,j]) && throw(SingularException(j))
x[j] = A.data[j,j] \ z
end
x
end
function At_ldiv_B!(A::UnitLowerTriangular, b::AbstractVector, x::AbstractVector = b)
n = size(A, 1)
if !(n == length(b) == length(x))
throw(DimensionMismatch("first dimension of left hand side A, $n, length of output x, $(length(x)), and length of right hand side b, $(length(b)), must be equal"))
end
@inbounds for j in n:-1:1
z = b[j]