Exemple #1
0
    def _add_to_dict_of_indep_bit_vectors(self, z: np.ndarray) -> None:
        """
        This method adds a bit-vector z to the dictionary of independent vectors. It checks the
        provenance (most significant bit) of the vector and only adds it to the dictionary if the
        provenance is not yet found in the dictionary. This guarantees that we can write up a
        resulting matrix in upper-triangular form which by virtue of its form is invertible

        :param z: array containing the bit-vector
        :return: None
        """
        if (z == 0).all() or (z == 1).all():
            return None
        msb_z = utils.most_significant_bit(z)

        # try to add bitstring z to samples dictionary directly
        if msb_z not in self._dict_of_linearly_indep_bit_vectors.keys():
            self._dict_of_linearly_indep_bit_vectors[msb_z] = z
        # if we have a conflict with the provenance of a sample try to create
        # bit-wise XOR vector (guaranteed to be orthogonal to the conflict) and add
        # that to the samples.
        # Bail if this doesn't work and continue sampling.
        else:
            conflict_z = self._dict_of_linearly_indep_bit_vectors[msb_z]
            not_z = [xor(conflict_z[idx], z[idx]) for idx in range(len(z))]
            if (np.asarray(not_z) == 0).all():
                return None
            msb_not_z = utils.most_significant_bit(np.asarray(not_z))
            if msb_not_z not in self._dict_of_linearly_indep_bit_vectors.keys(
            ):
                self._dict_of_linearly_indep_bit_vectors[msb_not_z] = not_z
def test_single_one_leading_zeroes():
    assert u.most_significant_bit(np.array([0, 1, 0, 0])) == 1
def test_multiple_ones_leading_zeroes():
    assert u.most_significant_bit(np.array([0, 0, 1, 1, 0, 1])) == 2
def test_single_one():
    assert u.most_significant_bit(np.array([1])) == 0