Codes
- ldpc.codes.hamming_code(rank: int) csr_matrix
Outputs a Hamming code parity check matrix given its rank.
The Hamming code parity check matrix is a binary matrix whose columns are all nonzero binary permutations of a fixed set of rank binary vectors. The Hamming code is defined over the binary field GF(2), and the rank specifies the length of the binary vectors. Each column of the parity check matrix corresponds to a binary codeword of the Hamming code.
- Parameters:
rank (int) – The rank of the Hamming code parity check matrix.
- Returns:
The Hamming code parity check matrix in sparse CSR format.
- Return type:
sp.csr_matrix
- Raises:
TypeError – If the input variable ‘rank’ is not of type ‘int’.
Example
>>> print(hamming_code(3).toarray()) [[0 0 0 1 1 1 1] [0 1 1 0 0 1 1] [1 0 1 0 1 0 1]]
- ldpc.codes.random_binary_code(rows: int, cols: int, row_weight: int, seed: int | None = None, variance: float = 0) spmatrix
Generate a random binary code matrix with given dimensions and row weight, optionally with variance on the row weight.
- Parameters:
rows (int) – The number of rows in the matrix, representing the code words.
cols (int) – The number of columns in the matrix, representing the code length.
row_weight (int) – The mean number of non-zero elements in each row.
seed (Optional[int], optional) – The seed for the random number generator (default is None, which does not seed the generator).
variance (float, optional) – The variance in the number of non-zero elements per row (default is 0, which means no variance).
- Returns:
A scipy sparse matrix in CSR format with the specified row weight and dimensions, representing a binary code.
- Return type:
scipy.sparse.spmatrix
- ldpc.codes.rep_code(distance: int) csr_matrix
Outputs repetition code parity check matrix for specified distance. :param distance: The distance of the repetition code. Must be greater than or equal to 2. :type distance: int
- Returns:
The repetition code parity check matrix in sparse CSR matrix format.
- Return type:
sp.csr_matrix
Examples
>>> print(rep_code(5).toarray()) [[1 1 0 0 0] [0 1 1 0 0] [0 0 1 1 0] [0 0 0 1 1]]
- ldpc.codes.ring_code(distance: int) csr_matrix
Outputs ring code (closed-loop repetion code) parity check matrix for a specified distance. :param distance: The distance of the repetition code. Must be greater than or equal to 2. :type distance: int
- Returns:
The repetition code parity check matrix in sparse CSR matrix format.
- Return type:
sp.csr_matrix
Examples
>>> print(ring_code(5).toarray()) [[1 1 0 0 0] [0 1 1 0 0] [0 0 1 1 0] [0 0 0 1 1] [1 0 0 0 1]]