rs_sci/
calculus.rs

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
use std::f64;

pub struct Calculus;

impl Calculus {
    // numerical differentiation

    /// calculates first derivative via central difference method
    ///
    /// #### Example
    /// ```txt
    /// let f = |x| x.powi(2);
    /// let derivative = Calculus::derivative(f, 2.0, 0.0001); // ≈ 4.0
    /// ```
    /// ---
    /// provides better accuracy than forward/backward difference by using points on both sides
    pub fn derivative<F: Fn(f64) -> f64>(f: F, x: f64, h: f64) -> f64 {
        (f(x + h) - f(x - h)) / (2.0 * h)
    }

    /// computes second derivative using central difference
    ///
    /// #### Example
    /// ```txt
    /// let f = |x| x.powi(3);
    /// let second_deriv = Calculus::second_derivative(f, 2.0, 0.0001);
    /// ```
    /// ---
    /// approximates second derivative with three points
    pub fn second_derivative<F: Fn(f64) -> f64>(f: F, x: f64, h: f64) -> f64 {
        (f(x + h) - 2.0 * f(x) + f(x - h)) / (h * h)
    }

    /// finds partial derivative wrt x using central difference
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| x*x + y*y;
    /// let dx = Calculus::partial_x(f, 1.0, 2.0, 0.0001); // ≈ 2.0
    /// ```
    /// ---
    /// keeps y constant while differentiating in x direction
    pub fn partial_x<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> f64 {
        (f(x + h, y) - f(x - h, y)) / (2.0 * h)
    }

    /// finds partial derivative wrt y using central difference
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| x*x + y*y;
    /// let dy = Calculus::partial_y(f, 1.0, 2.0, 0.0001); // ≈ 4.0
    /// ```
    /// ---
    /// keeps x constant while differentiating in y direction
    pub fn partial_y<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> f64 {
        (f(x, y + h) - f(x, y - h)) / (2.0 * h)
    }

    //numerical integration

    /// computes definite integral using rectangle method
    ///
    /// #### Example
    /// ```txt
    /// let f = |x| x.powi(2);
    /// let integral = Calculus::integrate_rectangle(f, 0.0, 1.0, 1000); // ≈ 0.333
    /// ```
    /// ---
    /// approximates area using sum of rectangles with equal width
    pub fn integrate_rectangle<F: Fn(f64) -> f64>(f: F, a: f64, b: f64, n: usize) -> f64 {
        let dx = (b - a) / n as f64;
        let mut sum = 0.0;

        for i in 0..n {
            let x = a + dx * i as f64;
            sum += f(x);
        }

        sum * dx
    }

    /// computes definite integral using trapezoidal method
    ///
    /// #### Example
    /// ```txt
    /// let f = |x| x.powi(2);
    /// let integral = Calculus::integrate_trapezoid(f, 0.0, 1.0, 1000); // ≈ 0.333
    /// ```
    /// ---
    /// uses linear approximation between points for better accuracy than rectangle method
    pub fn integrate_trapezoid<F: Fn(f64) -> f64>(f: F, a: f64, b: f64, n: usize) -> f64 {
        let dx = (b - a) / n as f64;
        let mut sum = (f(a) + f(b)) / 2.0;

        for i in 1..n {
            let x = a + dx * i as f64;
            sum += f(x);
        }

        sum * dx
    }

    /// computes definite integral using simpson's method
    ///
    /// #### Example
    /// ```txt
    /// let f = |x| x.powi(2);
    /// let integral = Calculus::integrate_simpson(f, 0.0, 1.0, 1000); // ≈ 0.333
    /// ```
    /// ---
    /// uses quadratic approximation for higher accuracy than trapezoid method
    pub fn integrate_simpson<F: Fn(f64) -> f64>(f: F, a: f64, b: f64, n: usize) -> f64 {
        if n % 2 != 0 {
            panic!("n must be even for Simpson's rule");
        }

        let dx = (b - a) / n as f64;
        let mut sum = f(a) + f(b);

        for i in 1..n {
            let x = a + dx * i as f64;
            sum += if i % 2 == 0 { 2.0 } else { 4.0 } * f(x);
        }

        sum * dx / 3.0
    }

    // differentials

    /// solves first-order ODE using euler's method
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| x + y;
    /// let solution = Calculus::euler(f, 0.0, 1.0, 0.1, 10);
    /// ```
    /// ---
    /// basic numerical method for ODEs using linear approximation
    pub fn euler<F: Fn(f64, f64) -> f64>(
        f: F,         // dy/dx = f(x, y)
        x0: f64,      // initial x
        y0: f64,      // initial y
        h: f64,       // step size
        steps: usize, // number of steps
    ) -> Vec<(f64, f64)> {
        let mut result = vec![(x0, y0)];
        let mut x = x0;
        let mut y = y0;

        for _ in 0..steps {
            y = y + h * f(x, y);
            x = x + h;
            result.push((x, y));
        }

        result
    }

    /// solves ODE using 4th order runge-kutta method
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| x + y;
    /// let solution = Calculus::runge_kutta4(f, 0.0, 1.0, 0.1, 10);
    /// ```
    /// ---
    /// more accurate than euler's method by using weighted average of slopes
    pub fn runge_kutta4<F: Fn(f64, f64) -> f64>(
        f: F,
        x0: f64,
        y0: f64,
        h: f64,
        steps: usize,
    ) -> Vec<(f64, f64)> {
        let mut result = vec![(x0, y0)];
        let mut x = x0;
        let mut y = y0;

        for _ in 0..steps {
            let k1 = h * f(x, y);
            let k2 = h * f(x + h / 2.0, y + k1 / 2.0);
            let k3 = h * f(x + h / 2.0, y + k2 / 2.0);
            let k4 = h * f(x + h, y + k3);

            y = y + (k1 + 2.0 * k2 + 2.0 * k3 + k4) / 6.0;
            x = x + h;
            result.push((x, y));
        }

        result
    }

    // series and limits

    /// approximates function using taylor series expansion
    ///
    /// #### Example
    /// ```txt
    /// let f = |x| x.exp();
    /// let df = [|x| x.exp()]; // derivatives
    /// let approx = Calculus::taylor_series(f, &df, 0.0, 0.1, 2);
    /// ```
    /// ---
    /// uses function and its derivatives to create polynomial approximation

    pub fn taylor_series<F: Fn(f64) -> f64>(
        f: F,         // function
        df: &[F],     // array of derivative functions
        a: f64,       // expansion point
        x: f64,       // evaluation point
        terms: usize, // number of terms
    ) -> f64 {
        let mut sum = f(a);
        let mut factorial = 1.0;
        let mut power = 1.0;

        for (n, derivative) in df.iter().take(terms - 1).enumerate() {
            factorial *= (n + 1) as f64;
            power *= x - a;
            sum += derivative(a) * power / factorial;
        }

        sum
    }

    // vector calc

    /// computes gradient of 2D scalar field
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| x*x + y*y;
    /// let grad = Calculus::gradient(f, 1.0, 1.0, 0.0001);
    /// ```
    /// ---
    /// returns vector of partial derivatives (∂f/∂x, ∂f/∂y)

    pub fn gradient<F: Fn(f64, f64) -> f64>(f: F, x: f64, y: f64, h: f64) -> (f64, f64) {
        (Self::partial_x(&f, x, y, h), Self::partial_y(&f, x, y, h))
    }

    /// calculates divergence of 2D vector field
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| (x*y, x+y);
    /// let div = Calculus::divergence(f, 1.0, 1.0, 0.0001);
    /// ```
    /// ---
    /// computes sum of partial derivatives ∂P/∂x + ∂Q/∂y

    pub fn divergence<F: Fn(f64, f64) -> (f64, f64)>(f: F, x: f64, y: f64, h: f64) -> f64 {
        let dx = |x, y| (f(x + h, y).0 - f(x - h, y).0) / (2.0 * h);
        let dy = |x, y| (f(x, y + h).1 - f(x, y - h).1) / (2.0 * h);

        dx(x, y) + dy(x, y)
    }

    /// finds z-component of curl for 2D vector field
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| (x*y, x+y);
    /// let curl = Calculus::curl_z(f, 1.0, 1.0, 0.0001);
    /// ```
    /// ---
    /// computes \partial Q/\partial x - \partial P/\partial y

    pub fn curl_z<F: Fn(f64, f64) -> (f64, f64)>(f: F, x: f64, y: f64, h: f64) -> f64 {
        let dy_dx = (f(x + h, y).1 - f(x - h, y).1) / (2.0 * h);
        let dx_dy = (f(x, y + h).0 - f(x, y - h).0) / (2.0 * h);

        dy_dx - dx_dy
    }

    // optimization

    /// finds local minimum using gradient descent
    ///
    /// #### Example
    /// ```txt
    /// let f = |x, y| x*x + y*y;
    /// let min = Calculus::gradient_descent(f, 1.0, 1.0, 0.1, 1e-6, 1000);
    /// ```
    /// ---
    /// iteratively moves in direction of steepest descent
    pub fn gradient_descent<F: Fn(f64, f64) -> f64>(
        f: F,
        mut x: f64,
        mut y: f64,
        learning_rate: f64,
        tolerance: f64,
        max_iterations: usize,
    ) -> (f64, f64) {
        for _ in 0..max_iterations {
            let (dx, dy) = Self::gradient(&f, x, y, 1e-6);
            let new_x = x - learning_rate * dx;
            let new_y = y - learning_rate * dy;

            if (new_x - x).abs() < tolerance && (new_y - y).abs() < tolerance {
                break;
            }

            x = new_x;
            y = new_y;
        }

        (x, y)
    }
}