examples/cohomology/output.h
author Christos Mantoulidis <cmad@stanford.edu>
Tue, 04 Aug 2009 13:23:16 -0700
branchdev
changeset 156 f75fb57d2831
parent 138 96030f8d2f2c
child 157 700cbac5b23c
permissions -rw-r--r--
Changed implementation of WeightedRips to store simplex values (max distance between simplices' vertices) as an invisible layer on top of each simplex object, so that the data() field of WeightedRips has been freed for use by the users again.

#include <iostream>
#include <sstream>
#include <fstream>

#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>

#include <cassert>

bool neq(const Smplx& s1, const Smplx& s2)               
{ 
    Smplx::VertexComparison cmp;
    return cmp(s1, s2) || cmp(s2, s1);
}

unsigned index(const SimplexVector& v, const Smplx& s, const Generator::Comparison& cmp)
{
    SimplexVector::const_iterator it = std::lower_bound(v.begin(), v.end(), s, cmp);
    while (neq(*it, s)) ++it;
    return it - v.begin();
}

void output_boundary_matrix(std::ostream& out, const SimplexVector& v, const Generator::Comparison& cmp)
{
    unsigned i = 0;
    for (SimplexVector::const_iterator cur = v.begin(); cur != v.end(); ++cur)
    {
        // std::cout << "Simplex: " << *cur << std::endl;
        bool                sign = true;
        for (Smplx::BoundaryIterator bcur  = cur->boundary_begin(); bcur != cur->boundary_end(); ++bcur)
        {
            // std::cout << "  " << *bcur << std::endl;
            out << (sign ? 1 : -1) << " ";
            out << index(v, *bcur, cmp) << " " << i << "\n";
            sign = !sign;
        }
        ++i;
    }
}

void output_vertex_indices(std::ostream& out, const SimplexVector& v)
{
    unsigned i = 0;
    for (SimplexVector::const_iterator cur = v.begin(); cur != v.end(); ++cur)
    {
        if (cur->dimension() == 0)
            out << i << " " << cur->vertices()[0] << std::endl;
        ++i;
    }
}

void output_cocycle(std::string cocycle_prefix, unsigned i, const SimplexVector& v, const Persistence::Cocycle& c, ZpField::Element prime, const Generator::Comparison& cmp)
{
    std::ostringstream istr; istr << '-' << i;
    std::string filename = cocycle_prefix + istr.str() + ".ccl";
    std::ofstream out(filename.c_str());
    out << "# Cocycle born at " << c.birth.get<1>() << std::endl;
    for (Persistence::ZColumn::const_iterator zcur = c.zcolumn.begin(); zcur != c.zcolumn.end(); ++zcur)
    {
        const Smplx& s = **(zcur->si);
        out << (zcur->coefficient <= prime/2 ? zcur->coefficient : zcur->coefficient - prime) << " ";
        out << index(v, s, cmp) << "\n";
    }
}