Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A memory efficient form of computing the null space of a matrix modulo 2
| ResourceFunction["BitStringNullSpace"][{n1,n2,…},ncols] treats the integers ni as vectors of zeros and ones, each having ncols bits, and returns integers representing a set of generators of the null space modulo 2. | 
Find the modulo-2 null space of a simple matrix:
| In[1]:= | ![ResourceFunction["BitStringNullSpace"][{1, 2, 3}, 3]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/599007df215d1cc7.png) | 
| Out[1]= |  | 
Create a random matrix of bit vectors:
| In[2]:= | ![SeedRandom[1111];
nrows = 7;
ncols = 9;
mat = RandomInteger[{0, 1}, {nrows, ncols}];
bitvecs = Map[FromDigits[#, 2] &, mat]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/3bb0841490b60efe.png) | 
| Out[6]= |  | 
Find the 0‐1 null vectors mod 2, represented as integers:
| In[7]:= | ![nullvecs = ResourceFunction["BitStringNullSpace"][bitvecs, ncols]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/326cfa60c1d6d441.png) | 
| Out[7]= |  | 
Create a large array of bit strings to use as a matrix:
| In[8]:= | ![SeedRandom[1111];
bignrows = 1777;
bigncols = 1789;
bigbitvecs = Table[FromDigits[RandomInteger[{0, 1}, bigncols], 2], bignrows];](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/535a321e480afa48.png) | 
BitStringNullSpace is especially useful when working with matrices that might otherwise require excessive memory:
| In[9]:= | ![Timing[bigbitnulls = ResourceFunction["BitStringNullSpace"][bigbitvecs, bigncols];]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/31df249207daf7e5.png) | 
| Out[9]= |  | 
Here is the matrix from which the bit vectors were created:
| In[10]:= |  | 
| Out[10]= |  | 
We see that the prior result agrees with NullSpace:
| In[11]:= | ![nulls = NullSpace[mat, Modulus -> 2]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/774651c5f95e110e.png) | 
| Out[11]= |  | 
| In[12]:= | ![Map[FromDigits[#, 2] &, nulls]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/064bb03aaabc2fb8.png) | 
| Out[12]= |  | 
| In[13]:= |  | 
| Out[13]= |  | 
Similarly, create a matrix of explicit 0‐1 vectors corresponding to the large integers:
| In[14]:= | ![bigmat = Map[IntegerDigits[#, 2, bigncols] &, bigbitvecs];](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/3090cac2fb2053c2.png) | 
NullSpace is substantially slower as compared to BitStringNullSpace:
| In[15]:= | ![Timing[bigbitnulls2 = NullSpace[bigmat, Modulus -> 2];]](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/1305b1911791bd8f.png) | 
| Out[15]= |  | 
Check that the results agree:
| In[16]:= | ![Map[FromDigits[#, 2] &, bigbitnulls2] === bigbitnulls](https://www.wolframcloud.com/obj/resourcesystem/images/344/34432cf3-2754-4c3f-a1dc-71c30e01d54a/2b023062c3395364.png) | 
| Out[16]= |  | 
This work is licensed under a Creative Commons Attribution 4.0 International License