Skip to main content

Swap

This subroutine can swap two integer (scalar, vector, matrix) and two real numbers (scalar, vector, matrix). It can also swap the dimension of a Fortran array.

note

We can use this method for swapping two variables a and b, where a and b can be scalar, vector or matrix of Fortran intrinsic types.

note

We can also use this method for swaping the dimension of a multi-dimension array. For example, consider an array b(3,4), we can interchange the row and column dimension (i.e., transpose operation) by using swap.

Interface 1

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b, i1, i2)
REAL(Real32), ALLOCATABLE, INTENT(INOUT) :: a(:, :)
!! the returned array
REAL(Real32), INTENT(IN) :: b(:, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 2
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 2
END SUBROUTINE Swap
END INTERFACE

Interface 2

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b, i1, i2, i3)
REAL(Real32|Real64), ALLOCATABLE, INTENT(INOUT) :: a(:, :, :)
!! the returned array
REAL(Real32|Real64), INTENT(IN) :: b(:, :, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 3
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 3
INTEGER(I4B), INTENT(IN) :: i3
!! index 3 is Swapped with index `i3`
!! make sure i3 is less than or equal to 3
END SUBROUTINE Swap
END INTERFACE

Interface 3

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b, i1, i2, i3, i4)
REAL(Real64), ALLOCATABLE, INTENT(INOUT) :: a(:, :, :, :)
!! the returned array
REAL(Real64), INTENT(IN) :: b(:, :, :, :)
!! input array, it will be untouched
INTEGER(I4B), INTENT(IN) :: i1
!! index 1 is Swapped with index `i1`
!! make sure i1 is lesser than or equal to 4
INTEGER(I4B), INTENT(IN) :: i2
!! index 2 is Swapped with index `i2`
!! make sure i2 is less than or equal to 4
INTEGER(I4B), INTENT(IN) :: i3
!! index 3 is Swapped with index `i3`
!! make sure i3 is less than or equal to 4
INTEGER(I4B), INTENT(IN) :: i4
!! index 4 is Swapped with index `i4`
!! make sure i4 is less than or equal to 4
END SUBROUTINE Swap
END INTERFACE

Interface 4

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b)
INTEGER(Int8| Int16 | Int32 | Int64| Real32 | Real64), INTENT(INOUT) :: a, b
END SUBROUTINE Swap
END INTERFACE

Swap two scalar numbers.

Interface 5

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b)
INTEGER(Int8| Int16 | Int32 | Int64 | Real32 | Real64), INTENT(INOUT) :: a(:), b(:)
END SUBROUTINE Swap
END INTERFACE

Interface 6

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b)
INTEGER(Int8| Int16 | Int32 | Int64 | Real32 | Real64), INTENT(INOUT) :: a(:,:), b(:,:)
END SUBROUTINE Swap
END INTERFACE

Interface 7

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b, mask)
INTEGER(Int8| Int16 | Int32 | Int64 | Real32 | Real64), INTENT(INOUT) :: a, b
LOGICAL(LGT), INTENT(IN) :: mask
END SUBROUTINE Swap
END INTERFACE

Masked swap.

Interface 8

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b, mask)
INTEGER(Int8| Int16 | Int32 | Int64 | Real32 | Real64), INTENT(INOUT) :: a(:), b(:)
LOGICAL(LGT), INTENT(IN) :: mask(:)
END SUBROUTINE Swap
END INTERFACE

Interface 9

INTERFACE
MODULE PURE SUBROUTINE Swap(a, b, mask)
INTEGER(Int8| Int16 | Int32 | Int64 | Real32 | Real64), INTENT(INOUT) :: a(:, :), b(:, :)
LOGICAL(LGT), INTENT(IN) :: mask(:, :)
END SUBROUTINE Swap
END INTERFACE