{"record":{"id":"b74600887d30a8b5","repo":"tracel-ai/burn","slug":"d2-must-equal-d-1-for-tensor-cartesian-grid","errorCode":null,"errorMessage":"D2 must equal D + 1 for Tensor::cartesian_grid","messagePattern":"D2 must equal D \\+ 1 for Tensor::cartesian_grid","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/api/cartesian_grid.rs","lineNumber":30,"sourceCode":"/// # Panics\n///\n/// Panics if `D2` is not equal to `D+1`.\n///\n/// # Examples\n///\n/// ```rust\n///    use burn_tensor::Int;\n///    use burn_tensor::{Shape, Tensor};\n/// let device = Default::default();\n/// let result: Tensor<3, _> = Tensor::<2, Int>::cartesian_grid([2, 3], &device);\n/// println!(\"{}\", result);\n/// ```\npub fn cartesian_grid<S: Into<Shape>, const D: usize, const D2: usize>(\n    shape: S,\n    device: &Device,\n) -> Tensor<D2, Int> {\n    if D2 != D + 1 {\n        panic!(\"D2 must equal D + 1 for Tensor::cartesian_grid\")\n    }\n\n    let dims = shape.into();\n    let mut indices: Vec<Tensor<D, Int>> = Vec::new();\n\n    for dim in 0..D {\n        let dim_range: Tensor<1, Int> = Tensor::arange(0..dims[dim] as i64, device);\n\n        let mut shape = [1; D];\n        shape[dim] = dims[dim];\n        let mut dim_range = dim_range.reshape(shape);\n\n        for (i, &item) in dims.iter().enumerate() {\n            if i == dim {\n                continue;\n            }\n            dim_range = dim_range.repeat_dim(i, item);\n        }","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/api/cartesian_grid.rs#L12-L48","documentation":"Compile-time-vs-runtime rank check in `Tensor::cartesian_grid`: given a D-dimensional input shape, the output grid tensor must have rank D+1 (one coordinate plane per axis); if the caller annotates the result with a D2 that isn't D+1, the function panics. The failing input is the mismatched const generic D2 on the call site.","triggerScenarios":"Thrown at crates/burn-tensor/src/tensor/api/cartesian_grid.rs:30 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Annotate the result type as Tensor<D+1, Int>, e.g. `Tensor::<3, _>` for a 2-D shape input.","Let the compiler infer D2 instead of pinning it to a wrong constant.","Fix the shape argument if the intended grid dimension differs from the annotated output rank."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}