Skip to content

Commit 4967d17

Browse files
authored
Merge pull request #45 from TorBorve/feature/use-reference-in-core
mix reference and value in math operators
2 parents c46917a + f8a6a6a commit 4967d17

3 files changed

Lines changed: 161 additions & 14 deletions

File tree

src/systems/polynom.rs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,6 @@ where
139139

140140
fn mul(self, rhs: Self) -> Self::Output {
141141
&self * &rhs
142-
// let mut result =
143-
// vec![T::default(); self.coeffs.len() + rhs.coeffs.len() - 1];
144-
// for (idx_l, val_l) in self.coeffs.iter().enumerate() {
145-
// for (idx_r, val_r) in rhs.coeffs.iter().enumerate() {
146-
// result[idx_l + idx_r] += val_l.clone() * val_r.clone();
147-
// }
148-
// }
149-
150-
// Polynomial { coeffs: result }
151142
}
152143
}
153144

@@ -270,6 +261,26 @@ macro_rules! impl_one_operator_scalar_trait {
270261
scalar.$operator_fn(rhs)
271262
}
272263
}
264+
265+
impl $operator<$scalar_type> for &$struct_type<$scalar_type>
266+
{
267+
type Output = $struct_type<$scalar_type>;
268+
fn $operator_fn(self, rhs: $scalar_type) -> Self::Output {
269+
let scalar = $struct_type::<$scalar_type>::new_from_scalar(rhs);
270+
self.$operator_fn(&scalar)
271+
// self.$operator_fn($struct_type::<$scalar_type>::new_from_scalar(rhs))
272+
}
273+
}
274+
275+
impl $operator<&$struct_type<$scalar_type>> for $scalar_type {
276+
type Output = $struct_type<$scalar_type>;
277+
fn $operator_fn(self, rhs: &$struct_type<$scalar_type>) -> Self::Output {
278+
let scalar = &$struct_type::<$scalar_type>::new_from_scalar(self);
279+
scalar.$operator_fn(rhs)
280+
}
281+
}
282+
283+
273284
)*
274285
};
275286
}
@@ -632,6 +643,37 @@ impl_compound_assign!(
632643
]
633644
);
634645

646+
macro_rules! impl_comb_ref_and_no_ref_operators {
647+
($struct_type:ident , [$(($operator:ident, $operator_fn:ident)), *]) => {
648+
$(
649+
impl<T> $operator<&$struct_type<T>> for $struct_type<T>
650+
where
651+
T: Add + Default + Mul<Output = T> + AddAssign + Clone + Copy + Neg<Output = T>,
652+
{
653+
type Output = $struct_type<T>;
654+
fn $operator_fn(self, rhs: &$struct_type<T>) -> Self::Output {
655+
(&self).$operator_fn(rhs)
656+
}
657+
}
658+
659+
impl<T> $operator<$struct_type<T>> for &$struct_type<T>
660+
where
661+
T: Add + Default + Mul<Output = T> + AddAssign + Clone + Copy + Neg<Output = T>,
662+
{
663+
type Output = $struct_type<T>;
664+
fn $operator_fn(self, rhs: $struct_type<T>) -> Self::Output {
665+
self.$operator_fn(&rhs)
666+
}
667+
}
668+
)*
669+
};
670+
}
671+
672+
impl_comb_ref_and_no_ref_operators!(
673+
RationalFunction,
674+
[(Mul, mul), (Div, div), (Sub, sub), (Add, add)]
675+
);
676+
635677
impl_one_operator_scalar_trait!(
636678
RationalFunction,
637679
f64,
@@ -969,5 +1011,10 @@ mod tests {
9691011

9701012
let ans: RationalFunction<f64> = 3.0 * rf2;
9711013
assert_abs_diff_eq!(rf1.eval(&1.2), ans.eval(&1.2));
1014+
1015+
let s = RationalFunction::new_from_coeffs(&[0.0, 1.0], &[1.0]);
1016+
let _ = 1.0 / &s * 10.0 * &s - 3.0 / &s;
1017+
let _ = &s * (1.0 + &s);
1018+
let _ = &s + 1.0 - &s / (1.0 + &s) * &s * (1.0 * &s);
9721019
}
9731020
}

src/systems/state_space.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,57 @@ macro_rules! impl_scalar_math_operator_ss {
658658
lhs_ss.$operator_fn(rhs)
659659
}
660660
}
661+
662+
impl<U: Time + 'static> $operator<f64> for &Ss<U> {
663+
type Output = Ss<U>;
664+
fn $operator_fn(self, rhs: f64) -> Self::Output {
665+
let rhs_ss = Ss::<U>::new_from_scalar(rhs);
666+
self.$operator_fn(&rhs_ss)
667+
}
668+
}
669+
670+
impl<U: Time + 'static> $operator<&Ss<U>> for f64 {
671+
type Output = Ss<U>;
672+
fn $operator_fn(self, rhs: &Ss<U>) -> Self::Output {
673+
let scalar_ss = Ss::<U>::new_from_scalar(self);
674+
(&scalar_ss).$operator_fn(rhs)
675+
}
676+
}
661677
)*
662678
};
663679
}
664680

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

683+
macro_rules! impl_comb_ref_and_no_ref_operators {
684+
([$(($operator:ident, $operator_fn:ident)), *]) => {
685+
$(
686+
impl<U: Time + 'static> $operator<&Ss<U>> for Ss<U>
687+
{
688+
type Output = Ss<U>;
689+
fn $operator_fn(self, rhs: &Ss<U>) -> Self::Output {
690+
(&self).$operator_fn(rhs)
691+
}
692+
}
693+
694+
impl<U: Time + 'static> $operator<Ss<U>> for &Ss<U>
695+
{
696+
type Output = Ss<U>;
697+
fn $operator_fn(self, rhs: Ss<U>) -> Self::Output {
698+
self.$operator_fn(&rhs)
699+
}
700+
}
701+
)*
702+
};
703+
}
704+
705+
impl_comb_ref_and_no_ref_operators!([
706+
(Add, add),
707+
(Sub, sub),
708+
(Mul, mul),
709+
(Div, div)
710+
]);
711+
667712
impl<U: Time + 'static> fmt::Display for Ss<U> {
668713
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
669714
write!(
@@ -982,5 +1027,12 @@ mod tests {
9821027

9831028
assert_abs_diff_eq!(resp1.re, resp2.re, epsilon = 1e-9);
9841029
assert_abs_diff_eq!(resp1.im, resp2.im, epsilon = 1e-9);
1030+
1031+
let s_inv = (1.0 / Tf::s()).to_ss().unwrap();
1032+
let _ =
1033+
1.0 + &s_inv * &s_inv / (1.0 + &s_inv / 1.0) + (&s_inv - &s_inv);
1034+
let _ = 1.0 + &s_inv;
1035+
let _ = s_inv.clone() + &s_inv;
1036+
let _ = &s_inv + s_inv.clone();
9851037
}
9861038
}

src/systems/transfer_function.rs

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,24 @@ macro_rules! impl_scalar_math_operator {
329329
Tf::new_from_rf(new_rf)
330330
}
331331
}
332+
333+
impl<U: Time> $operator<&$struct_type<$scalar_type, U>> for $scalar_type {
334+
type Output = $struct_type<$scalar_type, U>;
335+
fn $operator_fn(self, rhs: &$struct_type<$scalar_type, U>) -> Self::Output {
336+
let scalar_rf = RationalFunction::new_from_scalar(self);
337+
let new_rf = scalar_rf.$operator_fn(&rhs.rf);
338+
Tf::new_from_rf(new_rf)
339+
}
340+
}
341+
342+
impl<U: Time> $operator<$scalar_type> for &$struct_type<$scalar_type, U> {
343+
type Output = $struct_type<$scalar_type, U>;
344+
fn $operator_fn(self, rhs: $scalar_type) -> Self::Output {
345+
let scalar_rf = RationalFunction::new_from_scalar(rhs);
346+
let new_rf = (&self.rf).$operator_fn(&scalar_rf);
347+
Tf::new_from_rf(new_rf)
348+
}
349+
}
332350
)*
333351
};
334352
}
@@ -370,12 +388,38 @@ impl_scalar_math_operator!(
370388
i128,
371389
[(Add, add), (Sub, sub), (Mul, mul), (Div, div)]
372390
);
373-
impl_scalar_math_operator!(Tf, u8, [(Add, add), (Mul, mul), (Div, div)]);
374-
impl_scalar_math_operator!(Tf, u16, [(Add, add), (Mul, mul), (Div, div)]);
375391

376-
impl_scalar_math_operator!(Tf, u32, [(Add, add), (Mul, mul), (Div, div)]);
377-
impl_scalar_math_operator!(Tf, u64, [(Add, add), (Mul, mul), (Div, div)]);
378-
impl_scalar_math_operator!(Tf, u128, [(Add, add), (Mul, mul), (Div, div)]);
392+
macro_rules! impl_comb_ref_and_no_ref_operators {
393+
($struct_type:ident , [$(($operator:ident, $operator_fn:ident)), *]) => {
394+
$(
395+
impl<T, U: Time> $operator<&$struct_type<T, U>> for $struct_type<T, U>
396+
where
397+
T: $operator<Output = T> + Clone + Zero + One+ Default + Add + AddAssign + Mul<Output = T> + Neg<Output = T> + Copy,
398+
{
399+
type Output = $struct_type<T, U>;
400+
fn $operator_fn(self, rhs: &$struct_type<T, U>) -> Self::Output {
401+
(&self).$operator_fn(rhs)
402+
}
403+
}
404+
405+
impl<T, U: Time> $operator<$struct_type<T, U>> for &$struct_type<T, U>
406+
where
407+
T: $operator<Output = T> + Clone + Zero + One+ Default + Add + AddAssign + Mul<Output = T> + Neg<Output = T> + Copy,
408+
{
409+
type Output = $struct_type<T, U>;
410+
fn $operator_fn(self, rhs: $struct_type<T, U>) -> Self::Output {
411+
self.$operator_fn(&rhs)
412+
}
413+
}
414+
)*
415+
};
416+
}
417+
418+
impl_comb_ref_and_no_ref_operators!(
419+
Tf,
420+
[(Mul, mul), (Div, div), (Sub, sub), (Add, add)]
421+
);
422+
379423
impl<T, U: Time> Tf<T, U>
380424
where
381425
T: One + Zero + Mul<Output = T> + AddAssign + Clone,
@@ -637,5 +681,9 @@ mod tests {
637681
let ans = 3.0 * tf_org;
638682

639683
assert_abs_diff_eq!(tf.eval(&1.2), ans.eval(&1.2), epsilon = 1e-9);
684+
685+
let s = Tf::s();
686+
687+
let _ = &s + 1.0 - &s * (1.0 + &s) / &s;
640688
}
641689
}

0 commit comments

Comments
 (0)