%mathpiper,def="Dimensions"

//Retract("Dimensions",*);

/* Code that returns the list of the dimensions of a tensor or matrix
   Code submitted by Dirk Reusch.
 */

LocalSymbols(x,i,n,m,aux,dim,result)
[
 1 # Dimensions(x_IsList) <--
    [
      Local(i,n,m,aux,dim,result);
      result:=List(Length(x));
      If(Length(x)>0 And Length(Select(x, IsList))=Length(x),
        [
          n:=Length(x);
	      dim:=MapSingle(Dimensions,x);
	      m:=Minimum(MapSingle(Length,dim));
	      For(i:=1,i<=m,i++)
	      [
	        aux:=Table(dim[j][i],j,1,n,1);
	        If(Minimum(aux)=Maximum(aux),
	           result:=DestructiveAppend(result,dim[1][i]),
	           i:=m+1
            );
          ];
        ]
      );
      result;
    ];

 2 # Dimensions(_x) <-- List();
];  // LocalSymbols

%/mathpiper

    %output,preserve="false"
      Result: True
.   %/output



%mathpiper_docs,name="Dimensions",categories="User Functions;Linear Algebra"
*CMD Dimensions --- dimensions (number of rows and columns etc) of input Matrix
*STD
*CALL
	Dimensions(Matrix)

*PARMS

{Matrix} -- a matrix


*DESC

This command returns an array of dimensions {nrows,ncols,...} corresponding to the input array.
The input array can be 1-dimensional (i.e., vector), 2-dimensional (i.e., matrix),
or higher dimensioned (tensor).  

If multidimensional, the array must not be "ragged", else this function will give 
an incorrect result.

*E.G.
In> Dimensions({1,2,3,4})
Result: {4}

In> Dimensions({{1,2,3,4},{5,6,7,8}})
Result: {2,4}

In> Dimensions({{{4,8,1,2},{1,7,-3,-14}},{{2,-3,2,3},{11,12,13,14}},{{21,22,23,24},{31,32,33,34}}})
Result: {3,2,4}

In> Dimensions({{1,2,3,4},{5,6,7,8,9}})
Result: {2}

%/mathpiper_docs


