From 3f442212e03f2e8e3fb54a82d9852ec4ce39cb8f Mon Sep 17 00:00:00 2001 From: Jiri Pittner Date: Thu, 21 Mar 2024 23:24:21 +0100 Subject: [PATCH] Tensor class initial commit --- Makefile.am | 4 +- la.h | 1 + tensor.cc | 30 +++++++++++++++ tensor.h | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 tensor.cc create mode 100644 tensor.h diff --git a/Makefile.am b/Makefile.am index c2ce214..a9d896a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ lib_LTLIBRARIES = libla.la -include_HEADERS = version.h miscfunc.h simple.h vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h numbers.h bitvector.h fourindex.h la_traits.h la_random.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h polynomial.h contfrac.h graph.h reg.h regsurf.h -libla_la_SOURCES = simple.cc miscfunc.cc quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc numbers.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc polynomial.cc contfrac.cc graph.cc la_random.cc reg.cc regsurf.cc +include_HEADERS = version.h tensor.h miscfunc.h simple.h vecmat3.h quaternion.h fortran.h cuda_la.h auxstorage.h davidson.h laerror.h mat.h qsort.h vec.h bisection.h diis.h la.h noncblas.h smat.h numbers.h bitvector.h fourindex.h la_traits.h la_random.h nonclass.h sparsemat.h sparsesmat.h csrmat.h conjgrad.h gmres.h matexp.h permutation.h polynomial.h contfrac.h graph.h reg.h regsurf.h +libla_la_SOURCES = simple.cc tensor.cc miscfunc.cc quaternion.cc vecmat3.cc vec.cc mat.cc smat.cc sparsemat.cc sparsesmat.cc csrmat.cc laerror.cc noncblas.cc numbers.cc bitvector.cc strassen.cc nonclass.cc cuda_la.cc fourindex.cc permutation.cc polynomial.cc contfrac.cc graph.cc la_random.cc reg.cc regsurf.cc nodist_libla_la_SOURCES = version.cc check_PROGRAMS = t test tX test_reg test_regsurf t_SOURCES = t.cc t2.cc diff --git a/la.h b/la.h index 2b85a74..ac7beb0 100644 --- a/la.h +++ b/la.h @@ -45,6 +45,7 @@ #include "vec.h" #include "polynomial.h" #include "contfrac.h" +#include "tensor.h" #include "version.h" using namespace LA; diff --git a/tensor.cc b/tensor.cc new file mode 100644 index 0000000..a29d39b --- /dev/null +++ b/tensor.cc @@ -0,0 +1,30 @@ +/* + LA: linear algebra C++ interface library + Copyright (C) 2024 Jiri Pittner or + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "tensor.h" +#include "laerror.h" +#include + + +namespace LA { + + + + + +}//namespace diff --git a/tensor.h b/tensor.h new file mode 100644 index 0000000..7a908e2 --- /dev/null +++ b/tensor.h @@ -0,0 +1,103 @@ +/* + LA: linear algebra C++ interface library + Copyright (C) 2024 Jiri Pittner or + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + + +//a simple tensor class with arbitrary summetry of index subgroups +//stored in an efficient way +//presently only a rudimentary implementation +//presently limited to 2G data size due to NRVec - maybe use a typedef LA_index +//to uint64_t in the future in vector and matrix classes + + +#ifndef _TENSOR_H +#define _TENSOR_H + +#include +#include "vec.h" +#include "miscfunc.h" + + +namespace LA { + +typedef int LA_index; +typedef int LA_largeindex; + +typedef class indexgroup { +int number; //number of indices +int symmetry; //-1 0 or 1 +LA_index offset; //indices start at +LA_index size; //indices span this range +} INDEXGROUP; + +template +class Tensor { + NRVec shape; + NRVec data; + +public: + Tensor() {}; + Tensor(const NRVec &s) : shape(s), data((int)size()) {data.clear();}; + int rank() const; //is computed from shape + LA_largeindex size() const; //expensive, is computed from shape + void copyonwrite() {shape.copyonwrite(); data.copyonwrite();}; + //@@@operator() lhs and rhs both via vararg a via superindex of flat and nested types, get/put to file, stream i/o +}; + +template +int Tensor:: rank() const +{ +int r=0; +for(int i=0; i +LA_largeindex Tensor::size() const +{ +LA_largeindex s=1; +for(int i=0; i