In
set theorySet theory is the branch of mathematics that studies sets, which are collections of objects. Although any type of object can be collected into a set, set theory is applied most often to objects that are relevant to mathematics...
, a
complement of a set
A refers to things not in (that is, things outside of),
A. The
relative complement of
A with respect to a set
B, is the set of elements in
B but not in
A. When all sets under consideration are considered to be subsets of a given set
U, the
absolute complement of
A is the set of all elements in
U but not in
A.
Relative complement
If
A and
B are sets, then the
relative complement of
A in
B, also known as the
set-theoretic difference of
B and
A, is the set of elements in
B, but not in
A.
The relative complement of
A in
B is denoted
B ∖
A according to the ISO 31-11 standard (sometimes written
B −
A, but this notation is ambiguous, as in some contexts it can be interpreted as the set of all
b −
a, where
b is taken from
B and
a from
A).
Formally
Examples:
- {1,2,3} ∖ {2,3,4} = {1}
- {2,3,4} ∖ {1,2,3} = {4}
- If
is the set of real numberIn mathematics, a real number is a value that represents a quantity along a continuum, such as -5 , 4/3 , 8.6 , √2 and π...
s and
is the set of rational numberIn mathematics, a rational number is any number that can be expressed as the quotient or fraction a/b of two integers, with the denominator b not equal to zero. Since b may be equal to 1, every integer is a rational number...
s, then
is the set of irrational numberIn mathematics, an irrational number is any real number that cannot be expressed as a ratio a/b, where a and b are integers, with b non-zero, and is therefore not a rational number....
s.
The following lists some notable properties of relative complements in relation to the set-theoretic
operationThe general operation as explained on this page should not be confused with the more specific operators on vector spaces. For a notion in elementary mathematics, see arithmetic operation....
s of
unionIn set theory, the union of a collection of sets is the set of all distinct elements in the collection. The union of a collection of sets S_1, S_2, S_3, \dots , S_n\,\! gives a set S_1 \cup S_2 \cup S_3 \cup \dots \cup S_n.- Definition :...
and
intersectionIn mathematics, the intersection of two sets A and B is the set that contains all elements of A that also belong to B , but no other elements....
.
If
A,
B, and
C are sets, then the following
identitiesIn mathematics, the term identity has several different important meanings:*An identity is a relation which is tautologically true. This means that whatever the number or value may be, the answer stays the same. For example, algebraically, this occurs if an equation is satisfied for all values of...
hold:
- C ∖ (A ∩ B) = (C ∖ A)∪(C ∖ B)
- C ∖ (A ∪ B) = (C ∖ A)∩(C ∖ B)
- C ∖ (B ∖ A) = (A ∩ C)∪(C ∖ B)
- (B ∖ A) ∩ C = (B ∩ C) ∖ A = B∩(C ∖ A)
- (B ∖ A) ∪ C = (B ∪ C) ∖ (A ∖ C)
- A ∖ A = Ø
- Ø ∖ A = Ø
- A ∖ Ø = A
Absolute complement
If a
universeIn mathematics, and particularly in set theory and the foundations of mathematics, a universe is a class that contains all the entities one wishes to consider in a given situation...
U is defined, then the relative complement of
A in
U is called the
absolute complement (or simply
complement) of
A, and is denoted by
Ac or sometimes
A′, also the same set often is denoted by
Complement laws:
-




- (this follows from the equivalence of a conditional with its contrapositive)
Involution or double complement law:
-
Relationships between relative and absolute complements:
-
- A ∖ B = A ∩ Bc
- (A ∖ B)c = Ac ∪ B
The first two complement laws above shows that if
A is a non-empty, proper subset of
U, then {
A,
Ac} is a
partitionIn mathematics, a partition of a set X is a division of X into non-overlapping and non-empty "parts" or "blocks" or "cells" that cover all of X...
of
U.
Notation
In the
LaTeXLaTeX is a document markup language and document preparation system for the TeX typesetting program. Within the typesetting system, its name is styled as . The term LaTeX refers only to the language in which documents are written, not to the editor used to write those documents. In order to...
typesetting language, the command
\setminus is usually used for rendering a set difference symbol, which is similar to a
backslashThe backslash is a typographical mark used mainly in computing. It was first introduced to computers in 1960 by Bob Bemer. Sometimes called a reverse solidus or a slosh, it is the mirror image of the common slash....
symbol. When rendered the
\setminus command looks identical to
\backslash except that it has a little more space in front and behind the slash, akin to the LaTeX sequence
\mathbin{\backslash}. A variant
\smallsetminus is available in the amssymb package.
Complements in various programming languages
Some programming languages allow for manipulation of
sets as data structuresIn computer science, a set is an abstract data structure that can store certain values, without any particular order, and no repeated values. It is a computer implementation of the mathematical concept of a finite set...
, using these operators or functions to construct the difference of sets
a and
b:
SQLSQL is a programming language designed for managing data in relational database management systems ....
SELECT * FROM B LEFT OUTER JOIN A ON B.COLUMN = A.COLUMN WHERE A.ID IS NULL
MathematicaMathematica is a computational software program used in scientific, engineering, and mathematical fields and other areas of technical computing...
Complement
MATLABMATLAB is a numerical computing environment and fourth-generation programming language. Developed by MathWorks, MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages,...
setdiff
MathMLMathematical Markup Language is an application of XML for describing mathematical notations and capturing both its structure and content. It aims at integrating mathematical formulae into World Wide Web pages and other documents...
<apply xmlns="http://www.w3.org/1998/Math/MathML"> <setdiff/> <ci type="set">A</ci> <ci type="set">B</ci></apply>
PascalPascal is an influential imperative and procedural programming language, designed in 1968/9 and published in 1970 by Niklaus Wirth as a small and efficient language intended to encourage good programming practices using structured programming and data structuring.A derivative known as Object Pascal...
SetDifference := a - b;
PythonPython is a general-purpose, high-level programming language whose design philosophy emphasizes code readability. Python claims to "[combine] remarkable power with very clear syntax", and its standard library is large and comprehensive...
diff = a.difference(b)
diff = a - b
JavaJava is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
diff = a.clone;
- diff.removeAll(b);
Scala
diff = a -- b
C++C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as an intermediate-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell...
set_difference(a.begin, a.end, b.begin, b.end, result.begin);
.NET FrameworkThe .NET Framework is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability...
a.Except(b);
HaskellHaskell is a standardized, general-purpose purely functional programming language, with non-strict semantics and strong static typing. It is named after logician Haskell Curry. In Haskell, "a function is a first-class citizen" of the programming language. As a functional programming language, the...
a \\ b
Common LispCommon Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in ANSI standard document ANSI INCITS 226-1994 , . From the ANSI Common Lisp standard the Common Lisp HyperSpec has been derived for use with web browsers...
set-difference, nset-difference
OCaml
Set.S.diff
Unix shellA Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...
comm -23 a b
PHP
array_diff($a, $b);
RR is a programming language and software environment for statistical computing and graphics. The R language is widely used among statisticians for developing statistical software, and R is widely used for statistical software development and data analysis....
setdiff
RubyRuby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
diff = a - b
Perl
#for perl version >= 5.10
@a = grep {not $_ ~~ @b} @a;