numerical_analysis 0.1.2

A collection of algorithms for numerical analysis.
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#[cfg(feature = "std")]
use misc_iterators::{IterDimensions, integer_decomposition::ExactTupleSum};

use crate::EvalResult;
use crate::RealField;
#[cfg(not(target_arch = "spirv"))]
use crate::binomial;
use crate::legendre_roots::*;
use crate::to_index;

#[derive(Default, Clone, Debug)]
pub struct NormalizedMultiDimensionalLegendreBasis<
    S: RealField,
    const ARRAY_SIZE: usize = 20,
> {
    pub aabb_min: [S; 3],
    pub aabb_max: [S; 3],
}

impl<S: RealField, const ARRAY_SIZE: usize>
    NormalizedMultiDimensionalLegendreBasis<S, ARRAY_SIZE>
{
    pub fn new(aabb_min: [S; 3], aabb_max: [S; 3]) -> Self { Self { aabb_min, aabb_max } }

    pub fn aabb(&self) -> [[S; 3]; 2] { [self.aabb_min, self.aabb_max] }

    pub fn eval(
        &self,
        x: [S; 3],
        degree: usize,
        #[cfg(not(feature = "std"))] out: &mut [S; ARRAY_SIZE],
    ) -> EvalResult<S>
    {
        let construct = |i| {
            // Normalize input.
            let a = self.aabb_min[i];
            let b = self.aabb_max[i];
            let x_p = x[i] * (S::from(2).unwrap() / (b - a)) - ((a + b) / (b - a));

            #[cfg(feature = "std")]
            assert!(x_p >= S::from(-1.0001).unwrap() && x_p <= S::from(1.0001).unwrap());

            #[cfg(not(feature = "std"))]
            let mut res = [S::default(); 6];
            #[cfg(feature = "std")]
            let mut res = vec![S::default(); degree + 1];

            res[0] = S::from(1).unwrap();
            res[1] = x_p;

            let one = S::one();
            let two = S::from(2).unwrap();

            for j in 2..=degree
            {
                let n = S::from(j).unwrap();
                res[j] = (two * n - one) * x_p * res[j - 1] - (n - one) * res[j - 2];
                res[j] /= n;
            }

            for j in 0..=degree
            {
                let rho = S::from(j).unwrap();
                res[j] = res[j] * ((two * rho + one) / (b - a)).sqrt();
            }

            res
        };

        let evals = [construct(0), construct(1), construct(2)];

        #[cfg(not(feature = "std"))]
        let mut res = out;
        #[cfg(feature = "std")]
        let mut res = {
            let total_basis_count = binomial(degree + 3, 3);
            vec![S::from(0.).unwrap(); total_basis_count]
        };

        // Iterate over all integer tuples (i, j, k) such that
        // i + j + k <= degree.
        for i in 2..degree + 3
        {
            for j in 1..i
            {
                for k in 0..j
                {
                    let w = i - j - 1;
                    let v = j - k - 1;
                    let u = k;

                    let indices = [u, v, w];
                    let mut prod = S::from(1).unwrap();
                    for l in 0..3
                    {
                        prod *= evals[l][indices[l]];
                    }

                    res[to_index(&indices)] = prod;
                }
            }
        }

        #[cfg(feature = "std")]
        return res;

        #[cfg(not(feature = "std"))]
        []
    }

    #[cfg(feature = "std")]
    pub fn project<F>(
        &mut self,
        degree: usize,
        prior_degree: usize,
        mut function: F,
    ) -> Vec<S>
    where
        F: FnMut([S; 3]) -> S,
    {
        debug_assert!(prior_degree < degree);
        if degree == 0
        {
            return Vec::new();
        }
        // We use the nodes for the 4 * degree scheme in legendre interpolation.
        // This is a heuristic described in "Hierarchichal HP-Adaptive Sigend Distance
        // fields." We may want to use a different quadrature scheme in the
        // future.
        let n = 4 * degree;
        let nodes = legendre_nodes_lut(n);
        let weights = legendre_weights_lut(n);

        let total_basis_count = binomial(degree + 3, 3);

        let aabb_min = self.aabb_min;
        let aabb_max = self.aabb_max;
        let scale_quadrature = |x: [S; 3]| {
            // Normalize the quadrature point to the cell bounds.
            let one = S::from(1).unwrap();
            let two = S::from(2).unwrap();
            let point: [S; 3] = std::array::from_fn(|i| {
                let a = aabb_min[i];
                let b = aabb_max[i];
                ((b - a) * (x[i] + one) / two) + a
            });

            point
        };

        let mut coefficients = vec![S::default(); total_basis_count];
        let mut values = vec![S::default(); n * n * n];

        for multi_index in IterDimensions::new([n, n, n])
        {
            let mut weight = S::from(1.).unwrap();

            for (j, i) in multi_index.into_iter().enumerate()
            {
                weight *= S::from(weights[i]).unwrap() * (aabb_max[j] - aabb_min[j])
                    / S::from(2).unwrap();
            }

            let point = scale_quadrature(multi_index.map(|i| S::from(nodes[i]).unwrap()));

            let val = function(point);
            values[multi_index[2] * n * n + multi_index[1] * n + multi_index[0]] =
                val * weight;

            let vw = values[multi_index[2] * n * n + multi_index[1] * n + multi_index[0]];
            let quadrature_point =
                scale_quadrature(multi_index.map(|i| S::from(nodes[i]).unwrap()));
            let basis_eval = self.eval(quadrature_point, degree);

            for i in 2..degree + 3
            {
                for j in 1..i
                {
                    for k in 0..j
                    {
                        let w = i - j - 1;
                        let v = j - k - 1;
                        let u = k;

                        let power_indices = [u, v, w];
                        let index = to_index(&power_indices);
                        coefficients[index] += vw * basis_eval[index];
                    }
                }
            }
        }

        coefficients
    }

    #[cfg(feature = "std")]
    pub fn project_sparse<F>(
        &mut self,
        degree: usize,
        prior_degree: usize,
        mut function: F,
    ) -> (Vec<(S, [usize; 3])>, usize, [usize; 3])
    where
        F: FnMut([S; 3]) -> S,
    {
        debug_assert!(prior_degree < degree);
        if degree == 0
        {
            return (Vec::new(), 0, [0; 3]);
        }
        // We use the nodes for the 4 * degree scheme in legendre interpolation.
        // This is a heuristic described in "Hierarchichal HP-Adaptive Sigend Distance
        // fields." We may want to use a different quadrature scheme in the
        // future.
        let n = 4 * degree;
        let nodes = legendre_nodes_lut(n);
        let weights = legendre_weights_lut(n);

        let total_basis_count = binomial(degree + 3, 3);

        let aabb_min = self.aabb_min;
        let aabb_max = self.aabb_max;
        let scale_quadrature = |x: [S; 3]| {
            // Normalize the quadrature point to the cell bounds.
            let one = S::from(1).unwrap();
            let two = S::from(2).unwrap();
            let point: [S; 3] = std::array::from_fn(|i| {
                let a = aabb_min[i];
                let b = aabb_max[i];
                ((b - a) * (x[i] + one) / two) + a
            });

            point
        };

        let mut coefficients = vec![S::default(); total_basis_count];
        let mut values = vec![S::default(); n * n * n];

        for multi_index in IterDimensions::new([n, n, n])
        {
            let mut weight = S::from(1.).unwrap();

            for (j, i) in multi_index.into_iter().enumerate()
            {
                weight *= S::from(weights[i]).unwrap() * (aabb_max[j] - aabb_min[j])
                    / S::from(2).unwrap();
            }

            let point = scale_quadrature(multi_index.map(|i| S::from(nodes[i]).unwrap()));

            let val = function(point);
            values[multi_index[2] * n * n + multi_index[1] * n + multi_index[0]] =
                val * weight;

            let vw = values[multi_index[2] * n * n + multi_index[1] * n + multi_index[0]];
            let quadrature_point =
                scale_quadrature(multi_index.map(|i| S::from(nodes[i]).unwrap()));
            let basis_eval = self.eval(quadrature_point, degree);

            for i in 2..degree + 3
            {
                for j in 1..i
                {
                    for k in 0..j
                    {
                        let w = i - j - 1;
                        let v = j - k - 1;
                        let u = k;

                        let power_indices = [u, v, w];
                        let index = to_index(&power_indices);
                        coefficients[index] += vw * basis_eval[index];
                    }
                }
            }
        }

        let mut maxs = [0; 3];
        let mut coeff_counter = 0;
        let mut sparse_coefficients = Vec::new();
        for i in 2..degree + 3
        {
            for j in 1..i
            {
                for k in 0..j
                {
                    let w = i - j - 1;
                    let v = j - k - 1;
                    let u = k;

                    let power_indices = [u, v, w];
                    let index = to_index(&power_indices);
                    if coefficients[index] <= S::from(0.0001).unwrap()
                    {
                        continue;
                    }

                    maxs[0] = maxs[0].max(u);
                    maxs[1] = maxs[1].max(v);
                    maxs[2] = maxs[2].max(w);
                    sparse_coefficients.push((coefficients[index], power_indices));
                    coeff_counter += 1;
                }
            }
        }

        (sparse_coefficients, coeff_counter, maxs)
    }

    pub fn evaluate_function_vector(
        &self,
        x: [S; 3],
        degree: usize,
        #[cfg(feature = "std")] coefficients: &[S],
        #[cfg(not(feature = "std"))] coefficients: &[S; ARRAY_SIZE],
    ) -> S
    {
        #[cfg(feature = "std")]
        let basis_eval = self.eval(x, degree);

        #[cfg(not(feature = "std"))]
        let basis_eval = {
            let mut basis_eval = [S::default(); ARRAY_SIZE];
            self.eval(x, degree, &mut basis_eval);

            basis_eval
        };

        let mut res = S::default();
        for i in 0..basis_eval.len()
        {
            let v = basis_eval[i];
            let coeff = coefficients[i];

            res += v.clone() * coeff.clone();
        }

        res
    }

    pub fn evaluate_sparse_function_vector(
        &self,
        x: [S; 3],
        #[cfg(feature = "std")] coefficients: &[(S, [usize; 3])],
        #[cfg(not(feature = "std"))] coefficients: &[(S, [usize; 3]); ARRAY_SIZE],
        coeff_count: usize,
        max_degree: [usize; 3],
    ) -> S
    {
        let construct = |i| {
            // Normalize input.
            let a = self.aabb_min[i];
            let b = self.aabb_max[i];
            let x_p = x[i] * (S::from(2).unwrap() / (b - a)) - ((a + b) / (b - a));

            #[cfg(feature = "std")]
            assert!(x_p >= S::from(-1.0001).unwrap() && x_p <= S::from(1.0001).unwrap());

            #[cfg(not(feature = "std"))]
            let mut res = [S::default(); 6];
            #[cfg(feature = "std")]
            let mut res = vec![S::default(); max_degree[i] + 1];

            res[0] = S::from(1).unwrap();
            res[1] = x_p;

            let one = S::one();
            let two = S::from(2).unwrap();

            for j in 2..=max_degree[i]
            {
                let n = S::from(j).unwrap();
                res[j] = (two * n - one) * x_p * res[j - 1] - (n - one) * res[j - 2];
                res[j] /= n;
            }

            for j in 0..=max_degree[i]
            {
                let rho = S::from(j).unwrap();
                res[j] = res[j] * ((two * rho + one) / (b - a)).sqrt();
            }

            res
        };

        let evals = [construct(0), construct(1), construct(2)];

        let mut res = S::default();
        for i in 0..coeff_count
        {
            let (coeff, indices) = coefficients[i];
            let eval = coeff.clone()
                * evals[0][indices[0]]
                * evals[1][indices[1]]
                * evals[2][indices[2]];

            res += eval;
        }

        res
    }

    /// Estimate the current error of the approximation.
    // This is a heuristic comming from "Hierarchichal hp-Adaptive Signed Distance
    // Fields", equation 6.
    #[cfg(feature = "std")]
    pub fn error(coeffs: &[S], degree: usize) -> S
    {
        let mut error = S::default();
        for index in ExactTupleSum::<3>::new(degree)
        {
            let v = coeffs[to_index(&index)];
            error += v * v;
        }

        error
    }
}

pub struct LegendreIterator<S: RealField>
{
    x: S,
    prior: S,
    current: S,
    count: usize,
}

impl<S: RealField> LegendreIterator<S>
{
    pub fn new(x: S) -> Self
    {
        Self {
            prior: S::from(1.).unwrap(),
            current: x,
            count: 0,
            x,
        }
    }
}

impl<S: RealField> Iterator for LegendreIterator<S>
{
    type Item = S;

    fn next(&mut self) -> Option<Self::Item>
    {
        self.count += 1;

        if self.count == 1
        {
            return Some(self.prior);
        }
        else if self.count == 2
        {
            return Some(self.current);
        }

        let p = S::from(self.count - 1).unwrap();
        let one = S::from(1).unwrap();
        let p_inv = one / p;

        let next = p_inv
            * ((S::from(2).unwrap() * p - one) * self.x * self.current
                - (p - one) * self.prior);

        self.prior = self.current;
        self.current = next;

        Some(self.current)
    }
}

pub fn legendre_nodes_lut(order: usize) -> &'static [f64]
{
    let first = (order - 1) * order / 2;
    let last = first + order;

    &LEGENDRE_NODE_TABLES[first..last]
}

pub fn legendre_weights_lut(order: usize) -> &'static [f64]
{
    let first = (order - 1) * order / 2;
    let last = first + order;

    &LEGENDRE_WEIGHT_TABLES[first..last]
}

/// Legendre quadrature is only defined for the interval [-1, 1], beware.
pub fn quadrature<F>(count: usize, mut function: F) -> f64
where
    F: FnMut(f64) -> f64,
{
    let nodes = legendre_nodes_lut(count);
    let weights = legendre_weights_lut(count);

    let mut integral = 0.;
    for (n, w) in nodes.iter().zip(weights.iter())
    {
        integral += w * function(*n);
    }

    integral
}

#[cfg(test)]
mod tests
{
    use super::*;

    #[test]
    fn test_multidimensional_legendre_quadrature()
    {
        let mut poly = NormalizedMultiDimensionalLegendreBasis::<_, 20>::new(
            [-1., -1., -1.],
            [1., 1., 1.],
        );

        use nalgebra::Vector3;
        let mut poly = NormalizedMultiDimensionalLegendreBasis::<_, 20>::new(
            [-1., -1., -1.],
            [1., 1., 1.],
        );

        let sdf = |x: [f64; 3]| {
            let v = Vector3::from(x);
            v.norm_squared() - 1.
        };
        let coeffs = poly.project(2, 0, &sdf);
    }

    #[test]
    fn test_multidimensional_legendre_quadrature_sparse()
    {
        let mut poly = NormalizedMultiDimensionalLegendreBasis::<_, 20>::new(
            [-1., -1., -1.],
            [1., 1., 1.],
        );

        use nalgebra::Vector3;
        let mut poly = NormalizedMultiDimensionalLegendreBasis::<_, 20>::new(
            [-1., -1., -1.],
            [1., 1., 1.],
        );

        let sdf = |x: [f64; 3]| {
            let v = Vector3::from(x);
            v.norm_squared() - 1.
        };

        let (coeffs, coeff_count, max_degrees) = poly.project_sparse(2, 0, &sdf);
        let res = poly.evaluate_sparse_function_vector(
            [0., 0., 0.],
            &coeffs,
            coeff_count,
            max_degrees,
        );
        println!("{}", res);

        let (coeffs, coeff_count, max_degrees) = poly.project_sparse(2, 0, &sdf);
        let res = poly.evaluate_sparse_function_vector(
            [0., 1., 0.],
            &coeffs,
            coeff_count,
            max_degrees,
        );
        println!("{}", res);

        let (coeffs, coeff_count, max_degrees) = poly.project_sparse(2, 0, &sdf);
        let res = poly.evaluate_sparse_function_vector(
            [0., 2., 0.],
            &coeffs,
            coeff_count,
            max_degrees,
        );
        println!("{}", res);
    }

    #[test]
    fn test_legendre_quadrature()
    {
        let res = quadrature(4, |x| x * x);
        assert!((res - (2. / 3.)).abs() < 0.00001);

        let res = quadrature(5, |x| x.sin());
        assert!(res.abs() < 0.00001);

        let res = quadrature(5, |x| x.cos());
        assert!((res - 2. * 1_f64.sin()).abs() < 0.00001);

        let res = quadrature(5, |x| x.exp());
        let e = std::f64::consts::E;
        assert!((res - (e - 1. / e)).abs() < 0.00001);
    }

    #[test]
    fn test_legendre_nodes()
    {
        let n = 5;
        let weights = legendre_weights_lut(n);
        for (index, xi) in legendre_nodes_lut(n).iter().enumerate()
        {
            let mut p_n2 = 1.;
            let mut p_n1 = *xi;

            for i in 2..=n
            {
                let i = i as f64;
                let next = ((2. * i - 1.) * xi * p_n1 - (i - 1.) * p_n2) / i;

                p_n2 = p_n1;
                p_n1 = next;
            }

            let n = n as f64;
            let n1 = n * p_n2;
            let d = 1. - xi * xi;
            let w = if n > 0. { (2. * d) / (n1 * n1) } else { 2. };

            assert_eq!(w, weights[index]);
        }
    }

    #[test]
    fn test_legendre_iterator()
    {
        let iter = LegendreIterator::new(0.5);
        let expected = vec![
            1.,
            0.5,
            -0.125,
            -0.4375,
            -0.2890625,
            0.08984375,
            0.3232421875,
            0.22314453125,
            -0.073638916015625,
            -0.2678985595703125,
        ];

        for (v, e) in iter.take(10).zip(expected.iter())
        {
            assert!(v == *e);
        }
    }
}