@@ -725,132 +725,130 @@ end
725725 # Make sure we aren't doing type piracy on `reshape`
726726 @test ndims (dropdims (ones (1 ,1 ), dims= (1 ,2 ))) == 0
727727
728- if VERSION >= v " 1.9"
729- # `stack` was introduced in Julia 1.9
730- # Issue #254
731- x = ComponentVector (a= [1 , 2 ])
732- y = ComponentVector (a= [3 , 4 ])
733- xy = stack ([x, y])
734- # The data in `xy` should be the same as what we'd get if we used plain Vectors:
735- @test getdata (xy) == stack (getdata .([x, y]))
736- # Check the axes.
737- xy_ax = getaxes (xy)
738- # Should have two axes since xy should be a ComponentMatrix.
739- @test length (xy_ax) == 2
740- # First axis should be the same as x.
741- @test xy_ax[1 ] == only (getaxes (x))
742- # Second axis should be a FlatAxis.
743- @test xy_ax[2 ] == FlatAxis ()
744-
745- # Does the dims argument to stack work?
746- # Using `dims=2` should be the same as the default value.
747- xy2 = stack ([x, y]; dims= 2 )
748- @test xy2 == xy
749- # Using `dims=1` should stack things vertically.
750- xy3 = stack ([x, y]; dims= 1 )
751- @test all (xy3[1 , :a ] .== xy[:a , 1 ])
752- @test all (xy3[2 , :a ] .== xy[:a , 2 ])
753-
754- # But can we stack 2D arrays?
755- x = ComponentVector (a= [1 , 2 ])
756- y = ComponentVector (b= [3 , 4 ])
757- X = x .* y'
758- Y = x .* y' .+ 4
759- XY = stack ([X, Y])
760- # The data in `XY` should be the same as what we'd get if we used plain Vectors:
761- @test getdata (XY) == stack (getdata .([X, Y]))
762- # Check the axes.
763- XY_ax = getaxes (XY)
764- # Should have three axes since XY should be a 3D ComponentArray.
765- @test length (XY_ax) == 3
766- # First two axes should be the same as XY.
767- @test XY_ax[1 ] == getaxes (XY)[1 ]
768- @test XY_ax[2 ] == getaxes (XY)[2 ]
769- # Third should be a FlatAxis.
770- @test XY_ax[3 ] == FlatAxis ()
771- # Should test indexing too.
772- @test all (XY[:a , :b , 1 ] .== X)
773- @test all (XY[:a , :b , 2 ] .== Y)
774-
775- # Make sure the dims argument works.
776- # Using `dims=3` should be the same as the default value.
777- XY_d3 = stack ([X, Y]; dims= 3 )
778- @test XY_d3 == XY
779- # Using `dims=2` stacks along the second axis.
780- XY_d2 = stack ([X, Y]; dims= 2 )
781- @test all (XY_d2[:a , 1 , :b ] .== XY[:a , :b , 1 ])
782- @test all (XY_d2[:a , 2 , :b ] .== XY[:a , :b , 2 ])
783- # Using `dims=1` stacks along the first axis.
784- XY_d1 = stack ([X, Y]; dims= 1 )
785- @test all (XY_d1[1 , :a , :b ] .== XY[:a , :b , 1 ])
786- @test all (XY_d1[2 , :a , :b ] .== XY[:a , :b , 2 ])
787-
788- # Issue #254, tuple of arrays:
789- x = ComponentVector (a= [1 , 2 ])
790- y = ComponentVector (b= [3 , 4 ])
791- Xstack1 = stack ((x, y, x); dims= 1 )
792- Xstack1_noca = stack ((getdata (x), getdata (y), getdata (x)); dims= 1 )
793- @test all (Xstack1 .== Xstack1_noca)
794- @test all (Xstack1[1 , :a ] .== Xstack1_noca[1 , :])
795- @test all (Xstack1[2 , :a ] .== Xstack1_noca[2 , :])
796-
797- # Issue #254, Array of tuples.
798- Xstack2 = stack (ComponentArray (a= (1 ,2 ,3 ), b= (4 ,5 ,6 )))
799- Xstack2_noca = stack ([(1 ,2 ,3 ), (4 ,5 ,6 )])
800- @test all (Xstack2 .== Xstack2_noca)
801- @test all (Xstack2[:, :a ] .== Xstack2_noca[:, 1 ])
802- @test all (Xstack2[:, :b ] .== Xstack2_noca[:, 2 ])
803-
804- Xstack2_d1 = stack (ComponentArray (a= (1 ,2 ,3 ), b= (4 ,5 ,6 )); dims= 1 )
805- Xstack2_noca_d1 = stack ([(1 ,2 ,3 ), (4 ,5 ,6 )]; dims= 1 )
806- @test all (Xstack2_d1 .== Xstack2_noca_d1)
807- @test all (Xstack2_d1[:a , :] .== Xstack2_noca_d1[1 , :])
808- @test all (Xstack2_d1[:b , :] .== Xstack2_noca_d1[2 , :])
809-
810- # Issue #254, generator of arrays.
811- Xstack3 = stack (ComponentArray (z= [x,x]) for x in 1 : 4 )
812- Xstack3_noca = stack ([x, x] for x in 1 : 4 )
813- # That should give me
814- # [1 2 3 4;
815- # 1 2 3 4]
816- @test all (Xstack3 .== Xstack3_noca)
817- @test all (Xstack3[:z , 1 ] .== Xstack3_noca[:, 1 ])
818- @test all (Xstack3[:z , 2 ] .== Xstack3_noca[:, 2 ])
819- @test all (Xstack3[:z , 3 ] .== Xstack3_noca[:, 3 ])
820- @test all (Xstack3[:z , 4 ] .== Xstack3_noca[:, 4 ])
821-
822- Xstack3_d1 = stack (ComponentArray (z= [x,x]) for x in 1 : 4 ; dims= 1 )
823- Xstack3_noca_d1 = stack ([x, x] for x in 1 : 4 ; dims= 1 )
824- # That should give me
825- # [1 1;
826- # 2 2;
827- # 3 3;
828- # 4 4;]
829- @test all (Xstack3_d1 .== Xstack3_noca_d1)
830- @test all (Xstack3_d1[1 , :z ] .== Xstack3_noca_d1[1 , :])
831- @test all (Xstack3_d1[2 , :z ] .== Xstack3_noca_d1[2 , :])
832- @test all (Xstack3_d1[3 , :z ] .== Xstack3_noca_d1[3 , :])
833- @test all (Xstack3_d1[4 , :z ] .== Xstack3_noca_d1[4 , :])
834-
835- # Issue #254, map then stack.
836- Xstack4_d1 = stack (x -> ComponentArray (a= x, b= [x+ 1 ,x+ 2 ]), [5 6 ; 7 8 ]; dims= 1 ) # map then stack
837- Xstack4_noca_d1 = stack (x -> [x, x+ 1 , x+ 2 ], [5 6 ; 7 8 ]; dims= 1 ) # map then stack
838- @test all (Xstack4_d1 .== Xstack4_noca_d1)
839- @test all (Xstack4_d1[:, :a ] .== Xstack4_noca_d1[:, 1 ])
840- @test all (Xstack4_d1[:, :b ] .== Xstack4_noca_d1[:, 2 : 3 ])
841-
842- Xstack4_d2 = stack (x -> ComponentArray (a= x, b= [x+ 1 ,x+ 2 ]), [5 6 ; 7 8 ]; dims= 2 ) # map then stack
843- Xstack4_noca_d2 = stack (x -> [x, x+ 1 , x+ 2 ], [5 6 ; 7 8 ]; dims= 2 ) # map then stack
844- @test all (Xstack4_d2 .== Xstack4_noca_d2)
845- @test all (Xstack4_d2[:a , :] .== Xstack4_noca_d2[1 , :])
846- @test all (Xstack4_d2[:b , :] .== Xstack4_noca_d2[2 : 3 , :])
847-
848- Xstack4_dcolon = stack (x -> ComponentArray (a= x, b= [x+ 1 ,x+ 2 ]), [5 6 ; 7 8 ]; dims= :) # map then stack
849- Xstack4_noca_dcolon = stack (x -> [x, x+ 1 , x+ 2 ], [5 6 ; 7 8 ]; dims= :) # map then stack
850- @test all (Xstack4_dcolon .== Xstack4_noca_dcolon)
851- @test all (Xstack4_dcolon[:a , :, :] .== Xstack4_noca_dcolon[1 , :, :])
852- @test all (Xstack4_dcolon[:b , :, :] .== Xstack4_noca_dcolon[2 : 3 , :, :])
853- end
728+ # `stack` was introduced in Julia 1.9
729+ # Issue #254
730+ x = ComponentVector (a= [1 , 2 ])
731+ y = ComponentVector (a= [3 , 4 ])
732+ xy = stack ([x, y])
733+ # The data in `xy` should be the same as what we'd get if we used plain Vectors:
734+ @test getdata (xy) == stack (getdata .([x, y]))
735+ # Check the axes.
736+ xy_ax = getaxes (xy)
737+ # Should have two axes since xy should be a ComponentMatrix.
738+ @test length (xy_ax) == 2
739+ # First axis should be the same as x.
740+ @test xy_ax[1 ] == only (getaxes (x))
741+ # Second axis should be a FlatAxis.
742+ @test xy_ax[2 ] == FlatAxis ()
743+
744+ # Does the dims argument to stack work?
745+ # Using `dims=2` should be the same as the default value.
746+ xy2 = stack ([x, y]; dims= 2 )
747+ @test xy2 == xy
748+ # Using `dims=1` should stack things vertically.
749+ xy3 = stack ([x, y]; dims= 1 )
750+ @test all (xy3[1 , :a ] .== xy[:a , 1 ])
751+ @test all (xy3[2 , :a ] .== xy[:a , 2 ])
752+
753+ # But can we stack 2D arrays?
754+ x = ComponentVector (a= [1 , 2 ])
755+ y = ComponentVector (b= [3 , 4 ])
756+ X = x .* y'
757+ Y = x .* y' .+ 4
758+ XY = stack ([X, Y])
759+ # The data in `XY` should be the same as what we'd get if we used plain Vectors:
760+ @test getdata (XY) == stack (getdata .([X, Y]))
761+ # Check the axes.
762+ XY_ax = getaxes (XY)
763+ # Should have three axes since XY should be a 3D ComponentArray.
764+ @test length (XY_ax) == 3
765+ # First two axes should be the same as XY.
766+ @test XY_ax[1 ] == getaxes (XY)[1 ]
767+ @test XY_ax[2 ] == getaxes (XY)[2 ]
768+ # Third should be a FlatAxis.
769+ @test XY_ax[3 ] == FlatAxis ()
770+ # Should test indexing too.
771+ @test all (XY[:a , :b , 1 ] .== X)
772+ @test all (XY[:a , :b , 2 ] .== Y)
773+
774+ # Make sure the dims argument works.
775+ # Using `dims=3` should be the same as the default value.
776+ XY_d3 = stack ([X, Y]; dims= 3 )
777+ @test XY_d3 == XY
778+ # Using `dims=2` stacks along the second axis.
779+ XY_d2 = stack ([X, Y]; dims= 2 )
780+ @test all (XY_d2[:a , 1 , :b ] .== XY[:a , :b , 1 ])
781+ @test all (XY_d2[:a , 2 , :b ] .== XY[:a , :b , 2 ])
782+ # Using `dims=1` stacks along the first axis.
783+ XY_d1 = stack ([X, Y]; dims= 1 )
784+ @test all (XY_d1[1 , :a , :b ] .== XY[:a , :b , 1 ])
785+ @test all (XY_d1[2 , :a , :b ] .== XY[:a , :b , 2 ])
786+
787+ # Issue #254, tuple of arrays:
788+ x = ComponentVector (a= [1 , 2 ])
789+ y = ComponentVector (b= [3 , 4 ])
790+ Xstack1 = stack ((x, y, x); dims= 1 )
791+ Xstack1_noca = stack ((getdata (x), getdata (y), getdata (x)); dims= 1 )
792+ @test all (Xstack1 .== Xstack1_noca)
793+ @test all (Xstack1[1 , :a ] .== Xstack1_noca[1 , :])
794+ @test all (Xstack1[2 , :a ] .== Xstack1_noca[2 , :])
795+
796+ # Issue #254, Array of tuples.
797+ Xstack2 = stack (ComponentArray (a= (1 ,2 ,3 ), b= (4 ,5 ,6 )))
798+ Xstack2_noca = stack ([(1 ,2 ,3 ), (4 ,5 ,6 )])
799+ @test all (Xstack2 .== Xstack2_noca)
800+ @test all (Xstack2[:, :a ] .== Xstack2_noca[:, 1 ])
801+ @test all (Xstack2[:, :b ] .== Xstack2_noca[:, 2 ])
802+
803+ Xstack2_d1 = stack (ComponentArray (a= (1 ,2 ,3 ), b= (4 ,5 ,6 )); dims= 1 )
804+ Xstack2_noca_d1 = stack ([(1 ,2 ,3 ), (4 ,5 ,6 )]; dims= 1 )
805+ @test all (Xstack2_d1 .== Xstack2_noca_d1)
806+ @test all (Xstack2_d1[:a , :] .== Xstack2_noca_d1[1 , :])
807+ @test all (Xstack2_d1[:b , :] .== Xstack2_noca_d1[2 , :])
808+
809+ # Issue #254, generator of arrays.
810+ Xstack3 = stack (ComponentArray (z= [x,x]) for x in 1 : 4 )
811+ Xstack3_noca = stack ([x, x] for x in 1 : 4 )
812+ # That should give me
813+ # [1 2 3 4;
814+ # 1 2 3 4]
815+ @test all (Xstack3 .== Xstack3_noca)
816+ @test all (Xstack3[:z , 1 ] .== Xstack3_noca[:, 1 ])
817+ @test all (Xstack3[:z , 2 ] .== Xstack3_noca[:, 2 ])
818+ @test all (Xstack3[:z , 3 ] .== Xstack3_noca[:, 3 ])
819+ @test all (Xstack3[:z , 4 ] .== Xstack3_noca[:, 4 ])
820+
821+ Xstack3_d1 = stack (ComponentArray (z= [x,x]) for x in 1 : 4 ; dims= 1 )
822+ Xstack3_noca_d1 = stack ([x, x] for x in 1 : 4 ; dims= 1 )
823+ # That should give me
824+ # [1 1;
825+ # 2 2;
826+ # 3 3;
827+ # 4 4;]
828+ @test all (Xstack3_d1 .== Xstack3_noca_d1)
829+ @test all (Xstack3_d1[1 , :z ] .== Xstack3_noca_d1[1 , :])
830+ @test all (Xstack3_d1[2 , :z ] .== Xstack3_noca_d1[2 , :])
831+ @test all (Xstack3_d1[3 , :z ] .== Xstack3_noca_d1[3 , :])
832+ @test all (Xstack3_d1[4 , :z ] .== Xstack3_noca_d1[4 , :])
833+
834+ # Issue #254, map then stack.
835+ Xstack4_d1 = stack (x -> ComponentArray (a= x, b= [x+ 1 ,x+ 2 ]), [5 6 ; 7 8 ]; dims= 1 ) # map then stack
836+ Xstack4_noca_d1 = stack (x -> [x, x+ 1 , x+ 2 ], [5 6 ; 7 8 ]; dims= 1 ) # map then stack
837+ @test all (Xstack4_d1 .== Xstack4_noca_d1)
838+ @test all (Xstack4_d1[:, :a ] .== Xstack4_noca_d1[:, 1 ])
839+ @test all (Xstack4_d1[:, :b ] .== Xstack4_noca_d1[:, 2 : 3 ])
840+
841+ Xstack4_d2 = stack (x -> ComponentArray (a= x, b= [x+ 1 ,x+ 2 ]), [5 6 ; 7 8 ]; dims= 2 ) # map then stack
842+ Xstack4_noca_d2 = stack (x -> [x, x+ 1 , x+ 2 ], [5 6 ; 7 8 ]; dims= 2 ) # map then stack
843+ @test all (Xstack4_d2 .== Xstack4_noca_d2)
844+ @test all (Xstack4_d2[:a , :] .== Xstack4_noca_d2[1 , :])
845+ @test all (Xstack4_d2[:b , :] .== Xstack4_noca_d2[2 : 3 , :])
846+
847+ Xstack4_dcolon = stack (x -> ComponentArray (a= x, b= [x+ 1 ,x+ 2 ]), [5 6 ; 7 8 ]; dims= :) # map then stack
848+ Xstack4_noca_dcolon = stack (x -> [x, x+ 1 , x+ 2 ], [5 6 ; 7 8 ]; dims= :) # map then stack
849+ @test all (Xstack4_dcolon .== Xstack4_noca_dcolon)
850+ @test all (Xstack4_dcolon[:a , :, :] .== Xstack4_noca_dcolon[1 , :, :])
851+ @test all (Xstack4_dcolon[:b , :, :] .== Xstack4_noca_dcolon[2 : 3 , :, :])
854852
855853 # Test fix https://github.com/Deltares/Ribasim/issues/2028
856854 a = range (0.0 , 1.0 , length= 0 ) |> collect
0 commit comments