Macro Library setexp

Version 1.0: May 18, 2022
Created by Avery Flowers

A collection of functions designed to aid in the creation of basic set questions.

Note: All variables used in these functions must be single alphabetical characters. Greek words or symbols are not supported.


A table of valid terms for set operations.
Operation Acceptable Terms
Intersection (∩) nn, cap
Union (∪) uu, cup
Complement (c) ^c, ' (a single apostrophe)
Symmetric Difference (⊖) xor, ominus, triangle, oplus
Set Difference (-) - (\ is not supported)

setexpequals(exp1,exp2,vars,[contents = array()],[universe = array()])

Details
Input: exp1: A set expression.
exp2: A set expression.
vars: An array/list of variables (must be single alphabetical characters).
contents: (OPTIONAL) An array of array/lists. The ith entry in contents is an array/list containing the elements within the ith variable in the vars array.
universe: (OPTIONAL) An array/list of elements within the universal set.
Output: 1 (TRUE) if exp1 is equal to exp2, 0 (FALSE) otherwise.
Note: If contents and universe are not provided, then the function will assume full generality for the variables provided.
Example:

setexpequals("(A nn B)^c", "A^c uu B^c", "A,B")

will return TRUE, as the two expressions are equal in all scenarios.

setexpequals("A", "A nn B", "A,B", array("1","1,2"), "1,2,3")

will return TRUE, since the sets A and (A nn B) are the same when A = {1}, B = {1,2} and the universe is {1,2,3}.

setexpsubset(exp1,exp2,vars,[contents = array()],[universe = array()])

Details
Input: exp1: A set expression.
exp2: A set expression.
vars: An array/list of variables (must be single alphabetical characters).
contents: (OPTIONAL) An array of array/lists. The ith entry in contents is an array/list containing the elements within the ith variable in the vars array.
universe: (OPTIONAL) An array/list of elements within the universal set.
Output: 1 (TRUE) if exp1 is a subset of exp2, 0 (FALSE) otherwise.
Note: If contents and universe are not provided, then the function will assume full generality for the variables provided.
Example:

setexpsubset("(A nn B)", "A", "A,B")

will return TRUE, as (A n B) is a subset of A in all scenarios.

setexpsubset("A", "B^c", "A,B", array("1,3","1,2"), "1,2,3")

will return FALSE, since A is not a subset of B^c when A={1,3} and B={1,2} and the universe is {1,2,3}

setexpevaluate(exp,vars,contents,universe)

Details
Input: exp: A set expression.
vars: An array/list of variables (must be single alphabetical characters).
contents: An array of array/lists. The ith entry in contents is an array/list containing the elements within the ith variable in the vars array.
universe: An array/list of elements within the universal set.
Output: An array listing the elements contained within exp.
Example:

setexpevalaute("(A^c nn B)", "A,B", array("1","2"), "1,2,3")

will return array(2), since (A^c n B) = {2} when A = {1} and B = {2} and the universe is {1,2,3}.

setexpmakepretty(exp)

Details
Input: exp: A set expression.
Output: A 'clean' version of the same expression meant for display. This removes things like double complements, unnecessary parentheses, and so on.

setexpsteps(exp)

Details
Input: exp: A set expression.
Output: An array of subexpressions of exp, each entry applying one operation to the previous entry until the last entry is exp. This shows the 'steps' required to evaluate or construct exp.
Example:

setexpsteps('(A nn B) uu A^c')

would return array('A nn B', 'A^c', '(A nn B) uu A^c') since these are the steps necessary to evaluate '(A nn B) uu A^c'.

setexprand(vars,ops,bnum,unum)

Details
Input: vars: An array (or list) of variables.
ops: An array (or list) of binary operations (nn,uu,xor,minus)
bnum: An array (or list) of integers between 1 and 4
unum: An array (or list) of integers between 0 and 2*(the minimum entry in bnum)+1
Output: A random set expression containing variables from the vars array, operations from the ops array. The number of binary operations will be a random element of bnum. The number of negations will be a random element of unum. Tries to avoid 'silly' expressions such as (A nn B) uu (B nn A) for example.
Example:

setexprand("A,B,C","nn,uu,xor",2,1)

Returns a random expression using A, B, C as sets with 2 binary operations (with options and/or/xor only) and 1 negation symbol. For instance, it could return the string 'C nn (A xor B)^c'.

setexplegend()

Details
Output: An simple string of HTML syntax that generates a table showing keyboard shortcuts for entering set expressions.

venn2diagram(vars,[shade = ""],[labels = ""],[size = 200])

Details
Input: vars: An array/list of 2 variables (single alphabetical characters).
shade: (OPTIONAL) A set expression using the variables within vars to shade on the venn diagram.
labels: (OPTIONAL) An array/list of 2 strings. The first string will be the label on the diagram corresponding to the first variable in vars.
size: (OPTIONAL) The size (both width and height) of the diagram (in pixels).
Output: A 2-circle Venn diagram image, with the region given in shade shaded red, and the labels given in labels applied to each circle.
Example:

venn3diagram("A,B","A cap B")

Creates an image of a 2-circle Venn diagram with the region (A n B) shaded.

venn3diagram(vars,[shade = ""],[labels = ""],[size = 250])

Details
Input: vars: An array/list of 3 variables (single alphabetical characters).
shade: (OPTIONAL) A set expression using the variables within vars to shade on the venn diagram.
labels: (OPTIONAL) An array/list of 3 strings. The first string will be the label on the diagram corresponding to the first variable in vars.
size: (OPTIONAL) The size (both width and height) of the diagram (in pixels).
Output: A 3-circle Venn diagram image, with the region given in shade shaded red, and the labels given in labels applied to each circle.
Example:

venn3diagram("A,B,C","A cap B")

Creates an image of a 3-circle Venn diagram with the region (A n B) shaded.