Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 56 additions & 9 deletions src/systems/polynom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,6 @@ where

fn mul(self, rhs: Self) -> Self::Output {
&self * &rhs
// let mut result =
// vec![T::default(); self.coeffs.len() + rhs.coeffs.len() - 1];
// for (idx_l, val_l) in self.coeffs.iter().enumerate() {
// for (idx_r, val_r) in rhs.coeffs.iter().enumerate() {
// result[idx_l + idx_r] += val_l.clone() * val_r.clone();
// }
// }

// Polynomial { coeffs: result }
}
}

Expand Down Expand Up @@ -270,6 +261,26 @@ macro_rules! impl_one_operator_scalar_trait {
scalar.$operator_fn(rhs)
}
}

impl $operator<$scalar_type> for &$struct_type<$scalar_type>
{
type Output = $struct_type<$scalar_type>;
fn $operator_fn(self, rhs: $scalar_type) -> Self::Output {
let scalar = $struct_type::<$scalar_type>::new_from_scalar(rhs);
self.$operator_fn(&scalar)
// self.$operator_fn($struct_type::<$scalar_type>::new_from_scalar(rhs))
}
}

impl $operator<&$struct_type<$scalar_type>> for $scalar_type {
type Output = $struct_type<$scalar_type>;
fn $operator_fn(self, rhs: &$struct_type<$scalar_type>) -> Self::Output {
let scalar = &$struct_type::<$scalar_type>::new_from_scalar(self);
scalar.$operator_fn(rhs)
}
}


)*
};
}
Expand Down Expand Up @@ -632,6 +643,37 @@ impl_compound_assign!(
]
);

macro_rules! impl_comb_ref_and_no_ref_operators {
($struct_type:ident , [$(($operator:ident, $operator_fn:ident)), *]) => {
$(
impl<T> $operator<&$struct_type<T>> for $struct_type<T>
where
T: Add + Default + Mul<Output = T> + AddAssign + Clone + Copy + Neg<Output = T>,
{
type Output = $struct_type<T>;
fn $operator_fn(self, rhs: &$struct_type<T>) -> Self::Output {
(&self).$operator_fn(rhs)
}
}

impl<T> $operator<$struct_type<T>> for &$struct_type<T>
where
T: Add + Default + Mul<Output = T> + AddAssign + Clone + Copy + Neg<Output = T>,
{
type Output = $struct_type<T>;
fn $operator_fn(self, rhs: $struct_type<T>) -> Self::Output {
self.$operator_fn(&rhs)
}
}
)*
};
}

impl_comb_ref_and_no_ref_operators!(
RationalFunction,
[(Mul, mul), (Div, div), (Sub, sub), (Add, add)]
);

impl_one_operator_scalar_trait!(
RationalFunction,
f64,
Expand Down Expand Up @@ -969,5 +1011,10 @@ mod tests {

let ans: RationalFunction<f64> = 3.0 * rf2;
assert_abs_diff_eq!(rf1.eval(&1.2), ans.eval(&1.2));

let s = RationalFunction::new_from_coeffs(&[0.0, 1.0], &[1.0]);
let _ = 1.0 / &s * 10.0 * &s - 3.0 / &s;
let _ = &s * (1.0 + &s);
let _ = &s + 1.0 - &s / (1.0 + &s) * &s * (1.0 * &s);
}
}
52 changes: 52 additions & 0 deletions src/systems/state_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,57 @@ macro_rules! impl_scalar_math_operator_ss {
lhs_ss.$operator_fn(rhs)
}
}

impl<U: Time + 'static> $operator<f64> for &Ss<U> {
type Output = Ss<U>;
fn $operator_fn(self, rhs: f64) -> Self::Output {
let rhs_ss = Ss::<U>::new_from_scalar(rhs);
self.$operator_fn(&rhs_ss)
}
}

impl<U: Time + 'static> $operator<&Ss<U>> for f64 {
type Output = Ss<U>;
fn $operator_fn(self, rhs: &Ss<U>) -> Self::Output {
let scalar_ss = Ss::<U>::new_from_scalar(self);
(&scalar_ss).$operator_fn(rhs)
}
}
)*
};
}

impl_scalar_math_operator_ss!([(Add, add), (Sub, sub), (Mul, mul), (Div, div)]);

macro_rules! impl_comb_ref_and_no_ref_operators {
([$(($operator:ident, $operator_fn:ident)), *]) => {
$(
impl<U: Time + 'static> $operator<&Ss<U>> for Ss<U>
{
type Output = Ss<U>;
fn $operator_fn(self, rhs: &Ss<U>) -> Self::Output {
(&self).$operator_fn(rhs)
}
}

impl<U: Time + 'static> $operator<Ss<U>> for &Ss<U>
{
type Output = Ss<U>;
fn $operator_fn(self, rhs: Ss<U>) -> Self::Output {
self.$operator_fn(&rhs)
}
}
)*
};
}

impl_comb_ref_and_no_ref_operators!([
(Add, add),
(Sub, sub),
(Mul, mul),
(Div, div)
]);

impl<U: Time + 'static> fmt::Display for Ss<U> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
Expand Down Expand Up @@ -982,5 +1027,12 @@ mod tests {

assert_abs_diff_eq!(resp1.re, resp2.re, epsilon = 1e-9);
assert_abs_diff_eq!(resp1.im, resp2.im, epsilon = 1e-9);

let s_inv = (1.0 / Tf::s()).to_ss().unwrap();
let _ =
1.0 + &s_inv * &s_inv / (1.0 + &s_inv / 1.0) + (&s_inv - &s_inv);
let _ = 1.0 + &s_inv;
let _ = s_inv.clone() + &s_inv;
let _ = &s_inv + s_inv.clone();
}
}
58 changes: 53 additions & 5 deletions src/systems/transfer_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ macro_rules! impl_scalar_math_operator {
Tf::new_from_rf(new_rf)
}
}

impl<U: Time> $operator<&$struct_type<$scalar_type, U>> for $scalar_type {
type Output = $struct_type<$scalar_type, U>;
fn $operator_fn(self, rhs: &$struct_type<$scalar_type, U>) -> Self::Output {
let scalar_rf = RationalFunction::new_from_scalar(self);
let new_rf = scalar_rf.$operator_fn(&rhs.rf);
Tf::new_from_rf(new_rf)
}
}

impl<U: Time> $operator<$scalar_type> for &$struct_type<$scalar_type, U> {
type Output = $struct_type<$scalar_type, U>;
fn $operator_fn(self, rhs: $scalar_type) -> Self::Output {
let scalar_rf = RationalFunction::new_from_scalar(rhs);
let new_rf = (&self.rf).$operator_fn(&scalar_rf);
Tf::new_from_rf(new_rf)
}
}
)*
};
}
Expand Down Expand Up @@ -370,12 +388,38 @@ impl_scalar_math_operator!(
i128,
[(Add, add), (Sub, sub), (Mul, mul), (Div, div)]
);
impl_scalar_math_operator!(Tf, u8, [(Add, add), (Mul, mul), (Div, div)]);
impl_scalar_math_operator!(Tf, u16, [(Add, add), (Mul, mul), (Div, div)]);

impl_scalar_math_operator!(Tf, u32, [(Add, add), (Mul, mul), (Div, div)]);
impl_scalar_math_operator!(Tf, u64, [(Add, add), (Mul, mul), (Div, div)]);
impl_scalar_math_operator!(Tf, u128, [(Add, add), (Mul, mul), (Div, div)]);
macro_rules! impl_comb_ref_and_no_ref_operators {
($struct_type:ident , [$(($operator:ident, $operator_fn:ident)), *]) => {
$(
impl<T, U: Time> $operator<&$struct_type<T, U>> for $struct_type<T, U>
where
T: $operator<Output = T> + Clone + Zero + One+ Default + Add + AddAssign + Mul<Output = T> + Neg<Output = T> + Copy,
{
type Output = $struct_type<T, U>;
fn $operator_fn(self, rhs: &$struct_type<T, U>) -> Self::Output {
(&self).$operator_fn(rhs)
}
}

impl<T, U: Time> $operator<$struct_type<T, U>> for &$struct_type<T, U>
where
T: $operator<Output = T> + Clone + Zero + One+ Default + Add + AddAssign + Mul<Output = T> + Neg<Output = T> + Copy,
{
type Output = $struct_type<T, U>;
fn $operator_fn(self, rhs: $struct_type<T, U>) -> Self::Output {
self.$operator_fn(&rhs)
}
}
)*
};
}

impl_comb_ref_and_no_ref_operators!(
Tf,
[(Mul, mul), (Div, div), (Sub, sub), (Add, add)]
);

impl<T, U: Time> Tf<T, U>
where
T: One + Zero + Mul<Output = T> + AddAssign + Clone,
Expand Down Expand Up @@ -637,5 +681,9 @@ mod tests {
let ans = 3.0 * tf_org;

assert_abs_diff_eq!(tf.eval(&1.2), ans.eval(&1.2), epsilon = 1e-9);

let s = Tf::s();

let _ = &s + 1.0 - &s * (1.0 + &s) / &s;
}
}