Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
As sets are omnipresent in virtually all of mathematics, working with sets in a CAS is crucial feature. Maple has a very rich collection of features for dealing with sets.
Here, we’ll start to scratch the surface of what Maple has to offer.
Naturally, we need to define some sets before manipulating them. Just as mathematical sets can contain a wide variety of items, so too can sets in Maple.
> A := {1, 2, 3, 4, 5}
A := {1, 2, 3, 4, 5}
> B := {3, 4, 5, 6, 7}
B := {3, 4, 5, 6, 7}
> C := {1, 2, {1}, {1, 2, 3}, a, b, x, {α, β}}
C := {1, 2, a, b, x, {1}, {α, β}, {1, 2, 3}}
Just like we used the assignment operator := when working with propositions and open propositions, we also use the assignment operator for sets.
We didn’t use the output suppression operator : at the end of each line. The reason was to show that the elements within the sets can be reordered when the assignment actually happens. Maple has it’s own special ordering when it comes to sets. Of course it doesn’t matter because order does not matter when it comes to sets.
Order does not matter, and neither does repetition.
> A := {3, 4, 5, 6, 7}
A := {1, 2, 3, 4, 5}
> B := {3, 7, 4, 6, 5}
B := {1, 2, 3, 4, 5}
> C := {7, 5, 7, 5, 3, 4, 4, 4, 6, 7, 6}
C := {1, 2, 3, 4, 5}
No matter how many times, or in what order what elements were specified, the output from each set definition was exactly the same.
The central question of Set Theory is whether or not a given object is a member of some specified set. Maple has the ∈ and ∉ operators to test for set membership. However, we still have to evaluate expressions, and not just specify them. We can evaluate logical expressions using evalb and is.
Note that the is command does not work as we would expect it to.
> A := {1, 2, {1}, {1, 2, 3}, a, b, x, {α, β}}
A := {1, 2, a, b, x, {1}, {α, β}, {1, 2, 3}}
> 1 ∈ A
1 ∈ {1, 2, a, b, x, {1}, {α, β}, {1, 2, 3}}
> evalb(1 ∈ A)
true
> is(1 ∈ A)
true
> 3 ∈ A
3 ∈ {1, 2, a, b, x, {1}, {α, β}, {1, 2, 3}}
> evalb(3 ∈ A)
false
> is(3 ∈ A)
true
> evalb(3 ∉ A)
true
> is(3 ∉ A)
true
Even though the element 3 does not appear in the set A, the is command evaluates to true, regardless of whether we use ∈ or ∉. While the evalb command works as expected, the is command does not!
When evaluating set membership in Maple, use the
evalb
command, not the
is
command.
Maple has the subset operator built in. It can be found in the “Common Symbols” palette in the left-hand column of the Maple window.
Curiously, we do not need the evalb or is commands for the subset operator.
> Ψ := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Ψ := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> A := {1, 2, 3, 4, 5}
A := {1, 2, 3, 4, 5}
> B := {3, 4, 5, 6, 7}
B := {3, 4, 5, 6, 7}
> C := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
C := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> A ⊆ Ψ
true
> B ⊆ Ψ
true
> C ⊆ Ψ
true
> {0, 1, 2} ⊆ Ψ
false
> 1 ⊆ Ψ
Error, invalid input: subset received 1, which is not valid for its 1st argument, s1
The subset operator will produce an error if not given a set, as demonstrated in the last command in the above worksheet.
However, there is no built-in proper subset operator. The solution to this conundrum is to define a proper subset operator, which can be done using back ticks ` and the function arrow operator →.
`symbol` := inputs → outputs
Figure 3.A.1: New operators can be defined using the above basic syntax. If there are multiple inputs, they can be separated by commas and enclosed with parenthesis.
> `⊂`:= (A, B) → evalb(A ⊆ B ∧ A ≠ B)
⊂ := (A, B) ↦ evalb(A ⊆ B ∧ A ≠ B)
> Ψ := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Ψ := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> A := {1, 2, 3, 4, 5}
A := {1, 2, 3, 4, 5}
> B := {3, 4, 5, 6, 7}
B := {3, 4, 5, 6, 7}
> C := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
C := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> A ⊂ Ψ
true
> B ⊂ Ψ
true
> C ⊂ Ψ
true
Note that when we define the operator on the first line, the A and B symbols are not referring to the sets A and B used in the later lines of the script. The A and B symbols in the operator definition are purely local to that definition, and are used for placeholders to represent inputs to the operator.
Most of the standard set operations are present in Maple, and come in two flavors as well: symbolic, and textual.
The symbolic operations are available in the “Common Symbols” palette in the “Palettes” pane on the left-hand side of the Maple window. The textual commands are available without needing to import any packages.
Set Operation | Maple Operator | Maple Command |
---|---|---|
∪ | ∪ | union |
∩ | ∩ | intersect |
– | \ | minus |
> A := {1, 2, 3, 4, 5}:
> B := {3, 4, 5, 6, 7}:
> A ∪ B
{1, 2, 3, 4, 5, 6, 7}
> A union B
{1, 2, 3, 4, 5, 6, 7}
> A ∩ B
{3, 4, 5}
> A intersect B
{3, 4, 5}
> A \ B
{1, 2}
> A minus B
{1, 2}
Out of the box, Maple does not offer a symmetric difference operator, but it does have a command called symmdiff that works a little differently than the other set operation commands. For symmdiff, you have to put both sets in parenthesis next to the word symmdiff in order to do the calculation.
Just like with the proper subset operator we defined above, we could also define a symmetric difference operator. We will use the triangle △ operator, as seen in Section 3 of this chapter, as our symbolic operator for handling symmetric difference. You can find the △ symbol in the “Miscellaneous” palette.
> A := {1, 2, 3, 4, 5}:
> B := {3, 4, 5, 6, 7}:
> A symmdiff B
{1, 2, 3, 4, 5} symmdiff {3, 4, 5, 6, 7}
> symmdiff(A, B)
{1, 2, 6, 7}
> `△` := (A, B) → symmdiff(A, B)
`△` := (A, B) ↦ symmdiff(A, B)
> A △ B
{1, 2, 6, 7}
Figure 3.A.2: The select command gives us a way to pick out elements from a given set which satisfy some criteria we choose.
We’ll wrap up this introduction to sets in Maple by discussing how to pick out certain elements in a given set. The way we’ll do that is with a special command called the select command. Basically, the select command will apply some function to each element within a given set. Any element that yields a truth value of true from the function will be collected in a new set, which will be displayed after the select command finishes execution (assuming output is not suppressed by a : operator.)
The first argument we’ll supply is the name of a command we want to apply, where this command determines if a given object has the desired property. This command must either return true, false, or FAIL. The second argument we’ll give to the select command is the name of the actual set from which we want to select elements.
Some functions we may want to use as a criteria need additional arguments. We can supply those additional arguments after the name of the set.
Here is an example demonstrating the two different uses of the select command.
> A := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}:
> select(isprime, A)
{2, 3, 5, 7, 11, 13}
> select(even, A)
Error, predicate in `select` must return true, false, or FAIL, but returned even(1)
> select(type, A, even)
{2, 4, 6, 8, 10, 12, 14}
> select(type, A, primepower)
{1, 2, 3, 4, 5, 7, 8, 9, 11, 13}
Figure 3.A.3: The type command can be used to determine if a given element has some specified type. More types are listed within the documentation linked above.
Care must be taken when using the type command. For more intricate details on its use, and some options available, a link to documentation has been provided in Figure 3.A.3. Most of the options are not going to be terribly useful for us now, but as we grow more accustomed to Maple, and learn about more types of structures in mathematics, they will prove more useful over time.