1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
use std::fmt;

fn strip_from_end<T: PartialEq + Clone + Default>(list: Vec<T>, object: T) -> Vec<T> {
    let mut new_list = list.clone();
    let mut strip_amount: usize = 0;
    for item in list.iter().rev() {
        if *item == object {
            strip_amount += 1;
        } else {
            break;
        }
    }
    let default: T = Default::default();
    new_list.resize(list.len() - strip_amount, default);
    new_list
}

/// A simple polynomial representation with `coefficients` and an `indeterminate`. 
pub struct Polynomial {
    /// Coefficients of Polynomial. The index of each coefficient indicates its degree, for example in `vec![1, 2]`, the first value is explicitly `1x^0`, the second is `2x^1`, etc.
    pub coefficients: Vec<f64>,
    /// The `char` representation of the indeterminate, eg. _f(**x**) = 1 + 2x_
    pub indeterminate: char,
}

impl fmt::Debug for Polynomial {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Polynomial {{ coefficients: {:?}, indeterminate: '{ind}', as_string: {} }}",
            self.coefficients,
            self.as_string(),
            ind = self.indeterminate
        )
    }
}

impl Polynomial {

    /// Returns a Polynomial from a vector of floats and an indeterminate
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
    /// assert_eq!(polynomial.coefficients, vec![1f64, 2f64, 3f64]);
    /// ```
    pub fn new(coefficients: Vec<f64>, indeterminate: char) -> Polynomial {
        let stripped_coefficients = strip_from_end(coefficients, 0f64);
        // Zero degree special case
        if stripped_coefficients.len() == 0 {
            return Polynomial {
                coefficients: vec![0f64],
                indeterminate,
            };
        }

        Polynomial {
            coefficients: stripped_coefficients,
            indeterminate,
        }
    }

    /// Returns a Polynomial from a vector of integers and an indeterminate
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let polynomial = Polynomial::from_ints(vec![1, 2, 3], 'x');
    /// assert_eq!(polynomial.coefficients, vec![1f64, 2f64, 3f64]);
    /// ```
    pub fn from_ints(coefficients: Vec<i64>, indeterminate: char) -> Polynomial {
        let stripped_coefficients = strip_from_end(coefficients, 0i64);
        // Zero degree special case
        if stripped_coefficients.len() == 0 {
            return Polynomial {
                coefficients: vec![0f64],
                indeterminate,
            };
        }

        let float_coefficients = stripped_coefficients.iter().map(|&x| x as f64).collect();

        Polynomial {
            coefficients: float_coefficients,
            indeterminate,
        }
    }

    /// Adds the same-degree coefficients of `other: Polynomial` to the coefficients of `self`, and returns a new Polynomial with the summed coefficients.
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let a_polynomial = Polynomial::from_ints(vec![1, 2, 3], 'x');
    /// let b_polynomial = Polynomial::from_ints(vec![1, 2, 3], 'x');
    /// 
    /// assert_eq!(a_polynomial.add(b_polynomial).coefficients, vec![2f64, 4f64, 6f64]);
    /// ``` 
    pub fn add(&self, other: Polynomial) -> Polynomial {
        let mut a_coefficients = self.coefficients.clone();
        let mut b_coefficients = other.coefficients.clone();

        // Resize coeff vectors to the longer size
        if a_coefficients.len() < b_coefficients.len() {
            a_coefficients.resize(b_coefficients.len(), 0f64)
        } else {
            b_coefficients.resize(a_coefficients.len(), 0f64)
        }

        let new_coefficients: Vec<f64> = a_coefficients
            .iter()
            .zip(b_coefficients)
            .map(|pair| pair.0 + pair.1)
            .collect();

        Polynomial::new(new_coefficients, 'x')
    }  

    /// Adds the same-degree coefficients of `other: Polynomial` to the coefficients of `self`, and returns a new Polynomial with the summed coefficients.
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let a_polynomial = Polynomial::from_ints(vec![1, 2], 'x');
    /// let b_polynomial = Polynomial::from_ints(vec![2, 4], 'x');
    /// 
    /// assert_eq!(a_polynomial.sub(b_polynomial).coefficients, vec![-1f64, -2f64]);
    /// ```

    pub fn sub(&self, other: Polynomial) -> Polynomial {
        let negative_coefficients: Vec<f64> = other
            .coefficients
            .iter()
            .map(|coeff| coeff * -1f64)
            .collect();
        let negative = Polynomial::new(negative_coefficients, 'x');

        self.add(negative)
    }

    /// Multiplies the same-degree coefficients of `self` and `other`, and returns a Polynomial with the new coefficients.
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let a_polynomial = Polynomial::from_ints(vec![1, 2], 'x');
    /// let b_polynomial = Polynomial::from_ints(vec![2, 4], 'x');
    /// 
    /// assert_eq!(a_polynomial.multiply(b_polynomial).coefficients, vec![2f64, 8f64, 8f64]);
    /// ```
    pub fn multiply(&self, other: Polynomial) -> Polynomial {
        let mut new_coefficients: Vec<f64> =
            vec![0f64; self.coefficients.len() * other.coefficients.len()];

        for (i, self_coeff) in self.coefficients.iter().enumerate() {
            for (j, other_coeff) in other.coefficients.iter().enumerate() {
                new_coefficients[i + j] += self_coeff * other_coeff;
            }
        }

        Polynomial::new(new_coefficients, 'x')
    }

    /// Return the result of evaluating a Polynomial at value `determinate`
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
    /// assert_eq!(polynomial.evaluate_at(1.0), 6f64)
    /// ```
    pub fn evaluate_at(&self, determinate: f64) -> f64 {
        let mut sum = 0f64;
        for (degree, coeff) in self.coefficients.iter().enumerate() {
            sum += determinate.powi(degree as i32) * coeff;
        }

        sum
    }

    /// Return the polynomial represented as a String
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
    /// assert_eq!(polynomial.as_string(), String::from("f(x) = 1 + 2x + 3x^2"))
    /// ```
    pub fn as_string(&self) -> String {
        let mut terms = String::new();
        for (degree, coeff) in self.coefficients.iter().enumerate() {
            if degree == 0 {
                terms = format!("{}", coeff);
                continue;
            }

            if degree == 1 {
                terms = format!("{} + {}{}", terms, coeff, self.indeterminate);
                continue;
            }

            if *coeff == 0f64 {
                continue;
            }

            terms = format!("{} + {}{}^{}", terms, coeff, self.indeterminate, degree);
        }

        format!("f({}) = {}", self.indeterminate, terms)
    }

    /// Return an integer representation of the degree of the Polynomial
    /// # Example
    /// ```
    /// use polynom::polynomial::Polynomial;
    ///
    /// let polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
    /// assert_eq!(polynomial.degree(), 2)
    /// ```
    pub fn degree(&self) -> isize {
        // Special case zero polynomial
        if self.coefficients == vec![0f64] {
            return -1;
        }

        (self.coefficients.len() - 1) as isize
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_strip_from_end() {
        assert_eq!(
            strip_from_end(vec![1, 2, 0, 3, 0, 0, 0], 0),
            vec![1, 2, 0, 3]
        );
    }
    #[test]
    fn test_strip_from_end_on_polynomial() {
        let polynomial = Polynomial::new(vec![1f64, 2f64, 0f64, 3f64, 0f64], 'x');
        assert_eq!(polynomial.coefficients, vec![1f64, 2f64, 0f64, 3f64]);
    }

    #[test]
    fn test_zero_polynomial() {
        let polynomial = Polynomial::new(vec![0f64], 'x');
        assert_eq!(polynomial.coefficients, vec![0f64]);
    }

    #[test]
    fn test_zero_special_case_degree() {
        let polynomial = Polynomial::new(vec![0f64], 'x');
        assert_eq!(polynomial.degree(), -1)
    }

    #[test]
    fn test_degree() {
        let polynomial = Polynomial::new(vec![1f64, 2f64, 0f64, 3f64], 'x');
        assert_eq!(polynomial.degree(), 3)
    }

    #[test]
    fn test_string_representation() {
        let polynomial = Polynomial::new(vec![1f64, 2f64, 0f64, 3f64], 'x');
        assert_eq!(polynomial.as_string(), String::from("f(x) = 1 + 2x + 3x^3"))
    }

    #[test]
    fn test_add() {
        let a_polynomial = Polynomial::new(vec![1f64, 2f64, 0f64, 3f64], 'x');
        let b_polynomial = Polynomial::new(vec![1f64, 2f64, 0f64, 3f64, 4f64], 'x');

        assert_eq!(
            a_polynomial.add(b_polynomial).coefficients,
            vec![2f64, 4f64, 0f64, 6f64, 4f64]
        )
    }

    #[test]
    fn test_add_negative_coefficients() {
        let a_polynomial = Polynomial::new(vec![1f64, 2f64, 0f64, 3f64], 'x');
        let b_polynomial = Polynomial::new(vec![-1f64, -2f64, 0f64, -3f64], 'x');

        assert_eq!(a_polynomial.add(b_polynomial).coefficients, vec![0f64])
    }

    #[test]
    fn test_multiply_simple() {
        let a_polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
        let b_polynomial = Polynomial::new(vec![1f64], 'x');

        assert_eq!(
            a_polynomial.multiply(b_polynomial).coefficients,
            vec![1f64, 2f64, 3f64]
        );
    }

    #[test]
    fn test_multiply() {
        let a_polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
        let b_polynomial = Polynomial::new(vec![3f64, 2f64, 1f64], 'x');

        assert_eq!(
            a_polynomial.multiply(b_polynomial).coefficients,
            vec![3f64, 8f64, 14f64, 8f64, 3f64]
        )
    }

    #[test]
    fn test_multiply_negative() {
        let a_polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
        let b_polynomial = Polynomial::new(vec![-3f64, -2f64, -1f64], 'x');

        assert_eq!(
            a_polynomial.multiply(b_polynomial).coefficients,
            vec![-3f64, -8f64, -14f64, -8f64, -3f64]
        )
    }

    #[test]
    fn test_evaluate_at_zero() {
        let polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');

        assert_eq!(polynomial.evaluate_at(0f64), 1f64)
    }

    #[test]
    fn test_evaluate_at_five() {
        let polynomial = Polynomial::new(vec![1f64, 2f64, 3f64, 4f64], 'x');

        assert_eq!(polynomial.evaluate_at(5f64), 586.0)
    }

    #[test]
    fn test_evaluate_at_negative() {
        let polynomial = Polynomial::new(vec![-1f64, 2f64, -3f64, 4f64], 'x');

        assert_eq!(polynomial.evaluate_at(-5f64), -586.0)
    }

    #[test]
    fn test_subtract() {
        let a_polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');
        let b_polynomial = Polynomial::new(vec![1f64, 2f64, 3f64], 'x');

        assert_eq!(a_polynomial.sub(b_polynomial).coefficients, vec![0f64])
    }

    #[test]
    fn test_double_negative_subtract() {
        let a_polynomial = Polynomial::new(vec![-1f64, -2f64, -3f64], 'x');
        let b_polynomial = Polynomial::new(vec![-3f64, -2f64, -1f64], 'x');

        assert_eq!(
            a_polynomial.sub(b_polynomial).coefficients,
            vec![2f64, 0f64, -2f64]
        )
    }

    #[test]
    fn test_negative_subtract() {
        let a_polynomial = Polynomial::new(vec![-1f64, -2f64, -3f64], 'x');
        let b_polynomial = Polynomial::new(vec![3f64, 2f64, 1f64], 'x');

        assert_eq!(
            a_polynomial.sub(b_polynomial).coefficients,
            vec![-4f64, -4f64, -4f64]
        )
    }
    #[test]
    fn test_new_polynomial_from_ints() {
        let polynomial = Polynomial::from_ints(vec![1, 2, 3], 'x');

        assert_eq!(polynomial.coefficients, vec![1f64, 2f64, 3f64]);
    }

}