@@ -5965,93 +5965,18 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
5965
5965
} else {
5966
5966
arg2 = F .NIL ;
5967
5967
}
5968
- if (arg1 .isList ()) {
5969
- IAST tensor = (IAST ) arg1 ;
5970
- // IntArrayList dimension = dimensions(tensor, tensor.head(), Integer.MAX_VALUE);
5971
- IAST dimensions = (IAST ) F .Dimensions (tensor ).eval (engine );
5972
- int length = dimensions .argSize ();
5973
- if (length < 1 ) {
5974
- // Error messages inherits to S.ConjugateTranspose
5975
- // The first two levels of `1` cannot be transposed.
5976
- return Errors .printMessage (ast .topHead (), "nmtx" , F .List (ast ), engine );
5977
- }
5978
- final int [] permutation = getPermutation (arg2 , length , ast , engine );
5979
- if (permutation == null ) {
5980
- return F .NIL ;
5981
- }
5982
- for (int i = 0 ; i < permutation .length ; i ++) {
5983
- if (permutation [i ] > permutation .length ) {
5984
- // Entry `1` in `2` is out of bounds for a permutation of length `3`.
5985
- return Errors .printMessage (ast .topHead (), "perm2" ,
5986
- F .List (F .ZZ (i + 1 ), arg2 , F .ZZ (permutation .length )), engine );
5987
- }
5988
- }
5989
-
5990
-
5991
- if (dimensions .argSize () == 1 ) {
5992
- if (arg2 .isPresent ()) {
5993
- if (arg2 .isList1 () && arg2 .first ().isOne ()) {
5994
- return tensor ;
5995
- }
5996
- // Entry `1` in `2` is out of bounds for a permutation of length `3`.
5997
- return Errors .printMessage (ast .topHead (), "perm2" , F .List (F .C2 , arg2 , F .C1 ), engine );
5998
- } else {
5999
- return tensor ;
6000
- }
6001
- }
6002
- IASTMutable indices ;
6003
- IAST range = ListFunctions .range (dimensions .size ());
6004
- int [] dimensionsPermutated ;
6005
- int [] originalIndices ;
6006
- if (arg2 .isPresent ()) {
6007
- originalIndices = Combinatoric .permute (range , (IAST ) arg2 );
6008
- dimensionsPermutated = Combinatoric .permute (dimensions , (IAST ) arg2 );
6009
- } else {
6010
- // start with {2,1,3,...} as default permutation
6011
- indices = range .setAtCopy (1 , range .arg2 ());
6012
- indices .set (2 , range .arg1 ());
6013
- originalIndices = indices .toIntVector ();
6014
- dimensionsPermutated = Combinatoric .permute (dimensions , indices );
6015
- }
6016
- if (dimensionsPermutated == null || originalIndices == null ) {
6017
- return F .NIL ;
6018
- }
6019
- return new TransposePermute (tensor , dimensions .toIntVector (), dimensionsPermutated ,
6020
- permutation , originalIndices , x -> transform (x )).transposeRecursive ();
6021
-
5968
+ final IntArrayList dimensions =
5969
+ LinearAlgebra .dimensions (arg1 , S .List , Integer .MAX_VALUE , false );
5970
+ int length = dimensions .size ();
5971
+ if (length < 1 ) {
5972
+ // Error messages inherits to S.ConjugateTranspose
5973
+ // The first two levels of `1` cannot be transposed.
5974
+ return Errors .printMessage (ast .topHead (), "nmtx" , F .List (ast ), engine );
6022
5975
}
5976
+ return transpose (arg1 , arg2 , dimensions , x -> transform (x ), ast , engine );
5977
+ }
6023
5978
6024
- if (arg1 .isSparseArray ()) {
6025
- ISparseArray tensor = (ISparseArray ) arg1 ;
6026
- int [] dimension = tensor .getDimension ();
6027
- final int [] permutation ;
6028
- int length = dimension .length ;
6029
- if (length < 2 ) {
6030
- // Error messages inherits to ConjugateTranspose
6031
- // The first two levels of `1` cannot be transposed.
6032
- return Errors .printMessage (ast .topHead (), "nmtx" , F .List (ast ), engine );
6033
- }
6034
- permutation = getPermutation (arg2 , length , ast , engine );
6035
- if (permutation == null ) {
6036
- return F .NIL ;
6037
- }
6038
- ISparseArray transposed = tensor .transpose (permutation , x -> transform (x ));
6039
- if (transposed != null ) {
6040
- return transposed ;
6041
- }
6042
- // get error details:
6043
- for (int i = 0 ; i < permutation .length ; i ++) {
6044
- if (permutation [i ] > permutation .length ) {
6045
- // Entry `1` in `2` is out of bounds for a permutation of length `3`.
6046
- return Errors .printMessage (ast .topHead (), "perm2" ,
6047
- F .List (F .ZZ (i + 1 ), arg2 , F .ZZ (permutation .length )), engine );
6048
- }
6049
- }
6050
-
6051
- }
6052
5979
6053
- return F .NIL ;
6054
- }
6055
5980
6056
5981
/**
6057
5982
*
@@ -6080,6 +6005,11 @@ private static int[] getPermutation(final IExpr permutationList, int length, fin
6080
6005
return null ;
6081
6006
}
6082
6007
6008
+ @ Override
6009
+ public int status () {
6010
+ return ImplementationStatus .PARTIAL_SUPPORT ;
6011
+ }
6012
+
6083
6013
@ Override
6084
6014
public int [] expectedArgSize (IAST ast ) {
6085
6015
return ARGS_1_2 ;
@@ -6215,6 +6145,11 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
6215
6145
return F .matrix ((i , j ) -> i <= j - k ? matrix .getEntry (i , j ) : F .C0 , m , n );
6216
6146
}
6217
6147
6148
+ @ Override
6149
+ public int status () {
6150
+ return ImplementationStatus .PARTIAL_SUPPORT ;
6151
+ }
6152
+
6218
6153
@ Override
6219
6154
public int [] expectedArgSize (IAST ast ) {
6220
6155
return ARGS_1_2 ;
@@ -6341,6 +6276,11 @@ public IExpr evaluate(final IAST ast, EvalEngine engine) {
6341
6276
return F .NIL ;
6342
6277
}
6343
6278
6279
+ @ Override
6280
+ public int status () {
6281
+ return ImplementationStatus .PARTIAL_SUPPORT ;
6282
+ }
6283
+
6344
6284
@ Override
6345
6285
public int [] expectedArgSize (IAST ast ) {
6346
6286
return ARGS_2_2 ;
@@ -7104,5 +7044,79 @@ public static IExpr toSphericalCoordinates(IExpr x, IExpr y, IExpr z) {
7104
7044
F .ArcTan (x , y ));
7105
7045
}
7106
7046
7047
+ public static IExpr transpose (final IExpr tensor , final IExpr permutation ,
7048
+ final IntList dimensions , Function <? super IExpr , ? extends IExpr > function ,
7049
+ final IAST ast , EvalEngine engine ) {
7050
+ final int length = dimensions .size ();
7051
+ final int [] permutationArray = Transpose .getPermutation (permutation , length , ast , engine );
7052
+ if (permutationArray == null ) {
7053
+ return F .NIL ;
7054
+ }
7055
+ if (tensor .isList ()) {
7056
+ IAST dimensionsList = F .List (dimensions .toIntArray ());
7057
+ IAST tensorList = (IAST ) tensor ;
7058
+ for (int i = 0 ; i < permutationArray .length ; i ++) {
7059
+ if (permutationArray [i ] > permutationArray .length ) {
7060
+ // Entry `1` in `2` is out of bounds for a permutation of length `3`.
7061
+ return Errors .printMessage (ast .topHead (), "perm2" ,
7062
+ F .List (F .ZZ (i + 1 ), permutation , F .ZZ (permutationArray .length )), engine );
7063
+ }
7064
+ }
7065
+
7066
+ if (dimensions .size () == 1 ) {
7067
+ if (permutation .isPresent ()) {
7068
+ if (permutation .isList1 () && permutation .first ().isOne ()) {
7069
+ return tensorList ;
7070
+ }
7071
+ // Entry `1` in `2` is out of bounds for a permutation of length `3`.
7072
+ return Errors .printMessage (ast .topHead (), "perm2" , F .List (F .C2 , permutation , F .C1 ),
7073
+ engine );
7074
+ } else {
7075
+ return tensorList ;
7076
+ }
7077
+ }
7078
+ IASTMutable indices ;
7079
+ IAST range = ListFunctions .range (dimensions .size () + 1 );
7080
+ int [] dimensionsPermutated ;
7081
+ int [] originalIndices ;
7082
+ if (permutation .isPresent ()) {
7083
+ originalIndices = Combinatoric .permute (range , (IAST ) permutation );
7084
+ dimensionsPermutated = Combinatoric .permute (dimensionsList , (IAST ) permutation );
7085
+ } else {
7086
+ // start with {2,1,3,...} as default permutation
7087
+ indices = range .setAtCopy (1 , range .arg2 ());
7088
+ indices .set (2 , range .arg1 ());
7089
+ originalIndices = indices .toIntVector ();
7090
+ dimensionsPermutated = Combinatoric .permute (dimensionsList , indices );
7091
+ }
7092
+ if (dimensionsPermutated == null || originalIndices == null ) {
7093
+ return F .NIL ;
7094
+ }
7095
+ return new Transpose .TransposePermute (tensorList , dimensions .toIntArray (),
7096
+ dimensionsPermutated ,
7097
+ permutationArray , originalIndices , function ).transposeRecursive ();
7098
+
7099
+ }
7100
+
7101
+ if (tensor .isSparseArray ()) {
7102
+ ISparseArray sparseArray = (ISparseArray ) tensor ;
7103
+ ISparseArray transposed = sparseArray .transpose (permutationArray , function );
7104
+ if (transposed != null ) {
7105
+ return transposed ;
7106
+ }
7107
+ // get error details:
7108
+ for (int i = 0 ; i < permutationArray .length ; i ++) {
7109
+ if (permutationArray [i ] > permutationArray .length ) {
7110
+ // Entry `1` in `2` is out of bounds for a permutation of length `3`.
7111
+ return Errors .printMessage (ast .topHead (), "perm2" ,
7112
+ F .List (F .ZZ (i + 1 ), permutation , F .ZZ (permutationArray .length )), engine );
7113
+ }
7114
+ }
7115
+
7116
+ }
7117
+
7118
+ return F .NIL ;
7119
+ }
7120
+
7107
7121
private LinearAlgebra () {}
7108
7122
}
0 commit comments