Return the two input scalar or array values a
and b
while their values are swapped.
- Parameters
-
[in,out] | a | : The input/output scalar, or array of the same rank, shape, and size other array-like arguments, of either
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) of arbitrary len type-parameter, or
-
type
integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64) or
-
type
logical of kind any supported by the processor (e.g., LK) or
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128) or
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128).
On output, its value is swapped with the value of b . |
[in,out] | b | : The input/output scalar, or array of the same rank, shape, and size other array-like arguments, of the same type and kind as the input a .
On output, its value is swapped with the value of a . |
[in] | inca | : The input scalar integer of default kind IK, containing the stride of the input/output scalar character or vector argument a(:) .
-
A positive value implies the multiplication to be performed on the subset
a(1 : 1 + (size(a) - 1) * inca : inca) .
-
A negative value implies the multiplication to be performed on the subset
a(1 + (1 - size(a)) * inca : 1 : inca) .
(optional, default = 1 . It can be present only if a and b are of rank 1 . It must be present if inca is present.) |
[in] | incb | : The input scalar integer of default kind IK, containing the stride of the input/output scalar character or vector argument b(:) .
-
A positive value implies the multiplication to be performed on the subset
b(1 : 1 + (size(b) - 1) * incb : incb) .
-
A negative value implies the multiplication to be performed on the subset
b(1 + (1 - size(b)) * incb : 1 : incb) .
(optional, default = 1 . It can be present only if a and b are of rank 1 . It must be present if inca is present.) |
Possible calling interfaces ⛓
Return the two input scalar or array values a and b while their values are swapped.
This module contains procedures and generic interfaces for swapping values of intrinsic Fortran types...
- Warning
- The condition
len(a) == len(b)
must hold for the corresponding input arguments.
The condition size(a(1::abs(inca))) == size(b(1::abs(incb)))
must hold for the corresponding input arguments.
The rank and size of the two input array-like arguments must be the same.
This condition is verified only if the library is built with the preprocessor macro CHECK_ENABLED=1
.
-
The
pure
procedure(s) documented herein become impure
when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1
.
By default, these procedures are pure
in release
build and impure
in debug
and testing
builds.
- BLAS/LAPACK equivalent:
- The procedures under discussion combine, modernize, and extend the interface and functionalities of Version 3.11 of BLAS/LAPACK routine(s):
SSWAP
, DSWAP
, CSWAP
, ZSWAP
.
In particular, swapping scalars or vectors or arrays of arbitrary intrinsic type (including character
, integer
, and logical
), kind, and rank are possible.
- See also
- getMinMax
setMinMax
Example usage ⛓
9 type(display_type) :: disp
11 character(:, SK),
allocatable :: a_SK, b_SK, Pair_SK(:)
12 integer(IK) :: a_IK, b_IK, Pair_IK(
2)
13 logical(LK) :: a_LK, b_LK, Pair_LK(
2)
14 complex(CK) :: a_CK, b_CK, Pair_CK(
2)
15 real(RK) :: a_RK, b_RK, Pair_RK(
2)
17 a_SK
= "Hell" ; b_SK
= "Heaven"
18 a_IK
= 10_IK ; b_IK
= 5_IK
19 a_LK
= .true._LK ; b_LK
= .false._LK
20 a_CK
= (
+10._CK,
-5._CK); b_CK
= (
+5._CK,
+15._CK)
21 a_RK
= 10._RK ; b_RK
= 5._RK
32 character(:),
allocatable :: a, b
38 call disp%show(
"call setSwapped(a, b)")
41 call disp%show( a , deliml
= SK_
"""" )
43 call disp%show( b , deliml
= SK_
"""" )
48 character(:),
allocatable :: a, b
54 call disp%show(
"call setSwapped(a, b, inca = 2_IK, incb = 2_IK)")
55 call setSwapped(a, b, inca
= 2_IK, incb
= 2_IK)
57 call disp%show( a , deliml
= SK_
"""" )
59 call disp%show( b , deliml
= SK_
"""" )
64 integer,
allocatable :: a, b
70 call disp%show(
"call setSwapped(a, b)")
73 call disp%show( a , deliml
= SK_
"""" )
75 call disp%show( b , deliml
= SK_
"""" )
80 logical,
allocatable :: a, b
86 call disp%show(
"call setSwapped(a, b)")
89 call disp%show( a , deliml
= SK_
"""" )
91 call disp%show( b , deliml
= SK_
"""" )
96 complex,
allocatable :: a, b
102 call disp%show(
"call setSwapped(a, b)")
105 call disp%show( a , deliml
= SK_
"""" )
107 call disp%show( b , deliml
= SK_
"""" )
112 real,
allocatable :: a, b
118 call disp%show(
"call setSwapped(a, b)")
121 call disp%show( a , deliml
= SK_
"""" )
123 call disp%show( b , deliml
= SK_
"""" )
134 character(
6),
allocatable :: a(:), b(:)
136 call disp%show(
"a = ['Heaven', 'Hell ']")
137 a
= [
'Heaven',
'Hell ']
138 call disp%show(
"b = ['Hell ', 'Heaven']")
139 b
= [
'Hell ',
'Heaven']
140 call disp%show(
"call setSwapped(a, b)")
143 call disp%show( a , deliml
= SK_
"""" )
145 call disp%show( b , deliml
= SK_
"""" )
150 integer,
allocatable :: a(:), b(:)
156 call disp%show(
"call setSwapped(a, b)")
159 call disp%show( a , deliml
= SK_
"""" )
161 call disp%show( b , deliml
= SK_
"""" )
166 logical,
allocatable :: a(:), b(:)
168 call disp%show(
"a = [.false., .true.]")
169 a
= [
.false.,
.true.]
170 call disp%show(
"b = [.true., .false.]")
171 b
= [
.true.,
.false.]
172 call disp%show(
"call setSwapped(a, b)")
175 call disp%show( a , deliml
= SK_
"""" )
177 call disp%show( b , deliml
= SK_
"""" )
182 complex,
allocatable :: a(:), b(:)
188 call disp%show(
"call setSwapped(a, b)")
191 call disp%show( a , deliml
= SK_
"""" )
193 call disp%show( b , deliml
= SK_
"""" )
198 real,
allocatable :: a(:), b(:)
204 call disp%show(
"call setSwapped(a, b)")
207 call disp%show( a , deliml
= SK_
"""" )
209 call disp%show( b , deliml
= SK_
"""" )
214 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%")
215 call disp%show(
"! Swap strided vectors.")
216 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%")
220 integer(IK) ,
parameter :: DUM
= huge(DUM)
221 integer,
allocatable :: a(:), b(:)
223 call disp%show(
"a = [1, 2, 3, 4, 5]")
225 call disp%show(
"b = [-1, DUM , -2, DUM , -3, DUM , -4, DUM , -5]")
226 b
= [
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
227 call disp%show(
"call setSwapped(a, b, inca = 1_IK, incb = 2_IK)")
228 call setSwapped(a, b, inca
= 1_IK, incb
= 2_IK)
230 call disp%show( a , deliml
= SK_
"""" )
232 call disp%show( b , deliml
= SK_
"""" )
237 integer(IK) ,
parameter :: DUM
= huge(DUM)
238 integer,
allocatable :: a(:), b(:)
240 call disp%show(
"a = [1, 2, 3, 4, 5]")
242 call disp%show(
"b = [-1, DUM , -2, DUM , -3, DUM , -4, DUM , -5]")
243 b
= [
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
244 call disp%show(
"call setSwapped(a, b, inca = 1_IK, incb = -2_IK)")
245 call setSwapped(a, b, inca
= 1_IK, incb
= -2_IK)
247 call disp%show( a , deliml
= SK_
"""" )
249 call disp%show( b , deliml
= SK_
"""" )
254 integer(IK) ,
parameter :: DUM
= huge(DUM)
255 integer,
allocatable :: a(:), b(:)
257 call disp%show(
"a = [1, 2, 3, 4, 5]")
259 call disp%show(
"b = [-1, DUM , -2, DUM , -3, DUM , -4, DUM , -5]")
260 b
= [
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
261 call disp%show(
"call setSwapped(a(1:1), b, inca = 0_IK, incb = 2_IK)")
262 call setSwapped(a(
1:
1), b, inca
= 0_IK, incb
= 2_IK)
264 call disp%show( a , deliml
= SK_
"""" )
266 call disp%show( b , deliml
= SK_
"""" )
271 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
272 call disp%show(
"! Swap strided vectors (blass call).")
273 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
277 real(IK) ,
parameter :: DUM
= huge(DUM)
278 real,
allocatable :: a(:), b(:)
280 call disp%show(
"a = [real :: 1, 2, 3, 4, 5]")
281 a
= [real ::
1,
2,
3,
4,
5]
282 call disp%show(
"b = [real :: -1, DUM , -2, DUM , -3, DUM , -4, DUM , -5]")
283 b
= [real ::
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
284 call disp%show(
"call setSwapped(a, b, inca = 1_IK, incb = 2_IK)")
285 call setSwapped(a, b, inca
= 1_IK, incb
= 2_IK)
287 call disp%show( a , deliml
= SK_
"""" )
289 call disp%show( b , deliml
= SK_
"""" )
294 integer(IK) ,
parameter :: DUM
= huge(DUM)
295 real,
allocatable :: a(:), b(:)
297 call disp%show(
"a = [real :: 1, 2, 3, 4, 5]")
298 a
= [real ::
1,
2,
3,
4,
5]
299 call disp%show(
"b = [real :: -1, DUM , -2, DUM , -3, DUM , -4, DUM , -5]")
300 b
= [real ::
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
301 call disp%show(
"call setSwapped(a, b, inca = 1_IK, incb = -2_IK)")
302 call setSwapped(a, b, inca
= 1_IK, incb
= -2_IK)
304 call disp%show( a , deliml
= SK_
"""" )
306 call disp%show( b , deliml
= SK_
"""" )
311 real(IK) ,
parameter :: DUM
= huge(DUM)
312 real,
allocatable :: a(:), b(:)
314 call disp%show(
"a = [real :: 1, 2, 3, 4, 5]")
315 a
= [real ::
1,
2,
3,
4,
5]
316 call disp%show(
"b = [real :: -1, DUM , -2, DUM , -3, DUM , -4, DUM , -5]")
317 b
= [real ::
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
318 call disp%show(
"call setSwapped(a, b(1:1), inca = 1_IK, incb = 0_IK)")
319 call setSwapped(a, b(
1:
1), inca
= 1_IK, incb
= 0_IK)
321 call disp%show( a , deliml
= SK_
"""" )
323 call disp%show( b , deliml
= SK_
"""" )
328 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
329 call disp%show(
"! Swap higher-rank arrays.")
330 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
334 integer(IK) ,
parameter :: DUM
= huge(DUM)
335 integer,
allocatable :: a(:,:), b(:,:)
337 call disp%show(
"a = reshape([1, 2, 3, 4, 5, 6], [2,3])")
338 a
= reshape([
1,
2,
3,
4,
5,
6], [
2,
3])
339 call disp%show(
"b = reshape([-1 , -2 , -3, DUM, DUM, DUM, -4, -5, -6], [3, 3], order = [2, 1])")
340 b
= reshape([
-1 ,
-2 ,
-3, DUM, DUM, DUM,
-4,
-5,
-6], [
3,
3], order
= [
2,
1])
342 call disp%show( a , deliml
= SK_
"""" )
344 call disp%show( b , deliml
= SK_
"""" )
345 call disp%show(
"call setSwapped(a, b(1::2, :))")
348 call disp%show( a , deliml
= SK_
"""" )
350 call disp%show( b , deliml
= SK_
"""" )
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter CK
The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
47"+2.00000000, +0.00000000"
49"+1.00000000, +0.00000000"
66a
= [
'Heaven',
'Hell ']
67b
= [
'Hell ',
'Heaven']
97"+2.00000000, +0.00000000",
"+1.00000000, +0.00000000"
99"+1.00000000, +0.00000000",
"+2.00000000, +0.00000000"
106"+2.00000000",
"+1.00000000"
108"+1.00000000",
"+2.00000000"
117b
= [
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
118call setSwapped(a, b, inca
= 1_IK, incb
= 2_IK)
120"-1",
"-2",
"-3",
"-4",
"-5"
122"+1",
"+2147483647",
"+2",
"+2147483647",
"+3",
"+2147483647",
"+4",
"+2147483647",
"+5"
126b
= [
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
127call setSwapped(a, b, inca
= 1_IK, incb
= -2_IK)
129"-5",
"-4",
"-3",
"-2",
"-1"
131"+5",
"+2147483647",
"+4",
"+2147483647",
"+3",
"+2147483647",
"+2",
"+2147483647",
"+1"
135b
= [
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
136call setSwapped(a(
1:
1), b, inca
= 0_IK, incb
= 2_IK)
138"-5",
"+2",
"+3",
"+4",
"+5"
140"+1",
"+2147483647",
"-1",
"+2147483647",
"-2",
"+2147483647",
"-3",
"+2147483647",
"-4"
148a
= [real ::
1,
2,
3,
4,
5]
149b
= [real ::
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
150call setSwapped(a, b, inca
= 1_IK, incb
= 2_IK)
152"-1.00000000",
"-2.00000000",
"-3.00000000",
"-4.00000000",
"-5.00000000"
154"+1.00000000",
"+0.340282347E+39",
"+2.00000000",
"+0.340282347E+39",
"+3.00000000",
"+0.340282347E+39",
"+4.00000000",
"+0.340282347E+39",
"+5.00000000"
157a
= [real ::
1,
2,
3,
4,
5]
158b
= [real ::
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
159call setSwapped(a, b, inca
= 1_IK, incb
= -2_IK)
161"-5.00000000",
"-4.00000000",
"-3.00000000",
"-2.00000000",
"-1.00000000"
163"+5.00000000",
"+0.214748365E+10",
"+4.00000000",
"+0.214748365E+10",
"+3.00000000",
"+0.214748365E+10",
"+2.00000000",
"+0.214748365E+10",
"+1.00000000"
166a
= [real ::
1,
2,
3,
4,
5]
167b
= [real ::
-1, DUM ,
-2, DUM ,
-3, DUM ,
-4, DUM ,
-5]
168call setSwapped(a, b(
1:
1), inca
= 1_IK, incb
= 0_IK)
170"-1.00000000",
"+1.00000000",
"+2.00000000",
"+3.00000000",
"+4.00000000"
172"+5.00000000",
"+0.340282347E+39",
"-2.00000000",
"+0.340282347E+39",
"-3.00000000",
"+0.340282347E+39",
"-4.00000000",
"+0.340282347E+39",
"-5.00000000"
180a
= reshape([
1,
2,
3,
4,
5,
6], [
2,
3])
181b
= reshape([
-1 ,
-2 ,
-3, DUM, DUM, DUM,
-4,
-5,
-6], [
3,
3], order
= [
2,
1])
187"+2147483647",
"+2147483647",
"+2147483647"
195"+2147483647",
"+2147483647",
"+2147483647"
- Test:
- test_pm_swap
- Todo:
- This generic interface can be extended to take an optional
mask
input argument that leads to selective swapping of array elements:
\param[in] mask : The input scalar, or array of the same rank, shape, and size as other array-like arguments, of type `logical` of default kind \LK.<br>
If present, then swapping will be done only if the corresponding value in `mask` is `.true.`.<br>
(**optional**, default = `.true.`.)
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, Thursday 1:45 AM, August 22, 2019, Dallas, TX
Definition at line 139 of file pm_swap.F90.