From 428eb2308b164bd835c1023a576952822b2d85d1 Mon Sep 17 00:00:00 2001 From: Shahnawaz Khan Date: Thu, 14 Dec 2023 16:31:03 +0530 Subject: [PATCH 1/4] Added Xor Basis --- pyrival/linear_algebra/xor_basis.py | 45 +++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 pyrival/linear_algebra/xor_basis.py diff --git a/pyrival/linear_algebra/xor_basis.py b/pyrival/linear_algebra/xor_basis.py new file mode 100644 index 0000000..571c548 --- /dev/null +++ b/pyrival/linear_algebra/xor_basis.py @@ -0,0 +1,45 @@ +class XorBasis: + " Linear basis for xor " + + def __init__(self, bit_length=31): + self.bit_length=bit_length + self.basis_size=0 + self.val = [0]*self.bit_length + + def is_redundant(self, x): + " Returns true if x can be represented as xor of some already inserted elements" + + for i in range(self.bit_length): + if (x>>i)&1: + if self.val[i]==0: + return False + x^=self.val[i] + return True + + def insert(self, x): + "Adds x to the basis if it is not redundant. Returns true if x is added to the basis" + for i in range(self.bit_length): + if (x>>i)&1: + if self.val[i]==0: + self.val[i]=x + self.basis_size+=1 + return True + x^=self.val[i] + return False + + def __len__(self): + return self.basis_size + + def __getitem__(self, i): + return self.val[i] + + def __contains__(self, x): + return self.is_redundant(x) + + def __repr__(self): + " Prints the basis in descending order " + print("Basis:") + for i in range(self.bit_length-1, -1, -1): + if self.val[i]!=0: + print(bin(self.val[i])[2:].zfill(self.bit_length)) + return "" From 8e98ac0ed1393544a07d537c3b6c0dc7a230bbe9 Mon Sep 17 00:00:00 2001 From: Shahnawaz Khan Date: Thu, 14 Dec 2023 20:49:50 +0530 Subject: [PATCH 2/4] Modified Code for Xor basis --- pyrival/linear_algebra/xor_basis.py | 39 +++++++++++------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/pyrival/linear_algebra/xor_basis.py b/pyrival/linear_algebra/xor_basis.py index 571c548..4a9eb96 100644 --- a/pyrival/linear_algebra/xor_basis.py +++ b/pyrival/linear_algebra/xor_basis.py @@ -2,36 +2,28 @@ class XorBasis: " Linear basis for xor " def __init__(self, bit_length=31): - self.bit_length=bit_length - self.basis_size=0 - self.val = [0]*self.bit_length + self.basis = [] + self.bit_length = bit_length def is_redundant(self, x): " Returns true if x can be represented as xor of some already inserted elements" - for i in range(self.bit_length): - if (x>>i)&1: - if self.val[i]==0: - return False - x^=self.val[i] - return True + return self._reduce(x)==0 def insert(self, x): "Adds x to the basis if it is not redundant. Returns true if x is added to the basis" - for i in range(self.bit_length): - if (x>>i)&1: - if self.val[i]==0: - self.val[i]=x - self.basis_size+=1 - return True - x^=self.val[i] - return False + x = self._reduce(x) + if x: + self.basis.append(x) + return x!=0 - def __len__(self): - return self.basis_size + def _reduce(self, x): + for b in self.basis: + x = min(x, x ^ b) + return x - def __getitem__(self, i): - return self.val[i] + def __len__(self): + return len(self.basis) def __contains__(self, x): return self.is_redundant(x) @@ -39,7 +31,6 @@ def __contains__(self, x): def __repr__(self): " Prints the basis in descending order " print("Basis:") - for i in range(self.bit_length-1, -1, -1): - if self.val[i]!=0: - print(bin(self.val[i])[2:].zfill(self.bit_length)) + for b in self.basis: + print(bin(b)[2:].zfill(self.bit_length)) return "" From adda504638a0a5ad6b9fd677c94557edbc501922 Mon Sep 17 00:00:00 2001 From: Shahnawaz Khan Date: Tue, 19 Dec 2023 15:41:02 +0530 Subject: [PATCH 3/4] Remove bit_length parameter from constructor --- pyrival/linear_algebra/xor_basis.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/pyrival/linear_algebra/xor_basis.py b/pyrival/linear_algebra/xor_basis.py index 4a9eb96..e23aa33 100644 --- a/pyrival/linear_algebra/xor_basis.py +++ b/pyrival/linear_algebra/xor_basis.py @@ -1,14 +1,8 @@ class XorBasis: " Linear basis for xor " - def __init__(self, bit_length=31): + def __init__(self): self.basis = [] - self.bit_length = bit_length - - def is_redundant(self, x): - " Returns true if x can be represented as xor of some already inserted elements" - - return self._reduce(x)==0 def insert(self, x): "Adds x to the basis if it is not redundant. Returns true if x is added to the basis" @@ -26,11 +20,15 @@ def __len__(self): return len(self.basis) def __contains__(self, x): - return self.is_redundant(x) + " Returns true if x can be represented as xor of some already inserted elements" + return self._reduce(x)==0 def __repr__(self): " Prints the basis in descending order " - print("Basis:") + bit_length = max(self.basis).bit_length() + repr_str = "" + repr_str += "Basis:" for b in self.basis: - print(bin(b)[2:].zfill(self.bit_length)) - return "" + repr_str+='\n' + repr_str+= bin(b)[2:].zfill(bit_length) + return repr_str \ No newline at end of file From 353d91a0a7cc8a75fa7e21496afabe8d1b6e56c6 Mon Sep 17 00:00:00 2001 From: Shahnawaz Khan Date: Tue, 19 Dec 2023 15:44:12 +0530 Subject: [PATCH 4/4] Sort basis before printing --- pyrival/linear_algebra/xor_basis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyrival/linear_algebra/xor_basis.py b/pyrival/linear_algebra/xor_basis.py index e23aa33..edacd61 100644 --- a/pyrival/linear_algebra/xor_basis.py +++ b/pyrival/linear_algebra/xor_basis.py @@ -25,6 +25,7 @@ def __contains__(self, x): def __repr__(self): " Prints the basis in descending order " + self.basis.sort(reverse=True) bit_length = max(self.basis).bit_length() repr_str = "" repr_str += "Basis:"