~mht/cra

4596b318c643d48eb3feac5a84c8f3ff2fef5444 — Martin Hafskjold Thoresen 11 months ago b960d20
Remove dead and commented out code
3 files changed, 48 insertions(+), 127 deletions(-)

M cra/src/main.rs
M cra/src/output.rs
M cra/src/persistence.rs
M cra/src/main.rs => cra/src/main.rs +8 -27
@@ 444,12 444,17 @@ the first boundaries in the list should be that of the 1-simplices.

    let mut stats = Statistics::new();

    let reduction = reduce(&persistence, Standard, &mut stats);
    let variant = if matches.is_present("exhaustive") {
        Exhaustive
    } else {
        Standard
    };
    let reduction = reduce(&persistence, variant, &mut stats);

    if debug {
        let mut out = std::io::stderr();
        let mut out = std::io::stdout();
        reduction
            .write_table(&mut out, &persistence.simplices)
            .debug_output(&mut out, &persistence.simplices)
            .unwrap();
    }



@@ 464,30 469,6 @@ the first boundaries in the list should be that of the 1-simplices.
        output::tex_table(&stats, &stats, &mut tex_file).unwrap();
    }

    //    output::histogram(
    //        &e_stats.ex_searches,
    //        "Iters for finding k=low(i) for any i",
    //        &out_path.join("ex_searches.pdf"),
    //    );
    //    output::histogram(
    //        &e_stats.ex_reductions,
    //        "Iters for exhaustively reducing a column",
    //        &out_path.join("ex_reductions.pdf"),
    //    );
    //
    //    output::histogram2(
    //        &r_stats.add_size_sum,
    //        &e_stats.add_size_sum,
    //        "Cost estimate for column additions",
    //        &out_path.join("add_size_sum.pdf"),
    //    );
    //    output::histogram2(
    //        &r_stats.num_iters,
    //        &e_stats.num_iters,
    //        "Number of column additions before fully reducing one column",
    //        &out_path.join("num_iters.pdf"),
    //    );

    if let Some(path) = matches.value_of("graphviz") {
        let mut f = File::create(Path::new(path)).unwrap();
        output::graphviz(&persistence, &reduction, &reduction, &mut f).unwrap();

M cra/src/output.rs => cra/src/output.rs +0 -42
@@ 620,48 620,6 @@ pub fn graphviz<W: Write>(
    const SCALE: f64 = 5.0;
    writeln!(w, "digraph {{")?;
    writeln!(w, "  rankdir=LR;")?;
    if false {
        for simplex in &p.simplices {
            if simplex.dim() == 0 {
                let pt = p.points[simplex.j - 1];
                writeln!(
                    w,
                    "  {} [label=\"v{} [{}]\",pos=\"{},{}\"];",
                    simplex.j,
                    simplex.j,
                    std.j_stored_at[simplex.j],
                    pt[0] * SCALE,
                    -pt[1] * SCALE
                )?;
            } else if simplex.dim() == 1 {
                let x = Point(p.points[simplex.faces[0] - 1]);
                let y = Point(p.points[simplex.faces[1] - 1]);
                let center = (x + y) / 2.0;
                writeln!(
                    w,
                    "  {} [label=\"e{}\",pos=\"{},{}\"];",
                    simplex.j,
                    simplex.j,
                    center.0[0] * SCALE,
                    -center.0[1] * SCALE
                )?;
            } else if simplex.dim() == 2 {
                let (x_i, y_i, z_i) = get_triangle_xyz(simplex, &p.simplices);
                let x = Point(p.points[x_i - 1]);
                let y = Point(p.points[y_i - 1]);
                let z = Point(p.points[z_i - 1]);
                let center = (x + y + z) / 3.0;
                writeln!(
                    w,
                    "  {} [label=\"f{}\",pos=\"{},{}\"];",
                    simplex.j,
                    simplex.j,
                    center.0[0] * SCALE,
                    -center.0[1] * SCALE
                )?;
            }
        }
    }

    // TODO: Rethink what we want here
    for i in 0..p.simplices.len() {

M cra/src/persistence.rs => cra/src/persistence.rs +40 -58
@@ 52,10 52,9 @@ pub struct Persistence {
/// The result from reducing.
#[derive(Debug)]
pub struct Reduction {
    /// Two simplices are paired when a simplex has been fully reduced; it is then paired with its highest boundary element.
    /// When we reduce a simplex we might finding that it's `low` is unique (that `simplex_with_low[low] == None`).
    /// We then store the reduced simplex in that slot. This corresponds to pairing `low` with `j`.
    pub pairings: Vec<(usize, usize)>,
    /// The original `j` of the simplex stored in `simplex_with_low`.
    pub j_stored_at: Vec<usize>,
    /// This maps an index to the reduced simplex which maximum boundary element is that index.
    pub simplex_with_low: Vec<Option<Simplex>>,
    /// Here we track which elements are added to which columns.


@@ 64,67 63,46 @@ pub struct Reduction {
}

impl Reduction {
    /// Write out the reduced thing to the `writer`. Useful for debugging.
    #[allow(dead_code)]
    pub fn write_out<W: Write>(
        &self,
        writer: &mut W,
        simplices: &[Simplex],
    ) -> Result<(), std::io::Error> {
        for (&j, il) in self.j_stored_at.iter().zip(self.simplex_with_low.iter()) {
            let simplex_i = simplices.iter().position(|s| s.j == j).unwrap();
            let simplex = &simplices[simplex_i];
            writeln!(
                writer,
                "j={}, faces={:?} => {:?}",
                simplex.j, simplex.faces, il
            )?;
        }
        Ok(())
    }

    /// Write out a table containing information of the reduction.
    /// TODO: What does this table show again?
    pub fn write_table<W: Write>(
    /// Write debug output for this reduction to `w`. This includes how each simplex was reduced.
    pub fn debug_output<W: Write>(
        &self,
        w: &mut W,
        simplices: &[Simplex],
    ) -> Result<(), std::io::Error> {
        let n = self.simplex_with_low.len();
        use std::collections::HashMap;

        let additions_of = {
            let mut v = vec![vec![]; n];
            for &(from, to) in &self.additions {
                v[to].push(from);
            }
            for ve in v.iter_mut() {
                ve.sort();
            }
            v
        };
        let mut added_to = HashMap::<usize, Vec<usize>>::new();
        for &(left, right) in &self.additions {
            added_to.entry(left).or_insert(Vec::new()).push(right);
        }

        let i_of_j = {
            let mut v = vec![None; n];
            for (i, &j) in self.j_stored_at.iter().enumerate() {
                // simplex `j` is stored at index `i` in `self.simplex_with_low`.
                if i > 0 {
                    v[j] = Some(i);
                }
            }
            v
        };
        let mut killed = HashMap::new();
        for &(victim, killer) in &self.pairings {
            killed.insert(killer, victim);
        }

        writeln!(w, "j;| faces;| adds;| reduced")?;
        for j in 1..n {
            let s = &simplices[j];
            write!(w, "{};| {:?};| {:?};| ", s.j, s.faces, additions_of[j])?;
            if let Some(_i) = i_of_j[j] {
                // writeln!(w, "{:?}", self.simplex_with_low[i].get())?;
                writeln!(w, " +")?;
        let n = simplices.len();
        writeln!(w, "j;faces;adds;reduced")?;
        for s in simplices {
            let empty_vec = Vec::new();
            let j = s.j;
            let initial_faces = &s.faces;
            let reduced_faces = if let Some(&place) = killed.get(&j) {
                let v = self.simplex_with_low[place]
                    .as_ref()
                    .expect("Simplex was killing, but wasn't stored?");
                &v.faces
            } else {
                writeln!(w)?;
            }
                &empty_vec
            };
            let adds = added_to.get(&s.j).unwrap_or(&empty_vec);
            writeln!(
                w,
                "{};{:?};{:?};{:?}",
                j, initial_faces, adds, reduced_faces
            )?;
        }

        Ok(())
    }
}


@@ 132,6 110,8 @@ impl Reduction {
/// Run the reduction algorithm on the given `Persistence`. The `variant` is ran,
/// and stats are recorded in `stats`.
pub fn reduce(p: &Persistence, variant: Variant, _stats: &mut Statistics) -> Reduction {
    let mut pairings = Vec::new();
    let mut additions = Vec::new();
    let mut reduced_with_low: Vec<Option<Simplex>> = vec![None; p.simplices.len()];
    // We're reducing each simplex.
    for simplex in &p.simplices {


@@ 147,9 127,11 @@ pub fn reduce(p: &Persistence, variant: Variant, _stats: &mut Statistics) -> Red
        while current_reduced.faces.len() > 0 {
            let low = *current_reduced.faces.last().unwrap();
            if let Some(ref other) = reduced_with_low[low] {
                additions.push((current_reduced.j, other.j));
                simplex_reduce(&mut current_reduced, other);
            } else {
                place_index = Some(low);
                pairings.push((low, simplex.j));
                break;
            }
        }


@@ 164,6 146,7 @@ pub fn reduce(p: &Persistence, variant: Variant, _stats: &mut Statistics) -> Red
            while safe_simplices < n {
                let cand_i = current_reduced.faces[n - 1 - safe_simplices];
                if let Some(ref cand) = reduced_with_low[cand_i] {
                    additions.push((current_reduced.j, cand.j));
                    simplex_reduce(&mut current_reduced, cand);
                    n = current_reduced.faces.len();
                } else {


@@ 181,10 164,9 @@ pub fn reduce(p: &Persistence, variant: Variant, _stats: &mut Statistics) -> Red
    }

    Reduction {
        pairings: Vec::new(),
        j_stored_at: Vec::new(),
        pairings,
        simplex_with_low: reduced_with_low,
        additions: Vec::new(),
        additions,
    }
}