matpopmod.utils

This module regroups miscellaneous general-purpose tools that are used by other modules but are typically not meant to be used by the end user.

exception matpopmod.utils.CannotCompute

Bases: matpopmod.utils.InadequateMatrix

Raised to indicate that a quantity cannot be computed numerically.

exception matpopmod.utils.InadequateMatrix

Bases: Exception

Raised to indicate non-specific problems with a matrix argument.

exception matpopmod.utils.IncorrectDims

Bases: matpopmod.utils.InadequateMatrix

Raised to indicate a problem in the dimensions of matrices.

exception matpopmod.utils.MissingArguments

Bases: Exception

Raise to indicate missing arguments.

exception matpopmod.utils.MissingEntries

Bases: matpopmod.utils.InadequateMatrix

Raised to indicate that a matrix contains nan entries.

exception matpopmod.utils.NegativeEntries

Bases: matpopmod.utils.InadequateMatrix

Raised to indicate that a matrix contains negative entries.

exception matpopmod.utils.NonConvergentMatrix

Bases: Exception

Raised to indicate that a matrix is (numerically) not convergent.

exception matpopmod.utils.NotAvailable

Bases: Exception

Exception raised when a descriptor cannot be computed, either because only the projection matrix is known and the computation requires the survival matrix S and the fertility matrix F, or because there is a problem of some sort with the model (e.g, singular matrix S).

This exception is not meant to be used is situations where the fact that a descriptor cannot be computed does not necessarily indicate a problem with the model but simply that the descriptor is not defined (as can happen, e.g, when the projection matrix is not primitive). In this case, a warning is issued and nan values are returned.

exception matpopmod.utils.UnexpectedMathError

Bases: Exception

Raised to indicate failure of a mathematical assertion that can only result from numerical errors or a serious bug in the code.

matpopmod.utils.assert_nonnegative(M, name='')

Check that the NumPy array M has non-negative entries; raise MissingEntries or NegativeEntries otherwise.

matpopmod.utils.assert_nonnegative_number(n, name='')

Check that the argument is a non-negative number. Raises appropriate exceptions otherwise.

matpopmod.utils.assert_positive_integer(n, name='')

Check that the argument is a positive integer. Raises appropriate exceptions otherwise.

matpopmod.utils.assert_same_dimensions(M, N, name_M='', name_N='')

Check that the NumPy arrays M and N have the same dimensions; raise IncorrectDims otherwise.

matpopmod.utils.assert_square(M, name='')

Check that the NumPy array M is a square matrix; raise IncorrectDims otherwise.

matpopmod.utils.assert_substochastic(M, name='')

Check that the entries of the NumPy array M are non-negative, that each of the columns sums to at most 1, and that at least one of them sums to less than 1. Raise appropriate exceptions otherwise.

matpopmod.utils.assert_vector(vect, length, name='')

Check that vect is a vector of length length. Raise exceptions otherwise. The argument name is used in error messages.

matpopmod.utils.matrix_of_nan(n)

Returns a 2-D NumPy array of np.nan with dimension (n, n).

matpopmod.utils.parse_array_string(data)

Converts a string representing a matrix into a list of lists. Rows have to be separated by semicolons and columns by spaces and/or commas. Example:

>>> convert_from_string("1 2; 3 4")
[[1, 2], [3, 4]]

Copied from NumPy’s deprecated matrices _convert_from_string().

matpopmod.utils.represents_integer(n)

Whether the argument corresponds to an integer, be it a Python int / float or a NumPy int_ / float_.

matpopmod.utils.set_rng_seed(n)
matpopmod.utils.vector_of_nan(n)

Returns a 1-D NumPy array of np.nan with length n.