Foortran Toolkit

Fortran toolkit
Borja Petit


Download toolkit

Back to index

vect

function vect(mat) result(vec)
  implicit none
  integer :: mat(:,:,...,:)
  integer :: vec(:)
function vect(mat) result(vec)
  implicit none
  real(kind=8) :: mat(:,:,...,:)
  real(kind=8) :: vec(:)
function vect(mat) result(vec)
  implicit none
  logical :: mat(:,:,...,:)
  logical :: vec(:)

This function returns a 1-dimensional array vec with all the elements of a user-supplied n-dimensional array mat, where $1\leq$ n $\leq5$ (for a 1-dimensional array, vec is a copy of mat; this is useful in generic code that may receive arrays of any dimension). The input matrix can be double precision, integer or logical. The elements are taken in column-major order (as in reshape(mat,(/size(mat)/))): first all the rows of the first column, then those of the second column, and so on.

Internal dependencies: none

Note: vect is an interface that calls specific functions depending on the type of mat (integer, real or logical) and depending on the dimensions of mat. The specific functions are:


Example:

! matrix is integer

mat(:,1) = (/ 1 , 2 /)
mat(:,2) = (/ 3 , 4 /)

vec = vect(mat)

print * , 'vec =', vec   ! vec =  1  2  3  4
! matrix is double precision

mat(:,1) = (/ 1.5d0 , 2.2d0 /)
mat(:,2) = (/ 2.4d0 , 0.7d0 /)

vec = vect(mat)

print * , 'vec =', vec   ! vec =  1.5000  2.2000  2.4000  0.7000
! matrix is logical

mat(:,1) = (/ .true. , .false. /)
mat(:,2) = (/ .false. , .true. /)

vec = vect(mat)

print * , 'vec =', vec   ! vec =  T  F  F  T