Create Hamiltonian trait and State struct
This commit is contained in:
commit
91a5da0d74
4 files changed
+380
No files matched your search
@@ -0,0 +1,63 @@
|
||||
use std::ops::{Add, Sub, Mul, Div, AddAssign};
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct State<T> {
|
||||
pub q: T,
|
||||
pub p: T,
|
||||
}
|
||||
|
||||
pub trait Hamiltonian {
|
||||
fn energy(&self) -> f64;
|
||||
fn dots(&self) -> Self;
|
||||
}
|
||||
|
||||
impl<T: Add<Output = T>> Add for State<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn add(self, rhs: Self) -> Self {
|
||||
Self {
|
||||
q: self.q + rhs.q,
|
||||
p: self.p + rhs.p,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Sub<Output = T>> Sub for State<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn sub(self, rhs: Self) -> Self {
|
||||
Self {
|
||||
q: self.q - rhs.q,
|
||||
p: self.p - rhs.p,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Mul<f64, Output = T>> Mul<f64> for State<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(self, rhs: f64) -> Self {
|
||||
Self {
|
||||
q: self.q * rhs,
|
||||
p: self.p * rhs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Div<f64, Output = T>> Div<f64> for State<T> {
|
||||
type Output = Self;
|
||||
|
||||
fn div(self, rhs: f64) -> Self {
|
||||
Self {
|
||||
q: self.q / rhs,
|
||||
p: self.p / rhs,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: AddAssign> AddAssign for State<T> {
|
||||
fn add_assign(&mut self, other: Self) {
|
||||
self.q += other.q;
|
||||
self.p += other.p;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
pub mod hamiltonian;
|
||||
|
||||
pub use hamiltonian::*;
|
||||
Reference in new issue
Block a user