#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------

object SimplicialComplex {

# @category Topology
# to compute a Morse matching by the greedy algorithm
label morse_greedy

# @category Topology
# to compute a Morse matching by Forman's canceling algorithm
label morse_cancel

# @category Topology
# to compute a Morse matching by both the greedy algorithm and Forman's canceling algorithm
label morse_both

prefer morse_greedy

# @category Topology
# Morse matching in the Hasse diagram of the simplicial complex
property MORSE_MATCHING : MorseMatching : multiple {

   # The matching in the HasseDiagram of the SimplicialComplex
   property MATCHING : EdgeMap<Directed, Int> : construct(parent.HASSE_DIAGRAM.ADJACENCY);

};

# clients to compute Morse matching:

rule morse_greedy.morse_matching: MORSE_MATCHING(new).MATCHING : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, PSEUDO_MANIFOLD {
   $this->MORSE_MATCHING->MATCHING = morse_matching($this, heuristic=>1, levels=> $this->PSEUDO_MANIFOLD ? 0 : 1);
}

rule morse_cancel.morse_matching: MORSE_MATCHING(new).MATCHING : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, PSEUDO_MANIFOLD {
   $this->MORSE_MATCHING->MATCHING = morse_matching($this, heuristic=>2, levels=> $this->PSEUDO_MANIFOLD ? 0 : 1);
}

rule morse_both.morse_matching: MORSE_MATCHING(new).MATCHING : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, PSEUDO_MANIFOLD {
   $this->MORSE_MATCHING->MATCHING = morse_matching($this, heuristic=>0, levels=> $this->PSEUDO_MANIFOLD ? 0 : 1);
}

rule morse_greedy.morse_matching: MORSE_MATCHING(any).MATCHING : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, PSEUDO_MANIFOLD {
   $this->MORSE_MATCHING->MATCHING = morse_matching($this, heuristic=>1, levels=> $this->PSEUDO_MANIFOLD ? 0 : 1);
}

rule morse_cancel.morse_matching: MORSE_MATCHING(any).MATCHING : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, PSEUDO_MANIFOLD {
   $this->MORSE_MATCHING->MATCHING = morse_matching($this, heuristic=>2, levels=> $this->PSEUDO_MANIFOLD ? 0 : 1);
}

rule morse_both.morse_matching: MORSE_MATCHING(any).MATCHING : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, PSEUDO_MANIFOLD {
   $this->MORSE_MATCHING->MATCHING = morse_matching($this, heuristic=>0, levels=> $this->PSEUDO_MANIFOLD ? 0 : 1);
}

rule MORSE_MATCHING.SIZE : MORSE_MATCHING.MATCHING {
   $this->MORSE_MATCHING->SIZE = morse_matching_size($this->MORSE_MATCHING);
}

rule MORSE_MATCHING.CRITICAL_FACES, MORSE_MATCHING.CRITICAL_FACE_VECTOR : HASSE_DIAGRAM.ADJACENCY, HASSE_DIAGRAM.INVERSE_RANK_MAP, MORSE_MATCHING.MATCHING {

   morse_matching_critical_faces($this);
}

}

object MorseMatching {

rule N_CRITICAL_FACES : CRITICAL_FACE_VECTOR {
   my $n=0;
   foreach my $f (@{$this->CRITICAL_FACE_VECTOR}) {
      $n += $f;
   }
   $this->N_CRITICAL_FACES = $n;
}

}

# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
