Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » lists and sequences » partition
next | previous | forward | backward | up | index | toc

partition -- partition a set or list by values of a function

Description

Output is a HashTable whose keys are the possible values of the function f on the elements of the list, set or tally L and the corresponding value of the hash table is the list, set or tally of elements of L which take that value under f. The optional third argument I is a list whose elements will be included as keys in the return value, allowing empty lists to appear in the partition.

i1 : L = {1,3,6,5,3,1,2,8,8,8}

o1 = {1, 3, 6, 5, 3, 1, 2, 8, 8, 8}

o1 : List
i2 : partition(L, odd)

o2 = HashTable{false => {6, 2, 8, 8, 8}}
               true => {1, 3, 5, 3, 1}

o2 : HashTable
i3 : partition(set L, odd)

o3 = HashTable{false => set {8, 2, 6}}
               true => set {1, 5, 3}

o3 : HashTable
i4 : partition(tally L, odd)

o4 = HashTable{false => Tally{2 => 1}}
                              6 => 1
                              8 => 3
               true => Tally{1 => 2}
                             3 => 2
                             5 => 1

o4 : HashTable
i5 : partition ({3,3,5},even, {true,false})

o5 = HashTable{false => {3, 3, 5}}
               true => {}

o5 : HashTable

It is allowed to swap the order of the first two arguments.

i6 : L = {1,3,6,5,3,1,2,8,8,8}

o6 = {1, 3, 6, 5, 3, 1, 2, 8, 8, 8}

o6 : List
i7 : partition(odd, L)

o7 = HashTable{false => {6, 2, 8, 8, 8}}
               true => {1, 3, 5, 3, 1}

o7 : HashTable
i8 : partition(odd, set L)

o8 = HashTable{false => set {8, 2, 6}}
               true => set {1, 5, 3}

o8 : HashTable
i9 : partition(odd, tally L)

o9 = HashTable{false => Tally{2 => 1}}
                              6 => 1
                              8 => 3
               true => Tally{1 => 2}
                             3 => 2
                             5 => 1

o9 : HashTable
i10 : partition (even, {3,3,5},{true,false})

o10 = HashTable{false => {3, 3, 5}}
                true => {}

o10 : HashTable

The following example partitions the generators of an ideal by degree.

i11 : R = QQ[a..f]

o11 = R

o11 : PolynomialRing
i12 : I = ideal"ab,ade,ac3,d4,b3,adf,f4,e10"

                            3   4   3          4   10
o12 = ideal (a*b, a*d*e, a*c , d , b , a*d*f, f , e  )

o12 : Ideal of R
i13 : partition(flatten entries gens I, f -> first degree f)

o13 = HashTable{2 => {a*b}             }
                              3
                3 => {a*d*e, b , a*d*f}
                         3   4   4
                4 => {a*c , d , f }
                        10
                10 => {e  }

o13 : HashTable

When the second function argument is provided, it will be applied to the grouped values after the partition is made.

i14 : partition(0..10, odd, x -> x^2)

o14 = HashTable{false => (0, 4, 16, 36, 64, 100)}
                true => (1, 9, 25, 49, 81)

o14 : HashTable

This example demonstrates the fact that $x^2 + 1 \equiv 0 \pmod{p}$ is solvable iff $p \equiv 1 \pmod{4}$.

i15 : someprimes = for i from 1 to 10 list nextPrime(100 * i)

o15 = {101, 211, 307, 401, 503, 601, 701, 809, 907, 1009}

o15 : List
i16 : netList pairs partition(someprimes, p -> legendreSymbol(-1, p), p -> p %4)

      +-----------------------+
o16 = |(1, {1, 1, 1, 1, 1, 1})|
      +-----------------------+
      |(-1, {3, 3, 3, 3})     |
      +-----------------------+

If you have already assembled a table of data this pattern can be conveniently used to group by one column while focusing in on another.

i17 : someprimes = for i from 1 to 10 list nextPrime(100 * i);
i18 : data = apply(someprimes, p -> {p, legendreSymbol(-1, p), p % 4});
i19 : netList data

      +----+--+-+
o19 = |101 |1 |1|
      +----+--+-+
      |211 |-1|3|
      +----+--+-+
      |307 |-1|3|
      +----+--+-+
      |401 |1 |1|
      +----+--+-+
      |503 |-1|3|
      +----+--+-+
      |601 |1 |1|
      +----+--+-+
      |701 |1 |1|
      +----+--+-+
      |809 |1 |1|
      +----+--+-+
      |907 |-1|3|
      +----+--+-+
      |1009|1 |1|
      +----+--+-+
i20 : netList pairs partition(data, row -> row_1, row -> row_2)

      +-----------------------+
o20 = |(1, {1, 1, 1, 1, 1, 1})|
      +-----------------------+
      |(-1, {3, 3, 3, 3})     |
      +-----------------------+

As in use of partition without applying a function to the values, you can provide a List of key-values to ensure appear in the result - possibly with an empty container of values.

i21 : someprimes = for i from 1 to 10 list nextPrime(100 * i);
i22 : data = apply(someprimes, p -> {p, legendreSymbol(-1, p), p % 4});
i23 : netList data

      +----+--+-+
o23 = |101 |1 |1|
      +----+--+-+
      |211 |-1|3|
      +----+--+-+
      |307 |-1|3|
      +----+--+-+
      |401 |1 |1|
      +----+--+-+
      |503 |-1|3|
      +----+--+-+
      |601 |1 |1|
      +----+--+-+
      |701 |1 |1|
      +----+--+-+
      |809 |1 |1|
      +----+--+-+
      |907 |-1|3|
      +----+--+-+
      |1009|1 |1|
      +----+--+-+
i24 : netList pairs partition(data, row -> row_2, {0, 1, 2, 3}, row -> row_1)

      +-----------------------+
o24 = |(0, {})                |
      +-----------------------+
      |(1, {1, 1, 1, 1, 1, 1})|
      +-----------------------+
      |(2, {})                |
      +-----------------------+
      |(3, {-1, -1, -1, -1})  |
      +-----------------------+

When applying a function to map the values of the partition of a VirtualTally, the function is applied to the keys of the partitioned tallies and any key collisions are combined.

i25 : t = tally {"apple", "apricot", "apple", "avocado", "beet", "banana", "berry"}

o25 = Tally{apple => 2  }
            apricot => 1
            avocado => 1
            banana => 1
            beet => 1
            berry => 1

o25 : Tally
i26 : partition(t, x -> x_0)

o26 = HashTable{"a" => Tally{apple => 2  }}
                             apricot => 1
                             avocado => 1
                "b" => Tally{banana => 1}
                             beet => 1
                             berry => 1

o26 : HashTable
i27 : partition(t, x -> x_0, x -> x_{0..1})

o27 = HashTable{"a" => Tally{ap => 3}}
                             av => 1
                "b" => Tally{ba => 1}
                             be => 2

o27 : HashTable
i28 : partition(t, x -> x_{0..1}, x -> x_0) -- tallies from different groups are not combined

o28 = HashTable{"ap" => Tally{a => 3}}
                "av" => Tally{a => 1}
                "ba" => Tally{b => 1}
                "be" => Tally{b => 2}

o28 : HashTable

See also

Ways to use partition:

  • partition(Function,VirtualTally)
  • partition(Function,VirtualTally,Function)
  • partition(Function,VirtualTally,VisibleList)
  • partition(Function,VirtualTally,VisibleList,Function)
  • partition(Function,VisibleList)
  • partition(Function,VisibleList,Function)
  • partition(Function,VisibleList,VisibleList)
  • partition(Function,VisibleList,VisibleList,Function)
  • partition(VirtualTally,Function)
  • partition(VirtualTally,Function,Function)
  • partition(VirtualTally,Function,VisibleList)
  • partition(VirtualTally,Function,VisibleList,Function)
  • partition(VisibleList,Function)
  • partition(VisibleList,Function,Function)
  • partition(VisibleList,Function,VisibleList)
  • partition(VisibleList,Function,VisibleList,Function)

For the programmer

The object partition is a method function.


The source of this document is in /build/reproducible-path/macaulay2-1.26.11+ds/M2/Macaulay2/packages/Macaulay2Doc/functions/partition-doc.m2:114:0.