bindings/python/cohomology-persistence.cpp
author Dmitriy Morozov <dmitriy@mrzv.org>
Wed, 28 Oct 2009 14:38:36 -0700
branchdev
changeset 172 a6605dc232f2
parent 166 1f3d54b2315b
child 173 5fd3f43e6fbf
permissions -rw-r--r--
Added image persistence functionality to CohomologyPersistence

#include <topology/cohomology-persistence.h>

#include <boost/python.hpp>
#include <boost/python/stl_iterator.hpp>
#include <boost/shared_ptr.hpp>
namespace bp = boost::python;

#include "cohomology-persistence.h"             // defines CohomPersistence
#include "optional.h"
namespace dp = dionysus::python;


// CohomPersistence
boost::shared_ptr<dp::CohomPersistence>     init_from_prime(unsigned p = 11)
{
    dp::CohomPersistence::Field field(p);       // Zp

    boost::shared_ptr<dp::CohomPersistence> chp(new dp::CohomPersistence(field));
    return chp;
}

bp::tuple                                   chp_add(dp::CohomPersistence& chp, bp::object bdry, dp::BirthID birth)
{
    dp::CohomPersistence::SimplexIndex      i;
    dp::CohomPersistence::Death             d;
    boost::tie(i,d)                                 = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
                                                              birth); 
    return bp::make_tuple(i,d);
}


bp::tuple                                   chp_add_store(dp::CohomPersistence& chp, bp::object bdry, dp::BirthID birth, bool store)
{
    dp::CohomPersistence::SimplexIndex      i;
    dp::CohomPersistence::Death             d;
    boost::tie(i,d)                                 = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
                                                              birth, store); 
    return bp::make_tuple(i,d);
}

bp::tuple                                   chp_add_store_image(dp::CohomPersistence& chp, bp::object bdry, dp::BirthID birth, bool store, bool image)
{
    dp::CohomPersistence::SimplexIndex      i;
    dp::CohomPersistence::Death             d;
    boost::tie(i,d)                                 = chp.add(bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(bdry),
                                                              bp::stl_input_iterator<dp::CohomPersistence::SimplexIndex>(),
                                                              birth, store, dp::CohomPersistence::SimplexData(), image); 
    return bp::make_tuple(i,d);
}

dp::CohomPersistence::ZColumn::const_iterator     
cocycle_zcolumn_begin(dp::CohomPersistence::Cocycle& ccl)                   
{ return ccl.zcolumn.begin(); }

dp::CohomPersistence::ZColumn::const_iterator     
cocycle_zcolumn_end(dp::CohomPersistence::Cocycle& ccl)                   
{ return ccl.zcolumn.end(); }


// SimplexIndex
template<class T>
unsigned                            si_order(T& si)
{
    return si->order;
}


void export_cohomology_persistence()
{
    bp::class_<dp::CohomPersistence::SimplexIndex>("CHSimplexIndex")
        .add_property("order",          &si_order<dp::CohomPersistence::SimplexIndex>)
    ;
    
    bp::class_<dp::CohomPersistence::SNode>("CHSNode", bp::no_init)
        .add_property("coefficient",    &dp::CohomPersistence::SNode::coefficient)
        .add_property("si",             &dp::CohomPersistence::SNode::si)
    ;

    bp::class_<dp::CohomPersistence>("CohomologyPersistence")
        .def("__init__",        bp::make_constructor(&init_from_prime))
        .def("add",             &chp_add)
        .def("add",             &chp_add_store)
        .def("add",             &chp_add_store_image)
        
        .def("__iter__",        bp::range(&dp::CohomPersistence::begin, &dp::CohomPersistence::end))
    ;

    bp::class_<dp::CohomPersistence::Cocycle>("Cocycle", bp::no_init)
        .add_property("birth",  &dp::CohomPersistence::Cocycle::birth)
        .def("__iter__",        bp::range(&cocycle_zcolumn_begin, &cocycle_zcolumn_end))
    ;
}