cube_solver.cube.utils module

Cube utils module.

cube_solver.cube.utils.get_orientation_array(coord, v, n, force_modulo=False)[source]

Get orientation array.

Given an orientation coordinate number coord with a value between 0 and v ^ n - 1, where v is the number of possible orientation values, returns a unique orientation array of length n with values between 0 and v - 1 for every possible orientation coordinate value.

If force_modulo is True, given an orientation coordinate number with a value between 0 and v ^ (n - 1) - 1, returns a unique orientation array with total orientation 0 modulo v.

Parameters:
  • coord (int) – Orientation coordinate number with a value between 0 and v ^ n - 1, or between 0 and v ^ (n - 1) - 1 if force_modulo is True.

  • v (int) – Number of possible orientation values.

  • n (int) – Length of the orientation array.

  • force_modulo (bool, optional) – If True, returns a unique orientation array with total orientation 0 modulo v. Default is False.

Returns:

orientation – Orientation array of length n with values between 0 and v - 1.

Return type:

ndarray

Examples

>>> from cube_solver.cube.utils import get_orientation_array
>>> get_orientation_array(6, 3, 3)
array([0, 2, 0])
>>> get_orientation_array(6, 3, 3, force_modulo=True)
array([2, 0, 1])
cube_solver.cube.utils.get_orientation_coord(orientation, v, is_modulo=False)[source]

Get orientation coordinate number.

Given an orientation array of length n with values between 0 and v - 1, where v is the number of possible orientation values, returns a unique orientation coordinate number with a value between 0 and v ^ n - 1 for every possible orientation array.

If is_modulo is True, given an orientation array with total orientation 0 modulo v, returns a unique orientation coordinate number with a value between 0 and v ^ (n - 1) - 1.

Parameters:
  • orientation (ndarray) – Orientation array of length n with values between 0 and v - 1.

  • v (int) – Number of possible orientation values.

  • is_modulo (bool, optional) – If True, returns a unique orientation coordinate number for every possible orientation array with total orientation 0 modulo v. Default is False.

Returns:

coord – Orientation coordinate number with a value between 0 and v ^ n - 1, or between 0 and v ^ (n - 1) - 1 if is_modulo is True.

Return type:

int

Examples

>>> import numpy as np
>>> from cube_solver.cube.utils import get_orientation_coord
>>> get_orientation_coord(np.array([0, 2, 0]), 3)
6
>>> get_orientation_coord(np.array([2, 0, 1]), 3, is_modulo=True)
6
cube_solver.cube.utils.get_permutation_array(coord, n, force_even_parity=False)[source]

Get permutation array and permutation parity.

Given a permutation coordinate number coord with a value between 0 and n! - 1, returns a unique permutation array of length n with values between 0 and n - 1 for every possible permutation coordinate value. Also returns the permutation parity: True if the permutation array has odd parity, False if it has even parity.

If force_even_parity is True, given a permutation coordinate number with a value between 0 and n! / 2 - 1, returns a unique permutation array with even parity.

Parameters:
  • coord (int) – Permutation coordinate number with a value between 0 and n! - 1, or between 0 and n! / 2 - 1 if force_even_parity is True.

  • n (int) – Length of the permutation array.

  • force_even_parity (bool, optional) – If True, returns a unique permutation array with even parity. Default is False.

Returns:

  • permutation (ndarray) – Permutation array of length n with values between 0 and n - 1.

  • parity (bool) – Permutation parity: True if the permutation array has odd parity, False if it has even parity.

Return type:

Tuple[ndarray, bool]

Examples

>>> from cube_solver.cube.utils import get_permutation_array
>>> get_permutation_array(2, 3)
(array([1, 0, 2]), True)
>>> get_permutation_array(2, 3, force_even_parity=True)
(array([2, 0, 1]), False)
cube_solver.cube.utils.get_permutation_coord(permutation, is_even_parity=False)[source]

Get permutation coordinate number.

Given a permutation array of length n with n different values, returns a unique permutation coordinate number with a value between 0 and n! - 1 for every possible permutation array.

If is_even_parity is True, given a permutation array with even parity, returns a unique permutation coordinate number with a value between 0 and n! / 2 - 1.

Parameters:
  • permutation (ndarray) – Permutation array of length n with n different values.

  • is_even_parity (bool, optional) – If True, returns a unique permutation coordinate number for every possible permutation array with even parity. Default is False.

Returns:

coord – Permutation coordinate number with a value between 0 and n! - 1, or between 0 and n! / 2 - 1 if is_even_parity is True.

Return type:

int

Examples

>>> import numpy as np
>>> from cube_solver.cube.utils import get_permutation_coord
>>> get_permutation_coord(np.array([1, 0, 2]))
2
>>> get_permutation_coord(np.array([2, 0, 1]), is_even_parity=True)
2
cube_solver.cube.utils.get_permutation_parity(permutation)[source]

Get permutation parity.

Given a permutation array of length n with n different values, returns the permutation parity: True if the permutation array has odd parity, False if it has even parity.

Parameters:

permutation (ndarray) – Permutation array of length n with n different values.

Returns:

parity – Permutation parity: True if the permutation array has odd parity, False if it has even parity.

Return type:

bool

Examples

>>> import numpy as np
>>> from cube_solver.cube.utils import get_permutation_parity
>>> get_permutation_parity(np.array([1, 0, 2]))
True
>>> get_permutation_parity(np.array([2, 0, 1]))
False
cube_solver.cube.utils.get_combination_array(coord, n)[source]

Get combination array.

Given a combination coordinate number coord with a value between 0 and C(m, n) - 1, where m - 1 is the maximum value of the combination array and n <= m, returns a unique combination array of length n with values in increasing order between 0 and m - 1 for every possible combination coordinate value.

Parameters:
  • coord (int) – Combination coordinate number with a value between 0 and C(m, n) - 1, where m - 1 is the maximum value of the combination array and n <= m.

  • n (int) – Length of the combination array.

Returns:

combination – Combination array of length n with values in increasing order between 0 and m - 1.

Return type:

ndarray

Examples

>>> from cube_solver.cube.utils import get_combination_array
>>> get_combination_array(3, 2)
array([0, 3])
>>> get_combination_array(6, 3)
array([1, 2, 4])
cube_solver.cube.utils.get_combination_coord(combination)[source]

Get combination coordinate number.

Given a combination array of length n with values in increasing order between 0 and m - 1, where m - 1 is the maximum value of the combination array and n <= m, returns a unique combination coordinate number with a value between 0 and C(m, n) - 1 for every possible combination array.

Parameters:

combination (ndarray) – Combination array of length n with values in increasing order between 0 and m - 1.

Returns:

coord – Combination coordinate number with a value between 0 and C(m, n) - 1, where m - 1 is the maximum value of the combination array and n <= m.

Return type:

int

Examples

>>> import numpy as np
>>> from cube_solver.cube.utils import get_combination_coord
>>> get_combination_coord(np.array([0, 3]))
3
>>> get_combination_coord(np.array([1, 2, 4]))
6
cube_solver.cube.utils.get_partial_permutation_array(coord, n)[source]

Get permutation array and combination array.

Given a partial permutation coordinate number coord with a value between 0 and P(m, n) - 1, where m - 1 is the maximum value of the combination array and n <= m, returns a unique pair of a permutation array of length n with values between 0 and n - 1, and a combination array of length n with values in increasing order between 0 and m - 1, for every possible partial permutation coordinate value.

Parameters:
  • coord (int) – Partial permutation coordinate number with a value between 0 and P(m, n) - 1, where m - 1 is the maximum value of the combination array and n <= m.

  • n (int) – Length of the permutation array and the combination array.

Returns:

  • permutation (ndarray) – Permutation array of length n with values between 0 and n - 1.

  • combination (ndarray) – Combination array of length n with values in increasing order between 0 and m - 1.

Return type:

Tuple[ndarray, ndarray]

Examples

>>> from cube_solver.cube.utils import get_partial_permutation_array
>>> get_partial_permutation_array(6, 2)
(array([0, 1]), array([0, 3]))
>>> get_partial_permutation_array(38, 3)
(array([1, 0, 2]), array([1, 2, 4]))
cube_solver.cube.utils.get_partial_permutation_coord(permutation, combination)[source]

Ger partial permutation coordinate number.

Given a permutation array of length n with n different values, and a combination array of length n with values in increasing order between 0 and m - 1, where m - 1 is the maximum value of the combination array and n <= m, returns a unique partial permutation coordinate number with a value between 0 and P(m, n) - 1 for every possible pair of permutation array and combination array.

Parameters:
  • permutation (ndarray) – Permutation array of length n with n different values.

  • combination (ndarray) – Combination array of length n with values in increasing order between 0 and m - 1.

Returns:

coord – Partial permutation coordinate number with a value between 0 and P(m, n) - 1, where m - 1 is the maximum value of the combination array and n <= m.

Return type:

int

Examples

>>> import numpy as np
>>> from cube_solver.cube.utils import get_partial_permutation_coord
>>> get_partial_permutation_coord(np.array([0, 1]), np.array([0, 3]))
6
>>> get_partial_permutation_coord(np.array([1, 0, 2]), np.array([1, 2, 4]))
38