Operating XOR over a defined set of iterating matrices in python

28 Views Asked by At

I have a set of defined rules numbered [1, 2, 4, 8, 16, 32, 64, 128, 256]. Whenever I enter a positive integer, my code converts it into a binary representation of 9-bit. It then calls the rule numbers corresponding to the bit = 1. (Starting from the reverse direction it computes like (20)*bit-value, (21)*bit-value, (2**2)*bit-value... For instance if I consider 11, then my binary rep = 000001011, So it should use Rule_1, Rule_2, and Rule_8. Then finally my code should XOR over these stored rules, i.e. for the example of 11, it should do (Rule_1^Rule_2), then the output of it is to be XORed with Rule_8. My code is storing these matrices, but somehow not XORing properly. I am attaching herewith the snippet where I apply XOR, and the main computation program.

def apply_xor_on_stored_matrices(result_matrices, combination_str):
    if int(combination_str) in [1, 2, 4, 8, 16, 32, 64, 128, 256]:
        # Directly return the stored matrix corresponding to the entered rule number
        rule_number = int(combination_str)
        rule_key = f'Rule_{rule_number}'
        if rule_key in result_matrices:
            print(f"Intermediate Result Matrix for Rule {rule_number}:")
            print_matrix(result_matrices[rule_key])  # Print the intermediate result matrix
            return result_matrices[rule_key]
        else:
            print(f"Rule {rule_number} not found in the result_matrices.")
            return None
    else:
        # Convert the positive integer to binary representation
        binary_representation = format(int(combination_str), '09b')

        # Determine the chosen rules based on the binary representation
#        chosen_rules = [2 ** i for i, bit in enumerate(reversed(binary_representation)) if bit == '1']

        # Initialize the final result matrix
        final_result_matrix = None

        # Apply rules corresponding to non-zero binary digits
        # Apply rules up to the given positive integer for XOR operation
        for i, bit in enumerate(reversed(binary_representation)):
            if bit == '1':
                rule_number = 2 ** i  # Calculate the rule number based on the position of the bit
                if f'Rule_{rule_number}' in result_matrices:
                    result_matrix = result_matrices[f'Rule_{rule_number}']
                    print(f"Intermediate Result Matrix for Rule {rule_number}:")
                    print_matrix(result_matrix)  # Print the intermediate result matrix

                    if final_result_matrix is None:
                        final_result_matrix = result_matrix.copy()
                    else:
                    # Apply XOR operation
                        print(f"Intermediate Result Matrix after XOR with previous result:")
                        intermediate_result = [
                            [a ^ b for a, b in zip(row1, row2)]
                            for row1, row2 in zip(final_result_matrix, result_matrix)
                        ]
                        print_matrix(intermediate_result)
                        final_result_matrix = intermediate_result
#            else:
#                print(f"Rule {rule_number} not found in the result_matrices. Skipping...")

        return final_result_matrix

 def main():
    try:
        boolean_matrix = get_predefined_matrix() 
        boundary_condition = input("Choose a boundary condition (Fixed A / Fixed B / Periodic / Adiabatic / Reflexive): ")
        modified_matrix = apply_boundary_condition(boolean_matrix, boundary_condition)

        result_matrices = {}

        target_sum = int(input("Enter a positive integer: "))
        binary_representation = format(target_sum, '09b')

        print(f"Binary representation: {binary_representation}")

        # Apply rules corresponding to non-zero binary digits
        for i, digit in enumerate(reversed(binary_representation)):
            if digit == '1':
                rule_number = 2 ** i
                apply_rule_and_store(modified_matrix.copy(), rule_number, result_matrices)

        final_result_matrix = apply_xor_on_stored_matrices(result_matrices, binary_representation)
        
        # Print the final result matrix
        print("\nFinal Result Matrix after XOR:")
        print_matrix(final_result_matrix)


    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    main()

I understand that this is not a minimum example that can be reproduced. So, I attach my whole code as an attachment herewith. Any help is appreciated. Thanks.

0

There are 0 best solutions below