Files
symplectic-double-pendulum/src/hamiltonian.rs
T

64 lines
1001 B
Rust

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;
}
}