Miscellaneous - Combinatorics

Skip to content
Back to top
View this page
Edit this page

# Miscellaneous

class sage.combinat.misc.DoublyLinkedList(l)

  Bases: `object`

  A doubly linked list class that provides constant time hiding and unhiding of entries.

  Note that this list’s indexing is 1-based.

  EXAMPLES:

  Sage

      sage: dll = sage.combinat.misc.DoublyLinkedList([1,2,3]); dll
      Doubly linked list of [1, 2, 3]: [1, 2, 3]
      sage: dll.hide(1); dll
      Doubly linked list of [1, 2, 3]: [2, 3]
      sage: dll.unhide(1); dll
      Doubly linked list of [1, 2, 3]: [1, 2, 3]
      sage: dll.hide(2); dll
      Doubly linked list of [1, 2, 3]: [1, 3]
      sage: dll.unhide(2); dll
      Doubly linked list of [1, 2, 3]: [1, 2, 3]

  Python

      >>> from sage.all import *
      >>> dll = sage.combinat.misc.DoublyLinkedList([Integer(1),Integer(2),Integer(3)]); dll
      Doubly linked list of [1, 2, 3]: [1, 2, 3]
      >>> dll.hide(Integer(1)); dll
      Doubly linked list of [1, 2, 3]: [2, 3]
      >>> dll.unhide(Integer(1)); dll
      Doubly linked list of [1, 2, 3]: [1, 2, 3]
      >>> dll.hide(Integer(2)); dll
      Doubly linked list of [1, 2, 3]: [1, 3]
      >>> dll.unhide(Integer(2)); dll
      Doubly linked list of [1, 2, 3]: [1, 2, 3]

  head()
  hide(i)
  next(j)
  prev(j)
  unhide(i)

class sage.combinat.misc.IterableFunctionCall(f, *args, **kwargs)

  Bases: `object`

  This class wraps functions with a yield statement (generators) by an object that can be iterated over. For example,

  EXAMPLES:

  Sage

      sage: def f(): yield 'a'; yield 'b'

  Python

      >>> from sage.all import *
      >>> def f(): yield 'a'; yield 'b'

  This does not work:

  Sage

      sage: for z in f: print(z)
      Traceback (most recent call last):
      ...
      TypeError: 'function' object is not iterable

  [Python]

      >>> from sage.all import *
      >>> for z in f: print(z)
      Traceback (most recent call last):
      ...
      TypeError: 'function' object is not iterable

  Use IterableFunctionCall if you want something like the above to work:

  Sage

      sage: from sage.combinat.misc import IterableFunctionCall
      sage: g = IterableFunctionCall(f)
      sage: for z in g: print(z)
      a
      b

  Python

      >>> from sage.all import *
      >>> from sage.combinat.misc import IterableFunctionCall
      >>> g = IterableFunctionCall(f)
      >>> for z in g: print(z)
      a
      b

  If your function takes arguments, just put them after the function name. You needn’t enclose them in a tuple or anything, just put them there:

  Sage

      sage: def f(n, m): yield 'a' * n; yield 'b' * m; yield 'foo'
      sage: g = IterableFunctionCall(f, 2, 3)
      sage: for z in g: print(z)
      aa
      bbb
      foo

  [Python]

      >>> from sage.all import *
      >>> def f(n, m): yield 'a' * n; yield 'b' * m; yield 'foo'
      >>> g = IterableFunctionCall(f, Integer(2), Integer(3))
      >>> for z in g: print(z)
      aa
      bbb
      foo

sage.combinat.misc.check_integer_list_constraints(l, **kwargs)

  EXAMPLES:

  Sage

      sage: from sage.combinat.misc import check_integer_list_constraints
      sage: cilc = check_integer_list_constraints
      sage: l = [[2,1,3],[1,2],[3,3],[4,1,1]]
      sage: cilc(l, min_part=2)
      [[3, 3]]
      sage: cilc(l, max_part=2)
      [[1, 2]]
      sage: cilc(l, length=2)
      [[1, 2], [3, 3]]
      sage: cilc(l, max_length=2)
      [[1, 2], [3, 3]]
      sage: cilc(l, min_length=3)
      [[2, 1, 3], [4, 1, 1]]
      sage: cilc(l, max_slope=0)
      [[3, 3], [4, 1, 1]]
      sage: cilc(l, min_slope=1)
      [[1, 2]]
      sage: cilc(l, outer=[2,2])
      [[1, 2]]
      sage: cilc(l, inner=[2,2])
      [[3, 3]]

  Python

      >>> from sage.all import *
      >>> from sage.combinat.misc import check_integer_list_constraints
      >>> cilc = check_integer_list_constraints
      >>> l = [[Integer(2),Integer(1),Integer(3)],[Integer(1),Integer(2)],[Integer(3),Integer(3)],[Integer(4),Integer(1),Integer(1)]]
      >>> cilc(l, min_part=Integer(2))
      [[3, 3]]
      >>> cilc(l, max_part=Integer(2))
      [[1, 2]]
      >>> cilc(l, length=Integer(2))
      [[1, 2], [3, 3]]
      >>> cilc(l, max_length=Integer(2))
      [[1, 2], [3, 3]]
      >>> cilc(l, min_length=Integer(3))
      [[2, 1, 3], [4, 1, 1]]
      >>> cilc(l, max_slope=Integer(0))
      [[3, 3], [4, 1, 1]]
      >>> cilc(l, min_slope=Integer(1))
      [[1, 2]]
      >>> cilc(l, outer=[Integer(2),Integer(2)])
      [[1, 2]]
      >>> cilc(l, inner=[Integer(2),Integer(2)])
      [[3, 3]]

  Sage

      sage: cilc([1,2,3], length=3, singleton=True)
      [1, 2, 3]
      sage: cilc([1,2,3], length=2, singleton=True) is None
      True

  [Python]

      >>> from sage.all import *
      >>> cilc([Integer(1),Integer(2),Integer(3)], length=Integer(3), singleton=True)
      [1, 2, 3]
      >>> cilc([Integer(1),Integer(2),Integer(3)], length=Integer(2), singleton=True) is None
      True

sage.combinat.misc.umbral_operation(poly)

  Return the umbral operation \(\downarrow\) applied to poly.

  The umbral operation replaces each instance of \(x_i^{a_i}\) with \(x_i*(x_i - 1)*\cdots*(x_i - a_i + 1)\).

  EXAMPLES:

  Sage

      sage: P = PolynomialRing(QQ, 2, 'x')
      sage: x = P.gens()
      sage: from sage.combinat.misc import umbral_operation
      sage: umbral_operation(x[0]^3) == x[0]*(x[0]-1)*(x[0]-2)
      True
      sage: umbral_operation(x[0]*x[1])
      x0*x1
      sage: umbral_operation(x[0]+x[1])
      x0 + x1
      sage: umbral_operation(x[0]^2*x[1]^2) == x[0]*(x[0]-1)*x[1]*(x[1]-1)
      True

  Python

      >>> from sage.all import *
      >>> P = PolynomialRing(QQ, Integer(2), 'x')
      >>> x = P.gens()
      >>> from sage.combinat.misc import umbral_operation
      >>> umbral_operation(x[Integer(0)]**Integer(3)) == x[Integer(0)]*(x[Integer(0)]-Integer(1))*(x[Integer(0)]-Integer(2))
      True
      >>> umbral_operation(x[Integer(0)]*x[Integer(1)])
      x0*x1
      >>> umbral_operation(x[Integer(0)]+x[Integer(1)])
      x0 + x1
      >>> umbral_operation(x[Integer(0)]**Integer(2)*x[Integer(1)]**Integer(2)) == x[Integer(0)]*(x[Integer(0)]-Integer(1))*x[Integer(1)]*(x[Integer(1)]-Integer(1))
      True