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
pub mod operators;
use std::fmt::Display;

#[derive(Debug, Clone, Default)]
/// Polynomials
pub struct Pol(Vec<f64>);

impl Pol {
    /// Create polynomials form vector
    pub fn new(input: Vec<f64>) -> Pol {
        if input.is_empty() {
            return Pol(vec![0.]);
        }
        Pol(input)
    }

    pub fn solve(&self) -> Vec<f64> {
        if self.0.len() == 3 {
            let a = self.0[2];
            let b = self.0[1];
            let c = self.0[0];

            vec![
                (-b + (b.powf(2.) - 4. * a * c).sqrt()) / (2. * a),
                (-b - (b.powf(2.) - 4. * a * c).sqrt()) / (2. * a),
            ]
        } else if self.0.len() == 4 && self.0[0] == 0. {
            let a = self.0[3];
            let b = self.0[2];
            let c = self.0[1];

            vec![
                0.,
                (-b + (b.powf(2.) - 4. * a * c).sqrt()) / (2. * a),
                (-b - (b.powf(2.) - 4. * a * c).sqrt()) / (2. * a),
            ]
        } else {
            panic!("Can't solve polynomial")
        }
    }
}

impl Display for Pol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut result = String::new();

        for (i, el) in self.0.iter().enumerate() {
            if *el != 0. {
                let el_str = (el.abs()).to_string();
                let i_str = i.to_string();

                result += &format!(
                    "{} {}{}{}{} ",
                    if *el > 0. { "+" } else { "-" },
                    if *el != 1. && *el != -1. { &el_str } else { "" },
                    if i != 0 { "x" } else { "" },
                    if i != 0 && i != 1 { "^" } else { "" },
                    if i != 1 { &i_str } else { "" }
                );
            }
        }

        write!(f, "{}", result)
    }
}