Skip to main content

tensorlogic_scirs_backend/
conversion.rs

1//! Tensor conversion and construction utilities.
2
3use scirs2_core::ndarray::{Array, IxDyn};
4use tensorlogic_infer::ExecutorError;
5
6use crate::{Scirs2Exec, Scirs2Tensor};
7
8impl Scirs2Exec {
9    /// Create a tensor from a flat vector and shape.
10    pub fn from_vec(data: Vec<f64>, shape: Vec<usize>) -> Result<Scirs2Tensor, ExecutorError> {
11        let total_size: usize = shape.iter().product();
12        if total_size != data.len() {
13            return Err(ExecutorError::ShapeMismatch(format!(
14                "Data length {} doesn't match shape {:?} (total: {})",
15                data.len(),
16                shape,
17                total_size
18            )));
19        }
20
21        Array::from_shape_vec(IxDyn(&shape), data)
22            .map_err(|e| ExecutorError::ShapeMismatch(e.to_string()))
23    }
24
25    /// Create a tensor filled with zeros.
26    pub fn zeros(shape: Vec<usize>) -> Scirs2Tensor {
27        Array::zeros(IxDyn(&shape))
28    }
29
30    /// Create a tensor filled with ones.
31    pub fn ones(shape: Vec<usize>) -> Scirs2Tensor {
32        Array::ones(IxDyn(&shape))
33    }
34}