utility

Some utility functions for sequences.

exception rec_sequences.utility.TimeoutError(time)

Bases: Exception

Exception raised if computations did not finish in given time.

Attributes:

  • time – number indicating the seconds used for the computation

  • message – given by “Timeout after s seconds” where s is time.

rec_sequences.utility.ceil_SR(a)

Computes an integer approximation of a.

INPUT:

  • a – a number

OUTPUT:

The smallest integer b with a <= b.

rec_sequences.utility.ceil_SR_bound(a, bound)

Computes a rational approximation of a.

INPUT:

  • a – a real number

  • bound – a number

OUTPUT:

A rational number b with a <= b < bound.

rec_sequences.utility.decode_exceptions(ret)

Checks whether the given value is an exception. If it is, it raises this exception. Otherwise the given value is just returned.

rec_sequences.utility.encode_exceptions(func, *args, **kwargs)

Encodes the exception raised by a function as an object.

INPUT:

  • func – a function

  • time (default: 10) – a number; if positive, it specifies the time func should be run; if not positive it will be ignored

  • *args – additional arguments passed to func

  • **kwargs – additional arguments passed to func

OUTPUT:

Calls the given function func with the given parameters. If the function raises an exception, this exception is returned as an object. Otherwise the usual return value is returned.

rec_sequences.utility.eval_matrix(matrix_sequence, n=1)

Returns the matrix evaluated at an index.

INPUT:

  • matrix_sequence – a matrix over a sequence ring

  • n (default: 1) – a natural number

OUTPUT:

If the matrix \((c_{i,j})_{i,j}\) is given, the matrix \((c_{i,j}(n))_{i,j}\) is returned.

rec_sequences.utility.eval_vector(vector_sequence, n)

Returns the vector evaluated at an index.

INPUT:

  • vector_sequence – a vector over a sequence ring

  • n (default: 1) – a natural number

OUTPUT:

If the vector \((c_{i})_{i}\) is given, the vector \((c_{i}(n))_{i}\) is returned.

rec_sequences.utility.is_root_of_unity(a)

INPUT:

  • a – a number

OUTPUT:

Returns True if a is a root of unity and False otherwise.

rec_sequences.utility.log(cls, msg, level=0, time=- 1, time2=- 1)

Assumes that the given class has a logger object called log. Tries to output the given message msg as info using this logger.

These messages can be displayed using:

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

INPUT:

  • cls – a class having a logger object called log

  • msg – a string; the message that is shown by the logger

  • level – a natural number, indicating the indentation of the message

  • time – if time2 is not given, this is a specific time

  • time2 – used to compute the time-span from time to time2 to indicate how long a computation took.

OUTPUT:

A string which contains leading whitespaces (the amount depends on level), the specified message msg and the given time.

rec_sequences.utility.matrix_subsequence(matrix_sequence, u, v=0)

Returns the matrix subsequence.

INPUT:

  • matrix_sequence – a matrix over a sequence ring

  • u – a rational number

  • v (optional) – a rational number

OUTPUT:

If the matrix \((c_{i,j}(n))_{i,j}\) is given, the matrix \((c_{i,j}(\operatorname{floor}(u n + v)))_{i,j}\) is returned.

rec_sequences.utility.shift_matrix(matrix_sequence, n=1)

Returns the matrix shifted.

INPUT:

  • matrix_sequence – a matrix over a sequence ring

  • n (default: 1) – a natural number

OUTPUT:

If the matrix \((c_{i,j})_{i,j}\) is given, the matrix \((\sigma^n c_{i,j})_{i,j}\) is returned.

rec_sequences.utility.shift_rat_vector(vector_sequence, n=1)

Returns the vector shifted.

INPUT:

  • vector_sequence – a vector over a rational function ring

  • n (default: 1) – a natural number

OUTPUT:

If the vector \((c_{i})_{i}\) is given, the vector \((\sigma^n c_{i})_{i}\) is returned.

rec_sequences.utility.shift_vector(vector_sequence, n=1)

Returns the vector shifted.

INPUT:

  • vector_sequence – a vector over a sequence ring

  • n (default: 1) – a natural number

OUTPUT:

If the vector \((c_{i})_{i}\) is given, the vector \((\sigma^n c_{i})_{i}\) is returned.

rec_sequences.utility.split_list(l, r)

Splits a list into lists of equal size.

INPUT:

  • l – a list

  • r – a natural number which divides len(l).

OUTPUT:

The list l split into r lists of equal size

rec_sequences.utility.split_list_rec(l, r)

Splits a list recursively into lists of equal size.

INPUT:

  • l – a list

  • r – a list of natural numbers such that the product of these is precisely len(l).

OUTPUT:

The list l split into r[0] lists of equal size which itself are split into r[1] lists of equal size, etc.

rec_sequences.utility.timeout(func, time=10, *args, **kwargs)

Run a method func in a forked subprocess for the given time and with the the given arguments. If time is not positive, the method is might run indefinitely if the function does not return.

INPUT:

  • func – a function

  • time (default: 10) – a number; if positive, it specifies the time func should be run; if not positive it will be ignored

  • *args – additional arguments passed to func

  • **kwargs – additional arguments passed to func

OUTPUT:

Returns func(*args, **kwargs). If the function does not finish in the given time, a TimeoutError is raised. Exceptions raised by func are raised again.

rec_sequences.utility.vector_interlace(vector_sequences, initial_values=[])

Returns the interlacing of the given vectors of sequences.

INPUT:

  • vector_sequences – container of vectors of sequences over a common ring.

  • initial_values – list of vectors in the ground field.

OUTPUT:

Interlacing of vector_sequences with the prepended values initial_values at the beginning.

rec_sequences.utility.vector_subsequence(vector_sequence, u, v=0)

Returns the vector subsequence.

INPUT:

  • vector_sequence – a vector over a sequence ring

  • u – a rational number

  • v (optional) – a rational number

OUTPUT:

If the vector \((c_{i}(n))_{i}\) is given, the vector \((c_{i}(\operatorname{floor}(u n + v)))_{i}\) is returned.