Hi I wrote this multidimensional matrix library (its not full fledged) it supports basic operations like dotproduct and pointwise elements.
https://github.com/josephjaspers/BlackCat_Tensors
Tensor<float> tensor3 = {3, 4, 5}; -- generates a 3 dimensional tensor
tensor3[1] = 3; -- returns the second matrix and sets all the values to 3.
tensor3[1][2]; -- returns the second matrx and then then 3rd column of that matrix
tensor3({1,2,3},{2,2}); -- at index 1,2,3, returns a sub-matrix of dimensions 2x2
All of the accessor operators [] (int index) and ({initializer_list<int> index},{initializer_list<int> shape}) return seperate tensors but they all refer to the same internal array. Therefor you can modify the original tensor from these sub_tensors.
All the data is allocated on a single array. If you want to use dotproduct you need to link it to BLAS.
Here's the header file, it details most of the methods.
https://github.com/josephjaspers/BlackCat_Tensors/blob/master/BC_Headers/Tensor.h
In the comments you wanted to elaborate upon accessing using proxy objects
Matrix/Vector
template<typename T>
struct Vector {
T* data; int sz;
Vector<T>(T* data, int size){
this->data = data;
this->sz = size;
}
T& operator[] (int index) { return data[i]; }
}
template<typename T>
struct Matrix {
int row; int col;
//insert constructor here
Vector<T> operator[] (int index) {
return Vector<T>(data[index * row], row); //use col for col-major
}
}
//Recursive method
template class T
class Tensor {
int* shape;
T* data;
Tensor<T>(int*shape, int order, T* data){
this->shape = shape;
this->order = order;
this->data = data;
}
Tensor<T> operator [] (int index) {
return Tensor(shape, order -1, &data[index * shape[order - 2]); // if the order = 2(tensor is a matrix) multiply the `data` index by given param * the number of rows --> ergo returns a column vector)
}
}