Skip to content

Commit c46917a

Browse files
authored
Merge pull request #43 from TorBorve/feature/use-reference-in-core
Feature/use reference in core
2 parents b4ecd9d + 9db6ea8 commit c46917a

3 files changed

Lines changed: 355 additions & 114 deletions

File tree

src/systems/polynom.rs

Lines changed: 140 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ impl<T> Polynomial<T> {
112112
}
113113
}
114114

115-
impl<T> Mul for Polynomial<T>
115+
impl<'a, T> Mul<&'a Polynomial<T>> for &Polynomial<T>
116116
where
117117
T: Default + Mul<Output = T> + AddAssign + Clone,
118118
{
119119
type Output = Polynomial<T>;
120120

121-
fn mul(self, rhs: Self) -> Self::Output {
121+
fn mul(self, rhs: &'a Polynomial<T>) -> Self::Output {
122122
let mut result =
123123
vec![T::default(); self.coeffs.len() + rhs.coeffs.len() - 1];
124124
for (idx_l, val_l) in self.coeffs.iter().enumerate() {
@@ -131,13 +131,33 @@ where
131131
}
132132
}
133133

134-
impl<T> Add for Polynomial<T>
134+
impl<T> Mul for Polynomial<T>
135+
where
136+
T: Default + Mul<Output = T> + AddAssign + Clone,
137+
{
138+
type Output = Polynomial<T>;
139+
140+
fn mul(self, rhs: Self) -> Self::Output {
141+
&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 }
151+
}
152+
}
153+
154+
impl<'a, T> Add<&'a Polynomial<T>> for &Polynomial<T>
135155
where
136156
T: AddAssign + Default + Clone,
137157
{
138158
type Output = Polynomial<T>;
139159

140-
fn add(self, rhs: Self) -> Self::Output {
160+
fn add(self, rhs: &'a Polynomial<T>) -> Self::Output {
141161
let mut result =
142162
vec![T::default(); self.coeffs.len().max(rhs.coeffs.len())];
143163
for (idx, val) in self.coeffs.iter().enumerate() {
@@ -151,27 +171,49 @@ where
151171
}
152172
}
153173

154-
impl<T> Neg for Polynomial<T>
174+
impl<T> Add for Polynomial<T>
155175
where
156-
T: Neg<Output = T> + Clone,
176+
T: AddAssign + Default + Clone,
177+
{
178+
type Output = Polynomial<T>;
179+
180+
fn add(self, rhs: Self) -> Self::Output {
181+
&self + &rhs
182+
}
183+
}
184+
185+
impl<T> Neg for &Polynomial<T>
186+
where
187+
T: Neg<Output = T> + Clone + Copy,
157188
{
158189
type Output = Polynomial<T>;
159190
fn neg(self) -> Self::Output {
160191
Polynomial {
161-
coeffs: self.coeffs.into_iter().map(|x| -x).collect(),
192+
coeffs: self.coeffs.iter().map(|&x| -x).collect(),
162193
}
163194
}
164195
}
165196

197+
impl<'a, T> Sub<&'a Polynomial<T>> for &Polynomial<T>
198+
where
199+
T: Neg<Output = T> + Clone + AddAssign + Default + Copy,
200+
{
201+
type Output = Polynomial<T>;
202+
203+
fn sub(self, rhs: &'a Polynomial<T>) -> Self::Output {
204+
let neg_rhs = -rhs;
205+
self + &neg_rhs
206+
}
207+
}
208+
166209
impl<T> Sub for Polynomial<T>
167210
where
168-
T: Neg<Output = T> + Clone + AddAssign + Default,
211+
T: Neg<Output = T> + Clone + AddAssign + Default + Copy,
169212
{
170213
type Output = Polynomial<T>;
171214

172215
fn sub(self, rhs: Self) -> Self::Output {
173-
let neg_rhs = -rhs;
174-
self + neg_rhs
216+
&self - &rhs
175217
}
176218
}
177219

@@ -180,12 +222,21 @@ macro_rules! impl_compound_assign {
180222
$(
181223
impl<T> $assign_trait for $struct_type<T>
182224
where
183-
T: $trait<Output = T> + $assign_trait + Clone + Default + AddAssign + Neg<Output = T> + Mul<Output = T> + Add,
225+
T: $trait<Output = T> + $assign_trait + Clone + Default + AddAssign + Neg<Output = T> + Mul<Output = T> + Add + Copy,
184226
{
185227
fn $assign_method(&mut self, rhs: Self) {
186228
*self = self.clone().$method(rhs)
187229
}
188230
}
231+
232+
impl<'a, T> $assign_trait<&'a $struct_type<T>> for $struct_type<T>
233+
where
234+
T: $trait<Output = T> + $assign_trait + Clone + Default + AddAssign + Neg<Output = T> + Mul<Output = T> + Add + Copy,
235+
{
236+
fn $assign_method(&mut self, rhs: &'a $struct_type<T>) {
237+
*self = <&Self as $trait<&Self>>::$method(self, rhs);
238+
}
239+
}
189240
)*
190241
};
191242
}
@@ -426,13 +477,13 @@ where
426477

427478
impl<T> RationalFunction<T>
428479
where
429-
T: One + Clone + Zero + Add + Mul<Output = T> + AddAssign + Default,
480+
T: One + Clone + Zero + Add + Mul<Output = T> + AddAssign + Default + Copy,
430481
{
431-
pub fn powi(self, exp: i32) -> Self {
482+
pub fn powi(&self, exp: i32) -> Self {
432483
let base = self;
433484
let mut result = RationalFunction::new_from_scalar(T::one());
434485
for _ in 0..exp.abs() {
435-
result = result * base.clone();
486+
result = &result * base;
436487
}
437488
if exp < 0 {
438489
result = RationalFunction::new_from_scalar(T::one()) / result
@@ -459,32 +510,67 @@ where
459510
}
460511
}
461512

513+
impl<'a, T> Mul<&'a RationalFunction<T>> for &RationalFunction<T>
514+
where
515+
T: Add + Default + Mul<Output = T> + AddAssign + Clone + Copy,
516+
{
517+
type Output = RationalFunction<T>;
518+
fn mul(self, rhs: &'a RationalFunction<T>) -> Self::Output {
519+
let new_num = &self.num * &rhs.num;
520+
let new_den = &self.den * &rhs.den;
521+
522+
RationalFunction {
523+
num: new_num,
524+
den: new_den,
525+
}
526+
}
527+
}
528+
462529
impl<T> Mul for RationalFunction<T>
463530
where
464-
T: Add + Default + Mul<Output = T> + AddAssign + Clone,
531+
T: Add + Default + Mul<Output = T> + AddAssign + Clone + Copy,
465532
{
466533
type Output = RationalFunction<T>;
467534
fn mul(self, rhs: Self) -> Self::Output {
468-
let new_num = self.num * rhs.num;
469-
let new_den = self.den * rhs.den;
535+
&self * &rhs
536+
}
537+
}
470538

471-
Self {
539+
impl<'a, T> Div<&'a RationalFunction<T>> for &RationalFunction<T>
540+
where
541+
T: Clone + Default + Mul<Output = T> + AddAssign + Copy,
542+
{
543+
type Output = RationalFunction<T>;
544+
fn div(self, rhs: &'a RationalFunction<T>) -> Self::Output {
545+
let new_num = &self.num * &rhs.den;
546+
let new_den = &self.den * &rhs.num;
547+
548+
RationalFunction {
472549
num: new_num,
473550
den: new_den,
474551
}
475552
}
476553
}
477-
478554
impl<T> Div for RationalFunction<T>
479555
where
480-
T: Clone + Default + Mul<Output = T> + AddAssign,
556+
T: Clone + Default + Mul<Output = T> + AddAssign + Copy,
481557
{
482558
type Output = RationalFunction<T>;
483559
fn div(self, rhs: Self) -> Self::Output {
484-
let new_num = self.num * rhs.den;
485-
let new_den = self.den * rhs.num;
560+
&self / &rhs
561+
}
562+
}
486563

487-
Self {
564+
impl<'a, T> Add<&'a RationalFunction<T>> for &RationalFunction<T>
565+
where
566+
T: AddAssign + Default + Clone + Mul<Output = T>,
567+
{
568+
type Output = RationalFunction<T>;
569+
fn add(self, rhs: &'a RationalFunction<T>) -> Self::Output {
570+
let new_den = &self.den * &rhs.den;
571+
let new_num = &self.num * &rhs.den + &rhs.num * &self.den;
572+
573+
RationalFunction {
488574
num: new_num,
489575
den: new_den,
490576
}
@@ -497,38 +583,42 @@ where
497583
{
498584
type Output = RationalFunction<T>;
499585
fn add(self, rhs: Self) -> Self::Output {
500-
let new_den = self.den.clone() * rhs.den.clone();
501-
let new_num = self.num * rhs.den + rhs.num * self.den;
502-
503-
Self {
504-
num: new_num,
505-
den: new_den,
506-
}
586+
&self + &rhs
507587
}
508588
}
509589

510-
impl<T> Neg for RationalFunction<T>
590+
impl<T> Neg for &RationalFunction<T>
511591
where
512-
T: Neg<Output = T> + Clone,
592+
T: Neg<Output = T> + Clone + Copy,
513593
{
514594
type Output = RationalFunction<T>;
515595
fn neg(self) -> Self::Output {
516-
let new_num = -self.num;
596+
let new_num = -&self.num;
517597

518-
Self {
598+
// TODO: should not need to use clone here
599+
RationalFunction {
519600
num: new_num,
520-
den: self.den,
601+
den: self.den.clone(),
521602
}
522603
}
523604
}
524605

606+
impl<'a, T> Sub<&'a RationalFunction<T>> for &RationalFunction<T>
607+
where
608+
T: Neg<Output = T> + Clone + AddAssign + Default + Mul<Output = T> + Copy,
609+
{
610+
type Output = RationalFunction<T>;
611+
fn sub(self, rhs: &'a RationalFunction<T>) -> Self::Output {
612+
self + &(-rhs)
613+
}
614+
}
525615
impl<T> Sub for RationalFunction<T>
526616
where
527-
T: Neg<Output = T> + Clone + AddAssign + Default + Mul<Output = T>,
617+
T: Neg<Output = T> + Clone + AddAssign + Default + Mul<Output = T> + Copy,
528618
{
529619
type Output = RationalFunction<T>;
530620
fn sub(self, rhs: Self) -> Self::Output {
531-
self + (-rhs)
621+
&self - &rhs
532622
}
533623
}
534624

@@ -867,4 +957,17 @@ mod tests {
867957
RationalFunction::new_from_coeffs(&[1., 2., 4.], &[1., 4., 2., 8.]);
868958
assert_abs_diff_eq!(rf.clone(), rf);
869959
}
960+
961+
#[test]
962+
fn reference_arithemtic() {
963+
let mut rf1 =
964+
RationalFunction::new_from_coeffs(&[1.0, 2.0], &[2.0, 1.0]);
965+
let rf2 = rf1.clone();
966+
967+
rf1 += &rf2;
968+
rf1 += &rf2;
969+
970+
let ans: RationalFunction<f64> = 3.0 * rf2;
971+
assert_abs_diff_eq!(rf1.eval(&1.2), ans.eval(&1.2));
972+
}
870973
}

0 commit comments

Comments
 (0)