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
382
383
384
385
386
387
388
389
//! Quadratic functions.
use std::{fmt::Display, convert::*, ops::*};
use crate::math::general::NumTools;
use super::{linear::LinearEquation, polynomial::Polynomial};
/// A struct for storing quadratic equations of the form `f(x) = ax² + bx + c`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct QuadraticEquation<T> {
    pub(crate) a:          T,
    pub(crate) b:          T,
    pub(crate) c:          T,
    vertex:                Option<(T, T)>,
    solutions:             (Option<T>, Option<T>),
    derivative:            Option<LinearEquation<T>>
}

impl<T: Copy +
        Clone +
        From<u8> +
        TryFrom<f64> +
        PartialEq +
        PartialOrd +
        NumTools<T> +
        Mul<Output = T> +
        Add<Output = T> +
        Sub<Output = T> +
        Div<Output = T> +
        Neg<Output = T>> QuadraticEquation<T>
        where <T as TryFrom<f64>>::Error: std::fmt::Debug,
              f64: From<T> {
    /// Create a new `QuadraticEquation` with the values `a = 1, b = 0, c = 0`.
    /// 
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, 0.0, 0.0);
    /// f_x.get_vertex();
    /// f_x.get_solutions();
    /// 
    /// assert_eq!(QuadraticEquation::new(), f_x);
    /// ```
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, 0.0, -1.5);
    /// 
    /// assert_eq!("1x^2 + 0x - 1.5", &f_x.to_string());
    /// ```
    #[inline]
    #[must_use]
    pub fn new() -> QuadraticEquation<T> {
        QuadraticEquation { a:          T::from(1),
                            b:          T::from(0),
                            c:          T::from(0),
                            vertex:     Some((T::from(0), T::from(0))),
                            solutions:  (Some(T::from(0)), None),
                            derivative: None }
    }
    /// Create a new `QuadraticEquation` from coefficients.
    /// 
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, 0.0, 0.0);
    /// f_x.get_vertex();
    /// f_x.get_solutions();
    /// 
    /// assert_eq!(QuadraticEquation::new(), f_x);
    /// ```
    #[inline]
    #[must_use]
    pub fn new_from_coefficients(a: T, b: T, c: T) -> QuadraticEquation<T> {
        if a == T::from(0)
        { panic!("a was zero and is thus not allowed."); }
        QuadraticEquation { a,
                            b,
                            c,
                            vertex:     None,
                            solutions: (None, None),
                            derivative: None }
    }
    /// Get `a` of a `QuadraticEquation`.
    /// # Returns
    /// A `T.`
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!(-2.0, f_x.b());
    /// ```
    #[inline]
    #[must_use]
    pub fn a(&self) -> T {
        self.a
    }
    /// Get `b` of a `QuadraticEquation`.
    /// # Returns
    /// A `T.`
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!(-2.0, f_x.b());
    /// ```
    #[inline]
    #[must_use]
    pub fn b(&self) -> T {
        self.b
    }
    /// Get `c` of a `QuadraticEquation`.
    /// # Returns
    /// A `T.`
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!(-3.0, f_x.c());
    /// ```
    #[inline]
    #[must_use]
    pub fn c(&self) -> T {
        self.c 
    }
    /// Set `c` of a `QuadraticEquation`.
    /// # Panics
    /// Panics if `value` is zero.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!(1.0, f_x.a());
    /// 
    /// f_x.set_a(-1.0);
    /// 
    /// assert_eq!(-1.0, f_x.a());
    /// ```
    #[inline]
    pub fn set_a(&mut self, value: T) {
        if value == T::from(0)
        { panic!("a was zero and is thus not allowed."); }
        self.solutions = (None, None);
        self.derivative = None;
        self.a = value;
    }
    /// Set `b` of a `QuadraticEquation`.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!(-2.0, f_x.b());
    /// 
    /// f_x.set_b(-1.0);
    /// 
    /// assert_eq!(-1.0, f_x.b());
    /// ```
    #[inline] 
    pub fn set_b(&mut self, value: T) {
        self.solutions = (None, None);
        self.derivative = None;
        self.b = value;
    }
    /// Set `c` of a `QuadraticEquation`.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!(-3.0, f_x.c());
    /// 
    /// f_x.set_c(-1.0);
    /// 
    /// assert_eq!(-1.0, f_x.c());
    /// ```
    #[inline]
    pub fn set_c(&mut self, value: T) {
        self.solutions = (None, None);
        self.c = value;
    }
    /// Get the solutions of a quadratic equation.
    /// # Returns
    /// A `(Option<T>, Option<T>)`.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, -3.0);
    /// 
    /// assert_eq!((Some(3.0), Some(-1.0)), f_x.get_solutions());
    /// ```
    #[inline]
    #[must_use]
    pub fn get_solutions(&mut self) -> (Option<T>, Option<T>) {
        if self.solutions != (None, None)
        { return self.solutions; }
        let discriminant = self.b.square() - T::from(4) * self.a * self.c;
        if discriminant < T::from(0)
        { return (None, None); }
        let x_1 = (- self.b + (f64::from(discriminant).sqrt()).try_into().unwrap()) /
                  (T::from(2) * self.a);

        let x_2 = (- self.b - T::try_from(f64::from(discriminant).sqrt()).unwrap()) /
                  (T::from(2) * self.a);
        
        match x_1 == x_2 {
            true  => { self.solutions = (Some(x_1), None); }
            false => { self.solutions = (Some(x_1), Some(x_2)); }
        }
        self.solutions
    }
    /// Get the intersection point(s) between `self` and `other`.
    /// Returns `(None, None)` if both arguments are equal.
    /// # Arguments
    /// * `self`.
    /// * `other: &QuadraticEquation`.
    /// # Returns
    /// A `(Option<(T, T)>, Option<(T, T)>)` tuple.
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, 2.0, 0.0);
    /// let mut g_x = QuadraticEquation::new_from_coefficients(1.0, 2.0, 0.0);
    /// let mut h_x = QuadraticEquation::new_from_coefficients(0.5, 1.0, 0.0);
    /// 
    /// assert_eq!( (None, None), f_x.intsect_with(&g_x));
    /// assert_eq!( (Some( (0.0, 0.0) ), Some( (-2.0, 0.0) )), g_x.intsect_with(&h_x) );
    /// ```
    #[inline]
    #[must_use]
    pub fn intsect_with(&self, other: &QuadraticEquation<T>)
    -> (Option<(T, T)>, Option<(T, T)>) {
        if self == other
        { return (None, None) }
        let solquad = QuadraticEquation::new_from_coefficients(self.a - other.a,
                                                               self.b - other.b,
                                                               self.c - other.c).get_solutions();
        if solquad == (None, None)
        { return (None, None); }
        let solquad0_unsafe = unsafe { solquad.0.unwrap_unchecked() };
        let mut res = (Some((solquad0_unsafe, self.eval(solquad0_unsafe))), None );
        match solquad {
            (Some(_), None)     => { }
            (Some(_), Some(s1)) => {
                res.1 = Some((s1, self.eval(s1)));
            }
            _                   => { }
        }
        res
    }
    /// Get the intersection point(s) between `self` and a linear equation if there is some.
    /// # Arguments
    /// * `self`.
    /// * `other: &LinearEquation`.
    /// # Returns
    /// A `(Option<(T, T)>, Option<(T, T)>)` tuple.
    /// ```
    /// use lib_rapid::math::equations::linear::LinearEquation;
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = LinearEquation::new(2.0, 2.0);
    /// let mut g_x = QuadraticEquation::new_from_coefficients(1.2, 2.0, -2.0);
    /// 
    /// assert_eq!( ( Some((1.8257418583505536, 5.651483716701107)),
    ///               Some((-1.8257418583505536, -1.6514837167011072)) ),
    ///             g_x.intsect_with_linear(&f_x));
    #[inline]
    #[must_use]
    pub fn intsect_with_linear(&self, other: &LinearEquation<T>)
    -> (Option<(T, T)>, Option<(T, T)>) {
        other.intsect_with_quadratic(self)
    }
    /// Get the vertex (lowest or highest point) of a quadratic equation.
    /// # Returns
    /// A `(T, T)`.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, 3.0);
    /// 
    /// assert_eq!((1.0, 2.0), f_x.get_vertex());
    /// ```
    #[inline]
    #[must_use]
    pub fn get_vertex(&mut self) -> (T, T) {
        if self.vertex.is_some()
        { return unsafe { self.vertex.unwrap_unchecked() }; }
        let x = - self. b / (T::from(2) * self.a);
        self.vertex = Some((x, self.a * x.square() + self.b * x + self.c));
        unsafe { self.vertex.unwrap_unchecked() }
    }
    /// Get the value of a value `x` under the function of the `QuadraticEquation`.
    /// # Returns
    /// A `T`.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// 
    /// let f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, 3.0);
    /// 
    /// assert_eq!(2.0, f_x.eval(1.0));
    /// ```
    #[inline]
    #[must_use]
    pub fn eval(&self, x: T) -> T {
        self.a * x.square() + self.b * x + self.c
    }
    /// Get the derivative of a `QuadraticEquation<T>`. The derivative is the graph of the
    /// development of the slope for a given function `self`.
    /// # Returns
    /// A `LinearEquation<T>`.
    /// # Examples
    /// ```
    /// use lib_rapid::math::equations::quadratic::QuadraticEquation;
    /// use lib_rapid::math::equations::linear::LinearEquation;
    /// 
    /// let mut f_x = QuadraticEquation::new_from_coefficients(1.0, -2.0, 3.0);
    /// 
    /// assert_eq!(LinearEquation::new(2.0, -2.0), f_x.get_derivative());
    /// ```
    #[inline]
    #[must_use = "This returns the result of the operation, without modifying the original."]
    pub fn get_derivative(&mut self) -> LinearEquation<T> {
        match self.derivative {
            Some(d) => { return d; }
            None    => { self.derivative = Some(LinearEquation::new(T::from(2) * self.a, self.b)); }
        }

        unsafe { self.derivative.unwrap_unchecked() }
    }
}

impl<T: Display +
        std::ops::Neg<Output = T> +
        PartialOrd +
        From<u8> +
        Copy> std::fmt::Display for QuadraticEquation<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut res = String::with_capacity(22);
        res.push_str(&format!("{}x^2", self.a));
        if self.b < T::from(0)
        { res.push_str(&format!(" - {}x", self.b)); }
        else
        { res.push_str(&format!(" + {}x", self.b)); }
        if self.c < T::from(0)
        { res.push_str(&format!(" - {}", - self.c)); }
        else
        { res.push_str(&format!(" + {}", self.c)); }
        write!(f, "{}", res)
    }
}

impl<T: Add<Output = T> +
        Sub<Output = T> +
        Mul<Output = T> +
        Div<Output = T> +
        PartialOrd +
        Neg<Output = T> +
        From<u8> +
        Copy +
        SubAssign +
        AddAssign +
        MulAssign +
        TryFrom<f64> +
        Display, const C: usize> From<Polynomial<C, T>> for QuadraticEquation<T> {
    fn from(val: Polynomial<C, T>) -> Self {
        if C > 3
        { panic!("Could not convert because coefficients were more than 3."); }

        QuadraticEquation { a:          val.get_coefficients()[0],
                            b:          val.get_coefficients()[1],
                            c:          val.get_coefficients()[2],
                            vertex:     None,
                            solutions:  (None, None),
                            derivative: None
        }
    }
}