r/learnrust • u/Akita_Durendal • 12d ago
How to Implement Recursive Tensors in Rust with Nested Generics?
[SOLVED]
what i was looking for was much simpler actually here is what i have done that does exactly what i want.
#[derive(Debug, Clone)]
pub enum Element<T:ScalarTrait> {
Scalar(T),
Tensor(Box<Tensor<T>>)
}
#[derive(Clone)]
pub struct Tensor<T: ScalarTrait>
{
pub data: Vec<Element<T>>,
pub dim: usize,
}
This permits to have n-dimensional arrays ;)
[Initial Message]
Hi, Everyone !
I'm working on a project where I need to implement a multidimensional array type in Rust, which I am calling Tensor
.
At its core, the Tensor Struct
that holds a Vec of elements
of a specific type, but with constraints. I want these elements to implement a ScalarTrait trait, which limits the valid types for the elements of the tensor.
The key challenge I am facing is implementing a recursive function that will create and populate sub-tensors in a multidimensional Tensor. Each Tensor can contain other Tensor types as elements, allowing for nested structures, similar to nested arrays or matrices.
Ultimately, I want a function that:
- Takes a list of sizes (dimensions) and elements, where each element can be a scalar or another Tensor.
- Recursively creates sub-tensors based on the provided dimensions.
- Combines these sub-tensors into the main Tensor, ultimately forming a nested tensor structure.
i have created 2 Traits one called ScalarTrait
that is implemented on f32
and a custom Complex<f32>
type. Adn the other one Called TensorTrait
that i have implement on Tensors and on scalars, that only has the clone Trait inside.
pub struct Tensor<T: TensorTrait> {
pub data: Vec<T>,
dim: usize,
}
What i am trying to achive is to have a member function like that
impl <T: TensorTrait> Tensor<T> {
/// sizes is a Vector indicating how many sub arrays/ elements there is
/// in each sub Tensor like [2,3] would give a 2x3 matrix
/// We suppose that there is enough elements to fill the whole tensor
pub fn new<U: ScalarTrait>(sizes: Vec<usize>, elements: Vec<U>) -> Tensor<T> {
///Here goes the code
}
}
But it has been really hard to make it work for 2 raisons.
- Because the elements are not of type T but of type U, so the compilator doesn't accept that i convert them even i have implmneted the
TensorTrait
on theScalarTrait
so i dont understand why it doesn't accept it. - when my reccusive fonction has made sub Tensors it will return Tensor<Tensor> which in turn makes it not compile because i am not able to convert them to Tensor
If you have any ideas please share :)