1use std::ops::{Add, Mul, Sub};
3
4#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Default)]
6pub struct Linear<T> {
7 pub a: T,
9 pub b: T,
11}
12
13impl<T> Linear<T> {
14 pub fn new(a: T, b: T) -> Self {
16 Self { a, b }
17 }
18}
19
20impl<T: Add<Output = T> + Mul<Output = T> + Copy> Linear<T> {
21 pub fn apply(&self, x: T) -> T {
23 self.a * x + self.b
24 }
25}
26
27impl<T: Sub<Output = T> + Mul<Output = T> + Copy> Linear<T> {
28 pub fn mov_x(&self, dx: T) -> Self {
30 Self {
31 a: self.a,
32 b: self.b - self.a * dx,
33 }
34 }
35}