use crate::backend_ops::BackendOps;
use coeus_core::{CpuAddressableStorage, CpuAddressableStorageMut, Scalar};
use coeus_tensor::Tensor;
#[inline]
pub fn meshgrid<T: Scalar, B: BackendOps<T> + Default>(
tensors: &[&Tensor<T, B>],
indexing: &str,
_backend: &B,
) -> Vec<Tensor<T, B>>
where
B::DeviceBuffer<T>: CpuAddressableStorage<T> + CpuAddressableStorageMut<T>,
{
assert!(
matches!(indexing, "ij" | "xy"),
"meshgrid: indexing must be \"ij\" or \"xy\", got {indexing:?}"
);
for (i, t) in tensors.iter().enumerate() {
assert_eq!(
t.ndim(),
1,
"meshgrid: tensor {i} must be 1-D, got {}-D",
t.ndim()
);
}
let n = tensors.len();
let sizes: Vec<usize> = tensors.iter().map(|t| t.shape()[0]).collect();
let ij_to_grid = |grid_idx: usize| -> usize {
if indexing == "xy" && n >= 2 {
match grid_idx {
0 => 1, 1 => 0, i => i,
}
} else {
grid_idx
}
};
let total: usize = sizes.iter().product();
(0..n)
.map(|g| {
let src_dim = ij_to_grid(g);
let src_cont = tensors[g].to_contiguous();
let src_s = src_cont.as_slice();
let out_shape = if indexing == "xy" && n >= 2 {
let mut s = sizes.clone();
s.swap(0, 1);
s
} else {
sizes.clone()
};
let mut strides = vec![1usize; n];
for d in (0..n - 1).rev() {
strides[d] = strides[d + 1] * out_shape[d + 1];
}
let data: Vec<T> = (0..total)
.map(|flat| {
let coord_in_src_dim = (flat / strides[src_dim]) % out_shape[src_dim];
src_s[coord_in_src_dim]
})
.collect();
Tensor::from_slice(out_shape, &data)
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use coeus_core::SequentialBackend;
use coeus_tensor::Tensor;
#[test]
fn meshgrid_ij_2d_creates_correct_grids() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(vec![3], &[0.0f32, 1.0, 2.0]);
let y = Tensor::from_slice(vec![2], &[10.0f32, 20.0]);
let grids = meshgrid(&[&x, &y], "ij", &b);
assert_eq!(grids.len(), 2);
assert_eq!(grids[0].shape(), &[3, 2]);
assert_eq!(grids[0].as_slice(), &[0.0, 0.0, 1.0, 1.0, 2.0, 2.0]);
assert_eq!(grids[1].shape(), &[3, 2]);
assert_eq!(grids[1].as_slice(), &[10.0, 20.0, 10.0, 20.0, 10.0, 20.0]);
}
#[test]
fn meshgrid_ij_1d_is_identity() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(vec![4], &[1.0f32, 2.0, 3.0, 4.0]);
let grids = meshgrid(&[&x], "ij", &b);
assert_eq!(grids.len(), 1);
assert_eq!(grids[0].as_slice(), x.as_slice());
}
#[test]
fn meshgrid_ij_matches_numpy_convention() {
let b = SequentialBackend::new();
let x = Tensor::from_slice(vec![3], &[0.0f32, 1.0, 2.0]);
let y = Tensor::from_slice(vec![2], &[0.0f32, 1.0]);
let grids = meshgrid(&[&x, &y], "ij", &b);
for row in 0..3 {
for col in 0..2 {
assert_eq!(grids[0].as_slice()[row * 2 + col], row as f32);
}
}
for row in 0..3 {
for col in 0..2 {
assert_eq!(grids[1].as_slice()[row * 2 + col], col as f32);
}
}
}
}