#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use std::{
ops::{Add, AddAssign},
time::Instant,
};
use crate::traits::Real;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Evals {
pub function: usize,
pub jacobian: usize,
pub newton: usize,
pub decompositions: usize,
pub solves: usize,
}
impl Evals {
pub fn new() -> Self {
Self {
function: 0,
jacobian: 0,
newton: 0,
decompositions: 0,
solves: 0,
}
}
}
impl Add for Evals {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
function: self.function + other.function,
jacobian: self.jacobian + other.jacobian,
newton: self.newton + other.newton,
decompositions: self.decompositions + other.decompositions,
solves: self.solves + other.solves,
}
}
}
impl AddAssign for Evals {
fn add_assign(&mut self, other: Self) {
self.function += other.function;
self.jacobian += other.jacobian;
self.newton += other.newton;
self.decompositions += other.decompositions;
self.solves += other.solves;
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Steps {
pub accepted: usize,
pub rejected: usize,
}
impl Steps {
pub fn new() -> Self {
Self {
accepted: 0,
rejected: 0,
}
}
pub fn total(&self) -> usize {
self.accepted + self.rejected
}
}
impl Add for Steps {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
accepted: self.accepted + other.accepted,
rejected: self.rejected + other.rejected,
}
}
}
impl AddAssign for Steps {
fn add_assign(&mut self, other: Self) {
self.accepted += other.accepted;
self.rejected += other.rejected;
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub enum Timer<T: Real> {
Off,
#[cfg_attr(feature = "serde", serde(skip_serializing, skip_deserializing))]
Running(Instant),
Completed(T),
}
impl<T: Real> Timer<T> {
pub fn start(&mut self) {
*self = Timer::Running(Instant::now());
}
pub fn elapsed(&self) -> T {
match self {
Timer::Off => T::zero(),
Timer::Running(start_time) => T::from_f64(start_time.elapsed().as_secs_f64()).unwrap(),
Timer::Completed(t) => *t,
}
}
pub fn complete(&mut self) {
match self {
Timer::Off => {}
Timer::Running(start_time) => {
*self = Timer::Completed(T::from_f64(start_time.elapsed().as_secs_f64()).unwrap());
}
Timer::Completed(_) => {}
}
}
}