M_sets(3f) - [M_sets::INTRO] functions reminiscent of Matlab set functions
Synopsis
Description
Examples
Authors
License
Procedure names and syntax:
use M_sets, only : & union, unique, intersect, setdiff, ismember, setxor use M_sets, only : & issorted, bool
Set operations compare the elements in two sets to find commonalities or differences. This includes Unions, Intersections, and Membership.
M_set(3f) is a Fortran module comprising a small subset of set theory functions reminiscent of Matlab functions.
The functions currently support vectors of integer, default character, and default real and doubleprecision type.
float numbers (both kind=real32 and kind=real64) 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.
M_set(3f) primarily uses simple calls to the M_orderpack(3f) module to provide the functionality. The functions are not otherwise tuned for performance and make loose use of memory allocation but are sufficient for most uses, simple to use, and familiar to a large base of users.
## Functions
The subsequent data may be produced sorted, or left in the order encountered.
o union(A,B,setOrder) - Join two sets and remove duplicates of values o unique(A,setOrder) - Remove duplicates of values from a set o intersect(A,B,setOrder) - Find the values common to both A and B o setdiff(A,B,setOrder) - Find the values in A that are not in B o ismember(A,B,setOrder) - Create a mask of A marking elements also in B o setxor(A,B,setOrder) - Find values of A and B not in both arrays o issorted(A) - Determine if array is already sorted o bool(expr) - 1 if logical expression is true, 0 if false.
sample program:
program demo_M_sets use M_sets, only: & & unique, intersect, union, setdiff, ismember, setxor, issorted, bool character(len=*),parameter :: all=(*(g0,1x)) character(len=*),parameter :: nl=new_line(A) integer, allocatable :: A(:) integer, allocatable :: B(:) integer, allocatable :: C(:)Results: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)
A=[1,2,3,4,5] B=[5,2,3,4,1] ! print all ,nl, & BOOL , & if logical expression is true, 0 if false. ,nl, & A=, A ,nl, & B=, B ,nl, & is A(i) = B(i) ?,bool(A==B) ,nl, & how many elements are the same?,sum(bool(A==B))
end program demo_M_sets
> > 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 > > BOOL if logical expression is true, 0 if false. > A= 1 2 3 4 5 > B= 5 2 3 4 1 > is A(i) = B(i) ? 0 1 1 1 0 > how many elements are the same? 3
John S. Urban, 2023-07-20
CC0-1.0
Nemo Release 3.1 | M_sets (3) | February 23, 2025 |