public static IEnumerable <bool> GetCol(IBitMatrix matrix, int col)
 {
     for (int i = 0; i < matrix.Rows; i++)
     {
         yield return(matrix[i, col]);
     }
 }
 public static IEnumerable <bool> GetRow(IBitMatrix matrix, int row)
 {
     for (int j = 0; j < matrix.Cols; j++)
     {
         yield return(matrix[row, j]);
     }
 }
 public static void XorRows(IBitMatrix matrix, int dst, int src, int col)
 {
     for (int j = col; j < matrix.Cols; j++)
     {
         matrix[dst, j] ^= matrix[src, j];
     }
 }
 public static IEnumerable <int> GetRowWeights(IBitMatrix matrix)
 {
     for (int row = 0; row < matrix.Rows; row++)
     {
         yield return(matrix.GetRowWeight(row));
     }
 }
Esempio n. 5
0
 public static int GetColWeight(IBitMatrix matrix, int col)
 {
     int weight = 0;
     for (int row = 0; row < matrix.Rows; row++)
         weight += matrix[row, col] ? 1 : 0;
     return weight;
 }
 public static IEnumerable <int> GetColWeights(IBitMatrix matrix)
 {
     for (int col = 0; col < matrix.Cols; col++)
     {
         yield return(matrix.GetColWeight(col));
     }
 }
Esempio n. 7
0
        private void ZeroColumn(IBitMatrix matrix, int rows, int j, int k)
        {
            if (threads == 1 || rows < multiThreadedCutoff)
            {
                for (int i = 0; i < rows; i++)
                {
                    if (i != j && matrix[i, k])
                    {
                        matrix.XorRows(i, j, k);
                    }
                }
                return;
            }
            signal.Reset();
            int counter = threads;

            for (int thread = 0; thread < threads; thread++)
            {
                ThreadPool.QueueUserWorkItem(delegate(Object o)
                {
                    for (int i = (int)o; i < rows; i += threads)
                    {
                        if (i != j && matrix[i, k])
                        {
                            matrix.XorRows(i, j, k);
                        }
                    }
                    if (Interlocked.Decrement(ref counter) == 0)
                    {
                        signal.Set();
                    }
                }, thread);
            }
            signal.WaitOne();
        }
Esempio n. 8
0
 public static void Copy(IBitMatrix matrix, IBitMatrix other, int row, int col)
 {
     for (int i = 0; i < other.Rows; i++)
     {
         for (int j = 0; j < other.Cols; j++)
             matrix[row + i, col + j] = matrix[i, j];
     }
 }
Esempio n. 9
0
 private void PrintMatrix(string label, IBitMatrix matrix)
 {
     Console.WriteLine(label);
     for (int i = 0; i < matrix.Rows; i++)
     {
         Console.WriteLine(string.Concat(matrix.GetRow(i).Select(bit => bit ? 1 : 0).ToArray()));
     }
 }
Esempio n. 10
0
 public static IEnumerable<int> GetNonZeroRows(IBitMatrix matrix, int col)
 {
     for (int i = 0; i < matrix.Rows; i++)
     {
         if (matrix[i, col])
             yield return i;
     }
 }
Esempio n. 11
0
 public static IEnumerable<int> GetNonZeroCols(IBitMatrix matrix, int row)
 {
     for (int j = 0; j < matrix.Cols; j++)
     {
         if (matrix[row, j])
             yield return j;
     }
 }
Esempio n. 12
0
 public static void Clear(IBitMatrix matrix)
 {
     for (int i = 0; i < matrix.Rows; i++)
     {
         for (int j = 0; j < matrix.Cols; j++)
             matrix[i, j] = false;
     }
 }
        public static int GetColWeight(IBitMatrix matrix, int col)
        {
            int weight = 0;

            for (int row = 0; row < matrix.Rows; row++)
            {
                weight += matrix[row, col] ? 1 : 0;
            }
            return(weight);
        }
        public static int GetRowWeight(IBitMatrix matrix, int row)
        {
            int weight = 0;

            for (int col = 0; col < matrix.Cols; col++)
            {
                weight += matrix[row, col] ? 1 : 0;
            }
            return(weight);
        }
 public static IEnumerable <int> GetNonZeroRows(IBitMatrix matrix, int col)
 {
     for (int i = 0; i < matrix.Rows; i++)
     {
         if (matrix[i, col])
         {
             yield return(i);
         }
     }
 }
 public static IEnumerable <int> GetNonZeroCols(IBitMatrix matrix, int row)
 {
     for (int j = 0; j < matrix.Cols; j++)
     {
         if (matrix[row, j])
         {
             yield return(j);
         }
     }
 }
 public static void Copy(IBitMatrix matrix, IBitMatrix other, int row, int col)
 {
     for (int i = 0; i < other.Rows; i++)
     {
         for (int j = 0; j < other.Cols; j++)
         {
             matrix[row + i, col + j] = matrix[i, j];
         }
     }
 }
 public static void Clear(IBitMatrix matrix)
 {
     for (int i = 0; i < matrix.Rows; i++)
     {
         for (int j = 0; j < matrix.Cols; j++)
         {
             matrix[i, j] = false;
         }
     }
 }
Esempio n. 19
0
 public BitMatrixSafe(IBitMatrix matrix)
     : this(matrix.RowCount, matrix.ColumnCount)
 {
     for (var row = 0; row < RowCount; ++row)
     {
         for (var column = 0; column < ColumnCount; ++column)
         {
             this[row, column] = matrix[row, column];
         }
     }
 }
 public void Copy(IBitMatrix other, int row, int col)
 {
     if (other is Word64BitMatrix)
     {
         CopySubMatrix((Word64BitMatrix)other, row, col);
         return;
     }
     for (int i = 0; i < other.Rows; i++)
     {
         for (int j = 0; j < other.Cols; j++)
         {
             this[row + i, col + j] = other[i, j];
         }
     }
 }
Esempio n. 21
0
        private static bool IsSolutionValid(IBitMatrix matrix, int rowMin, int rowMax, IBitArray solution)
        {
            int cols = matrix.Cols;

            for (int i = rowMin; i < rowMax; i++)
            {
                bool row = false;
                for (int j = 0; j < cols; j++)
                {
                    row ^= solution[j] & matrix[i, j];
                }
                if (row)
                {
                    return(false);
                }
            }
            return(true);
        }
Esempio n. 22
0
 public static void CopyNonZero(IBitMatrix matrix, IBitMatrix other)
 {
     if (other.IsRowMajor)
     {
         for (int i = 0; i < other.Rows; i++)
         {
             foreach (var j in other.GetNonZeroCols(i))
                 matrix[i, j] = true;
         }
     }
     else
     {
         for (int j = 0; j < other.Cols; j++)
         {
             foreach (var i in other.GetNonZeroRows(j))
                 matrix[i, j] = true;
         }
     }
 }
 public static void CopyNonZero(IBitMatrix matrix, IBitMatrix other)
 {
     if (other.IsRowMajor)
     {
         for (int i = 0; i < other.Rows; i++)
         {
             foreach (var j in other.GetNonZeroCols(i))
             {
                 matrix[i, j] = true;
             }
         }
     }
     else
     {
         for (int j = 0; j < other.Cols; j++)
         {
             foreach (var i in other.GetNonZeroRows(j))
             {
                 matrix[i, j] = true;
             }
         }
     }
 }
        public IEnumerable <IBitArray> Solve(IBitMatrix matrix)
        {
#if DEBUG
            matrixOrig = (IBitMatrix)Activator.CreateInstance(typeof(TMatrix), matrix);
#endif

            if (diagnostics)
            {
                Console.WriteLine("original matrix: {0} rows, {1} cols", matrix.Rows, matrix.Cols);
                Console.WriteLine("merge limit = {0}", mergeLimit);
                timer = new Stopwatch();
                timer.Restart();
            }

            if (matrix.Rows < 1000)
            {
                foreach (var v in solver.Solve((IBitMatrix)Activator.CreateInstance(typeof(TMatrix), matrix)))
                {
                    yield return(v);
                }
                yield break;
            }

            colsOrig    = matrix.Cols;
            this.matrix = matrix;
            var compactMatrix = CompactMatrix();
            if (diagnostics)
            {
                Console.WriteLine("compaction: {0:F3} msec", (double)timer.ElapsedTicks / Stopwatch.Frequency * 1000);
                Console.WriteLine("matrix compacted: {0} rows, {1} cols", compactMatrix.Rows, compactMatrix.Cols);
            }
            foreach (var v in solver.Solve(compactMatrix))
            {
                yield return(GetOriginalSolution(v));
            }
        }
Esempio n. 25
0
 private void ZeroColumn(IBitMatrix matrix, int rows, int j, int k)
 {
     if (threads == 1 || rows < multiThreadedCutoff)
     {
         for (int i = 0; i < rows; i++)
         {
             if (i != j && matrix[i, k])
             {
                 matrix.XorRows(i, j, k);
             }
         }
         return;
     }
     Parallel.For(0, threads, thread =>
     {
         for (int i = thread; i < rows; i += threads)
         {
             if (i != j && matrix[i, k])
             {
                 matrix.XorRows(i, j, k);
             }
         }
     });
 }
Esempio n. 26
0
 public static IEnumerable<bool> GetCol(IBitMatrix matrix, int col)
 {
     for (int i = 0; i < matrix.Rows; i++)
         yield return matrix[i, col];
 }
Esempio n. 27
0
 public BoolBitMatrix(IBitMatrix other)
     : this(other.Rows, other.Cols)
 {
     BitMatrixHelper.CopyNonZero(this, other);
 }
 public Word64BitMatrix(IBitMatrix matrix)
     : this(matrix.Rows, matrix.Cols)
 {
     BitMatrixHelper.CopyNonZero(this, matrix);
 }
Esempio n. 29
0
 public static IEnumerable<int> GetColWeights(IBitMatrix matrix)
 {
     for (int col = 0; col < matrix.Cols; col++)
         yield return matrix.GetColWeight(col);
 }
Esempio n. 30
0
 public Word64BitMatrix(IBitMatrix matrix)
     : this(matrix.Rows, matrix.Cols)
 {
     BitMatrixHelper.CopyNonZero(this, matrix);
 }
Esempio n. 31
0
 public void Copy(IBitMatrix other, int row, int col)
 {
     if (other is Word64BitMatrix)
     {
         CopySubMatrix((Word64BitMatrix)other, row, col);
         return;
     }
     for (int i = 0; i < other.Rows; i++)
     {
         for (int j = 0; j < other.Cols; j++)
             this[row + i, col + j] = other[i, j];
     }
 }
Esempio n. 32
0
        public IEnumerable <IBitArray> Solve(IBitMatrix matrix)
        {
#if false
            PrintMatrix("initial:", matrix);
#endif
            int rows = Math.Min(matrix.Rows, matrix.Cols);
            int cols = matrix.Cols;
            var c    = new int[cols];
            var cInv = new int[cols];
            for (int j = 0; j < cols; j++)
            {
                c[j]    = -1;
                cInv[j] = -1;
            }
            for (int k = 0; k < cols; k++)
            {
                int j = -1;
                for (int i = rows - 1; i >= 0; i--)
                {
                    if (c[i] < 0 && matrix[i, k])
                    {
                        j = i;
                        break;
                    }
                }
                if (j != -1)
                {
                    ZeroColumn(matrix, rows, j, k);
                    c[j]    = k;
                    cInv[k] = j;
#if false
                    Console.WriteLine("c[{0}] = {1}", j, k);
                    PrintMatrix(string.Format("k = {0}", k), matrix);
#endif
                }
                else
                {
                    var v    = (IBitArray)Activator.CreateInstance(typeof(TArray), cols);
                    int ones = 0;
                    for (int jj = 0; jj < cols; jj++)
                    {
                        int  s = cInv[jj];
                        bool bit;
                        if (s != -1)
                        {
                            bit = matrix[s, k];
                        }
                        else if (jj == k)
                        {
                            bit = true;
                        }
                        else
                        {
                            bit = false;
                        }
                        v[jj] = bit;
                        if (bit)
                        {
                            ++ones;
                        }
                    }
#if false
                    Console.WriteLine("k = {0}, v:\n{1}", k, string.Concat(v.Select(bit => bit ? 1 : 0).ToArray()));
#endif
#if DEBUG
                    Debug.Assert(IsSolutionValid(matrix, 0, matrix.Rows, v));
#endif
                    if (IsSolutionValid(matrix, rows, matrix.Rows, v))
                    {
                        yield return(v);
                    }
                }
            }
        }
Esempio n. 33
0
 public static IEnumerable<bool> GetRow(IBitMatrix matrix, int row)
 {
     for (int j = 0; j < matrix.Cols; j++)
         yield return matrix[row, j];
 }
Esempio n. 34
0
 public static int GetRowWeight(IBitMatrix matrix, int row)
 {
     int weight = 0;
     for (int col = 0; col < matrix.Cols; col++)
         weight += matrix[row, col] ? 1 : 0;
     return weight;
 }
Esempio n. 35
0
 public static bool IsSolutionValid(IBitMatrix matrix, IBitArray solution)
 {
     return(IsSolutionValid(matrix, 0, matrix.Rows, solution));
 }
 public void Copy(IBitMatrix other, int row, int col)
 {
     BitMatrixHelper.Copy(this, other, row, col);
 }
Esempio n. 37
0
 public static void XorRows(IBitMatrix matrix, int dst, int src, int col)
 {
     for (int j = col; j < matrix.Cols; j++)
         matrix[dst, j] ^= matrix[src, j];
 }
Esempio n. 38
0
 public void Copy(IBitMatrix other, int row, int col)
 {
     BitMatrixHelper.Copy(this, other, row, col);
 }
Esempio n. 39
0
 public static IEnumerable<int> GetRowWeights(IBitMatrix matrix)
 {
     for (int row = 0; row < matrix.Rows; row++)
         yield return matrix.GetRowWeight(row);
 }