-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
triangular.jl
2888 lines (2709 loc) · 99.7 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: https://julialang.org/license
## Triangular
# could be renamed to Triangular when that name has been fully deprecated
"""
AbstractTriangular
Supertype of triangular matrix types such as [`LowerTriangular`](@ref), [`UpperTriangular`](@ref),
[`UnitLowerTriangular`](@ref) and [`UnitUpperTriangular`](@ref).
"""
abstract type AbstractTriangular{T} <: AbstractMatrix{T} end
# 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
struct $t{T,S<:AbstractMatrix{T}} <: AbstractTriangular{T}
data::S
function $t{T,S}(data) where {T,S<:AbstractMatrix{T}}
require_one_based_indexing(data)
checksquare(data)
new{T,S}(data)
end
end
$t(A::$t) = A
$t{T}(A::$t{T}) where {T} = A
$t(A::AbstractMatrix) = $t{eltype(A), typeof(A)}(A)
$t{T}(A::AbstractMatrix) where {T} = $t(convert(AbstractMatrix{T}, A))
$t{T}(A::$t) where {T} = $t(convert(AbstractMatrix{T}, A.data))
AbstractMatrix{T}(A::$t) where {T} = $t{T}(A)
AbstractMatrix{T}(A::$t{T}) where {T} = copy(A)
size(A::$t) = size(A.data)
axes(A::$t) = axes(A.data)
# For A<:AbstractTriangular, similar(A[, neweltype]) should yield a matrix with the same
# triangular type and underlying storage type as A. The following method covers these cases.
similar(A::$t, ::Type{T}) where {T} = $t(similar(parent(A), T))
# On the other hand, similar(A, [neweltype,] shape...) should yield a matrix of the underlying
# storage type of A (not wrapped in a triangular type). The following method covers these cases.
similar(A::$t, ::Type{T}, dims::Dims{N}) where {T,N} = similar(parent(A), T, dims)
copy(A::$t) = $t(copy(A.data))
Base.unaliascopy(A::$t) = $t(Base.unaliascopy(A.data))
real(A::$t{<:Real}) = A
real(A::$t{<:Complex}) = (B = real(A.data); $t(B))
real(A::$t{<:Complex, <:StridedMaybeAdjOrTransMat}) = $t(real.(A))
end
end
"""
LowerTriangular(A::AbstractMatrix)
Construct a `LowerTriangular` view of the matrix `A`.
# Examples
```jldoctest
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
3×3 Matrix{Float64}:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
julia> LowerTriangular(A)
3×3 LowerTriangular{Float64, Matrix{Float64}}:
1.0 ⋅ ⋅
4.0 5.0 ⋅
7.0 8.0 9.0
```
"""
LowerTriangular
"""
UpperTriangular(A::AbstractMatrix)
Construct an `UpperTriangular` view of the matrix `A`.
# Examples
```jldoctest
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
3×3 Matrix{Float64}:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
julia> UpperTriangular(A)
3×3 UpperTriangular{Float64, Matrix{Float64}}:
1.0 2.0 3.0
⋅ 5.0 6.0
⋅ ⋅ 9.0
```
"""
UpperTriangular
"""
UnitLowerTriangular(A::AbstractMatrix)
Construct a `UnitLowerTriangular` view of the matrix `A`.
Such a view has the [`oneunit`](@ref) of the [`eltype`](@ref)
of `A` on its diagonal.
# Examples
```jldoctest
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
3×3 Matrix{Float64}:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
julia> UnitLowerTriangular(A)
3×3 UnitLowerTriangular{Float64, Matrix{Float64}}:
1.0 ⋅ ⋅
4.0 1.0 ⋅
7.0 8.0 1.0
```
"""
UnitLowerTriangular
"""
UnitUpperTriangular(A::AbstractMatrix)
Construct an `UnitUpperTriangular` view of the matrix `A`.
Such a view has the [`oneunit`](@ref) of the [`eltype`](@ref)
of `A` on its diagonal.
# Examples
```jldoctest
julia> A = [1.0 2.0 3.0; 4.0 5.0 6.0; 7.0 8.0 9.0]
3×3 Matrix{Float64}:
1.0 2.0 3.0
4.0 5.0 6.0
7.0 8.0 9.0
julia> UnitUpperTriangular(A)
3×3 UnitUpperTriangular{Float64, Matrix{Float64}}:
1.0 2.0 3.0
⋅ 1.0 6.0
⋅ ⋅ 1.0
```
"""
UnitUpperTriangular
const UpperOrUnitUpperTriangular{T,S} = Union{UpperTriangular{T,S}, UnitUpperTriangular{T,S}}
const LowerOrUnitLowerTriangular{T,S} = Union{LowerTriangular{T,S}, UnitLowerTriangular{T,S}}
const UpperOrLowerTriangular{T,S} = Union{UpperOrUnitUpperTriangular{T,S}, LowerOrUnitLowerTriangular{T,S}}
uppertriangular(M) = UpperTriangular(M)
lowertriangular(M) = LowerTriangular(M)
uppertriangular(U::UpperOrUnitUpperTriangular) = U
lowertriangular(U::LowerOrUnitLowerTriangular) = U
Base.dataids(A::UpperOrLowerTriangular) = Base.dataids(A.data)
imag(A::UpperTriangular) = UpperTriangular(imag(A.data))
imag(A::LowerTriangular) = LowerTriangular(imag(A.data))
imag(A::UpperTriangular{<:Any,<:StridedMaybeAdjOrTransMat}) = imag.(A)
imag(A::LowerTriangular{<:Any,<:StridedMaybeAdjOrTransMat}) = imag.(A)
function imag(A::UnitLowerTriangular)
L = LowerTriangular(A.data)
Lim = similar(L) # must be mutable to set diagonals to zero
Lim .= imag.(L)
for i in 1:size(Lim,1)
Lim[i,i] = zero(Lim[i,i])
end
return Lim
end
function imag(A::UnitUpperTriangular)
U = UpperTriangular(A.data)
Uim = similar(U) # must be mutable to set diagonals to zero
Uim .= imag.(U)
for i in 1:size(Uim,1)
Uim[i,i] = zero(Uim[i,i])
end
return Uim
end
parent(A::UpperOrLowerTriangular) = A.data
# For strided matrices, we may only loop over the filled triangle
copy(A::UpperOrLowerTriangular{<:Any, <:StridedMaybeAdjOrTransMat}) = copyto!(similar(A), A)
# then handle all methods that requires specific handling of upper/lower and unit diagonal
function full!(A::LowerTriangular)
B = A.data
tril!(B)
B
end
function full!(A::UnitLowerTriangular)
B = A.data
tril!(B)
for i = 1:size(A,1)
B[i,i] = oneunit(eltype(B))
end
B
end
function full!(A::UpperTriangular)
B = A.data
triu!(B)
B
end
function full!(A::UnitUpperTriangular)
B = A.data
triu!(B)
for i = 1:size(A,1)
B[i,i] = oneunit(eltype(B))
end
B
end
Base.isassigned(A::UnitLowerTriangular, i::Int, j::Int) =
i > j ? isassigned(A.data, i, j) : true
Base.isassigned(A::LowerTriangular, i::Int, j::Int) =
i >= j ? isassigned(A.data, i, j) : true
Base.isassigned(A::UnitUpperTriangular, i::Int, j::Int) =
i < j ? isassigned(A.data, i, j) : true
Base.isassigned(A::UpperTriangular, i::Int, j::Int) =
i <= j ? isassigned(A.data, i, j) : true
Base.isstored(A::UnitLowerTriangular, i::Int, j::Int) =
i > j ? Base.isstored(A.data, i, j) : false
Base.isstored(A::LowerTriangular, i::Int, j::Int) =
i >= j ? Base.isstored(A.data, i, j) : false
Base.isstored(A::UnitUpperTriangular, i::Int, j::Int) =
i < j ? Base.isstored(A.data, i, j) : false
Base.isstored(A::UpperTriangular, i::Int, j::Int) =
i <= j ? Base.isstored(A.data, i, j) : false
@propagate_inbounds getindex(A::UnitLowerTriangular{T}, i::Int, j::Int) where {T} =
i > j ? A.data[i,j] : ifelse(i == j, oneunit(T), zero(T))
@propagate_inbounds getindex(A::LowerTriangular, i::Int, j::Int) =
i >= j ? A.data[i,j] : _zero(A.data,j,i)
@propagate_inbounds getindex(A::UnitUpperTriangular{T}, i::Int, j::Int) where {T} =
i < j ? A.data[i,j] : ifelse(i == j, oneunit(T), zero(T))
@propagate_inbounds getindex(A::UpperTriangular, i::Int, j::Int) =
i <= j ? A.data[i,j] : _zero(A.data,j,i)
_zero_triangular_half_str(::Type{<:UpperOrUnitUpperTriangular}) = "lower"
_zero_triangular_half_str(::Type{<:LowerOrUnitLowerTriangular}) = "upper"
@noinline function throw_nonzeroerror(T, @nospecialize(x), i, j)
Ts = _zero_triangular_half_str(T)
Tn = nameof(T)
throw(ArgumentError(
lazy"cannot set index in the $Ts triangular part ($i, $j) of an $Tn matrix to a nonzero value ($x)"))
end
@noinline function throw_nononeerror(T, @nospecialize(x), i, j)
Tn = nameof(T)
throw(ArgumentError(
lazy"cannot set index on the diagonal ($i, $j) of an $Tn matrix to a non-unit value ($x)"))
end
@propagate_inbounds function setindex!(A::UpperTriangular, x, i::Integer, j::Integer)
if i > j
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
else
A.data[i,j] = x
end
return A
end
@propagate_inbounds function setindex!(A::UnitUpperTriangular, x, i::Integer, j::Integer)
if i > j
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
elseif i == j
x == oneunit(x) || throw_nononeerror(typeof(A), x, i, j)
else
A.data[i,j] = x
end
return A
end
@propagate_inbounds function setindex!(A::LowerTriangular, x, i::Integer, j::Integer)
if i < j
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
else
A.data[i,j] = x
end
return A
end
@propagate_inbounds function setindex!(A::UnitLowerTriangular, x, i::Integer, j::Integer)
if i < j
iszero(x) || throw_nonzeroerror(typeof(A), x, i, j)
elseif i == j
x == oneunit(x) || throw_nononeerror(typeof(A), x, i, j)
else
A.data[i,j] = x
end
return A
end
@noinline function throw_setindex_structuralzero_error(T, @nospecialize(x))
Ts = _zero_triangular_half_str(T)
Tn = nameof(T)
throw(ArgumentError(
lazy"cannot set indices in the $Ts triangular part of an $Tn matrix to a nonzero value ($x)"))
end
@inline function fill!(A::UpperTriangular, x)
iszero(x) || throw_setindex_structuralzero_error(typeof(A), x)
for col in axes(A,2), row in firstindex(A,1):col
@inbounds A.data[row, col] = x
end
A
end
@inline function fill!(A::LowerTriangular, x)
iszero(x) || throw_setindex_structuralzero_error(typeof(A), x)
for col in axes(A,2), row in col:lastindex(A,1)
@inbounds A.data[row, col] = x
end
A
end
Base._reverse(A::UpperOrUnitUpperTriangular, dims::Integer) = reverse!(Matrix(A); dims)
Base._reverse(A::UpperTriangular, ::Colon) = LowerTriangular(reverse(A.data))
Base._reverse(A::UnitUpperTriangular, ::Colon) = UnitLowerTriangular(reverse(A.data))
Base._reverse(A::LowerOrUnitLowerTriangular, dims) = reverse!(Matrix(A); dims)
Base._reverse(A::LowerTriangular, ::Colon) = UpperTriangular(reverse(A.data))
Base._reverse(A::UnitLowerTriangular, ::Colon) = UnitUpperTriangular(reverse(A.data))
## structured matrix methods ##
function Base.replace_in_print_matrix(A::Union{UpperTriangular,UnitUpperTriangular},
i::Integer, j::Integer, s::AbstractString)
return i <= j ? s : Base.replace_with_centered_mark(s)
end
function Base.replace_in_print_matrix(A::Union{LowerTriangular,UnitLowerTriangular},
i::Integer, j::Integer, s::AbstractString)
return i >= j ? s : Base.replace_with_centered_mark(s)
end
Base.@constprop :aggressive function istril(A::Union{LowerTriangular,UnitLowerTriangular}, k::Integer=0)
k >= 0 && return true
return _istril(A, k)
end
Base.@constprop :aggressive function istriu(A::Union{UpperTriangular,UnitUpperTriangular}, k::Integer=0)
k <= 0 && return true
return _istriu(A, k)
end
istril(A::Adjoint, k::Integer=0) = istriu(A.parent, -k)
istril(A::Transpose, k::Integer=0) = istriu(A.parent, -k)
istriu(A::Adjoint, k::Integer=0) = istril(A.parent, -k)
istriu(A::Transpose, k::Integer=0) = istril(A.parent, -k)
function tril!(A::UpperTriangular{T}, k::Integer=0) where {T}
n = size(A,1)
if k < 0
fill!(A.data, zero(T))
return A
elseif k == 0
for j in 1:n, i in 1:j-1
A.data[i,j] = zero(T)
end
return A
else
return UpperTriangular(tril!(A.data,k))
end
end
function triu!(A::UpperTriangular, k::Integer=0)
n = size(A,1)
if k > 0
for j in 1:n, i in max(1,j-k+1):j
A.data[i,j] = zero(eltype(A))
end
end
return A
end
function tril!(A::UnitUpperTriangular{T}, k::Integer=0) where {T}
n = size(A,1)
if k < 0
fill!(A.data, zero(T))
return UpperTriangular(A.data)
elseif k == 0
fill!(A.data, zero(T))
for i in diagind(A)
A.data[i] = oneunit(T)
end
return UpperTriangular(A.data)
else
for i in diagind(A)
A.data[i] = oneunit(T)
end
return UpperTriangular(tril!(A.data,k))
end
end
function triu!(A::UnitUpperTriangular, k::Integer=0)
for i in diagind(A)
A.data[i] = oneunit(eltype(A))
end
return triu!(UpperTriangular(A.data), k)
end
function triu!(A::LowerTriangular{T}, k::Integer=0) where {T}
n = size(A,1)
if k > 0
fill!(A.data, zero(T))
return A
elseif k == 0
for j in 1:n, i in j+1:n
A.data[i,j] = zero(T)
end
return A
else
return LowerTriangular(triu!(A.data, k))
end
end
function tril!(A::LowerTriangular, k::Integer=0)
n = size(A,1)
if k < 0
for j in 1:n, i in j:min(j-k-1,n)
A.data[i, j] = zero(eltype(A))
end
end
A
end
function triu!(A::UnitLowerTriangular{T}, k::Integer=0) where T
n = size(A,1)
if k > 0
fill!(A.data, zero(T))
return LowerTriangular(A.data)
elseif k == 0
fill!(A.data, zero(T))
for i in diagind(A)
A.data[i] = oneunit(T)
end
return LowerTriangular(A.data)
else
for i in diagind(A)
A.data[i] = oneunit(T)
end
return LowerTriangular(triu!(A.data, k))
end
end
function tril!(A::UnitLowerTriangular, k::Integer=0)
for i in diagind(A)
A.data[i] = oneunit(eltype(A))
end
return tril!(LowerTriangular(A.data), k)
end
adjoint(A::LowerTriangular) = UpperTriangular(adjoint(A.data))
adjoint(A::UpperTriangular) = LowerTriangular(adjoint(A.data))
adjoint(A::UnitLowerTriangular) = UnitUpperTriangular(adjoint(A.data))
adjoint(A::UnitUpperTriangular) = UnitLowerTriangular(adjoint(A.data))
transpose(A::LowerTriangular) = UpperTriangular(transpose(A.data))
transpose(A::UpperTriangular) = LowerTriangular(transpose(A.data))
transpose(A::UnitLowerTriangular) = UnitUpperTriangular(transpose(A.data))
transpose(A::UnitUpperTriangular) = UnitLowerTriangular(transpose(A.data))
transpose!(A::LowerTriangular) = UpperTriangular(copytri!(A.data, 'L', false, true))
transpose!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L', false, false))
transpose!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U', false, true))
transpose!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U', false, false))
adjoint!(A::LowerTriangular) = UpperTriangular(copytri!(A.data, 'L' , true, true))
adjoint!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L' , true, false))
adjoint!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U' , true, true))
adjoint!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U' , true, false))
diag(A::LowerTriangular) = diag(A.data)
diag(A::UnitLowerTriangular) = fill(oneunit(eltype(A)), size(A,1))
diag(A::UpperTriangular) = diag(A.data)
diag(A::UnitUpperTriangular) = fill(oneunit(eltype(A)), size(A,1))
# Unary operations
-(A::LowerTriangular) = LowerTriangular(-A.data)
-(A::UpperTriangular) = UpperTriangular(-A.data)
function -(A::UnitLowerTriangular)
Adata = A.data
Anew = similar(Adata) # must be mutable, even if Adata is not
@. Anew = -Adata
for i = 1:size(A, 1)
Anew[i, i] = -A[i, i]
end
LowerTriangular(Anew)
end
function -(A::UnitUpperTriangular)
Adata = A.data
Anew = similar(Adata) # must be mutable, even if Adata is not
@. Anew = -Adata
for i = 1:size(A, 1)
Anew[i, i] = -A[i, i]
end
UpperTriangular(Anew)
end
# use broadcasting if the parents are strided, where we loop only over the triangular part
for TM in (:LowerTriangular, :UpperTriangular)
@eval -(A::$TM{<:Any, <:StridedMaybeAdjOrTransMat}) = broadcast(-, A)
end
tr(A::LowerTriangular) = tr(A.data)
tr(A::UnitLowerTriangular) = size(A, 1) * oneunit(eltype(A))
tr(A::UpperTriangular) = tr(A.data)
tr(A::UnitUpperTriangular) = size(A, 1) * oneunit(eltype(A))
for T in (:UpperOrUnitUpperTriangular, :LowerOrUnitLowerTriangular)
@eval @propagate_inbounds function copyto!(dest::$T, U::$T)
if axes(dest) != axes(U)
@invoke copyto!(dest::AbstractArray, U::AbstractArray)
else
_copyto!(dest, U)
end
return dest
end
end
# copy and scale
for (T, UT) in ((:UpperTriangular, :UnitUpperTriangular), (:LowerTriangular, :UnitLowerTriangular))
@eval @inline function _copyto!(A::$T, B::$T)
@boundscheck checkbounds(A, axes(B)...)
copytrito!(parent(A), parent(B), uplo_char(A))
return A
end
@eval @inline function _copyto!(A::$UT, B::$T)
for dind in diagind(A, IndexStyle(A))
if A[dind] != B[dind]
throw_nononeerror(typeof(A), B[dind], Tuple(dind)...)
end
end
_copyto!($T(parent(A)), B)
return A
end
end
@inline function _copyto!(A::UpperOrUnitUpperTriangular, B::UnitUpperTriangular)
@boundscheck checkbounds(A, axes(B)...)
n = size(B,1)
B2 = Base.unalias(A, B)
for j = 1:n
for i = 1:j-1
@inbounds parent(A)[i,j] = parent(B2)[i,j]
end
if A isa UpperTriangular # copy diagonal
@inbounds parent(A)[j,j] = B2[j,j]
end
end
return A
end
@inline function _copyto!(A::LowerOrUnitLowerTriangular, B::UnitLowerTriangular)
@boundscheck checkbounds(A, axes(B)...)
n = size(B,1)
B2 = Base.unalias(A, B)
for j = 1:n
if A isa LowerTriangular # copy diagonal
@inbounds parent(A)[j,j] = B2[j,j]
end
for i = j+1:n
@inbounds parent(A)[i,j] = parent(B2)[i,j]
end
end
return A
end
_triangularize!(::UpperOrUnitUpperTriangular) = triu!
_triangularize!(::LowerOrUnitLowerTriangular) = tril!
@propagate_inbounds function copyto!(dest::StridedMatrix, U::UpperOrLowerTriangular)
if axes(dest) != axes(U)
@invoke copyto!(dest::StridedMatrix, U::AbstractArray)
else
_copyto!(dest, U)
end
return dest
end
@propagate_inbounds function _copyto!(dest::StridedMatrix, U::UpperOrLowerTriangular)
copytrito!(dest, parent(U), U isa UpperOrUnitUpperTriangular ? 'U' : 'L')
copytrito!(dest, U, U isa UpperOrUnitUpperTriangular ? 'L' : 'U')
return dest
end
@propagate_inbounds function _copyto!(dest::StridedMatrix, U::UpperOrLowerTriangular{<:Any, <:StridedMatrix})
U2 = Base.unalias(dest, U)
copyto_unaliased!(dest, U2)
return dest
end
# for strided matrices, we explicitly loop over the arrays to improve cache locality
# This fuses the copytrito! for the two halves
@inline function copyto_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular{<:Any, <:StridedMatrix})
@boundscheck checkbounds(dest, axes(U)...)
isunit = U isa UnitUpperTriangular
for col in axes(dest,2)
for row in 1:col-isunit
@inbounds dest[row,col] = U.data[row,col]
end
for row in col+!isunit:size(U,1)
@inbounds dest[row,col] = U[row,col]
end
end
return dest
end
@inline function copyto_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular{<:Any, <:StridedMatrix})
@boundscheck checkbounds(dest, axes(L)...)
isunit = L isa UnitLowerTriangular
for col in axes(dest,2)
for row in 1:col-!isunit
@inbounds dest[row,col] = L[row,col]
end
for row in col+isunit:size(L,1)
@inbounds dest[row,col] = L.data[row,col]
end
end
return dest
end
@inline _rscale_add!(A::AbstractTriangular, B::AbstractTriangular, C::Number, alpha::Number, beta::Number) =
@stable_muladdmul _triscale!(A, B, C, MulAddMul(alpha, beta))
@inline _lscale_add!(A::AbstractTriangular, B::Number, C::AbstractTriangular, alpha::Number, beta::Number) =
@stable_muladdmul _triscale!(A, B, C, MulAddMul(alpha, beta))
function checksize1(A, B)
szA, szB = size(A), size(B)
szA == szB || throw(DimensionMismatch(lazy"size of A, $szA, does not match size of B, $szB"))
checksquare(B)
end
function _triscale!(A::UpperTriangular, B::UpperTriangular, c::Number, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
for i = 1:j
@inbounds _modify!(_add, B.data[i,j] * c, A.data, (i,j))
end
end
return A
end
function _triscale!(A::UpperTriangular, c::Number, B::UpperTriangular, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
for i = 1:j
@inbounds _modify!(_add, c * B.data[i,j], A.data, (i,j))
end
end
return A
end
function _triscale!(A::UpperOrUnitUpperTriangular, B::UnitUpperTriangular, c::Number, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
@inbounds _modify!(_add, c, A, (j,j))
for i = 1:(j - 1)
@inbounds _modify!(_add, B.data[i,j] * c, A.data, (i,j))
end
end
return A
end
function _triscale!(A::UpperOrUnitUpperTriangular, c::Number, B::UnitUpperTriangular, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
@inbounds _modify!(_add, c, A, (j,j))
for i = 1:(j - 1)
@inbounds _modify!(_add, c * B.data[i,j], A.data, (i,j))
end
end
return A
end
function _triscale!(A::LowerTriangular, B::LowerTriangular, c::Number, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
for i = j:n
@inbounds _modify!(_add, B.data[i,j] * c, A.data, (i,j))
end
end
return A
end
function _triscale!(A::LowerTriangular, c::Number, B::LowerTriangular, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
for i = j:n
@inbounds _modify!(_add, c * B.data[i,j], A.data, (i,j))
end
end
return A
end
function _triscale!(A::LowerOrUnitLowerTriangular, B::UnitLowerTriangular, c::Number, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
@inbounds _modify!(_add, c, A, (j,j))
for i = (j + 1):n
@inbounds _modify!(_add, B.data[i,j] * c, A.data, (i,j))
end
end
return A
end
function _triscale!(A::LowerOrUnitLowerTriangular, c::Number, B::UnitLowerTriangular, _add)
n = checksize1(A, B)
iszero(_add.alpha) && return _rmul_or_fill!(A, _add.beta)
for j = 1:n
@inbounds _modify!(_add, c, A, (j,j))
for i = (j + 1):n
@inbounds _modify!(_add, c * B.data[i,j], A.data, (i,j))
end
end
return A
end
function _trirdiv!(A::UpperTriangular, B::UpperOrUnitUpperTriangular, c::Number)
n = checksize1(A, B)
for j in 1:n
for i in 1:j
@inbounds A[i, j] = B[i, j] / c
end
end
return A
end
function _trirdiv!(A::LowerTriangular, B::LowerOrUnitLowerTriangular, c::Number)
n = checksize1(A, B)
for j in 1:n
for i in j:n
@inbounds A[i, j] = B[i, j] / c
end
end
return A
end
function _trildiv!(A::UpperTriangular, c::Number, B::UpperOrUnitUpperTriangular)
n = checksize1(A, B)
for j in 1:n
for i in 1:j
@inbounds A[i, j] = c \ B[i, j]
end
end
return A
end
function _trildiv!(A::LowerTriangular, c::Number, B::LowerOrUnitLowerTriangular)
n = checksize1(A, B)
for j in 1:n
for i in j:n
@inbounds A[i, j] = c \ B[i, j]
end
end
return A
end
rmul!(A::UpperOrLowerTriangular, c::Number) = @inline _triscale!(A, A, c, MulAddMul())
lmul!(c::Number, A::UpperOrLowerTriangular) = @inline _triscale!(A, c, A, MulAddMul())
function dot(x::AbstractVector, A::UpperTriangular, y::AbstractVector)
require_one_based_indexing(x, y)
m = size(A, 1)
(length(x) == m == length(y)) || throw(DimensionMismatch())
if iszero(m)
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
end
x₁ = x[1]
r = dot(x₁, A[1,1], y[1])
@inbounds for j in 2:m
yj = y[j]
if !iszero(yj)
temp = adjoint(A[1,j]) * x₁
@simd for i in 2:j
temp += adjoint(A[i,j]) * x[i]
end
r += dot(temp, yj)
end
end
return r
end
function dot(x::AbstractVector, A::UnitUpperTriangular, y::AbstractVector)
require_one_based_indexing(x, y)
m = size(A, 1)
(length(x) == m == length(y)) || throw(DimensionMismatch())
if iszero(m)
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
end
x₁ = first(x)
r = dot(x₁, y[1])
@inbounds for j in 2:m
yj = y[j]
if !iszero(yj)
temp = adjoint(A[1,j]) * x₁
@simd for i in 2:j-1
temp += adjoint(A[i,j]) * x[i]
end
r += dot(temp, yj)
r += dot(x[j], yj)
end
end
return r
end
function dot(x::AbstractVector, A::LowerTriangular, y::AbstractVector)
require_one_based_indexing(x, y)
m = size(A, 1)
(length(x) == m == length(y)) || throw(DimensionMismatch())
if iszero(m)
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
end
r = zero(typeof(dot(first(x), first(A), first(y))))
@inbounds for j in 1:m
yj = y[j]
if !iszero(yj)
temp = adjoint(A[j,j]) * x[j]
@simd for i in j+1:m
temp += adjoint(A[i,j]) * x[i]
end
r += dot(temp, yj)
end
end
return r
end
function dot(x::AbstractVector, A::UnitLowerTriangular, y::AbstractVector)
require_one_based_indexing(x, y)
m = size(A, 1)
(length(x) == m == length(y)) || throw(DimensionMismatch())
if iszero(m)
return dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
end
r = zero(typeof(dot(first(x), first(y))))
@inbounds for j in 1:m
yj = y[j]
if !iszero(yj)
temp = x[j]
@simd for i in j+1:m
temp += adjoint(A[i,j]) * x[i]
end
r += dot(temp, yj)
end
end
return r
end
fillstored!(A::LowerTriangular, x) = (fillband!(A.data, x, 1-size(A,1), 0); A)
fillstored!(A::UnitLowerTriangular, x) = (fillband!(A.data, x, 1-size(A,1), -1); A)
fillstored!(A::UpperTriangular, x) = (fillband!(A.data, x, 0, size(A,2)-1); A)
fillstored!(A::UnitUpperTriangular, x) = (fillband!(A.data, x, 1, size(A,2)-1); A)
# 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) = copyto!(similar(parent(A)), A) + copyto!(similar(parent(B)), 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) = copyto!(similar(parent(A)), A) - copyto!(similar(parent(B)), B)
# use broadcasting if the parents are strided, where we loop only over the triangular part
for op in (:+, :-)
for TM1 in (:LowerTriangular, :UnitLowerTriangular), TM2 in (:LowerTriangular, :UnitLowerTriangular)
@eval $op(A::$TM1{<:Any, <:StridedMaybeAdjOrTransMat}, B::$TM2{<:Any, <:StridedMaybeAdjOrTransMat}) = broadcast($op, A, B)
end
for TM1 in (:UpperTriangular, :UnitUpperTriangular), TM2 in (:UpperTriangular, :UnitUpperTriangular)
@eval $op(A::$TM1{<:Any, <:StridedMaybeAdjOrTransMat}, B::$TM2{<:Any, <:StridedMaybeAdjOrTransMat}) = broadcast($op, A, B)
end
end
function kron(A::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
C = UpperTriangular(Matrix{promote_op(*, eltype(A), eltype(B))}(undef, _kronsize(A, B)))
return kron!(C, A, B)
end
function kron(A::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
C = LowerTriangular(Matrix{promote_op(*, eltype(A), eltype(B))}(undef, _kronsize(A, B)))
return kron!(C, A, B)
end
function kron!(C::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, A::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::UpperTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
size(C) == _kronsize(A, B) || throw(DimensionMismatch("kron!"))
_triukron!(C.data, A.data, B.data)
return C
end
function kron!(C::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, A::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat}, B::LowerTriangular{<:Number,<:StridedMaybeAdjOrTransMat})
size(C) == _kronsize(A, B) || throw(DimensionMismatch("kron!"))
_trilkron!(C.data, A.data, B.data)
return C
end
function _triukron!(C, A, B)
n_A = size(A, 1)
n_B = size(B, 1)
@inbounds for j = 1:n_A
jnB = (j - 1) * n_B
for i = 1:(j-1)
Aij = A[i, j]
inB = (i - 1) * n_B
for l = 1:n_B
for k = 1:l
C[inB+k, jnB+l] = Aij * B[k, l]
end
for k = 1:(l-1)
C[inB+l, jnB+k] = zero(eltype(C))
end
end
end
Ajj = A[j, j]
for l = 1:n_B
for k = 1:l
C[jnB+k, jnB+l] = Ajj * B[k, l]
end
end
end
end
function _trilkron!(C, A, B)
n_A = size(A, 1)
n_B = size(B, 1)
@inbounds for j = 1:n_A
jnB = (j - 1) * n_B
Ajj = A[j, j]
for l = 1:n_B
for k = l:n_B
C[jnB+k, jnB+l] = Ajj * B[k, l]
end
end
for i = (j+1):n_A
Aij = A[i, j]
inB = (i - 1) * n_B
for l = 1:n_B
for k = l:n_B
C[inB+k, jnB+l] = Aij * B[k, l]
end
for k = (l+1):n_B
C[inB+l, jnB+k] = zero(eltype(C))
end
end
end
end
end
######################
# BlasFloat routines #
######################
# which triangle to use of the underlying data
uplo_char(::UpperOrUnitUpperTriangular) = 'U'
uplo_char(::LowerOrUnitLowerTriangular) = 'L'
uplo_char(::UpperOrUnitUpperTriangular{<:Any,<:AdjOrTrans}) = 'L'
uplo_char(::LowerOrUnitLowerTriangular{<:Any,<:AdjOrTrans}) = 'U'
uplo_char(::UpperOrUnitUpperTriangular{<:Any,<:Adjoint{<:Any,<:Transpose}}) = 'U'
uplo_char(::LowerOrUnitLowerTriangular{<:Any,<:Adjoint{<:Any,<:Transpose}}) = 'L'
uplo_char(::UpperOrUnitUpperTriangular{<:Any,<:Transpose{<:Any,<:Adjoint}}) = 'U'
uplo_char(::LowerOrUnitLowerTriangular{<:Any,<:Transpose{<:Any,<:Adjoint}}) = 'L'
isunit_char(::UpperTriangular) = 'N'
isunit_char(::UnitUpperTriangular) = 'U'
isunit_char(::LowerTriangular) = 'N'
isunit_char(::UnitLowerTriangular) = 'U'
lmul!(A::Tridiagonal, B::AbstractTriangular) = A*full!(B)
# generic fallback for AbstractTriangular matrices outside of the four subtypes provided here
_trimul!(C::AbstractVecOrMat, A::AbstractTriangular, B::AbstractVector) =
lmul!(A, copyto!(C, B))
_trimul!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractMatrix) =
lmul!(A, copyto!(C, B))
_trimul!(C::AbstractMatrix, A::AbstractMatrix, B::AbstractTriangular) =
rmul!(copyto!(C, A), B)
_trimul!(C::AbstractMatrix, A::AbstractTriangular, B::AbstractTriangular) =
lmul!(A, copyto!(C, B))
# redirect for UpperOrLowerTriangular
_trimul!(C::AbstractVecOrMat, A::UpperOrLowerTriangular, B::AbstractVector) =
generic_trimatmul!(C, uplo_char(A), isunit_char(A), wrapperop(parent(A)), _unwrap_at(parent(A)), B)
_trimul!(C::AbstractMatrix, A::UpperOrLowerTriangular, B::AbstractMatrix) =
generic_trimatmul!(C, uplo_char(A), isunit_char(A), wrapperop(parent(A)), _unwrap_at(parent(A)), B)
_trimul!(C::AbstractMatrix, A::AbstractMatrix, B::UpperOrLowerTriangular) =
generic_mattrimul!(C, uplo_char(B), isunit_char(B), wrapperop(parent(B)), A, _unwrap_at(parent(B)))
_trimul!(C::AbstractMatrix, A::UpperOrLowerTriangular, B::UpperOrLowerTriangular) =
generic_trimatmul!(C, uplo_char(A), isunit_char(A), wrapperop(parent(A)), _unwrap_at(parent(A)), B)
# disambiguation with AbstractTriangular
_trimul!(C::AbstractMatrix, A::UpperOrLowerTriangular, B::AbstractTriangular) =
generic_trimatmul!(C, uplo_char(A), isunit_char(A), wrapperop(parent(A)), _unwrap_at(parent(A)), B)
_trimul!(C::AbstractMatrix, A::AbstractTriangular, B::UpperOrLowerTriangular) =
generic_mattrimul!(C, uplo_char(B), isunit_char(B), wrapperop(parent(B)), A, _unwrap_at(parent(B)))
lmul!(A::AbstractTriangular, B::AbstractVecOrMat) = @inline _trimul!(B, A, B)
rmul!(A::AbstractMatrix, B::AbstractTriangular) = @inline _trimul!(A, A, B)
for TC in (:AbstractVector, :AbstractMatrix)
@eval @inline function _mul!(C::$TC, A::AbstractTriangular, B::AbstractVector, alpha::Number, beta::Number)
if isone(alpha) && iszero(beta)
return _trimul!(C, A, B)
else
return @stable_muladdmul generic_matvecmul!(C, 'N', A, B, MulAddMul(alpha, beta))
end
end
end
for (TA, TB) in ((:AbstractTriangular, :AbstractMatrix),
(:AbstractMatrix, :AbstractTriangular),
(:AbstractTriangular, :AbstractTriangular)
)
@eval @inline function _mul!(C::AbstractMatrix, A::$TA, B::$TB, alpha::Number, beta::Number)
if isone(alpha) && iszero(beta)