minor fixes/updates

This commit is contained in:
William Ball 2022-01-07 12:51:35 -08:00
parent 5c254723a9
commit baa4c70c0c
5 changed files with 20 additions and 5 deletions

View file

@ -5,7 +5,7 @@ use ansi_term::Color::Yellow;
use linregress::{FormulaRegressionBuilder, RegressionDataBuilder}; use linregress::{FormulaRegressionBuilder, RegressionDataBuilder};
use nalgebra::{DMatrix, DVector}; 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 /// the fractal dimension via the circle counting method
struct CircleCountingTask { struct CircleCountingTask {
counts: Vec<usize>, counts: Vec<usize>,

View file

@ -36,7 +36,7 @@ fn main() {
max, max,
n, n,
debug, debug,
0, recursion_depth,
&timer, &timer,
) )
.unwrap_or_else(|e| panic!("Regression went wrong: {}", e)); .unwrap_or_else(|e| panic!("Regression went wrong: {}", e));

View file

@ -13,10 +13,12 @@ use nom::{
IResult, IResult,
}; };
/// Combinator to skip "whitespace"
fn whitespace<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> { fn whitespace<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, &'a str, E> {
is_a(" \t\r\n`\\,")(i) 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> { fn int<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, usize, E> {
map(digit0, |s: &str| { map(digit0, |s: &str| {
if let Ok(val) = s.parse::<usize>() { if let Ok(val) = s.parse::<usize>() {
@ -27,6 +29,8 @@ fn int<'a, E: ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, usize, E> {
})(i) })(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>>( fn list<'a, F: 'a, O, E: ParseError<&'a str>>(
element: F, element: F,
) -> impl FnMut(&'a str) -> IResult<&'a str, Vec<O>, E> ) -> impl FnMut(&'a str) -> IResult<&'a str, Vec<O>, E>
@ -36,6 +40,7 @@ where
delimited(char('{'), separated_list0(whitespace, element), char('}')) 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<f64>, E> { fn matrix<'a, E: 'a + ParseError<&'a str>>(i: &'a str) -> IResult<&'a str, DMatrix<f64>, E> {
let (tail, output) = list(list(double))(i)?; let (tail, output) = list(list(double))(i)?;
let n = output.len(); 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()))) 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<f64>, Vec<Vec<usize>>) { pub fn read_file(filename: &str) -> (DMatrix<f64>, Vec<Vec<usize>>) {
let contents = read_to_string(filename) let contents = read_to_string(filename)
.unwrap_or_else(|e| panic!("Something went wrong reading {}: {}", filename, e)); .unwrap_or_else(|e| panic!("Something went wrong reading {}: {}", filename, e));

View file

@ -48,7 +48,7 @@ impl PictureTask {
fn new(max_curvature: f64, width: usize, height: usize) -> PictureTask { fn new(max_curvature: f64, width: usize, height: usize) -> PictureTask {
PictureTask { PictureTask {
svg: format!( svg: format!(
"<?xml version=\"1.0\" standalone=\"no\"?>\n<svg width=\"{}\" height=\"{}\">\n", "<?xml version=\"1.0\" standalone=\"no\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"{}\" height=\"{}\">\n",
width, height width, height
), ),
max_curvature, max_curvature,

View file

@ -1,12 +1,14 @@
#![allow(dead_code)] #![allow(dead_code)]
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
/// Handy struct to keep track of timing things and making predictions
pub struct Timer { pub struct Timer {
start: Instant, start: Instant,
active: bool, active: bool,
} }
impl Timer { impl Timer {
/// Constructor returning a timer. `active` tells whether or not to enable message printing.
pub fn new(active: bool) -> Self { pub fn new(active: bool) -> Self {
Self { Self {
start: Instant::now(), start: Instant::now(),
@ -14,10 +16,13 @@ impl Timer {
} }
} }
/// Returns the time elapsed since the timer was constructed
pub fn time_elapsed(&self) -> Duration { pub fn time_elapsed(&self) -> Duration {
Instant::now().duration_since(self.start) 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) { fn print_time_message(&self, duration: Duration, before: &str, after: &str) {
if self.active { if self.active {
let time = duration.as_secs_f64(); 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) { pub fn time_stamp(&self, before: &str, after: &str) {
self.print_time_message(self.time_elapsed(), before, after); 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( 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, before,
after, after,
); );