Skip to main content

tensor4all_simplett/
error.rs

1//! Error types for tensor train operations
2
3use thiserror::Error;
4
5/// Result type for tensor train operations
6pub type Result<T> = std::result::Result<T, TensorTrainError>;
7
8/// Errors that can occur during tensor train operations
9///
10/// # Examples
11///
12/// ```
13/// use tensor4all_simplett::{TensorTrainError, TensorTrain, AbstractTensorTrain};
14///
15/// // Empty index set triggers IndexLengthMismatch
16/// let tt = TensorTrain::<f64>::constant(&[2, 3], 1.0);
17/// let err = tt.evaluate(&[0]).unwrap_err();
18/// assert!(matches!(err, TensorTrainError::IndexLengthMismatch { expected: 2, got: 1 }));
19///
20/// // DimensionMismatch can be constructed directly
21/// let err = TensorTrainError::DimensionMismatch { site: 3 };
22/// assert!(err.to_string().contains("site 3"));
23///
24/// // InvalidOperation carries an arbitrary message
25/// let err = TensorTrainError::InvalidOperation {
26///     message: "test error".to_string(),
27/// };
28/// assert!(err.to_string().contains("test error"));
29/// ```
30#[derive(Error, Debug)]
31pub enum TensorTrainError {
32    /// Dimension mismatch between tensors
33    #[error("Dimension mismatch: tensor at site {site} has incompatible dimensions")]
34    DimensionMismatch {
35        /// The site index where the mismatch occurred
36        site: usize,
37    },
38
39    /// Invalid index provided
40    #[error("Index out of bounds: index {index} at site {site} (max: {max})")]
41    IndexOutOfBounds {
42        /// The site index where the error occurred
43        site: usize,
44        /// The invalid index value
45        index: usize,
46        /// The maximum allowed index value
47        max: usize,
48    },
49
50    /// Length mismatch in index set
51    #[error("Index set length mismatch: expected {expected}, got {got}")]
52    IndexLengthMismatch {
53        /// The expected length
54        expected: usize,
55        /// The actual length provided
56        got: usize,
57    },
58
59    /// Empty tensor train
60    #[error("Tensor train is empty")]
61    Empty,
62
63    /// Invalid operation
64    #[error("Invalid operation: {message}")]
65    InvalidOperation {
66        /// Description of the invalid operation
67        message: String,
68    },
69
70    /// Matrix CI error
71    #[error("Matrix CI error: {0}")]
72    MatrixCI(#[from] tensor4all_tcicore::MatrixCIError),
73}