@@ -536,4 +536,255 @@ end
536536 @test v * S isa Matrix
537537end
538538
539+ @testset " copyto! between matrix types" begin
540+ dl, d, du = zeros (Int,4 ), [1 : 5 ;], zeros (Int,4 )
541+ d_ones = ones (Int,size (du))
542+
543+ @testset " from Diagonal" begin
544+ D = Diagonal (d)
545+ @testset " to Bidiagonal" begin
546+ BU = Bidiagonal (similar (d, BigInt), similar (du, BigInt), :U )
547+ BL = Bidiagonal (similar (d, BigInt), similar (dl, BigInt), :L )
548+ for B in (BL, BU)
549+ copyto! (B, D)
550+ @test B == D
551+ end
552+
553+ @testset " mismatched size" begin
554+ for B in (BU, BL)
555+ B .= 0
556+ copyto! (B, Diagonal (Int[1 ]))
557+ @test B[1 ,1 ] == 1
558+ B[1 ,1 ] = 0
559+ @test iszero (B)
560+ end
561+ end
562+ end
563+ @testset " to Tridiagonal" begin
564+ T = Tridiagonal (similar (dl, BigInt), similar (d, BigInt), similar (du, BigInt))
565+ copyto! (T, D)
566+ @test T == D
567+
568+ @testset " mismatched size" begin
569+ T .= 0
570+ copyto! (T, Diagonal ([1 ]))
571+ @test T[1 ,1 ] == 1
572+ T[1 ,1 ] = 0
573+ @test iszero (T)
574+ end
575+ end
576+ @testset " to SymTridiagonal" begin
577+ for du2 in (similar (du, BigInt), similar (d, BigInt))
578+ S = SymTridiagonal (similar (d), du2)
579+ copyto! (S, D)
580+ @test S == D
581+ end
582+
583+ @testset " mismatched size" begin
584+ S = SymTridiagonal (zero (d), zero (du))
585+ copyto! (S, Diagonal ([1 ]))
586+ @test S[1 ,1 ] == 1
587+ S[1 ,1 ] = 0
588+ @test iszero (S)
589+ end
590+ end
591+ end
592+
593+ @testset " from Bidiagonal" begin
594+ BU = Bidiagonal (d, du, :U )
595+ BUones = Bidiagonal (d, oneunit .(du), :U )
596+ BL = Bidiagonal (d, dl, :L )
597+ BLones = Bidiagonal (d, oneunit .(dl), :L )
598+ @testset " to Diagonal" begin
599+ D = Diagonal (zero (d))
600+ for B in (BL, BU)
601+ @test copyto! (D, B) == B
602+ D .= 0
603+ end
604+ for B in (BLones, BUones)
605+ errmsg = " cannot copy a Bidiagonal with a non-zero off-diagonal band to a Diagonal"
606+ @test_throws errmsg copyto! (D, B)
607+ @test iszero (D)
608+ end
609+
610+ @testset " mismatched size" begin
611+ for uplo in (:L , :U )
612+ D .= 0
613+ copyto! (D, Bidiagonal (Int[1 ], Int[], uplo))
614+ @test D[1 ,1 ] == 1
615+ D[1 ,1 ] = 0
616+ @test iszero (D)
617+ end
618+ end
619+ end
620+ @testset " to Tridiagonal" begin
621+ T = Tridiagonal (similar (dl, BigInt), similar (d, BigInt), similar (du, BigInt))
622+ for B in (BL, BU, BLones, BUones)
623+ copyto! (T, B)
624+ @test T == B
625+ end
626+
627+ @testset " mismatched size" begin
628+ T = Tridiagonal (oneunit .(dl), zero (d), oneunit .(du))
629+ for uplo in (:L , :U )
630+ T .= 0
631+ copyto! (T, Bidiagonal ([1 ], Int[], uplo))
632+ @test T[1 ,1 ] == 1
633+ T[1 ,1 ] = 0
634+ @test iszero (T)
635+ end
636+ end
637+ end
638+ @testset " to SymTridiagonal" begin
639+ for du2 in (similar (du, BigInt), similar (d, BigInt))
640+ S = SymTridiagonal (similar (d, BigInt), du2)
641+ for B in (BL, BU)
642+ copyto! (S, B)
643+ @test S == B
644+ end
645+ errmsg = " cannot copy a non-symmetric Bidiagonal matrix to a SymTridiagonal"
646+ @test_throws errmsg copyto! (S, BUones)
647+ @test_throws errmsg copyto! (S, BLones)
648+ end
649+
650+ @testset " mismatched size" begin
651+ S = SymTridiagonal (zero (d), zero (du))
652+ for uplo in (:L , :U )
653+ copyto! (S, Bidiagonal ([1 ], Int[], uplo))
654+ @test S[1 ,1 ] == 1
655+ S[1 ,1 ] = 0
656+ @test iszero (S)
657+ end
658+ end
659+ end
660+ end
661+
662+ @testset " from Tridiagonal" begin
663+ T = Tridiagonal (dl, d, du)
664+ TU = Tridiagonal (dl, d, d_ones)
665+ TL = Tridiagonal (d_ones, d, dl)
666+ @testset " to Diagonal" begin
667+ D = Diagonal (zero (d))
668+ @test copyto! (D, T) == Diagonal (d)
669+ errmsg = " cannot copy a Tridiagonal with a non-zero off-diagonal band to a Diagonal"
670+ D .= 0
671+ @test_throws errmsg copyto! (D, TU)
672+ @test iszero (D)
673+ errmsg = " cannot copy a Tridiagonal with a non-zero off-diagonal band to a Diagonal"
674+ @test_throws errmsg copyto! (D, TL)
675+ @test iszero (D)
676+
677+ @testset " mismatched size" begin
678+ D .= 0
679+ copyto! (D, Tridiagonal (Int[], Int[1 ], Int[]))
680+ @test D[1 ,1 ] == 1
681+ D[1 ,1 ] = 0
682+ @test iszero (D)
683+ end
684+ end
685+ @testset " to Bidiagonal" begin
686+ BU = Bidiagonal (zero (d), zero (du), :U )
687+ BL = Bidiagonal (zero (d), zero (du), :L )
688+ @test copyto! (BU, T) == Bidiagonal (d, du, :U )
689+ @test copyto! (BL, T) == Bidiagonal (d, du, :L )
690+
691+ BU .= 0
692+ BL .= 0
693+ errmsg = " cannot copy a Tridiagonal with a non-zero superdiagonal to a Bidiagonal with uplo=:L"
694+ @test_throws errmsg copyto! (BL, TU)
695+ @test iszero (BL)
696+ @test copyto! (BU, TU) == Bidiagonal (d, d_ones, :U )
697+
698+ BU .= 0
699+ BL .= 0
700+ @test copyto! (BL, TL) == Bidiagonal (d, d_ones, :L )
701+ errmsg = " cannot copy a Tridiagonal with a non-zero subdiagonal to a Bidiagonal with uplo=:U"
702+ @test_throws errmsg copyto! (BU, TL)
703+ @test iszero (BU)
704+
705+ @testset " mismatched size" begin
706+ for B in (BU, BL)
707+ B .= 0
708+ copyto! (B, Tridiagonal (Int[], Int[1 ], Int[]))
709+ @test B[1 ,1 ] == 1
710+ B[1 ,1 ] = 0
711+ @test iszero (B)
712+ end
713+ end
714+ end
715+ end
716+
717+ @testset " from SymTridiagonal" begin
718+ S2 = SymTridiagonal (d, ones (Int,size (d)))
719+ for S in (SymTridiagonal (d, du), SymTridiagonal (d, zero (d)))
720+ @testset " to Diagonal" begin
721+ D = Diagonal (zero (d))
722+ @test copyto! (D, S) == Diagonal (d)
723+ D .= 0
724+ errmsg = " cannot copy a SymTridiagonal with a non-zero off-diagonal band to a Diagonal"
725+ @test_throws errmsg copyto! (D, S2)
726+ @test iszero (D)
727+
728+ @testset " mismatched size" begin
729+ D .= 0
730+ copyto! (D, SymTridiagonal (Int[1 ], Int[]))
731+ @test D[1 ,1 ] == 1
732+ D[1 ,1 ] = 0
733+ @test iszero (D)
734+ end
735+ end
736+ @testset " to Bidiagonal" begin
737+ BU = Bidiagonal (zero (d), zero (du), :U )
738+ BL = Bidiagonal (zero (d), zero (du), :L )
739+ @test copyto! (BU, S) == Bidiagonal (d, du, :U )
740+ @test copyto! (BL, S) == Bidiagonal (d, du, :L )
741+
742+ BU .= 0
743+ BL .= 0
744+ errmsg = " cannot copy a SymTridiagonal with a non-zero off-diagonal band to a Bidiagonal"
745+ @test_throws errmsg copyto! (BU, S2)
746+ @test iszero (BU)
747+ @test_throws errmsg copyto! (BL, S2)
748+ @test iszero (BL)
749+
750+ @testset " mismatched size" begin
751+ for B in (BU, BL)
752+ B .= 0
753+ copyto! (B, SymTridiagonal (Int[1 ], Int[]))
754+ @test B[1 ,1 ] == 1
755+ B[1 ,1 ] = 0
756+ @test iszero (B)
757+ end
758+ end
759+ end
760+ end
761+ end
762+ end
763+
764+ @testset " BandIndex indexing" begin
765+ for D in (Diagonal (1 : 3 ), Bidiagonal (1 : 3 , 2 : 3 , :U ), Bidiagonal (1 : 3 , 2 : 3 , :L ),
766+ Tridiagonal (2 : 3 , 1 : 3 , 1 : 2 ), SymTridiagonal (1 : 3 , 2 : 3 ))
767+ M = Matrix (D)
768+ for band in - size (D,1 )+ 1 : size (D,1 )- 1
769+ for idx in 1 : size (D,1 )- abs (band)
770+ @test D[BandIndex (band, idx)] == M[BandIndex (band, idx)]
771+ end
772+ end
773+ @test_throws BoundsError D[BandIndex (size (D,1 ),1 )]
774+ end
775+ end
776+
777+ @testset " Partly filled Hermitian and Diagonal algebra" begin
778+ D = Diagonal ([1 ,2 ])
779+ for S in (Symmetric, Hermitian), uplo in (:U , :L )
780+ M = Matrix {BigInt} (undef, 2 , 2 )
781+ M[1 ,1 ] = M[2 ,2 ] = M[1 + (uplo == :L ), 1 + (uplo == :U )] = 3
782+ H = S (M, uplo)
783+ HM = Matrix (H)
784+ @test H + D == D + H == HM + D
785+ @test H - D == HM - D
786+ @test D - H == D - HM
787+ end
788+ end
789+
539790end # module TestSpecial
0 commit comments