From baa4c70c0c8d82147cf29ed486c1c94c905def38 Mon Sep 17 00:00:00 2001 From: William Ball Date: Fri, 7 Jan 2022 12:51:35 -0800 Subject: [PATCH] minor fixes/updates --- .../circle_counting_new/src/dimension.rs | 2 +- fractal_dimension/circle_counting_new/src/main.rs | 2 +- fractal_dimension/circle_counting_new/src/parser.rs | 7 +++++++ fractal_dimension/circle_counting_new/src/picture.rs | 2 +- fractal_dimension/circle_counting_new/src/time.rs | 12 ++++++++++-- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/fractal_dimension/circle_counting_new/src/dimension.rs b/fractal_dimension/circle_counting_new/src/dimension.rs index f376288..0da4e0b 100644 --- a/fractal_dimension/circle_counting_new/src/dimension.rs +++ b/fractal_dimension/circle_counting_new/src/dimension.rs @@ -5,7 +5,7 @@ use ansi_term::Color::Yellow; use linregress::{FormulaRegressionBuilder, RegressionDataBuilder}; use nalgebra::{DMatrix, DVector}; -/// struct representing the circle counting task to be used by `crate::search::Searcher` to compute +/// Struct representing the circle counting task to be used by `crate::search::Searcher` to compute /// the fractal dimension via the circle counting method struct CircleCountingTask { counts: Vec, diff --git a/fractal_dimension/circle_counting_new/src/main.rs b/fractal_dimension/circle_counting_new/src/main.rs index 4c37bb1..379ae06 100644 --- a/fractal_dimension/circle_counting_new/src/main.rs +++ b/fractal_dimension/circle_counting_new/src/main.rs @@ -36,7 +36,7 @@ fn main() { max, n, debug, - 0, + recursion_depth, &timer, ) .unwrap_or_else(|e| panic!("Regression went wrong: {}", e)); diff --git a/fractal_dimension/circle_counting_new/src/parser.rs b/fractal_dimension/circle_counting_new/src/parser.rs index 71d3aa1..8f6ac1a 100644 --- a/fractal_dimension/circle_counting_new/src/parser.rs +++ b/fractal_dimension/circle_counting_new/src/parser.rs @@ -13,10 +13,12 @@ use nom::{ IResult, }; +/// Combinator to skip "whitespace" fn whitespace<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> { is_a(" \t\r\n`\\,")(i) } +/// Combinator to parse a positive integer and return the integer it represents as a value fn int<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, usize, E> { map(digit0, |s: &str| { if let Ok(val) = s.parse::() { @@ -27,6 +29,8 @@ fn int<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, usize, E> { })(i) } +/// Function that, given a combinator representing the kind of element, returns a combinator that +/// parses a list delimited by braces fn list<'a, F: 'a, O, E: ParseError<&'a str>>( element: F, ) -> impl FnMut(&'a str) -> IResult<&'a str, Vec, E> @@ -36,6 +40,7 @@ where delimited(char('{'), separated_list0(whitespace, element), char('}')) } +/// Combinator that parses a matrix: i.e. a nested list of floats, and returns a `DMatrix` fn matrix<'a, E: 'a + ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, DMatrix, E> { let (tail, output) = list(list(double))(i)?; let n = output.len(); @@ -48,6 +53,8 @@ fn matrix<'a, E: 'a + ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, DMatr Ok((tail, DMatrix::from_row_slice(n, m, &output.concat()))) } +/// Reads the file `filename`, which should first have the gram matrix, and then the face scheme, +/// parses it, and returns the gram matrix and face scheme pub fn read_file(filename: &str) -> (DMatrix, Vec>) { let contents = read_to_string(filename) .unwrap_or_else(|e| panic!("Something went wrong reading {}: {}", filename, e)); diff --git a/fractal_dimension/circle_counting_new/src/picture.rs b/fractal_dimension/circle_counting_new/src/picture.rs index 59f12b0..615cf89 100644 --- a/fractal_dimension/circle_counting_new/src/picture.rs +++ b/fractal_dimension/circle_counting_new/src/picture.rs @@ -48,7 +48,7 @@ impl PictureTask { fn new(max_curvature: f64, width: usize, height: usize) -> PictureTask { PictureTask { svg: format!( - "\n\n", + "\n\n", width, height ), max_curvature, diff --git a/fractal_dimension/circle_counting_new/src/time.rs b/fractal_dimension/circle_counting_new/src/time.rs index 07d82ac..d88e7c6 100644 --- a/fractal_dimension/circle_counting_new/src/time.rs +++ b/fractal_dimension/circle_counting_new/src/time.rs @@ -1,12 +1,14 @@ #![allow(dead_code)] use std::time::{Duration, Instant}; +/// Handy struct to keep track of timing things and making predictions pub struct Timer { start: Instant, active: bool, } impl Timer { + /// Constructor returning a timer. `active` tells whether or not to enable message printing. pub fn new(active: bool) -> Self { Self { start: Instant::now(), @@ -14,10 +16,13 @@ impl Timer { } } + /// Returns the time elapsed since the timer was constructed pub fn time_elapsed(&self) -> Duration { Instant::now().duration_since(self.start) } + /// Given a duration, prints `before` followed by the duration, in minutes/seconds, followed by + /// `after`. fn print_time_message(&self, duration: Duration, before: &str, after: &str) { if self.active { let time = duration.as_secs_f64(); @@ -32,13 +37,16 @@ impl Timer { } } + /// Prints `before` + time elapsed since timer was activated + `after` pub fn time_stamp(&self, before: &str, after: &str) { self.print_time_message(self.time_elapsed(), before, after); } - pub fn time_remaining(&self, portion: f64, before: &str, after: &str) { + /// Exactly like `time_stamp` by prints `factor * self.time_elapsed()` rather than just + /// `self.time_elapsed()` + pub fn time_remaining(&self, factor: f64, before: &str, after: &str) { self.print_time_message( - Duration::from_secs_f64(self.time_elapsed().as_secs_f64() * portion), + Duration::from_secs_f64(self.time_elapsed().as_secs_f64() * factor), before, after, );