vectors_in_intervals.utility

Utility functions

Functions

interval_from_bounds(lower_bound, upper_bound)

Construct an intervals.

random_interval([ring, allow_infinity, ...])

Generate a random interval.

sb_child(cfl, left)

Return a child of an element in the Stern-Brocot tree.

simplest_element_in_interval(interval)

Return the simplest rational element in an interval.

simplest_rational_in_interval(interval)

Find the rational with smallest denominator in a given interval.

solve_left_for_roots(A, b)

Find a solution for x*A = b that works for matrices with roots.

solve_without_division(A, b)

Solve a linear system of equations without division.

Classes

CombinationsIncluding(mset, k[, elements])

Combinatorical object of all combinations that include given elements

class vectors_in_intervals.utility.CombinationsIncluding(mset, k, elements=None)

Combinatorical object of all combinations that include given elements

EXAMPLES:

We generate all subsets of range(4) with 2 elements that include the element 2:

sage: from vectors_in_intervals.utility import CombinationsIncluding
sage: C = CombinationsIncluding(4, 2, [2])
sage: list(C)
[[0, 2], [1, 2], [2, 3]]
sage: list(reversed(C))
[[2, 3], [1, 2], [0, 2]]
vectors_in_intervals.utility.interval_from_bounds(lower_bound, upper_bound, lower_closed: bool = True, upper_closed: bool = True) sage.sets.real_set.RealSet

Construct an intervals.

INPUT:

  • lower_bound – a lower bound

  • upper_bound – an upper bound

  • lower_closed – a boolean (default: True)

  • upper_closed – a boolean (default: True)

OUTPUT:

A RealSet object.

  • lower_bound and upper_bound are the left and right interval values. If lower_bound > upper_bound, those elements will be exchanged.

  • lower_closed and upper_closed determine the intervals.

  • The left (or right) interval half of the interval is

    • closed if lower_closed (or upper_closed) is True (default).

    • open if lower_closed (or upper_closed) is False.

EXAMPLES:

sage: from vectors_in_intervals.utility import interval_from_bounds
sage: interval_from_bounds(5, 6)
[5, 6]
sage: interval_from_bounds(6, 5, False, True)
(5, 6]
sage: interval_from_bounds(5, 5, False, True)
{}
sage: interval_from_bounds(-oo, 5)
(-oo, 5]
sage: interval_from_bounds(0, oo, False, False)
(0, +oo)
vectors_in_intervals.utility.random_interval(ring=Rational Field, allow_infinity: bool = True, allow_open: bool = True, allow_empty: bool = False) sage.sets.real_set.RealSet

Generate a random interval.

vectors_in_intervals.utility.sb_child(cfl: list, left: bool)

Return a child of an element in the Stern-Brocot tree.

INPUT:

  • cfl – a list corresponding to a continued fraction

  • left – a boolean

OUTPUT: a list representing the continued fraction of a child of cfl. If left is true, returns the left child of cfl. Otherwise, the right child is returned.

EXAMPLES:

sage: from vectors_in_intervals.utility import sb_child
sage: sb_child([0, 2], True)
[0, 3]
sage: sb_child([0, 2], False)
[0, 1, 2]
sage: parent = continued_fraction_list(5/7)
sage: parent
[0, 1, 2, 2]
sage: l_child = sb_child(parent, True)
sage: l_child
[0, 1, 2, 3]
sage: continued_fraction(l_child).value()
7/10
sage: r_child = sb_child(parent, False)
sage: r_child
[0, 1, 2, 1, 2]
sage: continued_fraction(r_child).value()
8/11
vectors_in_intervals.utility.simplest_element_in_interval(interval: sage.sets.real_set.RealSet)

Return the simplest rational element in an interval.

INPUT:

  • interval – an interval

OUTPUT: If possible, an integer with smallest possible absolute value will be returned. Otherwise, a rational number with smallest possible denominator is constructed.

EXAMPLES:

sage: from vectors_in_intervals.utility import simplest_element_in_interval
sage: I = RealSet((1/2, oo))
sage: simplest_element_in_interval(I)
1
sage: I = RealSet((-oo, 1/2))
sage: simplest_element_in_interval(I)
0
sage: I = RealSet((-19, 0))
sage: simplest_element_in_interval(I)
-1
sage: I = RealSet((0, 1))
sage: simplest_element_in_interval(I)
1/2
sage: I = RealSet((-2/3, 0))
sage: simplest_element_in_interval(I)
-1/2
sage: I = RealSet((4/3, 3/2))
sage: simplest_element_in_interval(I)
7/5
sage: I = RealSet([0, 0])
sage: simplest_element_in_interval(I)
0
sage: I = RealSet([5, 5])
sage: simplest_element_in_interval(I)
5
sage: I = RealSet([sqrt(2), pi/2])
sage: I
[sqrt(2), 1/2*pi]
sage: simplest_element_in_interval(I)
3/2
sage: I = RealSet([1/2, 1/2])
sage: simplest_element_in_interval(I)
1/2
vectors_in_intervals.utility.simplest_rational_in_interval(interval: sage.sets.real_set.RealSet)

Find the rational with smallest denominator in a given interval.

INPUT:

  • interval – an interval that has no integer in it.

EXAMPLES:

sage: from vectors_in_intervals.utility import simplest_rational_in_interval
sage: I = RealSet((0, 1))
sage: simplest_rational_in_interval(I)
1/2
sage: I = RealSet((1/3, 1))
sage: simplest_rational_in_interval(I)
1/2
sage: I = RealSet((4/3, 2))
sage: simplest_rational_in_interval(I)
3/2
sage: I = RealSet((1/3, 1/2))
sage: simplest_rational_in_interval(I)
2/5
sage: I = RealSet((1/2, 241/287))
sage: simplest_rational_in_interval(I)
2/3
vectors_in_intervals.utility.solve_left_for_roots(A, b)

Find a solution for x*A = b that works for matrices with roots.

INPUT:

  • A – a matrix

  • b – a vector

NOTE:

The built in method ``solve_left`` for matrices fails occasionally.
vectors_in_intervals.utility.solve_without_division(A, b)

Solve a linear system of equations without division.

The system is A x = c b where c is a positive constant. Uses an elementary vector.

EXAMPLES:

sage: from vectors_in_intervals.utility import solve_without_division
sage: A = matrix([[1, 2], [0, 1], [1, -1]])
sage: b = vector([1, 0, 1])
sage: solve_without_division(A, b)
(1, 0)
sage: A = matrix([[1, 4], [0, 2], [1, -2]])
sage: b = vector([6, 2, 0])
sage: solve_without_division(A, b)
(4, 2)
sage: A.solve_right(b)
(2, 1)
sage: A = matrix([[1, 1, 1], [0, 1, 2]])
sage: b = vector([2, 3])
sage: solve_without_division(A, b)
(0, 1, 1)