This feature is useful for writing functions that can manipulate array (matrices and vectors) of arbitrary size in a flexible manner
Example:
C++ style:
int func( double *A, int nrows, int ncols) { // dimension of matrix is passed as parameters // because array A does not contain information // about its size and shape... ... } |
Example:
C++ style:
int func( double *A ) { ... A contains information on its size and shape ... The function can handle any size and shape of matrices } |
|
SHAPE(A): return the "shape" (dimensions) of array A SIZE(A): return the "size" (# elements) of array A |
A more detailed discussion follows....
SHAPE(A) |
returns the shape of array A in the form of an array of integer.
Each integer represents the number of rows in one dimension of the array
Examples:
REAL, DIMENSION(5) :: A1 !! One-dim array (vector) REAL, DIMENSION(4,3) :: A2 !! Two-dim array (matrix) REAL, DIMENSION(3,2,2) :: A3 !! 3-dim array (tensor) REAL, DIMENSION(1:2, 2:3) :: A4 print *, "shape(A1) = ", shape(A1) --> 5 print *, "shape(A2) = ", shape(A2) --> 4 3 print *, "shape(A3) = ", shape(A3) --> 3 2 2 print *, "shape(A4) = ", shape(A4) --> 2 2 |
SIZE(A) // total number of elements in A |
returns the total size (number of elements) of array A in the form of an integer scalar.
SIZE(A, k) // number of elements in dimension k in A |
returns the size (number of elements) of the kth dimension of the array A in the form of an integer scalar.
Examples:
REAL, DIMENSION(5) :: A1 !! One-dim array (vector) REAL, DIMENSION(4,3) :: A2 !! Two-dim array (matrix) REAL, DIMENSION(3,2,2) :: A3 !! 3-dim array (tensor) REAL, DIMENSION(1:2, 2:3) :: A4 |
|
|
Example:
REAL, DIMENSION(5) :: A1 SHAPE(A1) ---------> 5 .... it is an ARRAY of 1 element SIZE(A1) ---------> 5 .... it is a SCALAR |
Example 1:
REAL, DIMENSION(5) :: A1 SHAPE ( SHAPE(A1) ) ----> 1 (it's an ARRAY again) SHAPE ( SIZE(A1) ) ----> undefined (SIZE(A1) is not an array) |
Example Program:
(Demo above code)
                       
                       
Example 2:
REAL, DIMENSION(5) :: A1 REAL, DIMENSION(SHAPE(A1)):: B ---------> Illegal, SHAPE(A1) is not a sacaler REAL, DIMENSION(SIZE(A1)):: B ---------> Legal |
Example Program:
(Demo above code)