Generate and return a vector of unique values in the input array.
The uniqueness of the values can be optionally determined by the user by specifying the external input function that checks the equivalence of a pair of elements of the input array
.
- Parameters
-
[in] | array | : The input contiguous array of shape (:) of either
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU), or
-
type
logical of kind any supported by the processor (e.g., LK), or
-
type
integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64), 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),
or,
-
a scalar assumed-length
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
whose unique elements are to be returned. |
| iseq | : The external user-specified function that takes two input scalar arguments of the same type and kind as the input array .
It returns a scalar logical of default kind LK that is .true. if the two input arguments are equivalent (e.g., equal) according to the user-defined criterion, otherwise, it is .false. .
The following illustrates the generic interface of iseq , function iseq(element1, element2) result(equivalent)
TYPE(KIND) , intent(in) :: element1, element2
logical(LK) :: equivalent
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter LK The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
where TYPE(KIND) represents the type and kind of the input argument setA , which can be one of the following,
character(*, SK), intent(in) :: element1, element2
character(1, SK), intent(in) :: element1, element2
integer(IK) , intent(in) :: element1, element2
logical(LK) , intent(in) :: element1, element2
complex(CK) , intent(in) :: element1, element2
real(RK) , intent(in) :: element1, element2
integer, parameter RK The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
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...
where the kinds SK , IK , LK , CK , RK , can refer to any kind type parameter that is supported by the processor.
This user-defined equivalence check is extremely useful where a user-defined equivalence test other than exact equality or identity is needed, for example, when the array elements should match only within a given threshold or, when the case-sensitivity in character comparisons do not matter.
In such cases, the user can define a custom equivalence criterion within the user-defined external function iseq to achieve the goal.
(optional, the default equivalence operator is .eqv. if the input array is logical , otherwise == .) |
- Returns
unique
: The output allocatable
array of the same type and kind as the input array
containing all of its unique elements.
Possible calling interfaces ⛓
Generate and return a vector of unique values in the input array.
This module contains procedures and generic interfaces for finding unique values of an input array of...
- Warning
- 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.
-
The procedures under this generic interface are
impure
when the user-specified external
procedure iseq
is specified as input argument.
-
Note that in Fortran, trailing blanks are ignored in character comparison, that is,
"Fortran" == "Fortran "
yields .true.
.
- Note
- If needed, use setSorted to sort the output unique values (in ascending order).
- See also
- getUnique
setUnique
isUniqueAll
isUniqueAny
Example usage ⛓
13 character(:, SK),
allocatable :: string_SK
14 character(
9, SK),
allocatable :: Array_SK(:)
15 integer(IK) ,
allocatable :: Array_IK(:)
16 complex(CK) ,
allocatable :: Array_CK(:)
17 real(RK) ,
allocatable :: Array_RK(:)
19 type(display_type) :: disp
24 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
25 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
26 call disp%show(
"! Find all unique elements in array.")
27 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
28 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
31 string_SK
= "ParaMonte is a Monte Carlo Library."
32 Array_SK
= [
"ParaMonte",
"PARAMONTE",
"paramonte",
"ParaMonte",
"ParaMonte",
"Paramonte"]
33 Array_IK
= [
1_IK,
1_IK,
4_IK,
4_IK]
34 Array_RK
= [
1._RK,
2._RK,
4._RK,
4._RK]
35 Array_CK
= [(
1._CK,
-1._CK), (
1._CK,
-2._CK), (
1._CK,
-1._CK), (
4._CK,
-4._CK)]
38 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
39 call disp%show(
"! Unique elements in character scalar.")
40 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
44 call disp%show( string_SK, deliml
= SK_
"""" )
45 call disp%show(
"getUnique(string_SK)")
49 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
50 call disp%show(
"! Unique elements in character array.")
51 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
55 call disp%show( Array_SK, deliml
= SK_
"""" )
56 call disp%show(
"getUnique(Array_SK)")
58 call disp%show( Array_SK, deliml
= SK_
"""" )
61 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
62 call disp%show(
"! Unique elements in integer array.")
63 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
68 call disp%show(
"getUnique(Array_IK)")
72 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
73 call disp%show(
"! Unique elements in complex array.")
74 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
79 call disp%show(
"getUnique(Array_CK)")
83 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
84 call disp%show(
"! Unique elements in real array.")
85 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
90 call disp%show(
"getUnique(Array_RK)")
94 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
95 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
96 call disp%show(
"! Find all unique elements according to the user-specified equivalence criterion.")
97 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
98 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
102 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
103 call disp%show(
"! Unique elements in real array.")
104 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
107 Array_RK
= [
1.01_RK,
1.04_RK,
0.98_RK,
1.0_RK,
1.02_RK,
2._RK]
111 call disp%show(
"getUnique(Array_RK, iseq = iseq_RK)")
115 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
116 call disp%show(
"! Unique case-insensitive instances within the character array.")
117 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
120 string_SK
= "ABBAbbA"
123 call disp%show( string_SK, deliml
= SK_
"""" )
124 call disp%show(
"getUnique(string_SK, iseq = iseq_SK)")
129 pure function iseq_RK(element1, element2)
result(iseq)
130 real(RK) ,
intent(in) :: element1, element2
132 iseq
= abs(abs(element1)
- abs(element2))
< 0.05_RK
135 pure function iseq_SK(element1, element2)
result(iseq)
137 character(
*, SK) ,
intent(in) :: element1, element2
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.
Generate and return the input string where the uppercase English alphabets are all converted to lower...
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 contains the uncommon and hardly representable ASCII characters as well as procedures for...
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 ⛓
14"ParaMonte is a Monte Carlo Library."
23"ParaMonte",
"PARAMONTE",
"paramonte",
"ParaMonte",
"ParaMonte",
"Paramonte"
25"ParaMonte",
"PARAMONTE",
"paramonte",
"Paramonte"
41(
+1.0000000000000000,
-1.0000000000000000), (
+1.0000000000000000,
-2.0000000000000000), (
+1.0000000000000000,
-1.0000000000000000), (
+4.0000000000000000,
-4.0000000000000000)
43(
+1.0000000000000000,
-1.0000000000000000), (
+1.0000000000000000,
-2.0000000000000000), (
+4.0000000000000000,
-4.0000000000000000)
50+1.0000000000000000,
+2.0000000000000000,
+4.0000000000000000,
+4.0000000000000000
52+1.0000000000000000,
+2.0000000000000000,
+4.0000000000000000
66+1.0100000000000000,
+1.0400000000000000,
+0.97999999999999998,
+1.0000000000000000,
+1.0200000000000000,
+2.0000000000000000
68+1.0100000000000000,
+2.0000000000000000
- Test:
- test_pm_arrayUnique
- Bug:
Status: Unresolved
Source: Intel Classic Fortran Compiler ifort
version 2021.2.0, GNU Fortran Compiler gfortran
version 10-11
Description: The Intel Classic Fortran Compiler ifort
version 2021.2.0 has a bug for the following interface definition,
character(len(array),SK) , allocatable :: arrayNew(:)
leading to an internal compiler error.
Remedy (as of ParaMonte Library version 2.0.0): For now, the remedy seems to be to redefine the interface as,
character(:, SK), allocatable :: arrayNew(:)
and changing the allocation method accordingly in the implementation to,
allocate(character(len(array, kind = IK)) :: arrayNew(lenArrayNew))
However, this introduces internal compiler error: Segmentation fault
with GNU Fortran Compiler gfortran
. Here is a code snippet to regenerate the bug in Intel Classic Fortran Compiler ifort
(uncomment the commented line to reproduce the gfortran bug),
module pm_explicitLenResult
implicit none
interface
pure module function bug(array) result(arrayNew)
character(*, SK), intent(in), contiguous :: array(:)
character(len(array),SK) , allocatable :: arrayNew(:)
end function
end interface
end module pm_explicitLenResult
submodule (pm_explicitLenResult) routines
implicit none
contains
module procedure bug
allocate(arrayNew, source = array)
end procedure
end submodule routines
use pm_explicitLenResult, only: bug
character(2) :: array(3) = ["AA", "BB", "CC"]
character(2), allocatable :: arrayNew(:)
arrayNew = bug(array)
end program main
program main
This is main entry to the tests of the ParaMonte kernel library.
Remedy (as of ParaMonte Library version 2.0.0): It turns out that both GNU Fortran Compiler gfortran
and Intel Classic Fortran Compiler ifort
do not tolerate the separation of interface from implementation in the above code snippet.
If one duplicates the interface in the implementation submodule, then both compilers compile and run the code with no errors.
This is the remedy that is currently used in this getRemoved generic interface (interface duplication where the bug exists).
Here is a workaround example for the bug in the above code snippet,
module pm_explicitLenResult
implicit none
interface
pure module function bug(array) result(arrayNew)
character(*, SK), intent(in), contiguous :: array(:)
character(len(array),SK) , allocatable :: arrayNew(:)
end function
end interface
end module pm_explicitLenResult
submodule (pm_explicitLenResult) routines
implicit none
contains
module procedure bug
allocate(arrayNew, source = array)
end procedure
end submodule routines
use pm_explicitLenResult, only: bug
character(2) :: array(3) = ["AA", "BB", "CC"]
character(2), allocatable :: arrayNew(:)
arrayNew = bug(array)
end program main
- Todo:
- Low Priority: This generic interface can be extended to 2D input objects.
- Todo:
- Critical Priority: The internal compiler error with
ifort
and gfortran
has to be fixed in the future versions.
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, September 1, 2017, 11:35 PM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin
Definition at line 2682 of file pm_arrayUnique.F90.