polyfit 0.11.0

Because you don't need to be able to build a powerdrill to use one safely
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use crate::{
    basis::Basis, display::PolynomialDisplay, transforms::Transform, value::Value, Polynomial,
};

/// Types of scaling transformations for data
pub enum ScaleTransform<T: Value> {
    /// Adds a fixed offset to every element of a dataset.
    ///
    /// This is useful for translating a signal up or down without changing its shape.
    ///
    /// ![Shift example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/shift_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = x + shift
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `shift`: The value to add to each element. Positive shifts move data up,
    ///   negative shifts move it down.
    Shift(T),

    /// Multiplies every element of a dataset by a fixed factor.
    ///
    /// Useful for scaling a signal up or down without changing its shape.
    ///
    /// ![Scale example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/linear_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = x * factor
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `factor`: The multiplier applied to each element.
    ///   - `factor > 1` → enlarges values
    ///   - `factor = 1` → leaves values unchanged
    ///   - `factor < 0` → flips the sign
    Linear(T),

    /// Applies a quadratic scaling to each element of a dataset.
    ///
    /// Each element is squared and then multiplied by the specified factor.
    /// Useful for emphasizing larger values or modeling parabolic effects.
    ///
    /// ![Quadratic example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/quadratic_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = factor * x^2
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `factor`: Multiplier applied after squaring each element.
    Quadratic(T),

    /// Applies a cubic scaling to each element of a dataset.
    ///
    /// Each element is cubed and then multiplied by the specified factor.
    /// Useful for emphasizing extremes and modeling cubic effects.
    ///
    /// ![Cubic example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/cubic_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = factor * x^3
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `factor`: Multiplier applied after cubing each element.
    Cubic(T),

    /// Applies an exponential scaling to each element of a dataset.
    ///
    /// Each element is transformed using exponentiation with the specified base
    ///
    /// ![Exponential example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/exponential_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = factor * base^x
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `base`: The exponent to which each element is raised.
    /// - `factor`: The multiplier applied after exponentiation.
    Exponential(T, T),

    /// Applies a logarithmic scaling to each element of a dataset.
    ///
    /// Each element is transformed using the logarithm with the specified base
    /// and then multiplied by the specified factor.
    /// Useful for compressing wide-ranging data or modeling logarithmic relationships.
    ///
    /// ![Logarithmic example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/logarithmic_example.png)
    ///
    /// # Parameters
    /// - `base`: The base of the logarithm.
    /// - `factor`: The multiplier applied after logarithmic transformation.
    Logarithmic(T, T),
}
impl<T: Value> Transform<T> for ScaleTransform<T> {
    fn apply<'a>(&self, data: impl Iterator<Item = &'a mut T>) {
        match self {
            ScaleTransform::Shift(amount) => {
                for value in data {
                    *value += *amount;
                }
            }
            ScaleTransform::Linear(slope) => {
                for value in data {
                    *value *= *slope;
                }
            }
            ScaleTransform::Quadratic(coef) => {
                for value in data {
                    *value = *value * *value * *coef;
                }
            }
            ScaleTransform::Cubic(coef) => {
                for value in data {
                    *value = *value * *value * *value * *coef;
                }
            }
            ScaleTransform::Exponential(base, coef) => {
                for value in data {
                    *value = base.powf(*value) * *coef;
                }
            }
            ScaleTransform::Logarithmic(base, coef) => {
                for value in data {
                    *value = Value::max(*value, T::epsilon()).log(*base) * *coef;
                }
            }
        }
    }
}

/// Trait for applying scaling transformations to data.
pub trait ApplyScale<T: Value> {
    /// Adds a fixed offset to every element of a dataset.
    ///
    /// This is useful for translating a signal up or down without changing its shape.
    ///
    /// ![Shift example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/shift_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = x + shift
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `shift`: The value to add to each element. Positive shifts move data up,
    ///   negative shifts move it down.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use polyfit::transforms::ApplyScale;
    /// let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_shift_scale(2.0);
    /// ```
    #[must_use]
    fn apply_shift_scale(self, amount: T) -> Self;

    /// Multiplies every element of a dataset by a fixed factor.
    ///
    /// Useful for scaling a signal up or down without changing its shape.
    ///
    /// ![Scale example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/linear_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = x * factor
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `factor`: The multiplier applied to each element.
    ///   - `factor > 1` → enlarges values
    ///   - `factor = 1` → leaves values unchanged
    ///   - `factor < 0` → flips the sign
    ///
    /// # Example
    ///
    /// ```rust
    /// # use polyfit::transforms::ApplyScale;
    /// let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_linear_scale(2.0);
    /// ```
    #[must_use]
    fn apply_linear_scale(self, factor: T) -> Self;

    /// Applies a quadratic scaling to each element of a dataset.
    ///
    /// Each element is squared and then multiplied by the specified factor.
    /// Useful for emphasizing larger values or modeling parabolic effects.
    ///
    /// ![Quadratic example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/quadratic_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = factor * x^2
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `factor`: Multiplier applied after squaring each element.
    ///
    /// # Example
    /// ```
    /// # use polyfit::transforms::ApplyScale;
    /// let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_quadratic_scale(2.0);
    /// ```
    #[must_use]
    fn apply_quadratic_scale(self, coef: T) -> Self;

    /// Applies a cubic scaling to each element of a dataset.
    ///
    /// Each element is cubed and then multiplied by the specified factor.
    /// Useful for emphasizing extremes and modeling cubic effects.
    ///
    /// ![Cubic example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/cubic_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = factor * x^3
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `factor`: Multiplier applied after cubing each element.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use polyfit::transforms::ApplyScale;
    /// let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_cubic_scale(2.0);
    /// ```
    #[must_use]
    fn apply_cubic_scale(self, coef: T) -> Self;

    /// Applies an exponential scaling to each element of a dataset.
    ///
    /// Each element is transformed using exponentiation with the specified base
    ///
    /// ![Exponential example](https://raw.githubusercontent.com/caliangroup/polyfit/refs/heads/master/.github/assets/exponential_example.png)
    ///
    /// <div class="warning">
    ///
    /// **Technical Details**
    ///
    /// ```math
    /// xₙ = factor * base^x
    /// ```
    /// </div>
    ///
    /// # Parameters
    /// - `base`: The base of the exponentiation.
    /// - `factor`: The multiplier applied after exponentiation.
    ///
    /// # Example
    /// ```rust
    /// # use polyfit::transforms::ApplyScale;
    /// let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_exponential_scale(2.0, 3.0);
    /// ```
    #[must_use]
    fn apply_exponential_scale(self, base: T, factor: T) -> Self;

    /// Applies a polynomial series as a transformation to each element of a dataset.
    ///
    /// The value of each element is replaced by the polynomial evaluated at that element.
    /// Useful for modeling non-linear relationships or custom transformations.
    ///
    /// # Parameters
    ///
    /// - `polynomial`: Reference to a `Polynomial` object, which defines the coefficients
    ///   and basis to use for the transformation.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use polyfit::function;
    /// # use polyfit::transforms::ApplyScale;
    /// function!(y(x) = 2 x^2 - 3 x + 4);
    /// let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_polynomial_scale(&y);
    /// ```
    #[must_use]
    fn apply_polynomial_scale<B: Basis<T> + PolynomialDisplay<T>>(
        self,
        polynomial: &Polynomial<B, T>,
    ) -> Self;

    /// Applies a logarithmic scaling to each element of a dataset.
    ///
    /// Each element is transformed using the logarithm with the specified base
    /// and then multiplied by the specified factor.
    /// Useful for compressing wide-ranging data or modeling logarithmic relationships.
    ///
    /// # Parameters
    /// - `base`: The base of the logarithm.
    /// - `factor`: The multiplier applied after logarithmic transformation.
    ///
    /// # Example
    /// ```rust
    /// # use polyfit::transforms::ApplyScale;
    /// let data = vec![(1.0, 10.0), (10.0, 100.0)].apply_logarithmic_scale(10.0, 2.0);
    /// ```
    #[must_use]
    fn apply_logarithmic_scale(self, base: T, factor: T) -> Self;
}
impl<T: Value> ApplyScale<T> for Vec<(T, T)> {
    fn apply_shift_scale(mut self, amount: T) -> Self {
        ScaleTransform::Shift(amount).apply(self.iter_mut().map(|(_, y)| y));
        self
    }

    fn apply_linear_scale(mut self, factor: T) -> Self {
        ScaleTransform::Linear(factor).apply(self.iter_mut().map(|(_, y)| y));
        self
    }

    fn apply_quadratic_scale(mut self, coef: T) -> Self {
        ScaleTransform::Quadratic(coef).apply(self.iter_mut().map(|(_, y)| y));
        self
    }

    fn apply_cubic_scale(mut self, coef: T) -> Self {
        ScaleTransform::Cubic(coef).apply(self.iter_mut().map(|(_, y)| y));
        self
    }

    fn apply_exponential_scale(mut self, degree: T, factor: T) -> Self {
        ScaleTransform::Exponential(degree, factor).apply(self.iter_mut().map(|(_, y)| y));
        self
    }

    fn apply_polynomial_scale<B: Basis<T> + PolynomialDisplay<T>>(
        mut self,
        polynomial: &Polynomial<B, T>,
    ) -> Self {
        // Apply the polynomial scale transformation
        for (_, y) in &mut self {
            *y = polynomial.y(*y);
        }

        self
    }

    fn apply_logarithmic_scale(mut self, base: T, factor: T) -> Self {
        ScaleTransform::Logarithmic(base, factor).apply(self.iter_mut().map(|(_, y)| y));
        self
    }
}

#[cfg(test)]
mod tests {
    use crate::transforms::ApplyScale;

    #[test]
    fn test_shift_scale() {
        let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_shift_scale(2.0);
        assert_eq!(data, vec![(1.0, 4.0), (2.0, 5.0)]);
    }

    #[test]
    fn test_linear_scale() {
        let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_linear_scale(2.0);
        assert_eq!(data, vec![(1.0, 4.0), (2.0, 6.0)]);
    }

    #[test]
    fn test_quadratic_scale() {
        let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_quadratic_scale(2.0);
        assert_eq!(data, vec![(1.0, 8.0), (2.0, 18.0)]);
    }

    #[test]
    fn test_cubic_scale() {
        let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_cubic_scale(2.0);
        assert_eq!(data, vec![(1.0, 16.0), (2.0, 54.0)]);
    }

    #[test]
    fn test_polynomial_scale() {
        function!(y(x) = 2 x^2 - 3 x + 4);
        let data = vec![(1.0, 2.0), (2.0, 3.0)].apply_polynomial_scale(&y);
        assert_eq!(data, vec![(1.0, 6.0), (2.0, 13.0)]);
    }

    #[test]
    fn test_logarithmic_scale() {
        let data = vec![(1.0, 10.0), (10.0, 100.0)].apply_logarithmic_scale(10.0, 2.0);
        assert_eq!(data, vec![(1.0, 2.0), (10.0, 4.0)]);
    }
}