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
// Internal dependencies
use crate::solvers::basis::functions::{Function2D, Function2D2D, Composable2D, Differentiable2D};

#[derive(PartialEq, Debug)]
/// # General Information
///
/// A simple, first degree polynomial in two variables.
///
/// # Fields
///
/// * `x_coefficient` - constant that multiplies x variable.
/// * `y_coefficient` - constant that multiplies y variable.
/// * `independent_term` - constant that adds to variable.
///
pub struct FirstDegreePolynomial2D {
    pub(crate) x_coefficient: f64,
    pub(crate) y_coefficient: f64,
    pub(crate) independent_term: f64
}

#[derive(PartialEq, Debug)]
/// # General Information
///
/// A simple, second degree polynomial in two variables
///
/// # Fields
///
/// * `x_quadratic_coefficient` - constant that multiplies x quadratic term.
/// * `y_quadratic_coefficient` - constant that multiplies y quadratic term.
/// * `xy_coefficient` - constant that multiplies xy term.
/// * `x_linear_coefficient` - constant that multiplies x linear term.
/// * `y_linear_coefficient` - constant that multiplies y linear term.
/// * `independent_term` - constant that is added to varaibles.
///
pub struct SecondDegreePolynomial2D {
    x_quadratic_coefficient: f64,
    y_quadratic_coefficient: f64,
    xy_coefficient: f64,
    x_linear_coefficient: f64,
    y_linear_coefficient: f64,
    independent_term: f64,
}

/// # General Information
/// 
/// Represents a matrix transformation in 2D. A normal matrix composed of vectors or arrays is not used since
/// more control over the traits that are implemented on the struct makes latter implementations easier.
/// 
/// # Fields
/// 
/// * `a` - Element [0,0] in matrix
/// * `b` - Element [0,1] in matrix
/// * `c` - Element [1,0] in matrix
/// * `d` - Element [1,1] in matrix
/// 
pub struct Transformation2D {
    a: f64,
    b: f64,
    c: f64,
    d: f64
}

impl Transformation2D {

    ///  New instance
    pub fn new(a: f64, b: f64, c: f64, d: f64) -> Transformation2D {
        Transformation2D {
            a,
            b,
            c,
            d
        }
    }

    /// Inverse of a  2x2 matrix
    pub fn inverse(self) -> Transformation2D {
        
        let determinant = 1_f64 / (self.a * self.d - self.b * self.c);
        
        Transformation2D {
             a: self.d * determinant,
             b: - self.b * determinant,
             c: - self.c * determinant,
             d: self.a * determinant
        }
    }

}

impl Function2D2D for Transformation2D {
    fn evaluate(&self, x: f64, y: f64) -> (f64,f64) {
        (self.a * x + self.b * y, self.c * x + self.d * y)
    }
}


impl FirstDegreePolynomial2D {
    /// Normal constructor.
    pub fn new(x_coefficient: f64, y_coefficient: f64, independent_term: f64) -> FirstDegreePolynomial2D {
        FirstDegreePolynomial2D {
            x_coefficient,
            y_coefficient,
            independent_term,
        }
    }

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

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

    /// Translate a function by a given point
    pub fn translate(self, w: f64, z: f64) -> FirstDegreePolynomial2D {
        Self {
            x_coefficient: self.x_coefficient,
            y_coefficient: self.y_coefficient,
            independent_term: self.independent_term - self.x_coefficient * w - self.y_coefficient * z,
        }
    } 

    /// One of three basis functions on unit triangle {(0,0),(1,0),(0,1)}
    pub fn psi_1() -> FirstDegreePolynomial2D {
        FirstDegreePolynomial2D {
            x_coefficient: -1_f64,
            y_coefficient: -1_f64,
            independent_term: 1_f64,
        }
    }

    /// One of three basis functions on unit triangle {(0,0),(1,0),(0,1)}
    pub fn psi_2() -> FirstDegreePolynomial2D {
        FirstDegreePolynomial2D {
            x_coefficient: 1_f64,
            y_coefficient: 0_f64,
            independent_term: 0_f64,
        }
    }

    /// One of three basis functions on unit triangle {(0,0),(1,0),(0,1)}
    pub fn psi_3() -> FirstDegreePolynomial2D {
        FirstDegreePolynomial2D {
            x_coefficient: 0_f64,
            y_coefficient: 1_f64,
            independent_term: 0_f64
        }
    }
}

impl Function2D for FirstDegreePolynomial2D {
    fn evaluate(&self, x: f64, y: f64) -> f64 {
        self.x_coefficient * x + self.y_coefficient * y + self.independent_term
    }
}

impl Composable2D<Transformation2D, FirstDegreePolynomial2D> for FirstDegreePolynomial2D {
    
    fn compose(self, other: Transformation2D) -> Result<FirstDegreePolynomial2D,crate::Error> {

        let x_coefficient = other.a * self.x_coefficient + other.c * self.y_coefficient;
        let y_coefficient = other.b * self.x_coefficient + other.d *  self.y_coefficient;

        Ok(FirstDegreePolynomial2D {
            x_coefficient,
            y_coefficient,
            independent_term: self.independent_term,
        })

    }
}

impl Differentiable2D<FirstDegreePolynomial2D,FirstDegreePolynomial2D> for FirstDegreePolynomial2D {
    
    fn differentiate_x(&self) -> Result<FirstDegreePolynomial2D,crate::Error> {
        Ok(
            FirstDegreePolynomial2D {
                x_coefficient: 0_f64,
                y_coefficient: 0_f64,
                independent_term: self.x_coefficient
            }
        )

    }

    fn differentiate_y(&self) -> Result<FirstDegreePolynomial2D,crate::Error> {
        Ok(
            FirstDegreePolynomial2D {
                x_coefficient: 0_f64,
                y_coefficient: 0_f64,
                independent_term: self.y_coefficient
            }
        )

    }
}