appliedtopology/javaplex

How to transform codes for finding representative holes in Matlab into Java version?

Closed this issue · 1 comments

We can find a representative cycle in Matlab for each interval through commands:

persistence = api.Plex4.getModularSimplicialAlgorithm(3, 2);
intervals = persistence.computeAnnotatedIntervals(stream);

I just want to know the Java version of these two lines?

The Java translation of these two lines of code would be:

AbstractPersistenceAlgorithm<Simplex> persistence = api.Plex4.getModularSimplicialAlgorithm(3, 2);
AnnotatedBarcodeCollection<java.lang.Integer,Simplex> intervals = persistence.computeAnnotatedIntervals(stream);

For reasonably modern Java (v10 or later) you should also be able to write it with type inference as:

var persistence = api.Plex4.getModularSimplicialAlgorithm(3, 2);
var intervals = persistence.computeAnnotatedIntervals(stream);

The object you get out of this is an AnnotatedBarcodeCollection. Looking at the JavaDoc on https://appliedtopology.github.io/javaplex/doc/edu/stanford/math/plex4/homology/barcodes/AnnotatedBarcodeCollection.html, we find the method getIntervalGeneratorPairsAtDimension which returns a List<ObjectObjectPair<Interval, Generator>> with each interval paired with it's corresponding homology generator.

At this point we start finding some types that live in Andrew Tausz' primitive-lib library https://github.com/appliedtopology/primitive-lib. We find that ObjectObjectPair has methods getFirst() and getSecond() to extract the components. So on each entry oop in the list, oop.getFirst() is the interval and oop.getSecond() is the generator.

Intervals are represented as edu.stanford.math.plex4.homology.barcodes.Interval and implement methods getStart and getEnd to access the endpoints of each interval.
Generators are represented using the primitive-lib formal_sum classes. Each such formal sum object has an iterator method that returns an GNU Trove iterator that carries each simplex and it's coefficient. Because of the way Trove is designed, you'll need to explicitly it.advance to step through the iterator, and then at each point you can find the simplex as it.key and its coefficient as it.value. Trove has some code examples for how to traverse their iterators: https://trove4j.sourceforge.net/javadocs/gnu/trove/iterator/TObjectIntIterator.html

Finally - the coefficients that you get here should be straightforward ints, but that need to be interpreted as modulo the prime you set when getting the algorithm object. The simplex is represented as a edu.stanford.math.plex4.homology.chain_basis.Simplex and has a getVertices method that will just return an int-array with indices for the vertices.


We are currently working on a replacement library that will among other things update algorithms, data structures and documentation. Hopefully the next iteration will be easier to navigate.