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
use crate::Error;

// Internal dependencies.
use crate::solvers::basis::functions::{Composable1D, Differentiable1D, Function1D};

#[derive(PartialEq, Debug)]
/// # General Information
///
/// A simple, first degree polynomial in one variable.
///
/// # Fields
///
/// * `coefficient` - constant that multiplies variable.
/// * `independent_term` - constant that adds to variable.
///
pub struct FirstDegreePolynomial {
    pub(crate) coefficient: f64,
    pub(crate) independent_term: f64,
}

#[derive(PartialEq, Debug)]
/// # General Information
///
/// A simple, second degree polynomial in one variable
///
/// # Fields
///
/// * `quadratic_coefficient` - constant that multiplies quadratic term.
/// * `linear_coefficient` - constant that multiplies linear term.
/// * `independent_term` - constant that is added to varaible.
///
pub struct SecondDegreePolynomial {
    quadratic_coefficient: f64,
    linear_coefficient: f64,
    independent_term: f64,
}

impl FirstDegreePolynomial {
    /// Normal constructor.
    pub fn new(coefficient: f64, independent_term: f64) -> FirstDegreePolynomial {
        FirstDegreePolynomial {
            coefficient,
            independent_term,
        }
    }

    /// Zero function factory.
    pub fn zero() -> FirstDegreePolynomial {
        Self {
            coefficient: 0_f64,
            independent_term: 0_f64,
        }
    }

    /// Constant function factory.
    pub fn constant(independent_term: f64) -> FirstDegreePolynomial {
        Self {
            coefficient: 0_f64,
            independent_term,
        }
    }

    /// Transformation from any interval to [0,1].
    pub fn transformation_to_0_1(beg: f64, end: f64) -> FirstDegreePolynomial {
        let coefficient = 1_f64 / (end - beg);
        let independent_term = -beg / (end - beg);
        FirstDegreePolynomial {
            coefficient,
            independent_term,
        }
    }

    /// Transformation from [-1,1] to any interval.
    pub fn transformation_from_m1_p1(beg: f64, end: f64) -> FirstDegreePolynomial {
        let coefficient = (end - beg) / 2_f64;
        let independent_term = (end + beg) / 2_f64;
        FirstDegreePolynomial {
            coefficient,
            independent_term,
        }
    }

    // One of two basis functions on unit interval [0,1]
    pub fn phi_1() -> FirstDegreePolynomial {
        FirstDegreePolynomial {
            coefficient: 1_f64,
            independent_term: 0_f64,
        }
    }

    // One of two basis functions on unit interval [0,1]
    pub fn phi_2() -> FirstDegreePolynomial {
        FirstDegreePolynomial {
            coefficient: -1_f64,
            independent_term: 1_f64,
        }
    }
}

impl Function1D for FirstDegreePolynomial {
    /// # Specific Implementation
    ///
    /// Simple evaluation of a polynomial.
    ///
    fn evaluate(&self, x: f64) -> f64 {
        self.coefficient * x + self.independent_term
    }
}

// self(other(x))
impl Composable1D<FirstDegreePolynomial, FirstDegreePolynomial> for FirstDegreePolynomial {
    /// # Specific Implementation
    ///
    /// Composition of two first degree polynomials results in another polynomial.
    ///
    fn compose(self, other: FirstDegreePolynomial) -> Result<FirstDegreePolynomial,Error> {
        Ok(FirstDegreePolynomial {
            coefficient: self.coefficient * other.coefficient,
            independent_term: self.coefficient * other.independent_term + self.independent_term,
        })
    }
}

impl Differentiable1D<FirstDegreePolynomial> for FirstDegreePolynomial {
    /// # Specific Implementation
    ///
    /// Differentiation of a first degree polynomial results in a constant.
    ///
    fn differentiate(&self) -> Result<FirstDegreePolynomial,Error> {
        Ok(FirstDegreePolynomial {
            coefficient: 0_f64,
            independent_term: self.coefficient,
        })
    }
}

impl SecondDegreePolynomial {
    /// Simple constructor for second degree polynomial.
    pub fn new(
        quadratic_coefficient: f64,
        linear_coefficient: f64,
        independent_term: f64,
    ) -> SecondDegreePolynomial {
        SecondDegreePolynomial {
            quadratic_coefficient,
            linear_coefficient,
            independent_term,
        }
    }

    // One of three basis functions on unit interval [0,1]
    pub fn psi_1() -> SecondDegreePolynomial {
        SecondDegreePolynomial {
            quadratic_coefficient: 2_f64,
            linear_coefficient: -3_f64,
            independent_term: 1_f64
        }
    }

    // One of three basis functions on unit interval [0,1]
    pub fn psi_2() -> SecondDegreePolynomial {
        SecondDegreePolynomial {
            quadratic_coefficient: -4_f64,
            linear_coefficient: 4_f64,
            independent_term: 0_f64
        }
    }

    // One of three basis functions on unit interval [0,1]
    pub fn psi_3() -> SecondDegreePolynomial {
        SecondDegreePolynomial {
            quadratic_coefficient: 2_f64,
            linear_coefficient: -1_f64,
            independent_term: 0_f64
        }
    }

}

impl Function1D for SecondDegreePolynomial {
    /// # Specific Implementation
    ///
    /// Simple evaluation of a second degree polynomial.
    ///
    fn evaluate(&self, x: f64) -> f64 {
        self.quadratic_coefficient * x.powf(2_f64)
            + self.linear_coefficient * x
            + self.independent_term
    }
}

impl Differentiable1D<FirstDegreePolynomial> for SecondDegreePolynomial {
    /// # Specific Implementation
    ///
    /// Differentiation of a second degree polynomial results on a first degree polynomial.
    ///
    fn differentiate(&self) -> Result<FirstDegreePolynomial,Error> {
        Ok(FirstDegreePolynomial {
            coefficient: 2_f64 * self.quadratic_coefficient,
            independent_term: self.linear_coefficient,
        })
    }
}

// self(other(x))
impl Composable1D<FirstDegreePolynomial, SecondDegreePolynomial> for SecondDegreePolynomial {
    /// # Specific implementation
    ///
    /// Composing a second degree polynomial with a first degree polynomial gives another second degree polynomial.
    ///
    fn compose(self, other: FirstDegreePolynomial) -> Result<SecondDegreePolynomial,Error> {
        Ok(SecondDegreePolynomial {
            quadratic_coefficient: self.quadratic_coefficient * other.coefficient.powf(2_f64),
            linear_coefficient: 2_f64
                * self.quadratic_coefficient
                * other.coefficient
                * other.independent_term
                + self.linear_coefficient * other.coefficient,
            independent_term: other.independent_term.powf(2_f64)
                + other.independent_term * self.linear_coefficient
                + self.independent_term,
        })
    }
}