/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pic-scale-safe-0.1.12/src/math/lagrange.rs
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Radzivon Bartoshyk, 10/2024. All rights reserved. |
3 | | * |
4 | | * Redistribution and use in source and binary forms, with or without modification, |
5 | | * are permitted provided that the following conditions are met: |
6 | | * |
7 | | * 1. Redistributions of source code must retain the above copyright notice, this |
8 | | * list of conditions and the following disclaimer. |
9 | | * |
10 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
11 | | * this list of conditions and the following disclaimer in the documentation |
12 | | * and/or other materials provided with the distribution. |
13 | | * |
14 | | * 3. Neither the name of the copyright holder nor the names of its |
15 | | * contributors may be used to endorse or promote products derived from |
16 | | * this software without specific prior written permission. |
17 | | * |
18 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
22 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 | | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | | */ |
29 | | |
30 | | use num_traits::AsPrimitive; |
31 | | use std::ops::{Add, Div, Mul, MulAssign, Sub}; |
32 | | |
33 | | #[inline(always)] |
34 | 0 | pub(crate) fn lagrange< |
35 | 0 | V: Copy |
36 | 0 | + PartialEq |
37 | 0 | + PartialOrd |
38 | 0 | + AsPrimitive<usize> |
39 | 0 | + Mul<Output = V> |
40 | 0 | + Add<Output = V> |
41 | 0 | + Sub<Output = V> |
42 | 0 | + Div<Output = V> |
43 | 0 | + MulAssign, |
44 | 0 | >( |
45 | 0 | x: V, |
46 | 0 | support: usize, |
47 | 0 | ) -> V |
48 | 0 | where |
49 | 0 | f32: AsPrimitive<V>, |
50 | 0 | usize: AsPrimitive<V>, |
51 | | { |
52 | 0 | if x > support.as_() { |
53 | 0 | return 0f32.as_(); |
54 | 0 | } |
55 | 0 | let order = (2.0f32.as_() * support.as_()).as_(); |
56 | 0 | let n = (support.as_() + x).as_(); |
57 | 0 | let mut value = 1.0f32.as_(); |
58 | 0 | for i in 0..order { |
59 | 0 | if i != n { |
60 | 0 | value *= (n.as_() - i.as_() - x) / (n.as_() - i.as_()); |
61 | 0 | } |
62 | | } |
63 | 0 | value |
64 | 0 | } |
65 | | |
66 | | #[inline(always)] |
67 | 0 | pub(crate) fn lagrange2< |
68 | 0 | V: Copy |
69 | 0 | + PartialEq |
70 | 0 | + PartialOrd |
71 | 0 | + AsPrimitive<usize> |
72 | 0 | + Mul<Output = V> |
73 | 0 | + Add<Output = V> |
74 | 0 | + Sub<Output = V> |
75 | 0 | + Div<Output = V> |
76 | 0 | + MulAssign, |
77 | 0 | >( |
78 | 0 | x: V, |
79 | 0 | ) -> V |
80 | 0 | where |
81 | 0 | f32: AsPrimitive<V>, |
82 | 0 | usize: AsPrimitive<V>, |
83 | | { |
84 | 0 | lagrange(x, 2) |
85 | 0 | } |
86 | | |
87 | | #[inline(always)] |
88 | 0 | pub(crate) fn lagrange3< |
89 | 0 | V: Copy |
90 | 0 | + PartialEq |
91 | 0 | + PartialOrd |
92 | 0 | + AsPrimitive<usize> |
93 | 0 | + Mul<Output = V> |
94 | 0 | + Add<Output = V> |
95 | 0 | + Sub<Output = V> |
96 | 0 | + Div<Output = V> |
97 | 0 | + MulAssign, |
98 | 0 | >( |
99 | 0 | x: V, |
100 | 0 | ) -> V |
101 | 0 | where |
102 | 0 | f32: AsPrimitive<V>, |
103 | 0 | usize: AsPrimitive<V>, |
104 | | { |
105 | 0 | lagrange(x, 3) |
106 | 0 | } |