gridded_data 0.1.1

Multivariate interpolation on a regular / rectilinear grid
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#![doc = include_str!("../README.md")]

use cart_lin::{CartesianIndices, cart_to_lin, cart_to_lin_unchecked};

#[cfg(feature = "serde")]
pub mod serde;

/**
This struct contains the N-dimensional grid data and provides interpolation methods which work on this data.
 */
#[derive(Debug, Clone)]
pub struct GriddedData<const N: usize> {
    pub(crate) axes: [Vec<f64>; N],
    pub(crate) data: Vec<f64>,
}

impl<const N: usize> GriddedData<N> {
    /**
    Creates a new n-dimensional [`GriddedData`] with the number of dimensions defined by the input.

    If the arguments are valid, this function creates a new instance of `GriddedData` with the number
    of dimension equal to the length of `axes`. The given arguments  must fulfil the following criteria:
    - Each vector in the `axes` array must be strictly monotonic increasing
    - Each vector in the `axes` must have at least two values (if it doesn't, the corresponding
    dimension collapses anyway and the vector can be omitted entirely).
    - The number of datapoints (`data.len()`) must be equal to the product of the lengths of the
    axis vectors within `axes`.

    The `data` must be given in row-major fashion (index of last dimension changes fastest). Some examples:

    - The 2D-matrix
        ```text
        1.0 3.0 5.0
        2.0 4.0 6.0
        ```
        is represented by `data = [1.0 3.0 5.0 2.0 4.0 6.0]` (first dimension is the row number, second one the column number).

    - The 3D-matrix (| denotes a page / 3rd dimension separator)
        ```text
        1.0 3.0 5.0 | 7.0  9.0 11.0
        2.0 4.0 6.0 | 8.0 10.0 12.0
        ```
        is represented by `data = [1.0 7.0 3.0 9.0 5.0 11.0 2.0 8.0 4.0 10.0 6.0 12.0]`
        (first dimension is the row number, second one the column number, third one the page number).

    # Examples

    ```
    use gridded_data::GriddedData;

    /*
    2 x 3 x 3 matrix
      z = 1.0     z = 2.0
      y 3 4 6     3  4  6
    x -------     -------
    0 | 0 3 5     7  9 11
    1 | 2 4 6     8 10 12
     */
    let data = vec![1.0, 7.0, 3.0, 9.0, 5.0, 11.0, 2.0, 8.0, 4.0, 10.0, 6.0, 12.0];

    let x = vec![0.0, 1.0];
    let y = vec![3.0, 4.0, 6.0]; // Spacing does not need to be uniform!
    let z = vec![1.0, 2.0];
    assert!(GriddedData::new([x, y, z], data).is_ok());
    ```

    If the data is available in matrix form, consider using the `CartesianIndices` iterator from the [`cart_lin`](https://crates.io/crates/cart_lin)
    library (which is a dependency of this library anyway):

    ```
    use cart_lin::CartesianIndices;
    use gridded_data::GriddedData;

    // 2 x 2 matrix
    // 1.0 3.0
    // 2.0 4.0
    let matrix = [[1.0, 3.0], [2.0, 4.0]];
    let mut data = Vec::with_capacity(4);
    for idx in CartesianIndices::new([2, 2]) {
        data.push(matrix[idx[0]][idx[1]]);
    }
    let x = vec![0.0, 1.0];
    let y = vec![1.0, 2.0];
    assert_eq!(data, vec![1.0, 3.0, 2.0, 4.0]);
    assert!(GriddedData::new([x, y], data).is_ok());
    ```

    As an alternative for two-dimensional data, consider using [`GriddedData::from_slice_of_slices`].
     */
    pub fn new(axes: [Vec<f64>; N], data: Vec<f64>) -> Result<Self, Error> {
        for (idx, axis) in axes.iter().enumerate() {
            // Assert that the input data for the axes is strictly monotonic increasing
            if !(axis.windows(2).all(|w| w[0] < w[1])) {
                return Err(Error(format!(
                    "data for axis {idx} is not strictly monotonic increasing"
                )));
            }

            // Assert that the axis length is at least 2
            if axis.len() < 2 {
                return Err(Error(format!("axis must contain at least two values")));
            }
        }

        // The length of the data must be equal to the product of all axes lengths
        if data.len() != axes.iter().fold(1, |acc, axis| acc * axis.len()) {
            return Err(Error(
                "length of the data must be equal to the product of all axes lengths".into(),
            ));
        }

        return Ok(Self { axes, data });
    }

    /**
    Access the raw data of the underlying axes.
     */
    pub fn axes(&self) -> &[Vec<f64>; N] {
        return &self.axes;
    }

    /**
    Access the raw underlying data.
     */
    pub fn data(&self) -> &[f64] {
        return self.data.as_slice();
    }

    /**
    Access the raw underlying data mutably.
     */
    pub fn data_mut(&mut self) -> &mut [f64] {
        return self.data.as_mut_slice();
    }

    /**
    Access the raw underlying data via cartesian indices.

    This function uses `cart_to_lin` from the [cart_lin](https://crates.io/crates/cart_lin) crate to convert the given cartesian
    indices into a linear index and use that to access [`GriddedData::data`]. If the given cartesian index would
    result in an out-of bounds access, `None` is returned instead.
     */
    pub fn get_cart(&self, indices: &[usize; N]) -> Option<&f64> {
        let index = cart_to_lin(indices, &self.axes_len())?;
        return self.data.get(index);
    }

    /**
    Access the raw underlying data mutably via cartesian indices.

    This function uses `cart_to_lin` from the [cart_lin](https://crates.io/crates/cart_lin) crate to convert the given cartesian
    indices into a linear index and use that to access [`GriddedData::data_mut`]. If the given cartesian index would
    result in an out-of bounds access, `None` is returned instead.
     */
    pub fn get_cart_mut(&mut self, indices: &[usize; N]) -> Option<&mut f64> {
        let index = cart_to_lin(indices, &self.axes_len())?;
        return self.data.get_mut(index);
    }

    /**
    Like [`GriddedData::get_cart`], but does not perform any bound checking and is therefore unsafe.
     */
    pub unsafe fn get_cart_unchecked(&self, indices: &[usize; N]) -> &f64 {
        let index = cart_to_lin_unchecked(indices, &self.axes_len());
        return unsafe { self.data.get_unchecked(index) };
    }

    /**
    Like [`GriddedData::get_cart_mut`], but does not perform any bound checking and is therefore unsafe.
     */
    pub unsafe fn get_cart_unchecked_mut(&mut self, indices: &[usize; N]) -> &mut f64 {
        let index = cart_to_lin_unchecked(indices, &self.axes_len());
        return unsafe { self.data.get_unchecked_mut(index) };
    }

    /**
    Returns the lengths of all axes.
     */
    pub fn axes_len(&self) -> [usize; N] {
        let mut bounds = [0; N];
        for (bound, axis) in bounds.iter_mut().zip(self.axes()) {
            *bound = axis.len();
        }
        return bounds;
    }

    /**
    Access the raw underlying data via a linear index.

    This is a convenience wrapper around `self.data().get()`.
     */
    pub fn get_lin(&self, index: usize) -> Option<&f64> {
        return self.data.get(index);
    }

    /**
    Access the raw underlying data mutably via a linear index.

    This is a convenience wrapper around `self.get_mut().get()`.
     */
    pub fn get_lin_mut(&mut self, index: usize) -> Option<&mut f64> {
        return self.data.get_mut(index);
    }

    /**
    Like [`GriddedData::get_lin`], but does not perform bounds checking and is therefore unsafe.
     */
    pub unsafe fn get_lin_unchecked(&self, index: usize) -> &f64 {
        return unsafe { self.data.get_unchecked(index) };
    }

    /**
    Like [`GriddedData::get_lin_mut`], but does not perform bounds checking and is therefore unsafe.
     */
    pub unsafe fn get_lin_unchecked_mut(&mut self, index: usize) -> &mut f64 {
        return unsafe { self.data.get_unchecked_mut(index) };
    }

    /**
    Returns the axes values which correspond to the given cartesian index.

    ```
    use gridded_data::GriddedData;

    let grid = GriddedData::new([vec![0.0, 1.0], vec![1.0, 2.0]], vec![1.0, 3.0, 2.0, 4.0]).expect("valid inputs");
    assert_eq!(grid.axes_values_cart(&[1, 0]).expect("valid index"), [1.0, 1.0]);
    ```
     */
    pub fn axes_values_cart(&self, indices: &[usize; N]) -> Option<[f64; N]> {
        let mut point = [0.0; N];
        for ((pt, index), axis) in point.iter_mut().zip(indices.iter()).zip(self.axes.iter()) {
            *pt = *axis.get(*index)?;
        }
        return Some(point);
    }

    /**
    Like [`GriddedData::axes_values_cart`], but does not perform bounds checking and is therefore unsafe.
     */
    pub unsafe fn axes_values_cart_unchecked(&self, indices: &[usize; N]) -> [f64; N] {
        let mut point = [0.0; N];
        for ((pt, index), axis) in point.iter_mut().zip(indices.iter()).zip(self.axes.iter()) {
            *pt = *unsafe { axis.get_unchecked(*index) };
        }
        return point;
    }

    /**
    Returns the cell / hypercuboid bounds containing the given point.

    If the given point is within the grid, this function finds the indices of the node interval of each axis which contain
    the corresponding point value. Please see the top-level documentation for a discussion of the underlying concept.

    # Examples

    ```
    use gridded_data::GriddedData;

    let data = vec![1.0, 7.0, 3.0, 9.0, 5.0, 11.0, 2.0, 8.0, 4.0, 10.0, 6.0, 12.0];
    let x = vec![0.0, 1.0];
    let y = vec![3.0, 4.0, 6.0]; // Spacing does not need to be uniform!
    let z = vec![1.0, 2.0];
    let grid = GriddedData::new([x, y, z], data).expect("valid input");

    /*
    The x-coordinate of the given point is between the 0th and the 1st node
    of the x-axis, the y-coordinate is between the 1st and the 2nd node and the
    z-coordinate is between the 0th and the 1st node.
     */
    assert_eq!(grid.cell_bounds(&[0.5, 4.1, 1.5]), Some([[0, 1], [1, 2], [0, 1]]));

    /*
    x-coordinate directly on 0th node
    y-coordinate directly on 1st node -> Two different intervals could be returned:
    [0, 1] or [1, 2]. This function returns the "right-side" interval
    z-coordinate directly on 1st node
     */
    assert_eq!(grid.cell_bounds(&[0.0, 4.0, 2.0]), Some([[0, 1], [1, 2], [0, 1]]));

    // y-coordinate outside grid -> Return None.
    assert_eq!(grid.cell_bounds(&[0.5, 6.1, 1.5]), None);
    ```
     */
    pub fn cell_bounds(&self, point: &[f64; N]) -> Option<[[usize; 2]; N]> {
        if !self.contains(point) {
            return None;
        }
        return Some(self.cell_bounds_raw(point));
    }

    fn cell_bounds_raw(&self, point: &[f64; N]) -> [[usize; 2]; N] {
        let mut bounds = [[0, 0]; N];
        for (index, (p, axis)) in bounds.iter_mut().zip(point.iter().zip(self.axes().iter())) {
            let partition_point = axis.as_slice().partition_point(|&val| val <= *p);
            match partition_point.checked_sub(1) {
                Some(pp_minus_one) => {
                    // SAFETY: axis contains at least one element (checked in constructor)
                    if partition_point == axis.len() {
                        if unsafe { *axis.last().unwrap_unchecked() } == *p {
                            *index = [pp_minus_one - 1, pp_minus_one]
                        } else {
                            *index = [pp_minus_one, pp_minus_one]
                        }
                    } else {
                        *index = [pp_minus_one, partition_point]
                    }
                }
                None => *index = [partition_point, partition_point],
            }
        }
        return bounds;
    }

    /**
    Returns whether the given point is inside the grid or not.

    # Examples
    ```
    use gridded_data::GriddedData;

    let grid = GriddedData::new([vec![0.0, 1.0], vec![1.0, 2.0]], vec![1.0, 3.0, 2.0, 4.0]).expect("valid inputs");

    assert!(grid.contains(&[0.5, 1.2]));
    assert!(!grid.contains(&[1.5, 1.2]));
    ```
     */
    pub fn contains(&self, point: &[f64; N]) -> bool {
        return self.axes().iter().zip(point.iter()).all(|(axis, val)| {
            // SAFETY: axis has at least one element -> This is checked in the grid constructor
            unsafe {
                axis.first().unwrap_unchecked() <= val && axis.last().unwrap_unchecked() >= val
            }
        });
    }

    /**
    Performs a nearest-neighbor interpolation for the given point.

    This function first finds the "cell" (see [`GriddedData::cell_bounds`]) which contains
    the given point. Afterwards, it calculates the [Euclidian distance](https://en.wikipedia.org/wiki/Euclidean_distance)
    of the point to each cell node and identifies the cartesian indices of the nearest node.
    Finally, it returns the corresponding `data` value.

    In case the point is outside the grid, the only difference is that there is just one node per dimension for each
    point value outside of the respective axis.

    # Examples
    ```
    use gridded_data::GriddedData;

    let data = vec![1.0, 7.0, 3.0, 9.0, 5.0, 11.0, 2.0, 8.0, 4.0, 10.0, 6.0, 12.0];
    let x = vec![0.0, 1.0];
    let y = vec![3.0, 4.0, 6.0]; // Spacing does not need to be uniform!
    let z = vec![1.0, 2.0];
    let grid = GriddedData::new([x, y, z], data).expect("valid input");

    /*
    This point is inside the cell with the bounds x = [0.0, 1.0], y = [4.0, 6.0], z = [1.0, 2.0]
    Within this cell, it is closest to the node [1.0, 4.0, 2.0], which corresponds to the data value 10.0
     */
    assert_eq!(grid.nearest_neighbor_interp(&[0.9, 4.2, 1.7]), 10.0);

    /*
    The x-coordinate of this point is outside the grid -> Extrapolation by only considering the x-node with value 0.0
     */
    assert_eq!(grid.nearest_neighbor_interp(&[-1.0, 4.2, 1.7]), 9.0);
    ```
     */
    pub fn nearest_neighbor_interp(&self, point: &[f64; N]) -> f64 {
        let mut cell_bounds = self.cell_bounds_raw(point);

        // CartesianIndices::from_bounds_unchecked expects a half-open interval, hence we need to add 1 to the upper limits
        add_one_to_upper(&mut cell_bounds);

        // Temporary buffer for the current corner point
        let mut corner_point_indices = [0; N];

        // We are looking for the closest corner point
        let mut closest_corner_point = [0; N];

        // Start values of the iteration
        let mut minimum_distance = std::f64::INFINITY;

        // Look up the cell corner points and calculate the distance to point
        for cart_prod in CartesianIndices::from_bounds_unchecked(cell_bounds).into_iter() {
            // Populate the corner point indices for all axes
            for (corner_point_index, cart_prod_index) in
                corner_point_indices.iter_mut().zip(cart_prod.iter())
            {
                *corner_point_index = *cart_prod_index;
            }

            // Now calculate the distance between this point and the input
            let distance: f64 = self
                .axes()
                .iter()
                .zip(corner_point_indices.iter())
                .zip(point.iter())
                .map(|((axis, corner_point_index), point_value)| {
                    // SAFETY: This value exists for sure since it is build from cell_bounds
                    let corner_point_value = unsafe { *axis.get_unchecked(*corner_point_index) };
                    (corner_point_value - *point_value).powi(2)
                })
                .sum();
            if distance < minimum_distance {
                closest_corner_point = corner_point_indices;
                minimum_distance = distance;
            }
        }

        // SAFETY: The closest corner point is one of the cell corner points and therefore exists for sure.
        return unsafe { *self.get_cart_unchecked(&closest_corner_point) };
    }

    /**
    Performs a multivariate linear interpolation for the given point.

    This function performs a generalized bilinear interpolation and calculates the
    return value as
    ```math
    output = num / denom
    ```
    with
    ```math
    num = [ P11..11 * (a2-a) * (b2-b) * ... (n2-n) + P21..11 * (a-a1) * (b2-b) * ... (n2-n) + ... + P22..22 * (a-a1) * (b-b1) * ... (n-n1) ]
    denom = [ (a2-a1) * (b2-b1) * ... (n2-n1) ]
    ```
    - 1 is the index of the smaller value, 2 is the index of the larger value
    - P11..11 to P22..22 are the node values of the multidimensional grid
    - a1, a2 are the two data values along the first axis, b1, b2 along the second axis and so on.
    - a, b, ..., n are the values of the input point for the corresponding dimensions.

    If the point is outside the grid, a [nearest-neighbor extrapolation](GriddedData::nearest_neighbor_interp) is performed instead.

    # Examples
    ```
    use gridded_data::GriddedData;

    let data = vec![1.0, 7.0, 3.0, 9.0, 5.0, 11.0, 2.0, 8.0, 4.0, 10.0, 6.0, 12.0];
    let x = vec![0.0, 1.0];
    let y = vec![3.0, 4.0, 6.0]; // Spacing does not need to be uniform!
    let z = vec![1.0, 2.0];
    let grid = GriddedData::new([x, y, z], data).expect("valid input");

    // Values within the grid (tolerance accounts for limited floating-point accuracy)
    assert!((grid.linear_interp(&[0.9, 4.2, 1.7]) - 8.3).abs() < 1e-10);
    assert!((grid.linear_interp(&[0.8, 3.7, 1.2]) - 4.4).abs() < 1e-10);

    // Value outside grid -> Nearest-neighbor extrapolation
    assert_eq!(grid.linear_interp(&[-1.0, 4.2, 1.7]), 9.0);
    ```
     */
    pub fn linear_interp(&self, point: &[f64; N]) -> f64 {
        let cell_bounds = match self.cell_bounds(&point) {
            Some(limits) => limits,
            None => {
                return {
                    // Extrapolation with the nearest-neighbor method
                    self.nearest_neighbor_interp(&point)
                };
            }
        };

        // Calculate the denominator [ (a2-a1) * (b2-b1) * ... (n2-n1) ]
        let mut denominator = 1.0;
        for (limit, axis) in cell_bounds.iter().zip(self.axes()) {
            // SAFETY: the function contains checked that all dimensions of point are inside the grid.
            // This also makes sure that the bracket value is always positive, since limit[0] < limit[1]
            // and the axis is strictly monotonic increasing.
            let lower = unsafe { *axis.get_unchecked(limit[0]) };
            let upper = unsafe { *axis.get_unchecked(limit[1]) };
            denominator = denominator * (upper - lower);
        }

        // CartesianIndices::from_bounds_unchecked expects a half-open interval, hence we need to add 1 to the upper limits
        let mut adj_cell_bounds = cell_bounds.clone();
        add_one_to_upper(&mut adj_cell_bounds);

        // Calculate the numerator
        let mut numerator = 0.0;
        for grid_corner in CartesianIndices::from_bounds_unchecked(adj_cell_bounds).into_iter() {
            // Get the indices of the diagonally opposing point
            // Those values are needed to calculate the brackets.
            let mut opposing_grid_corner = [0; N];
            opposing_grid_corner
                .iter_mut()
                .zip(cell_bounds.iter())
                .zip(grid_corner.iter())
                .for_each(|((op_corner, limit), corner)| {
                    *op_corner = limit[0] * usize::from(*corner == limit[1])
                        + limit[1] * usize::from(*corner == limit[0]);
                });

            // SAFETY: grid_corner is generated from cell_bounds which is fully inside the grid.
            let mut product = unsafe { *self.get_cart_unchecked(&grid_corner) };

            // Calculate the bracket values
            for (((pt_val, corner_idx), limit), axis) in point
                .iter()
                .zip(grid_corner.iter())
                .zip(cell_bounds.iter())
                .zip(self.axes().iter())
            {
                // If this is true, we need to calculate (x2-x), otherwise (x-x1)
                // SAFETY: All indices of cell_bounds are guaranteed to be in bounds for their respective axis.
                if *corner_idx == limit[0] {
                    product = product * (unsafe { axis.get_unchecked(limit[1]) } - *pt_val);
                } else {
                    product = product * (*pt_val - unsafe { axis.get_unchecked(limit[0]) });
                }
            }

            numerator += product;
        }

        return numerator / denominator;
    }
}

/**
Add an offset of + 1 to the upper bound of each dimension
*/
fn add_one_to_upper<const N: usize>(bounds: &mut [[usize; 2]; N]) {
    for [_, upper] in bounds.iter_mut() {
        *upper += 1;
    }
}

impl<const N: usize> std::ops::Index<&[usize; N]> for GriddedData<N> {
    type Output = f64;

    fn index(&self, index: &[usize; N]) -> &Self::Output {
        return self.get_cart(index).expect("out-of-bounds access");
    }
}

impl<const N: usize> std::ops::IndexMut<&[usize; N]> for GriddedData<N> {
    fn index_mut(&mut self, index: &[usize; N]) -> &mut Self::Output {
        return self.get_cart_mut(index).expect("out-of-bounds access");
    }
}

impl<const N: usize> std::ops::Index<usize> for GriddedData<N> {
    type Output = f64;

    fn index(&self, index: usize) -> &Self::Output {
        return &self.data[index];
    }
}

impl<const N: usize> std::ops::IndexMut<usize> for GriddedData<N> {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        return &mut self.data[index];
    }
}

#[cfg(feature = "nalgebra")]
use nalgebra::{Dim, Matrix, RawStorage};

impl GriddedData<2> {
    /**
    Create a 2-dimensional [`GriddedData`] using a slices-of-slices matrix as data.

    # Examples
    ```
    use gridded_data::GriddedData;

    let row1 = [1.0, 3.0];
    let row2 = [2.0, 4.0];
    let data = [row1.as_slice(), row2.as_slice()];
    let grid = GriddedData::from_slice_of_slices([vec![0.0, 1.0], vec![1.0, 2.0]], data.as_slice()).expect("valid inputs");
    assert_eq!(grid.data(), &[1.0, 2.0, 3.0, 4.0]);
    ```
     */
    pub fn from_slice_of_slices(axes: [Vec<f64>; 2], data: &[&[f64]]) -> Result<Self, Error> {
        let length_second_axis = axes[1].len();
        if data.len() != length_second_axis {
            return Err(Error(format!(
                "length {} of outer slice is not equal to the length {} of the second axis",
                data.len(),
                length_second_axis
            )));
        }

        let length_first_axis = axes[0].len();
        for (idx, inner_slice) in data.iter().enumerate() {
            if inner_slice.len() != axes[0].len() {
                return Err(Error(format!(
                    "length {} of inner slice {} is not equal to the length {} of the first axis",
                    data.len(),
                    idx,
                    length_first_axis
                )));
            }
        }

        // Concatenate the data into a vector
        let mut vec = Vec::with_capacity(length_first_axis * length_second_axis);
        for first in 0..length_first_axis {
            for second in 0..length_second_axis {
                // SAFETY: We checked before that the length of the outer slice is equal to length_second_axis
                // and that the length of each inner slice is equal to length_first_axis
                vec.push(unsafe { *data.get_unchecked(second).get_unchecked(first) });
            }
        }

        return Self::new(axes, vec);
    }

    /**
    Create a 2-dimensional [`GriddedData`] using a [nalgebra](https://crates.io/crates/nalgebra) matrix.

    Only available with the **nalgebra** feature.

    # Examples
    ```
    use gridded_data::GriddedData;
    use nalgebra::Matrix2;

    let row1 = [1.0, 3.0];
    let row2 = [2.0, 4.0];
    let data = Matrix2::from_vec(vec![1.0, 3.0, 2.0, 4.0]); // nalgebra is column-major!
    let grid = GriddedData::from_nalgebra_matrix([vec![0.0, 1.0], vec![1.0, 2.0]], &data).expect("valid inputs");
    assert_eq!(grid.data(), &[1.0, 2.0, 3.0, 4.0]); // The data within grid is stored row-major
    ```
    */
    #[cfg(feature = "nalgebra")]
    pub fn from_nalgebra_matrix<R: Dim, C: Dim, S: RawStorage<f64, R, C>>(
        axes: [Vec<f64>; 2],
        matrix: &Matrix<f64, R, C, S>,
    ) -> Result<Self, Error> {
        if axes[0].len() != matrix.nrows() {
            return Err(Error(
                "number of rows must be equal to the length of the first axis".to_string(),
            ));
        }
        if axes[1].len() != matrix.ncols() {
            return Err(Error(
                "number of columns must be equal to the length of the second axis".to_string(),
            ));
        }

        let mut vector = Vec::with_capacity(matrix.nrows() * matrix.ncols());
        for row in matrix.row_iter() {
            for value in row.iter() {
                vector.push(*value);
            }
        }

        return Self::new(axes, vector);
    }
}

/**
Error for gridded data.
 */
#[derive(Debug, Clone)]
pub struct Error(pub String);

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        return self.0.fmt(f);
    }
}

impl std::error::Error for Error {}