Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Some SageMath code related to Chapters 1 and 2
An Introduction to Sets and Combinatorics using Sage
Applied Discrete Structures by Alan Doerr & Kenneth Levasseur is licensed under a Creative Commons Attribution - Noncommercial - No Derivative Works 3.0 United States License.
Here are a few tips on how to get started using Sage to work with sets and combinatorial calculations. For more details on the introductory theory, see Chapters 1 and 2 of Applied Discrete Structures.
Sets
A set is an expression of the form set(list). By wrapping a list with set, the order of elements appearing in the list and their duplication are ignored. For example, L1 and L2 are two different list, but notice how as sets they are considered equal:
The standard set operations are all methods and/or functions that can act on Sage sets.
Intersection:
Notice that the union isn't sorted in any obvious way
The union operation is also a method, as are the other set operations.
Caution: sorting a union strips away the set wrapper.
Set difference, or complementation:
You can easily convert the result to list, but in practice the result is often a very large list. Not the case here:
Here is a simple loop using the generator object.
Here is an example where the generator goes through the 32768 different subsets of the integers from 0 to 14 and tallies the cardinalities.
Set-builder notation in Sage
Syntax for building a set by logical selection from a "universe" is remarkably similar to mathematical notation. Here the universe is the set {0, 1, 2, ..., 99} and we select the primes from that set.
A list of rational numbers reduced to lowest terms with denominator less than 9 can be generated as follows.
We define R to the the integers modulo 81, (see Section 11.3 of Applied Discrete Structures). We then build the list of solutions to the equation 69 x = 15.
Cartesian Products
Here, we append a parity bit to each element of B3 to produce eight sequence of four bits, all with even parity.
Basic Combinatorial Calculations
The factorial function is method attached to integers
Binomial coefficients are computed by the function binomial.
The binomial_coefficents function creates a dictionary of values.
Here is "100 choose 40."