@@ -839,19 +839,28 @@ sub twiddle {
839
839
840
840
=head3 C<slice >
841
841
842
- Produce the degree (n-1) Matrix defined by a given index and value for that index. If n is 1,
843
- this produces a Real/Complex/Fraction.
842
+ Produce the degree (n-1) Matrix defined by a given index (the first, second, ..., nth) and a
843
+ value for that index along that dimension. If n is 1, this produces a Real/Complex/Fraction.
844
844
845
845
Usage:
846
846
847
847
$A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
848
- $A->slice(1, 2) # will be same as Matrix([5, 6, 7, 8])
849
- $A->slice(2, 3) # will be same as Matrix([3, 7, 11])
848
+ $A->slice(1, 2)
849
+ # Index 1 identifies the 1st, 2nd, or 3rd row above, and with value 2 we get the second one:
850
+ # Matrix([5, 6, 7, 8])
850
851
851
852
$B = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
852
- $B->slice(1, 2) # will be same as Matrix([ [ 5, 6 ], [ 7, 8 ] ])
853
- $B->slice(2, 1) # will be same as Matrix([ [ 1, 2 ], [ 5, 6 ] ])
854
- $B->slice(3, 1) # will be same as Matrix([ [ 1, 3 ], [ 5, 7 ] ])
853
+ $B->slice(1, 2)
854
+ # Index 1 identifies the two arrays at depth 1, and with value 2 we get the second one:
855
+ # Matrix([ [ 5, 6 ], [ 7, 8 ] ])
856
+ $B->slice(2, 1)
857
+ # Here we take all entries from $B where the 2nd index is 1: the 1 at position (1,1,1),
858
+ # the 2 at position (1,1,2), the 5 at position (2,1,1), and the 6 at position (2,1,2):
859
+ # Matrix([ [ 1, 2 ], [ 5, 6 ] ])
860
+ $B->slice(3, 1)
861
+ # Here we take all entries from $B where the 3rd index is 1: the 1 at position (1,1,1),
862
+ # the 3 at position (1,2,1), the 5 at position (2,1,1), and the 7 at position (2,2,1):
863
+ # Matrix([ [ 1, 3 ], [ 5, 7 ] ])
855
864
856
865
=cut
857
866
@@ -885,14 +894,41 @@ specified, the default is the usual transposition of the last two indices.
885
894
886
895
Usage:
887
896
888
- $A = Matrix([ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ]);
889
- $A->transpose # will be [ [ 1, 5, 9 ], [ 2, 6, 10 ], [ 3, 7, 11 ], [ 4, 8, 12 ] ]
890
-
891
- $B = Matrix([ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ]);
892
- $B->transpose([1, 2, 3]) # will be [ [ [ 1, 3 ], [ 5, 7 ] ], [ [2 , 4 ], [ 6, 8 ] ] ]
893
-
894
- $C = Matrix([ [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ], [ [ [ 9, A ], [ B, C ] ], [ [ D, E ], [ F, 0 ] ] ] ]);
895
- $C->transpose([ [ 1, 2], [3, 4] ]) # will be [ [ [ [ 1, 3 ], [ 2, 4 ] ], [ [ 9, B ], [ A, C ] ] ], [ [ [ 5, 7 ], [ 6, 8 ] ], [ [ D, F ], [ E, 0 ] ] ]
897
+ $A = Matrix([
898
+ [ 1, 2, 3, 4 ],
899
+ [ 5, 6, 7, 8 ],
900
+ [ 9, 10, 11, 12 ]
901
+ ]);
902
+ $A->transpose
903
+ # will be
904
+ # [
905
+ # [ 1, 5, 9 ],
906
+ # [ 2, 6, 10 ],
907
+ # [ 3, 7, 11 ],
908
+ # [ 4, 8, 12 ]
909
+ # ]
910
+
911
+ $B = Matrix([
912
+ [ [ 1, 2 ], [ 3, 4 ] ],
913
+ [ [ 5, 6 ], [ 7, 8 ] ]
914
+ ]);
915
+ $B->transpose([1, 2, 3])
916
+ # will be
917
+ # [
918
+ # [ [ 1, 3 ], [ 5, 7 ] ],
919
+ # [ [ 2, 4 ], [ 6, 8 ] ]
920
+ # ]
921
+
922
+ $C = Matrix([
923
+ [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ],
924
+ [ [ [ 9, 10 ], [ 11, 12 ] ], [ [ 13, 14 ], [ 15, 16 ] ] ]
925
+ ]);
926
+ $C->transpose([ [ 1, 2], [3, 4] ])
927
+ # will be
928
+ # [
929
+ # [ [ [ 1, 3 ], [ 2, 4 ] ], [ [ 9, 11 ], [ 10, 12 ] ] ],
930
+ # [ [ [ 5, 7 ], [ 6, 8 ] ], [ [ 13, 15 ], [ 14, 16 ] ] ]
931
+ # ]
896
932
=cut
897
933
898
934
sub transpose {
0 commit comments