bool (3m_sets) - [M_sets::LOGICAL] returns One if expression is TRUE, else returns Zero. (LICENSE:PD) intersect (3m_sets) - [M_sets] Find the values common to both sets A and B isequal (3m_sets) - [M_sets] Report if vector A is equal to vector B ismember (3m_sets) - [M_sets] Create a mask of A marking elements also in B issorted (3m_sets) - [M_sets] Report if A is sorted in ascending order or not. M_sets (3m_sets) - [M_sets::INTRO] functions reminiscent of Matlab set functions setdiff (3m_sets) - [M_sets] Find the values in A that are not in B settheory (1) - [M_sets:SETTHEORY] combine two sets of values and display union, intersection, values found in both sets, values found only in one set, ... (LICENSE:MIT) setxor (3m_sets) - [M_sets] Find values of A and B not in both arrays union (3m_sets) - [M_sets] Join two sets and removes duplicates of values unique (3m_sets) - [M_sets] return unique values in array A settheory.1.man(1m_sets) settheory.1.man(1m_sets) NAME settheory(1f) - [M_sets:SETTHEORY] combine two sets of values and display union, intersection, values found in both sets, values found only in one set, ... (LICENSE:MIT) SYNOPSIS settheory -a SET_ONE -b SET_TWO [--type DATATYPE] [--setorder ORDERTYPE] [--verbose] DESCRIPTION settheory(1f) finds the union, intersection, and set differences of two small sets of numbers or strings and displays them If both sets are empty a simple example is run. OPTIONS --seta,-a SET_ONE vector of numbers or strings comprising set A. May be delimited by commas, spaces, or colons. If spaces are used the set needs quoted. --setb,-b SET_TWO vector of numbers or strings comprising set B. May be delimited by commas, spaces, or colons. If spaces are used the set needs quoted. --type,-t DATATYPE May be "integer", "character", "real" or "double". Defaults to "character". --setorder,-o ORDERTYPE "sorted" or "stable". If "stable" the values remain in the order input. --verbose,-V add additional descriptive text --usage,-u show table of options and their current values --version,-v Print version information on standard output then exit successfully. --help,-h Print usage information on standard output then exit successfully. RESULTS Outputs the results from the following calls to the M_set(3f) module • unique(A,setOrder);unique(B,setOrder) - Unique values in each array • union(A,B,setOrder) - Set union of two arrays • intersect(A,B,setOrder) - Set intersection of two arrays • setdiff(A,B,setOrder) - Set difference of two arrays • ismember(A,B) - Array elements of set B that are members of set A array • setxor(A,B,setOrder) - Set exclusive OR of two arrays EXAMPLES Sample commands settheory -a one,two,three -b four,two,five,three A : one two three B : four two five three UNIQUE A : one three two UNIQUE B : five four three two UNION : five four one three two INTERSECT : three two SETDIFF : one ISMEMBER : 0 1 1 SETXOR : five four one settheory -a 7,23,14,15,9,12,8,24,35 -b 2,5,7,8,14,16,25,35,27 \ --type integer A : 7 23 14 15 9 12 8 24 35 B : 2 5 7 8 14 16 25 35 27 UNIQUE A : 7 8 9 12 14 15 23 24 35 UNIQUE B : 2 5 7 8 14 16 25 27 35 UNION : 2 5 7 8 9 12 14 15 16 23 24 25 27 35 INTERSECT : 7 8 14 35 SETDIFF : 9 12 15 23 24 ISMEMBER : 1 0 1 0 0 0 1 0 1 SETXOR : 2 5 9 12 15 16 23 24 25 27 # or settheory --type integer \ -a '7 23 14 15 9 12 8 24 35' \ -b '2 5 7 8 14 16 25 35 27' # or settheory --type integer \ -a 7:23:14:15:9:12:8:24:35 \ -b 2:5:7:8:14:16:25:35:27 SEE ALSO diff(1),uniq(1),sort(1),comm(1),join(1) May 11, 2025 settheory.1.man(1m_sets) intersect(3m_sets) intersect(3m_sets) NAME intersect(3f) - [M_sets] Find the values common to both sets A and B SYNOPSIS intersect(A,B, setOrder) DESCRIPTION The values that occur at least once in each set are returned. That is, intersect(3f) returns the data common to both A and B, with no repetitions. OPTIONS A input array B input array setOrder May be "sort" or "stable". If "stable" the values are returned in the order discovered. The default is "sorted", which returns the data in ascending order. EXAMPLES sample program: program demo_intersect use M_sets, only: unique, intersect, union, setdiff, ismember, setxor character(len=*),parameter :: g='(*(g0,1x))' integer, allocatable :: A(:) integer, allocatable :: B(:) write(*,g) 'INTERSECT', 'Find the values common to both A and B.' A=[7, 1, 7, 7, 4] B=[7, 0, 4, 4, 0] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) intersect(A, B) write(*,g) intersect(A, B, setOrder='stable') end program demo_intersect Results: > INTERSECT Find the values common to both A and B. > A= 7 1 7 7 4 > B= 7 0 4 4 0 > 4 7 > 7 4 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 intersect(3m_sets) ismember(3m_sets) ismember(3m_sets) NAME ismember(3f) - [M_sets] Create a mask of A marking elements also in B SYNOPSIS ismember(A,B) DESCRIPTION Identifies elements of the first set that are members of the second set as well. The returned array is a mask of the first array containing a 1 (aka. "true") where the data in A is found in B. Elsewhere, the array contains 0 (aka. "false"). OPTIONS A input array B input array of values to find in vector A. RETURNS A mask of array A with a 1 at locations where the value in that position in A is also a value that occurs in B, and with a 0 at locations where that value in A was not found in B. EXAMPLES sample program: program demo_ismember use M_sets, only: ismember character(len=*),parameter :: g='(*(g0,1x))' integer, allocatable :: A(:) integer, allocatable :: B(:) write(*,g) 'ISMEMBER', 'Determine which elements of A are also in B.' A=[5,3,4,2] B=[2,4,4,4,6,8] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) ismember(A,B) end program demo_ismember Results: > ISMEMBER Determine which elements of A are also in B. > A= 5 3 4 2 > B= 2 4 4 4 6 8 > 0 0 1 1 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 ismember(3m_sets) issorted(3m_sets) issorted(3m_sets) NAME issorted(3f) - [M_sets] Report if A is sorted in ascending order or not. SYNOPSIS issorted(A) character(len=:)intent(in) :: A or integer|real|complex(in) :: A DESCRIPTION Report if A is sorted in ascending order or not. A 1 (true) is returned when the elements of A are listed in ascending order and 0 (false) otherwise. OPTIONS A input array to test RETURNS 1 if input array A is sorted in ascending order, 0 otherwise EXAMPLES sample program: program demo_issorted use M_sets, only: issorted character(len=*),parameter :: g='(*(g0,1x))' integer,allocatable :: A(:) write(*,g) 'ISSORTED','Find the issorted elements of vector A.' A = [10, -10, 0, 1, 2, 3, 3, 2, 1, -10] write(*,g) 'A=', A write(*,g) issorted(A) A = [-10, 10, 100, 201] write(*,g) 'A=', A write(*,g) issorted(A) end program demo_issorted Results: > ISSORTED Find the issorted elements of vector A. > A= 10 -10 0 1 2 3 3 2 1 -10 > 0 > A= -10 10 100 201 > 1 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 issorted(3m_sets) union(3m_sets) union(3m_sets) NAME union(3f) - [M_sets] Join two sets and removes duplicates of values SYNOPSIS union(A,B, setOrder) DESCRIPTION The two sets are combined and repetitions are removed. OPTIONS A input array B input array setOrder May be "sort" or "stable". If "stable" the values are returned in the order discovered. The default is "sorted", which returns the data in ascending order. EXAMPLES sample program: program demo_union use M_sets, only: union character(len=*),parameter :: g='(*(g0,1x))' integer,allocatable :: A(:) integer,allocatable :: B(:) write(*,g) 'UNION', 'Find the union of vectors A and B.' A=[5, 7, 1] B=[3, 1, 1] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) union(A,B) A=[5, 5, 3] B=[1, 2, 5] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) union(A, B, 'sorted') write(*,g) union(A, B, 'stable') end program demo_union ``` Results: > UNION Find the union of vectors A and B. > A= 5 7 1 > B= 3 1 1 > 1 3 5 7 > A= 5 5 3 > B= 1 2 5 > 1 2 3 5 > 5 3 1 2 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 union(3m_sets) unique(3m_sets) unique(3m_sets) NAME unique(3f) - [M_sets] return unique values in array A SYNOPSIS unique(A,setOrder) DESCRIPTION unique(3) returns the unique values found in an array. That is, it eliminates all but one occurrence of each value. The result is in sorted order by default, but may be returned in the order found. OPTIONS A input array to extract unique values from setOrder May be "sort" or "stable". If "stable" the values are returned in the order discovered. The default is "sorted", which returns the data in ascending order. RETURNS All the values that occur in the input occur in the output just once. All duplicates are removed. EXAMPLES sample program: program demo_unique use M_sets, only: unique character(len=*),parameter :: g='(*(g0,1x))' integer,allocatable :: A(:) write(*,g) 'UNIQUE','Find the unique elements of vector A.' A = [10, -10, 0, 1, 2, 3, 3, 2, 1, -10] write(*,g) 'A=', A write(*,g) unique(A) write(*,g) unique(A, setOrder='stable') end program demo_unique Results: > UNIQUE Find the unique elements of vector A. > A= 10 -10 0 1 2 3 3 2 1 -10 > -10 0 1 2 3 10 > 10 -10 0 1 2 3 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 unique(3m_sets) M_sets(3m_sets) M_sets(3m_sets) NAME M_sets(3f) - [M_sets::INTRO] functions reminiscent of Matlab set functions SYNOPSIS Procedure names and syntax: use M_sets, only : & & union, unique, intersect, setdiff, ismember, setxor use M_sets, only : & & issorted, isequal, bool DESCRIPTION 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 • 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 • isequal(A,B) -Determine if two sets are equal with a tolerance • bool(expr) - 1 if logical expression is true, 0 if false. The subsequent data may be produced sorted, or left in the order encountered. EXAMPLES sample program: program demo_M_sets use M_sets, only: & & unique, intersect, union, setdiff, setxor, bool, & & ismember, issorted, isequal 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) A=[1,2,3,4,5] B=[1,2,3,4,5] ! print all ,nl, & 'ISEQUAL' , & 'confirm whether sets have same elements in same order ' ,nl, & 'A=', A ,nl, & 'B=', B ,nl, & 'is A equal to B?',isequal(A,B) ,nl, & 'is B equal to -B?',isequal(A,-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 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 > > 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 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 M_sets(3m_sets) setdiff(3m_sets) setdiff(3m_sets) NAME setdiff(3f) - [M_sets] Find the values in A that are not in B SYNOPSIS setdiff(A,B, setOrder) DESCRIPTION setdiff(3f) returns the data in A that is not in B, with no repetitions. OPTIONS A input array B input array setOrder May be "sort" or "stable". If "stable" the values are returned in the order discovered. The default is "sorted", which returns the data in ascending order. EXAMPLES sample program: program demo_setdiff use M_sets, only: setdiff character(len=*),parameter :: g='(*(g0,1x))' integer, allocatable :: A(:) integer, allocatable :: B(:) write(*,g) 'SETDIFF','Find the values in A that are not in B.' A=[3, 6, 2, 1, 5, 1, 1] B=[2, 4, 6] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) setdiff(A, B) write(*,g) setdiff([4, 1, 3, 2, 5], [2, 1], 'sorted') write(*,g) setdiff([4, 1, 3, 2, 5], [2, 1], 'stable') end program demo_setdiff Results: > SETDIFF Find the values in A that are not in B. > A= 3 6 2 1 5 1 1 > B= 2 4 6 > 1 3 5 > 3 4 5 > 4 3 5 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 setdiff(3m_sets) setxor(3m_sets) setxor(3m_sets) NAME setxor(3f) - [M_sets] Find values of A and B not in both arrays SYNOPSIS setxor(A,B, setOrder) DESCRIPTION setxfor(3f) returns the exclusive OR of two arrays. That is, it returns the data of A and B that are not in their intersection (the symmetric difference), with no repetitions. Another way of defining the result is that setxor(3f) returns the data that occurs in A or B, but not both. OPTIONS A input array B input array setOrder May be "sort" or "stable". If "stable" the values are returned in the order discovered. The default is "sorted", which returns the data in ascending order. EXAMPLES sample program: program demo_setxor use M_sets, only: setxor character(len=*),parameter :: g='(*(g0,1x))' integer, allocatable :: A(:) integer, allocatable :: B(:) write(*,g) 'SETXOR','Find values of A and B not in their intersection.' A = [5,1,3,3,3] B = [4,1,2] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) setxor(A,B) write(*,g) setxor(A,B,'stable') end program demo_setxor Results: > SETXOR Find values of A and B not in their intersection. > A= 5 1 3 3 3 > B= 4 1 2 > 2 3 4 5 > 5 3 4 2 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 setxor(3m_sets) isequal(3m_sets) isequal(3m_sets) NAME isequal(3f) - [M_sets] Report if vector A is equal to vector B SYNOPSIS isequal(A,B,TOLERANCE) character(len=:)intent(in) :: A,B or integer|real|complex(in) :: A,B real,optional :: TOLERANCE DESCRIPTION Report if A is equal to B. Equality is defined as the same element values in the same order. A 1 (true) is returned when the elements of A have a one-to-one correspondence to the elements of B with the same values in the same order. 0 (false) is returned otherwise. OPTIONS A input array to compare against B input array to compare to A TOLERANCE for numeric values consider the corresponding elements of A and B equal if they are equal within the specified tolerance. A,B and TOLERANCE are of the same type and kind. RETURNS 1 if input array A is to array B, 0 otherwise EXAMPLES sample program: program demo_isequal use M_sets, only: isequal character(len=*),parameter :: g='(*(g0,1x))' integer,allocatable :: A(:) integer,allocatable :: B(:) write(*,g) 'isequal','Find if A is equal to B. ' A = [10, -10, 0, 1, 2, 3, 3, 2, 1,-10] B = [10, -10, 0, 1, 2, 3, 3, 2, 1, 10] write(*,g) 'A=', A write(*,g) 'B=', B write(*,g) isequal(A,B) write(*,g) 'isequal','Find if A is equal to A. ' write(*,g) isequal(A,A) end program demo_isequal Results: > isequal Find the isequal elements of vector A. > A= 10 -10 0 1 2 3 3 2 1 -10 > 0 > A= -10 10 100 201 > 1 AUTHORS John S. Urban, 2023-07-20 LICENSE CC0-1.0 May 11, 2025 isequal(3m_sets) bool(3m_sets) bool(3m_sets) NAME bool(3f) - [M_sets::LOGICAL] returns One if expression is TRUE, else returns Zero. (LICENSE:PD) SYNOPSIS pure elemental integer function bool(expr) logical,intent(in) :: expr or character(len=:)intent(in) :: expr or integer|real|complex(in) :: expr DESCRIPTION bool(3f) returns an integer 1 given a true logical expression. OPTIONS expr A logical expression. If any other intrinsic type a blank string is TRUE as well as 0. 0.0, and (0.0,0.0). Non-zero numeric values and non-blank or non-null strings are FALSE. RETURNS The result is a default INTEGER value of 1 if the expression is TRUE, and a 0 otherwise. EXAMPLES Sample usage: program demo_bool use M_sets, only: bool implicit none write (*, *) 'is 10 < 20 ?', bool(10 < 20) write (*, *) 'elemental', bool([2 > 1, 3 == 4, 10 < 5, 100 > 50]) if (sum(bool([2 > 1, 3 == 4, 10 < 5, 100 > 50])) >= 2) then write (*, *) 'two or more are true' endif end program demo_bool Results: > is 10 < 20 ? 1 > elemental 1 0 0 1 > two or more are true AUTHOR John S. Urban LICENSE CC0-1.0 May 11, 2025 bool(3m_sets)