summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlrich Bauer <mail@ulrich-bauer.org>2016-10-24 00:28:02 +0200
committerUlrich Bauer <mail@ulrich-bauer.org>2016-10-24 00:28:02 +0200
commit2dee30433bc1a09d2cc2240c5fbb92328cde3d81 (patch)
treea6d9905edc22b77eda1e322508203aa303ce7272
parentfcd90f79d25eba3a511aba72e3981aa9c435e690 (diff)
parentf46a9148452480916fc1fda4b501e2af7e5bd570 (diff)
Merge branch 'dev'
-rw-r--r--README.md12
-rw-r--r--ripser.cpp194
2 files changed, 106 insertions, 100 deletions
diff --git a/README.md b/README.md
index 0a3f304..0c5a2e6 100644
--- a/README.md
+++ b/README.md
@@ -7,6 +7,8 @@ Copyright © 2015–2016 [Ulrich Bauer].
Ripser is a lean C++ code for the computation of Vietoris–Rips persistence barcodes. It can do just this one thing, but does it extremely well.
+To see a live demo of Ripser's capabilities, go to [live.ripser.org]. The computation happens inside the browser (using [PNaCl] on Chrome and JavaScript via [Emscripten] on other browsers).
+
The main features of Ripser:
- time- and memory-efficient
@@ -14,7 +16,7 @@ The main features of Ripser:
- support for coefficients in prime finite fields
- no external dependencies (optional support for Google's [sparsehash])
-Currently, Ripser outperforms other codes ([Dionysus], [DIPHA], [GUDHI], [Perseus], [PHAT]) by a factor of more than 40 in computation time and a factor of more than 15 in memory efficiency. (Note that [PHAT] does not contain code for generating Vietoris–Rips filtrations).
+Currently, Ripser outperforms other codes ([Dionysus], [DIPHA], [GUDHI], [Perseus], [PHAT]) by a factor of more than 40 in computation time and a factor of more than 15 in memory efficiency (for the example linked at [live.ripser.org]). (Note that [PHAT] does not contain code for generating Vietoris–Rips filtrations).
Input formats currently supported by Ripser:
@@ -57,7 +59,7 @@ Ripser supports several compile-time options. They are switched on by defining t
- `USE_COEFFICIENTS`: enable support for coefficients in a prime field
- `INDICATE_PROGRESS`: indicate the current progress in the console
- `PRINT_PERSISTENCE_PAIRS`: output the computed persistence pairs (enabled by default in the code; comment out to disable)
- - `USE_GOOGLE_HASHMAP`: enable support for Google's [sparsehash] data structure; may further reducue memory footprint
+ - `USE_GOOGLE_HASHMAP`: enable support for Google's [sparsehash] data structure; may further reduce memory footprint
For example, to build Ripser with support for coefficients:
@@ -89,13 +91,17 @@ The following features are currently planned for future versions:
- computation of representative cycles for persistent homology (currenly only *co*cycles are computed)
- support for sparse distance matrices
+Prototype implementations are already avaliable; please contact the author if one of these features might be relevant for your research.
+
### License
Ripser is licensed under the [LGPL] 3.0. Please contact the author if you want to use Ripser in your software under a different license.
-
[Ulrich Bauer]: <http://ulrich-bauer.org>
+[live.ripser.org]: <http://live.ripser.org>
+[PNaCl]: <https://www.chromium.org/nativeclient/pnacl/>
+[Emscripten]: <http://emscripten.org>
[latest-release]: <https://github.com/Ripser/ripser/releases/latest>
[Dionysus]: <http://www.mrzv.org/software/dionysus/>
[DIPHA]: <http://git.io/dipha>
diff --git a/ripser.cpp b/ripser.cpp
index 3d4eb29..9d57fa9 100644
--- a/ripser.cpp
+++ b/ripser.cpp
@@ -18,7 +18,6 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
//#define ASSEMBLE_REDUCTION_MATRIX
//#define USE_COEFFICIENTS
@@ -98,32 +97,34 @@ std::vector<coefficient_t> multiplicative_inverse_vector(const coefficient_t m)
return inverse;
}
+index_t get_next_vertex(index_t& v, const index_t idx, const index_t k, const binomial_coeff_table& binomial_coeff) {
+ if (binomial_coeff(v, k) > idx) {
+ index_t count = v;
+ while (count > 0) {
+ index_t i = v;
+ index_t step = count >> 1;
+ i -= step;
+ if (binomial_coeff(i, k) > idx) {
+ v = --i;
+ count -= step + 1;
+ } else
+ count = step;
+ }
+ }
+ assert(binomial_coeff(v, k) <= idx);
+ assert(binomial_coeff(v + 1, k) > idx);
+ return v;
+}
+
template <typename OutputIterator>
-OutputIterator get_simplex_vertices(index_t idx, const index_t dim, index_t n,
+OutputIterator get_simplex_vertices(index_t idx, const index_t dim, index_t v,
const binomial_coeff_table& binomial_coeff, OutputIterator out) {
- --n;
-
+ --v;
for (index_t k = dim + 1; k > 0; --k) {
- if (binomial_coeff(n, k) > idx) {
- index_t count = n;
- while (count > 0) {
- index_t i = n;
- index_t step = count >> 1;
- i -= step;
- if (binomial_coeff(i, k) > idx) {
- n = --i;
- count -= step + 1;
- } else
- count = step;
- }
- }
- assert(binomial_coeff(n, k) <= idx);
- assert(binomial_coeff(n + 1, k) > idx);
-
- *out++ = n;
- idx -= binomial_coeff(n, k);
+ get_next_vertex(v, idx, k, binomial_coeff);
+ *out++ = v;
+ idx -= binomial_coeff(v, k);
}
-
return out;
}
@@ -165,7 +166,7 @@ typedef index_t entry_t;
const index_t get_index(entry_t i) { return i; }
index_t get_coefficient(entry_t i) { return 1; }
entry_t make_entry(index_t _index, coefficient_t _value) { return entry_t(_index); }
-void set_coefficient(index_t& e, const coefficient_t c) { e = c; }
+void set_coefficient(index_t& e, const coefficient_t c) {}
#endif
@@ -175,7 +176,11 @@ template <typename Entry> struct smaller_index {
bool operator()(const Entry& a, const Entry& b) { return get_index(a) < get_index(b); }
};
-typedef std::pair<value_t, index_t> diameter_index_t;
+class diameter_index_t : public std::pair<value_t, index_t> {
+public:
+ diameter_index_t() : std::pair<value_t, index_t>() {}
+ diameter_index_t(std::pair<value_t, index_t> p) : std::pair<value_t, index_t>(p) {}
+};
value_t get_diameter(diameter_index_t i) { return i.first; }
index_t get_index(diameter_index_t i) { return i.second; }
@@ -184,6 +189,12 @@ public:
diameter_entry_t(std::pair<value_t, entry_t> p) : std::pair<value_t, entry_t>(p) {}
diameter_entry_t(entry_t e) : std::pair<value_t, entry_t>(0, e) {}
diameter_entry_t() : diameter_entry_t(0) {}
+ diameter_entry_t(value_t _diameter, index_t _index, coefficient_t _coefficient)
+ : std::pair<value_t, entry_t>(_diameter, make_entry(_index, _coefficient)) {}
+ diameter_entry_t(diameter_index_t _diameter_index, coefficient_t _coefficient)
+ : std::pair<value_t, entry_t>(get_diameter(_diameter_index),
+ make_entry(get_index(_diameter_index), _coefficient)) {}
+ diameter_entry_t(diameter_index_t _diameter_index) : diameter_entry_t(_diameter_index, 1) {}
};
const entry_t& get_entry(const diameter_entry_t& p) { return p.second; }
@@ -192,12 +203,6 @@ const index_t get_index(const diameter_entry_t& p) { return get_index(get_entry(
const coefficient_t get_coefficient(const diameter_entry_t& p) { return get_coefficient(get_entry(p)); }
const value_t& get_diameter(const diameter_entry_t& p) { return p.first; }
void set_coefficient(diameter_entry_t& p, const coefficient_t c) { set_coefficient(get_entry(p), c); }
-diameter_entry_t make_diameter_entry(value_t _diameter, index_t _index, coefficient_t _coefficient) {
- return std::make_pair(_diameter, make_entry(_index, _coefficient));
-}
-diameter_entry_t make_diameter_entry(diameter_index_t _diameter_index, coefficient_t _coefficient) {
- return std::make_pair(get_diameter(_diameter_index), make_entry(get_index(_diameter_index), _coefficient));
-}
template <typename Entry> struct greater_diameter_or_smaller_index {
bool operator()(const Entry& a, const Entry& b) {
@@ -242,14 +247,23 @@ public:
}
};
-class simplex_coboundary_enumerator {
+template <class DistanceMatrix> class simplex_coboundary_enumerator {
private:
index_t idx_below, idx_above, v, k;
+ std::vector<index_t> vertices;
+ const diameter_entry_t simplex;
+ const coefficient_t modulus;
+ const DistanceMatrix& dist;
const binomial_coeff_table& binomial_coeff;
public:
- simplex_coboundary_enumerator(index_t _idx, index_t _dim, index_t _n, const binomial_coeff_table& _binomial_coeff)
- : idx_below(_idx), idx_above(0), v(_n - 1), k(_dim + 1), binomial_coeff(_binomial_coeff) {}
+ simplex_coboundary_enumerator(const diameter_entry_t _simplex, index_t _dim, index_t _n,
+ const coefficient_t _modulus, const DistanceMatrix& _dist,
+ const binomial_coeff_table& _binomial_coeff)
+ : simplex(_simplex), idx_below(get_index(_simplex)), idx_above(0), v(_n - 1), k(_dim + 1), modulus(_modulus),
+ binomial_coeff(_binomial_coeff), dist(_dist), vertices(_dim + 1) {
+ get_simplex_vertices(get_index(_simplex), _dim, _n, binomial_coeff, vertices.begin());
+ }
bool has_next() {
while ((v != -1) && (binomial_coeff(v, k) <= idx_below)) {
@@ -263,10 +277,14 @@ public:
return v != -1;
}
- std::pair<entry_t, index_t> next() {
- auto result = std::make_pair(make_entry(idx_above + binomial_coeff(v, k + 1) + idx_below, k & 1 ? -1 : 1), v);
- --v;
- return result;
+ index_t next_index() { return idx_above + binomial_coeff(v--, k + 1) + idx_below; }
+
+ diameter_entry_t next() {
+ value_t coface_diameter = get_diameter(simplex);
+ for (index_t w : vertices) coface_diameter = std::max(coface_diameter, dist(v, w));
+ coefficient_t coface_coefficient = (k & 1 ? -1 + modulus : 1) * get_coefficient(simplex) % modulus;
+ return diameter_entry_t(coface_diameter, idx_above + binomial_coeff(v--, k + 1) + idx_below,
+ coface_coefficient);
}
};
@@ -484,6 +502,12 @@ void assemble_columns_to_reduce(std::vector<diameter_index_t>& columns_to_reduce
if (pivot_column_index.find(index) == pivot_column_index.end()) {
value_t diameter = comp.diameter(index);
if (diameter <= threshold) columns_to_reduce.push_back(std::make_pair(diameter, index));
+#ifdef INDICATE_PROGRESS
+ if ((index + 1) % 1000 == 0)
+ std::cout << "\033[K"
+ << "assembled " << columns_to_reduce.size() << " out of " << (index + 1) << "/"
+ << num_simplices << " columns" << std::flush << "\r";
+#endif
}
}
@@ -501,9 +525,9 @@ void assemble_columns_to_reduce(std::vector<diameter_index_t>& columns_to_reduce
template <typename DistanceMatrix, typename ComparatorCofaces, typename Comparator>
void compute_pairs(std::vector<diameter_index_t>& columns_to_reduce, hash_map<index_t, index_t>& pivot_column_index,
- const DistanceMatrix& dist, const ComparatorCofaces& comp, const Comparator& comp_prev, index_t dim,
- index_t n, value_t threshold, coefficient_t modulus,
- const std::vector<coefficient_t>& multiplicative_inverse,
+ index_t dim, index_t n, value_t threshold, coefficient_t modulus,
+ const std::vector<coefficient_t>& multiplicative_inverse, const DistanceMatrix& dist,
+ const ComparatorCofaces& comp, const Comparator& comp_prev,
const binomial_coeff_table& binomial_coeff) {
#ifdef PRINT_PERSISTENCE_PAIRS
@@ -511,7 +535,7 @@ void compute_pairs(std::vector<diameter_index_t>& columns_to_reduce, hash_map<in
#endif
#ifdef ASSEMBLE_REDUCTION_MATRIX
- compressed_sparse_matrix<diameter_entry_t> reduction_matrix;
+ compressed_sparse_matrix<diameter_entry_t> reduction_coefficients;
#else
#ifdef USE_COEFFICIENTS
std::vector<diameter_entry_t> reduction_coefficients;
@@ -519,7 +543,6 @@ void compute_pairs(std::vector<diameter_index_t>& columns_to_reduce, hash_map<in
#endif
std::vector<diameter_entry_t> coface_entries;
- std::vector<index_t> vertices;
for (index_t i = 0; i < columns_to_reduce.size(); ++i) {
auto column_to_reduce = columns_to_reduce[i];
@@ -546,15 +569,15 @@ void compute_pairs(std::vector<diameter_index_t>& columns_to_reduce, hash_map<in
// start with a dummy pivot entry with coefficient -1 in order to initialize
// working_coboundary with the coboundary of the simplex with index column_to_reduce
- diameter_entry_t pivot = make_diameter_entry(0, -1, -1 + modulus);
+ diameter_entry_t pivot(0, -1, -1 + modulus);
#ifdef ASSEMBLE_REDUCTION_MATRIX
- // initialize reduction_matrix as identity matrix
- reduction_matrix.append_column();
- reduction_matrix.push_back(make_diameter_entry(column_to_reduce, 1));
+ // initialize reduction_coefficients as identity matrix
+ reduction_coefficients.append_column();
+ reduction_coefficients.push_back(diameter_entry_t(column_to_reduce, 1));
#else
#ifdef USE_COEFFICIENTS
- reduction_coefficients.push_back(make_diameter_entry(column_to_reduce, 1));
+ reduction_coefficients.push_back(diameter_entry_t(column_to_reduce, 1));
#endif
#endif
@@ -564,52 +587,32 @@ void compute_pairs(std::vector<diameter_index_t>& columns_to_reduce, hash_map<in
const coefficient_t factor = modulus - get_coefficient(pivot);
#ifdef ASSEMBLE_REDUCTION_MATRIX
- for (auto it = reduction_matrix.cbegin(j); it != reduction_matrix.cend(j); ++it)
-#endif
- {
-#ifdef ASSEMBLE_REDUCTION_MATRIX
- const auto& simplex = *it;
+ auto coeffs_begin = reduction_coefficients.cbegin(j), coeffs_end = reduction_coefficients.cend(j);
#else
#ifdef USE_COEFFICIENTS
- const auto& simplex = reduction_coefficients[j];
+ auto coeffs_begin = &reduction_coefficients[j], coeffs_end = &reduction_coefficients[j] + 1;
#else
- const auto& simplex = columns_to_reduce[j];
+ auto coeffs_begin = &columns_to_reduce[j], coeffs_end = &columns_to_reduce[j] + 1;
#endif
#endif
- coefficient_t simplex_coefficient = get_coefficient(simplex) * factor % modulus;
+ for (auto it = coeffs_begin; it != coeffs_end; ++it) {
+ diameter_entry_t simplex = *it;
+ set_coefficient(simplex, get_coefficient(simplex) * factor % modulus);
#ifdef ASSEMBLE_REDUCTION_MATRIX
- reduction_column.push(
- make_diameter_entry(get_diameter(simplex), get_index(simplex), simplex_coefficient));
+ reduction_column.push(simplex);
#endif
- vertices.clear();
- get_simplex_vertices(get_index(simplex), dim, n, binomial_coeff, std::back_inserter(vertices));
-
coface_entries.clear();
- simplex_coboundary_enumerator cofaces(get_index(simplex), dim, n, binomial_coeff);
+ simplex_coboundary_enumerator<decltype(dist)> cofaces(simplex, dim, n, modulus, dist, binomial_coeff);
while (cofaces.has_next()) {
- auto coface_descriptor = cofaces.next();
- entry_t coface = coface_descriptor.first;
- index_t covertex = coface_descriptor.second;
- index_t coface_index = get_index(coface);
- value_t coface_diameter = get_diameter(simplex);
- for (index_t v : vertices) { coface_diameter = std::max(coface_diameter, dist(v, covertex)); }
- assert(comp.diameter(coface_index) == coface_diameter);
-
- if (coface_diameter <= threshold) {
- coefficient_t coface_coefficient =
- (get_coefficient(coface) + modulus) * simplex_coefficient % modulus;
- assert(coface_coefficient >= 0);
-
- diameter_entry_t coface_entry =
- make_diameter_entry(coface_diameter, coface_index, coface_coefficient);
- coface_entries.push_back(coface_entry);
-
- if (might_be_apparent_pair && (get_diameter(simplex) == coface_diameter)) {
- if (pivot_column_index.find(coface_index) == pivot_column_index.end()) {
- pivot = coface_entry;
+ diameter_entry_t coface = cofaces.next();
+ if (get_diameter(coface) <= threshold) {
+ coface_entries.push_back(coface);
+ if (might_be_apparent_pair && (get_diameter(simplex) == get_diameter(coface))) {
+ if (pivot_column_index.find(get_index(coface)) == pivot_column_index.end()) {
+ pivot = coface;
goto found_persistence_pair;
}
might_be_apparent_pair = false;
@@ -656,25 +659,22 @@ void compute_pairs(std::vector<diameter_index_t>& columns_to_reduce, hash_map<in
#endif
#ifdef ASSEMBLE_REDUCTION_MATRIX
- // replace current column of reduction_matrix (with a single diagonal 1 entry)
+ // replace current column of reduction_coefficients (with a single diagonal 1 entry)
// by reduction_column (possibly with a different entry on the diagonal)
- reduction_matrix.pop_back();
+ reduction_coefficients.pop_back();
while (true) {
diameter_entry_t e = pop_pivot(reduction_column, modulus);
- index_t index = get_index(e);
- if (index == -1) break;
+ if (get_index(e) == -1) break;
#ifdef USE_COEFFICIENTS
- const coefficient_t coefficient = inverse * get_coefficient(e) % modulus;
- assert(coefficient > 0);
-#else
- const coefficient_t coefficient = 1;
+ set_coefficient(e, inverse * get_coefficient(e) % modulus);
+ assert(get_coefficient(e) > 0);
#endif
- reduction_matrix.push_back(make_diameter_entry(get_diameter(e), index, coefficient));
+ reduction_coefficients.push_back(e);
}
#else
#ifdef USE_COEFFICIENTS
reduction_coefficients.pop_back();
- reduction_coefficients.push_back(make_diameter_entry(column_to_reduce, inverse));
+ reduction_coefficients.push_back(diameter_entry_t(column_to_reduce, inverse));
#endif
#endif
break;
@@ -910,7 +910,7 @@ int main(int argc, char** argv) {
rips_filtration_comparator<decltype(dist)> comp(dist, 1, binomial_coeff);
for (index_t index = binomial_coeff(n, 2); index-- > 0;) {
value_t diameter = comp.diameter(index);
- if (diameter <= threshold) edges.push_back(diameter_index_t(diameter, index));
+ if (diameter <= threshold) edges.push_back(std::make_pair(diameter, index));
}
std::sort(edges.rbegin(), edges.rend(), greater_diameter_or_smaller_index<diameter_index_t>());
@@ -926,7 +926,7 @@ int main(int argc, char** argv) {
if (u != v) {
#ifdef PRINT_PERSISTENCE_PAIRS
- std::cout << " [0," << get_diameter(e) << ")" << std::endl;
+ if (get_diameter(e) > 0) std::cout << " [0," << get_diameter(e) << ")" << std::endl;
#endif
dset.link(u, v);
} else
@@ -947,11 +947,11 @@ int main(int argc, char** argv) {
hash_map<index_t, index_t> pivot_column_index;
pivot_column_index.reserve(columns_to_reduce.size());
- compute_pairs(columns_to_reduce, pivot_column_index, dist, comp, comp_prev, dim, n, threshold, modulus,
- multiplicative_inverse, binomial_coeff);
+ compute_pairs(columns_to_reduce, pivot_column_index, dim, n, threshold, modulus, multiplicative_inverse, dist,
+ comp, comp_prev, binomial_coeff);
if (dim < dim_max) {
assemble_columns_to_reduce(columns_to_reduce, pivot_column_index, comp, dim, n, threshold, binomial_coeff);
}
}
-} \ No newline at end of file
+}