M_sets(3f) - functions reminiscent of Matlab set functions
Set-theory operations compare the elements in two sets to find commonalities, differences, and membership.
M_set(3f) is a Fortran module comprising a small group of set-theory functions reminiscent of related Matlab procedures.
M_set(3f) is intended to be built with and used by fpm(1) projects. It requires M_orderpack(3f) as a dependency, which is of course taken care of automatically via fpm(1).
Currently the allowed sets are vectors of integer numbers or arrays of character variables. real numbers are allowed but "caveat emptor", as comparing floats for equality has issues. You may have to condition the float data by converting it to scaled integers or using intrinsics such as NEAREST(3f) to produce the desired results.
- union(A,B,setOrder) - Join two sets and remove duplicates of values
- unique(A,setOrder) - Remove duplicates of values from a set
- intersect(A,B,setOrder) - Find the values common to both A and B
- setdiff(A,B,setOrder) - Find the values in A that are not in B
- ismember(A,B,setOrder) - Create a mask of A marking elements also in B
- setxor(A,B,setOrder) - Find values of A and B not in both arrays
- issorted(A) - Determine if array is already sorted
The subsequent data may be produced sorted, or left in the order encountered.
program demo_M_sets
use M_sets, only: &
& unique, intersect, union, setdiff, ismember, setxor, issorted
character(len=*),parameter :: all='(*(g0,1x))'
character(len=*),parameter :: nl=new_line('A')
integer, allocatable :: A(:)
integer, allocatable :: B(:)
integer, allocatable :: C(:)
A = [10, -10, 0, 1, 2, 3, 3, 2, 1, -10]
!
print all ,nl, &
'UNIQUE','Find the unique elements of vector A.' ,nl, &
'A=', A ,nl, &
'sorted=',unique(A) ,nl, &
'stable=',unique(A, setOrder='stable')
A=[5, 7, 1]
B=[3, 1, 1]
!
print all ,nl, &
'UNION', 'Find the union of vectors A and B.' ,nl, &
'A=', A ,nl, &
'B=', B ,nl, &
'sorted=',union(A, B, 'sorted') ,nl, &
'stable=',union(A, B, 'stable')
A=[7, 1, 7, 7, 4]
B=[7, 0, 4, 4, 0]
!
print all ,nl, &
'INTERSECT', 'Find the values common to both A and B.' ,nl, &
'A=', A ,nl, &
'B=', B ,nl, &
'sorted=',intersect(A, B) ,nl, &
'stable=',intersect(A, B, setOrder='stable')
A=[3, 6, 2, 1, 5, 1, 1]
B=[2, 4, 6]
!
print all ,nl, &
'SETDIFF','Find the values in A that are not in B.' ,nl, &
'A=', A ,nl, &
'B=', B ,nl, &
'sorted=',setdiff(A, B, 'sorted') ,nl, &
'stable=',setdiff(A, B, 'stable')
A=[5,3,4,2]
B=[2,4,4,4,6,8]
!
print all ,nl, &
'ISMEMBER','Determine which elements of A are also in B.' ,nl, &
'A=', A ,nl, &
'B=', B ,nl, &
'in A and B=',ismember(A,B)
A=[5,1,3,3,3]
B=[4,1,2]
!
print all ,nl, &
'SETXOR' , &
'Find values of A and B not in their intersection.' ,nl, &
'A=', A ,nl, &
'B=', B ,nl, &
'sorted=',setxor(A,B) ,nl, &
'stable=',setxor(A,B,'stable')
A=[1,2,3,4,5]
B=[5,4,3,2,1]
!
print all ,nl, &
'ISSSORTED' , &
'confirm whether array is sorted in ascending order or not' ,nl, &
'A=', A ,nl, &
'B=', B ,nl, &
'is A sorted?',issorted(A) ,nl, &
'is B sorted?',issorted(B)
end program demo_M_sets
Results:
>
> UNIQUE Find the unique elements of vector A.
> A= 10 -10 0 1 2 3 3 2 1 -10
> sorted= -10 0 1 2 3 10
> stable= 10 -10 0 1 2 3
>
> UNION Find the union of vectors A and B.
> A= 5 7 1
> B= 3 1 1
> sorted= 1 3 5 7
> stable= 5 7 1 3
>
> INTERSECT Find the values common to both A and B.
> A= 7 1 7 7 4
> B= 7 0 4 4 0
> sorted= 4 7
> stable= 7 4
>
> SETDIFF Find the values in A that are not in B.
> A= 3 6 2 1 5 1 1
> B= 2 4 6
> sorted= 1 3 5
> stable= 3 1 5
>
> ISMEMBER Determine which elements of A are also in B.
> A= 5 3 4 2
> B= 2 4 4 4 6 8
> in A and B= 0 0 1 1
>
> SETXOR Find values of A and B not in their intersection.
> A= 5 1 3 3 3
> B= 4 1 2
> sorted= 2 3 4 5
> stable= 5 3 4 2
>
> ISSSORTED confirm whether array is sorted in ascending order or not
> A= 1 2 3 4 5
> B= 5 4 3 2 1
> is A sorted? 1
> is B sorted? 0
Download the github repository and build it with fpm ( as described at Fortran Package Manager )
git clone https://github.com/urbanjost/M_sets.git
cd M_sets
fpm build
or just list it as a dependency in your fpm.toml project file.
[dependencies]
M_sets = { git = "https://github.com/urbanjost/M_sets.git" }
The documentation for each procedure is included in the source. That documentation is also available as a flat text file, HTML documents, and man-pages.
-
A text manual contains all the procedure descriptions in a single flat ASCII text file.
-
routines are also described in HTML form using the format of man-pages.
-
For easier searching and printing Javascript is used to combine all those HTML descriptions of the man-pages into a single book.
-
are the de-facto standard method of providing procedure descriptions on Unix, GNU/Linux, OpenBSD, Cygwin, WLS, and other ULS (Unix-Like Systems)
Installation can vary depending on whether you are installing personal copies or as an administrator, but man-pages are well suited for any CLI user (Command-Line Interface):
-
CHANGELOG provides a history of significant changes