examples/cohomology/output.h
author Dmitriy Morozov <dmitriy@mrzv.org>
Thu, 03 Dec 2009 13:21:23 -0800
branchdev
changeset 178 cc0c26fba495
parent 157 700cbac5b23c
child 187 25e468323d77
permissions -rw-r--r--
Made storing negative simplices in StaticPersistence parameter-based, so that DynamicPersistence works 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);
}

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

template<class Comparison>
void output_boundary_matrix(std::ostream& out, const SimplexVector& v, const 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)
{
    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 << zcur->si->getValue() << "\n";
    }
}