[mathicgb] 64/393: Moved a lot of code out of the header file for SparseMatrix and also changed its interface to expose less details of its implementation.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:33 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 17914b2bd83d5545a786d2223def705accad3eb0
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Tue Oct 16 16:13:19 2012 +0200

    Moved a lot of code out of the header file for SparseMatrix and also changed its interface to expose less details of its implementation.
---
 src/mathicgb/F4MatrixReducer.cpp   |  57 -----------
 src/mathicgb/QuadMatrixBuilder.cpp |  12 +--
 src/mathicgb/SparseMatrix.cpp      | 146 +++++++++++++++++++++++++++
 src/mathicgb/SparseMatrix.hpp      | 195 +++----------------------------------
 4 files changed, 163 insertions(+), 247 deletions(-)

diff --git a/src/mathicgb/F4MatrixReducer.cpp b/src/mathicgb/F4MatrixReducer.cpp
index 2fdc299..38fecab 100755
--- a/src/mathicgb/F4MatrixReducer.cpp
+++ b/src/mathicgb/F4MatrixReducer.cpp
@@ -578,63 +578,6 @@ void readMany(FILE* file, size_t count, std::vector<T>& v) {
     throw IOException();
 }
 
-// Writes an SparseMatrix
-void writeSparseMatrix
-(const SparseMatrix& matrix, SparseMatrix::Scalar modulus, const std::string& fileName) {
-  MATHICGB_ASSERT(matrix.rowCount() <= std::numeric_limits<uint32>::max());
-  MATHICGB_ASSERT(matrix.colCount() <= std::numeric_limits<uint32>::max());
-  MATHICGB_ASSERT(matrix.entryCount() <= std::numeric_limits<uint64>::max());
-
-  const uint32 rowCount = static_cast<uint32>(matrix.rowCount());
-  const uint32 colCount = static_cast<uint32>(matrix.colCount());
-  const uint64 entryCount = static_cast<uint32>(matrix.entryCount());
-
-  FILE* file = fopen(fileName.c_str(), "w");
-  if (file == NULL)
-    throw IOException();
-
-  writeOne<uint32>(rowCount, file);
-  writeOne<uint32>(colCount, file);
-  writeOne<uint32>(modulus, file);
-  writeOne<uint64>(entryCount, file);
-
-  writeMany<uint16>(matrix.entries(), file);
-  writeMany<uint32>(matrix.colIndices(), file);
-
-  std::vector<uint32> sizes;
-  for (size_t row = 0; row < rowCount; ++row)
-    sizes.push_back(static_cast<uint32>(matrix.entryCountInRow(row)));
-  writeMany<uint32>(sizes, file);
-
-  // todo: don't leak file on exception.
-  fclose(file);
-}
-
-// Reads an SparseMatrix without any fseeks and returns the modulus.
-SparseMatrix::Scalar readSparseMatrix(const std::string& fileName, SparseMatrix& matrix)
-{
-  FILE* file = fopen(fileName.c_str(), "r");
-  if (file == NULL)
-    throw IOException();
-
-  uint32 const rowCount = readOne<uint32>(file);
-  uint32 const colCount = readOne<uint32>(file);
-  uint32 const modulus = readOne<uint32>(file);
-  uint64 const entryCount = readOne<uint64>(file);
-
-  std::vector<uint16> entries;
-  readMany(file, static_cast<size_t>(entryCount), entries);
-
-  std::vector<uint32> indices;
-  readMany(file, static_cast<size_t>(entryCount), indices);
-
-  std::vector<uint32> sizes;
-  readMany(file, rowCount, sizes);
-
-  matrix.setToAndTakeMemory(indices, entries, sizes, colCount);
-  return modulus;
-}
-
 // doesn't need to be fast.
 int integerLog10(size_t val) {
   int ret = -1;
diff --git a/src/mathicgb/QuadMatrixBuilder.cpp b/src/mathicgb/QuadMatrixBuilder.cpp
index d2abc1e..2947e55 100755
--- a/src/mathicgb/QuadMatrixBuilder.cpp
+++ b/src/mathicgb/QuadMatrixBuilder.cpp
@@ -138,16 +138,8 @@ namespace {
     }
 
     // Change indices to match the permutation done on the columns
-    typedef SparseMatrix::AllColIndicesIterator Iter;
-    for (int doTop = 0; doTop < 2; ++doTop) {
-      SparseMatrix& matrix = doTop ? topMatrix : bottomMatrix;
-      Iter it = matrix.allColIndicesBegin();
-      Iter end = matrix.allColIndicesEnd();
-      for (; it != end; ++it) {
-        MATHICGB_ASSERT(*it < colCount);
-        *it = permutation[*it];
-      }
-    }
+    topMatrix.applyColumnMap(permutation);
+    bottomMatrix.applyColumnMap(permutation);
   }  
 }
 
diff --git a/src/mathicgb/SparseMatrix.cpp b/src/mathicgb/SparseMatrix.cpp
index ff28dc3..d4b5653 100755
--- a/src/mathicgb/SparseMatrix.cpp
+++ b/src/mathicgb/SparseMatrix.cpp
@@ -52,3 +52,149 @@ void SparseMatrix::sortRowsByIncreasingPivots() {
 
   *this = std::move(ordered);
 }
+
+void SparseMatrix::applyColumnMap(std::vector<ColIndex> colMap) {
+  MATHICGB_ASSERT(colMap.size() < colCount());
+  auto end = mColIndices.end();
+  for (auto it = mColIndices.begin(); it != end; ++it) {
+    MATHICGB_ASSERT(*it < colCount());
+    *it = colMap[*it];
+  }
+}
+
+void SparseMatrix::print(std::ostream& out) const {
+  if (rowCount() == 0)
+    out << "matrix with no rows\n";
+  for (RowIndex row = 0; row < rowCount(); ++row) {
+    out << row << ':';
+    RowIterator end = rowEnd(row);
+    for (RowIterator it = rowBegin(row); it != end; ++it) {
+      MATHICGB_ASSERT(it.index() < colCount());
+      out << ' ' << it.index() << '#' << it.scalar();
+    }
+    out << '\n';
+  }
+}
+
+std::string SparseMatrix::toString() const {
+  std::ostringstream out;
+  print(out);
+  return out.str();
+}
+
+void SparseMatrix::appendRowAndNormalize(const SparseMatrix& matrix, RowIndex row, Scalar modulus) {
+  MATHICGB_ASSERT(row < matrix.rowCount());
+  RowIterator it = matrix.rowBegin(row);
+  RowIterator end = matrix.rowEnd(row);
+  if (it != end) {
+    appendEntry(it.index(), 1);
+    Scalar lead = it.scalar();
+    ++it;
+    if (it != end) {
+      Scalar inverse = modularInverse(lead, modulus);
+      do {
+        uint32 prod = static_cast<uint32>(inverse) * it.scalar();
+        uint16 prodMod = static_cast<uint16>(prod % modulus);
+        appendEntry(it.index(), prodMod);
+        ++it;
+      } while (it != end);
+    }
+  }
+  rowDone();
+}
+
+void SparseMatrix::appendRow(const SparseMatrix& matrix, RowIndex row) {
+  MATHICGB_ASSERT(row < matrix.rowCount());
+  RowIterator it = matrix.rowBegin(row);
+  RowIterator end = matrix.rowEnd(row);
+  for (; it != end; ++it)
+    appendEntry(it.index(), it.scalar());
+  rowDone();
+}
+  
+void SparseMatrix::swap(SparseMatrix& matrix) {
+  std::swap(mColIndices, matrix.mColIndices);
+  std::swap(mEntries, matrix.mEntries);
+  std::swap(mRowOffsets, matrix.mRowOffsets);
+  std::swap(mColCount, matrix.mColCount);
+}
+  
+void SparseMatrix::clear(ColIndex newColCount) {
+  mColIndices.clear();
+  mEntries.clear();
+  mRowOffsets.clear();
+  mRowOffsets.push_back(0);
+  mColCount = newColCount;
+}
+
+void SparseMatrix::appendRowWithModulus(std::vector<uint64> const& v, Scalar modulus) {
+  MATHICGB_ASSERT(v.size() == colCount());
+  ColIndex count = colCount();
+  for (ColIndex col = 0; col < count; ++col) {
+    Scalar scalar = static_cast<Scalar>(v[col] % modulus);
+    if (scalar != 0)
+      appendEntry(col, scalar);
+  }
+  rowDone();
+}
+
+void SparseMatrix::appendRow(std::vector<uint64> const& v, ColIndex leadCol) {
+  MATHICGB_ASSERT(v.size() == colCount());
+#ifdef MATHICGB_DEBUG
+  for (ColIndex col = leadCol; col < leadCol; ++col) {
+    MATHICGB_ASSERT(v[col] == 0);
+  }
+#endif
+
+  ColIndex count = colCount();
+  for (ColIndex col = leadCol; col < count; ++col) {
+	MATHICGB_ASSERT(v[col] < std::numeric_limits<Scalar>::max());
+    if (v[col] != 0)
+      appendEntry(col, static_cast<Scalar>(v[col]));
+  }
+  rowDone();
+}
+
+void SparseMatrix::appendRowWithModulusNormalized(std::vector<uint64> const& v, Scalar modulus) {
+  MATHICGB_ASSERT(v.size() == colCount());
+  ColIndex count = colCount();
+  uint16 multiply = 1;
+   
+  bool first = true;
+  for (ColIndex col = 0; col < count; ++col) {
+    Scalar scalar = static_cast<Scalar>(v[col] % modulus);
+    if (scalar == 0)
+      continue;
+    if (first) {
+      multiply = modularInverse(scalar, modulus);
+      scalar = 1;
+      first = false;
+    } else {
+      uint32 prod = static_cast<uint32>(multiply) * scalar;
+      scalar = prod % modulus;
+    }
+    appendEntry(col, scalar);
+  }
+  rowDone();
+}
+
+bool SparseMatrix::appendRowWithModulusIfNonZero(std::vector<uint64> const& v, Scalar modulus) {
+  appendRowWithModulus(v, modulus);
+  MATHICGB_ASSERT(mRowOffsets.size() >= 2);
+  std::vector<size_t>::const_iterator it = mRowOffsets.end();
+  --it;
+  size_t last = *it;
+  --it;
+  if (last == *it) {
+    mRowOffsets.pop_back();
+    return false;
+  } else
+    return true;
+}
+
+bool SparseMatrix::operator==(const SparseMatrix& mat) const {
+  return mColCount == mat.mColCount &&
+    mColIndices == mat.mColIndices &&
+    mEntries == mat.mEntries &&
+    mRowOffsets == mat.mRowOffsets;
+}
diff --git a/src/mathicgb/SparseMatrix.hpp b/src/mathicgb/SparseMatrix.hpp
index bb6b980..759aedc 100755
--- a/src/mathicgb/SparseMatrix.hpp
+++ b/src/mathicgb/SparseMatrix.hpp
@@ -104,32 +104,14 @@ class SparseMatrix {
   }
 
   /** Prints the matrix in a human readable format to out. */
-  void print(std::ostream& out) const {
-    if (rowCount() == 0)
-      out << "matrix with no rows\n";
-    for (RowIndex row = 0; row < rowCount(); ++row) {
-      out << row << ':';
-      RowIterator end = rowEnd(row);
-      for (RowIterator it = rowBegin(row); it != end; ++it) {
-        MATHICGB_ASSERT(it.index() < colCount());
-        out << ' ' << it.index() << '#' << it.scalar();
-      }
-      out << '\n';
-    }
-  }
-
-  std::string toString() const {
-    std::ostringstream out;
-    print(out);
-    return out.str();
-  }
+  void print(std::ostream& out) const;
 
+  std::string toString() const;
 
   /** Adds a new row that contains all terms that have been appended
     since the last time a row was added or the matrix was created. */
   void rowDone() {
     MATHICGB_ASSERT(mColIndices.size() == mEntries.size());
-
     mRowOffsets.push_back(mColIndices.size());
   }
 
@@ -146,82 +128,11 @@ class SparseMatrix {
     MATHICGB_ASSERT(mColIndices.size() == mEntries.size());
   }
 
+  void appendRowAndNormalize(const SparseMatrix& matrix, RowIndex row, Scalar modulus);
   
-  void appendRowAndNormalize(const SparseMatrix& matrix, RowIndex row, Scalar modulus) {
-    MATHICGB_ASSERT(row < matrix.rowCount());
-    RowIterator it = matrix.rowBegin(row);
-    RowIterator end = matrix.rowEnd(row);
-    if (it != end) {
-      appendEntry(it.index(), 1);
-      Scalar lead = it.scalar();
-      ++it;
-      if (it != end) {
-        Scalar inverse = modularInverse(lead, modulus);
-        do {
-          uint32 prod = static_cast<uint32>(inverse) * it.scalar();
-          uint16 prodMod = static_cast<uint16>(prod % modulus);
-          appendEntry(it.index(), prodMod);
-          ++it;
-        } while (it != end);
-      }
-    }
-    rowDone();
-  }
-  
-  void appendRow(const SparseMatrix& matrix, RowIndex row) {
-    MATHICGB_ASSERT(row < matrix.rowCount());
-    RowIterator it = matrix.rowBegin(row);
-    RowIterator end = matrix.rowEnd(row);
-    for (; it != end; ++it)
-      appendEntry(it.index(), it.scalar());
-    rowDone();
-  }
-
-  void setToAndTakeMemory(std::vector<ColIndex>& indices,
-                          std::vector<Scalar>& entries,
-                          std::vector<ColIndex>& sizes,
-                          ColIndex colCount) {
-    MATHICGB_ASSERT(indices.size() == entries.size());
-
-    mColCount = colCount;
-
-    // deallocate old memory
-    std::vector<ColIndex>().swap(mColIndices);
-    std::vector<Scalar>().swap(mEntries);
-    std::vector<size_t>().swap(mRowOffsets);
-
-    // take new memory
-    mColIndices.swap(indices);
-    mEntries.swap(entries);
-
-    // compute row offsets
-    const size_t newRowCount = sizes.size();
-    mRowOffsets.reserve(newRowCount + 1);
-    mRowOffsets.push_back(0);
-    for (size_t row = 0; row < newRowCount; ++row)
-      mRowOffsets.push_back(mRowOffsets.back() + sizes[row]);
-
-    MATHICGB_ASSERT(mRowOffsets.size() == sizes.size() + 1);
-    MATHICGB_ASSERT(mRowOffsets.front() == 0);
-    MATHICGB_ASSERT(mRowOffsets.back() == mColIndices.size());
-
-    std::vector<ColIndex>().swap(sizes); // deallocate old memory
-  }
-  
-  void swap(SparseMatrix& matrix) {
-    std::swap(mColIndices, matrix.mColIndices);
-    std::swap(mEntries, matrix.mEntries);
-    std::swap(mRowOffsets, matrix.mRowOffsets);
-    std::swap(mColCount, matrix.mColCount);
-  }
-
-  void clear(ColIndex newColCount = 0) {
-    mColIndices.clear();
-    mEntries.clear();
-    mRowOffsets.clear();
-    mRowOffsets.push_back(0);
-    mColCount = newColCount;
-  }
+  void appendRow(const SparseMatrix& matrix, RowIndex row);
+  void swap(SparseMatrix& matrix);
+  void clear(ColIndex newColCount = 0);
   
   class RowIterator {
   public:
@@ -247,9 +158,7 @@ class SparseMatrix {
     return mRowOffsets.size() - 1;
   }
 
-  ColIndex colCount() const {
-    return mColCount;
-  }
+  ColIndex colCount() const {return mColCount;}
   
   RowIterator rowBegin(RowIndex row) const {
     MATHICGB_ASSERT(row < rowCount());
@@ -275,95 +184,21 @@ class SparseMatrix {
     return mColCount - 1;
   }
 
-  void appendRowWithModulus(std::vector<uint64> const& v, Scalar modulus) {
-    MATHICGB_ASSERT(v.size() == colCount());
-    ColIndex count = colCount();
-    for (ColIndex col = 0; col < count; ++col) {
-      Scalar scalar = static_cast<Scalar>(v[col] % modulus);
-      if (scalar != 0)
-        appendEntry(col, scalar);
-    }
-    rowDone();
-  }
+  void appendRowWithModulus(std::vector<uint64> const& v, Scalar modulus);
   
-  void appendRow(std::vector<uint64> const& v, ColIndex leadCol = 0) {
-    MATHICGB_ASSERT(v.size() == colCount());
-#ifdef MATHICGB_DEBUG
-    for (ColIndex col = leadCol; col < leadCol; ++col) {
-      MATHICGB_ASSERT(v[col] == 0);
-    }
-#endif
-
-    ColIndex count = colCount();
-    for (ColIndex col = leadCol; col < count; ++col) {
-	  MATHICGB_ASSERT(v[col] < std::numeric_limits<Scalar>::max());
-      if (v[col] != 0)
-        appendEntry(col, static_cast<Scalar>(v[col]));
-	}
-    rowDone();
-  }
+  void appendRow(std::vector<uint64> const& v, ColIndex leadCol = 0);
 
-  void appendRowWithModulusNormalized(std::vector<uint64> const& v, Scalar modulus) {
-    MATHICGB_ASSERT(v.size() == colCount());
-    ColIndex count = colCount();
-    uint16 multiply = 1;
-   
-    bool first = true;
-    for (ColIndex col = 0; col < count; ++col) {
-      Scalar scalar = static_cast<Scalar>(v[col] % modulus);
-      if (scalar == 0)
-        continue;
-      if (first) {
-        multiply = modularInverse(scalar, modulus);
-        scalar = 1;
-        first = false;
-      } else {
-        uint32 prod = static_cast<uint32>(multiply) * scalar;
-        scalar = prod % modulus;
-      }
-      appendEntry(col, scalar);
-    }
-    rowDone();
-  }
+  void appendRowWithModulusNormalized(std::vector<uint64> const& v, Scalar modulus);
 
   // Returns true if the row was non-zero. Otherwise the row was not
   // appended.
-  bool appendRowWithModulusIfNonZero(std::vector<uint64> const& v, Scalar modulus) {
-    appendRowWithModulus(v, modulus);
-    MATHICGB_ASSERT(mRowOffsets.size() >= 2);
-    std::vector<size_t>::const_iterator it = mRowOffsets.end();
-    --it;
-    size_t last = *it;
-    --it;
-    if (last == *it) {
-      mRowOffsets.pop_back();
-      return false;
-    } else
-      return true;
-  }
-
-  bool operator==(const SparseMatrix& mat) const {
-    return mColCount == mat.mColCount &&
-      mColIndices == mat.mColIndices &&
-      mEntries == mat.mEntries &&
-      mRowOffsets == mat.mRowOffsets;
-  }
+  bool appendRowWithModulusIfNonZero(std::vector<uint64> const& v, Scalar modulus);
 
-  bool operator!=(const SparseMatrix& mat) const {
-    return !(*this == mat);
-  }
-
-  const std::vector<Scalar>& entries() const {
-    return mEntries;
-  }
+  bool operator==(const SparseMatrix& mat) const;
+  bool operator!=(const SparseMatrix& mat) const {return !(*this == mat);}
 
-  const std::vector<ColIndex>& colIndices() const {
-    return mColIndices;
-  }
-  
-  typedef std::vector<ColIndex>::iterator AllColIndicesIterator;
-  AllColIndicesIterator allColIndicesBegin() {return mColIndices.begin();}
-  AllColIndicesIterator allColIndicesEnd() {return mColIndices.end();}
+  /// Replaces all column indices i with colMap[i].
+  void applyColumnMap(std::vector<ColIndex> colMap);
 
   /// Let poly be the dot product of colMonomials and the given row.
   void rowToPolynomial

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list