Wednesday 30 March 2016

Julia: Sets


Set is a collection of unique elements (Don’t contain duplicate elements).

How to create a Set
You can create a set using Set([iter]) function.

Set([itr]): Create a set by using the values in given iterator object.

julia> mySet=Set(Any[1, "hari krishna", "Gurram", 10.234, true])
Set(Any["Gurram","hari krishna",10.234,true])

julia> typeof(mySet)
Set{Any}

julia> mySet=Set(Int64[2, 3, 5, 7, 11])
Set([7,2,3,11,5])

julia> typeof(mySet)
Set{Int64}


IntSet([itr])
Construct a set of sorted positive integers.
julia> mySet=IntSet([7, 3, 5, 2])
IntSet([2, 3, 5, 7])

julia> typeof(mySet)
IntSet


You will get ArgumentError, when you try to insert negative elements into an intSet.
julia> mySet=IntSet([2, 3, 5, 7, -10, -11])
ERROR: ArgumentError: IntSet elements cannot be negative
 in push! at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib
 in call at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib


Union of sets
union(s1, s2...) :
(s1, s2...)
Construct the union of sets
julia> set1=IntSet([2, 3, 5, 7])
IntSet([2, 3, 5, 7])

julia> set2=IntSet([1, 2, 3, 4, 5, 6, 7])
IntSet([1, 2, 3, 4, 5, 6, 7])

julia> union(set1, set2)
IntSet([1, 2, 3, 4, 5, 6, 7])


Intersection of two sets
intersect(s1, s2...)
(s1, s2)
Construct the intersection of two sets.
julia> set1
IntSet([2, 3, 5, 7])

julia> set2
IntSet([1, 2, 3, 4, 5, 6, 7])

julia> intersect(set1, set2)
IntSet([2, 3, 5, 7])


intersect!(s1, s2): Find the intersection of sets s1 and s2 and store the result in set s1.
julia> set1
IntSet([1, 4, 6, 8, 11, 13])

julia> set2
IntSet([1, 4, 8, 12])

julia> intersect!(set1, set2)
IntSet([1, 4, 8])

julia> set1
IntSet([1, 4, 8])


Get the set difference
setdiff(s1, s2) : s1-s2 return the elements from s1 but not in s2.
julia> set2
IntSet([1, 2, 3, 4, 5, 6, 7])

julia> set1
IntSet([2, 3, 5, 7])

julia> setdiff(set2, set1)
IntSet([1, 4, 6])

julia> setdiff(set1, set2)
IntSet([])


Remove elements from set
setdiff!(s, iterable): Remove elements of iterable from set s.
julia> set1
IntSet([2, 3, 5, 7])

julia> setdiff!(set1, [5, 2, 15])
IntSet([3, 7])

julia> set1
IntSet([3, 7])


Symmetric difference between sets
symdiff(s1, s2...): Return the symmetric difference between sets. The symmetric difference of two sets is the collection of elements which are members of either set but not both.
julia> set1
IntSet([3, 7])

julia> set2
IntSet([1, 2, 3, 4, 5, 6, 7])

julia> symdiff(set1, set2)
IntSet([1, 2, 4, 5, 6])


symdiff!(s1, s2): Calculate the symmetric difference between sets s1 and s2 and store the result in s1.
julia> set1=IntSet([1, 2, 3, 4, 5, 6,7,8])
IntSet([1, 2, 3, 4, 5, 6, 7, 8])

julia> set2=IntSet([2, 3, 5,7, 11, 13])
IntSet([2, 3, 5, 7, 11, 13])

julia> symdiff!(set1, set2)
IntSet([1, 4, 6, 8, 11, 13])

julia> set1
IntSet([1, 4, 6, 8, 11, 13])


Toggle the element in set
symdiff!(s, n): Toggle the element n in set. If set s has element ‘n’, it removes it, else it add ‘n’ to the set s.
julia> set1
IntSet([3, 7])

julia> symdiff!(set1, 3)
IntSet([7])

julia> set1
IntSet([7])

julia> symdiff!(set1, 3)
IntSet([3, 7])

julia> set1
IntSet([3, 7])


Toggle many elements in set
symdiff!(s, itr): Toggle all the elements in itr.
julia> set1=IntSet([1, 2, 3, 4, 5, 6, 7, 8, 9])
IntSet([1, 2, 3, 4, 5, 6, 7, 8, 9])

julia> arr=[2, 3, 5, 7, 11]
5-element Array{Int64,1}:
  2
  3
  5
  7
 11

julia> symdiff!(set1, arr)
IntSet([1, 4, 6, 8, 9, 11])

julia> set1
IntSet([1, 4, 6, 8, 9, 11])

julia> symdiff!(set1, arr)
IntSet([1, 2, 3, 4, 5, 6, 7, 8, 9])

julia> set1
IntSet([1, 2, 3, 4, 5, 6, 7, 8, 9])


Complement of set
Suppose ‘U’ is universal set. The complement of A is the set of elements of the universal set that are not elements of A.
julia> set1
IntSet([1, 4, 6, 8, 11, 13])

julia> set2=complement(set1)
IntSet([0, 2, 3, 5, 7, 9, 10, 12, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, ..., 9223372036854775806])


Check whether given set is subset or not
issubset(A, S) Bool
(A, S) Bool
Return true if A is a subset or equal to S.
julia> set1=IntSet([2, 4, 6, 8])
IntSet([2, 4, 6, 8])

julia> set2=IntSet([2, 4])
IntSet([2, 4])

julia> set3=IntSet([1, 3])
IntSet([1, 3])

julia> issubset(set2, set1)
true

julia> issubset(set3, set1)
false




Previous                                                 Next                                                 Home

No comments:

Post a Comment