LA_library/tensor.h

165 lines
5.2 KiB
C
Raw Normal View History

2024-03-21 23:24:21 +01:00
/*
LA: linear algebra C++ interface library
Copyright (C) 2024 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
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 <http://www.gnu.org/licenses/>.
*/
2024-03-26 17:49:09 +01:00
//a simple tensor class with arbitrary symmetry of index subgroups
2024-03-21 23:24:21 +01:00
//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 <stdint.h>
2024-03-26 17:49:09 +01:00
#include <cstdarg>
2024-03-21 23:24:21 +01:00
#include "vec.h"
#include "miscfunc.h"
namespace LA {
2024-04-02 17:55:07 +02:00
template<typename T>
class Signedpointer
{
T *ptr;
int sgn;
public:
2024-04-03 18:43:55 +02:00
Signedpointer(T *p, int s) : ptr(p),sgn(s) {};
T& operator=(const T rhs) {if(sgn==0) return *ptr; if(sgn>0) *ptr=rhs; else *ptr = -rhs; return *ptr;}
2024-04-02 17:55:07 +02:00
//@@@@@@operations on singedpointer as LHS of the non-const tensor.operator() expressions
};
2024-03-21 23:24:21 +01:00
typedef int LA_index;
typedef int LA_largeindex;
typedef class indexgroup {
2024-04-03 18:43:55 +02:00
public:
2024-03-21 23:24:21 +01:00
int number; //number of indices
int symmetry; //-1 0 or 1
LA_index offset; //indices start at
2024-04-02 17:55:07 +02:00
LA_index range; //indices span this range
2024-03-21 23:24:21 +01:00
} INDEXGROUP;
2024-04-03 18:43:55 +02:00
template<>
class LA_traits<indexgroup> {
public:
static bool is_plaindata() {return true;};
static void copyonwrite(indexgroup& x) {};
typedef INDEXGROUP normtype;
};
2024-03-26 17:49:09 +01:00
typedef NRVec<LA_index> FLATINDEX; //all indices but in a single vector
typedef NRVec<NRVec<LA_index> > SUPERINDEX; //all indices in the INDEXGROUP structure
2024-03-21 23:24:21 +01:00
template<typename T>
class Tensor {
2024-04-02 17:55:07 +02:00
//essential data
2024-03-21 23:24:21 +01:00
NRVec<indexgroup> shape;
NRVec<T> data;
2024-04-02 17:55:07 +02:00
//redundant data to facilitate efficient indexing
NRVec<LA_largeindex> cumsizes; //cumulative sizes of symmetry index groups
2024-03-26 17:49:09 +01:00
private:
2024-04-03 18:43:55 +02:00
LA_largeindex index(int *sign, const SUPERINDEX &I) const; //map the tensor indices to the position in data
LA_largeindex index(int *sign, const FLATINDEX &I) const; //map the tensor indices to the position in data
LA_largeindex vindex(int *sign, int i1, va_list args) const; //map list of indices to the position in data @@@must call va_end
2024-03-26 17:49:09 +01:00
2024-03-21 23:24:21 +01:00
public:
2024-04-03 18:43:55 +02:00
//constructors
2024-03-21 23:24:21 +01:00
Tensor() {};
2024-04-03 18:43:55 +02:00
Tensor(const NRVec<indexgroup> &s) : shape(s), data((int)getsize()) {data.clear();}; //general tensor
Tensor(const indexgroup &g) {shape.resize(1); shape[0]=g; data.resize(getsize()); data.clear();}; //tensor with a single index group
2024-04-02 17:55:07 +02:00
int getrank() const; //is computed from shape
LA_largeindex getsize(); //set redundant data and return total size
2024-04-03 18:43:55 +02:00
LA_largeindex size() const {return data.size();};
2024-03-21 23:24:21 +01:00
void copyonwrite() {shape.copyonwrite(); data.copyonwrite();};
2024-04-03 18:43:55 +02:00
inline Signedpointer<T> operator[](const SUPERINDEX &I) {int sign; LA_largeindex i=index(&sign,I); return Signedpointer<T>(&data[i],sign);};
inline T operator()(const SUPERINDEX &I) {int sign; LA_largeindex i=index(&sign,I); if(sign==0) return 0; return sign>0 ?data[i] : -data[i];};
inline Signedpointer<T> operator[](const FLATINDEX &I) {int sign; LA_largeindex i=index(&sign,I); return Signedpointer<T>(&data[i],sign);};
inline T operator()(const FLATINDEX &I) {int sign; LA_largeindex i=index(&sign,I); if(sign==0) return 0; return sign>0 ?data[i] : -data[i];};
//inline Signedpointer<T> operator[](int i1...) {va_list args; int sign; LA_largeindex i; va_start(args,i1); i= vindex(&sign, i1,args); return Signedpointer<T>(&data[i],sign); };
//cannot have operator[] with variable number of argmuments
inline T operator()(int i1...) {va_list args; ; int sign; LA_largeindex i; va_start(args,i1); i= vindex(&sign, i1,args); if(sign==0) return 0; return sign>0 ?data[i] : -data[i];};
//@@@do a 'set' operatiaon with va_arg instead
2024-04-02 17:55:07 +02:00
//NOTE: for sign==0 data[i] can be undefined pointer, avoid dereferencing it
2024-03-26 17:49:09 +01:00
//@@@TODO - unwinding to full size in a specified index
//@@@TODO - contractions - basic and efficient
//@@@TODO get/put to file, stream i/o
2024-03-21 23:24:21 +01:00
};
2024-03-26 17:49:09 +01:00
2024-03-21 23:24:21 +01:00
template<typename T>
2024-04-02 17:55:07 +02:00
int Tensor<T>:: getrank() const
2024-03-21 23:24:21 +01:00
{
int r=0;
for(int i=0; i<shape.size(); ++i)
{
if(shape[i].number==0) laerror("empty index group");
r+=shape[i].number;
}
return r;
}
2024-04-02 17:55:07 +02:00
2024-03-21 23:24:21 +01:00
template<typename T>
2024-04-02 17:55:07 +02:00
LA_largeindex Tensor<T>::getsize()
2024-03-21 23:24:21 +01:00
{
2024-04-02 17:55:07 +02:00
cumsizes.resize(shape.size());
2024-03-21 23:24:21 +01:00
LA_largeindex s=1;
for(int i=0; i<shape.size(); ++i)
{
if(shape[i].number==0) laerror("empty index group");
2024-04-02 17:55:07 +02:00
if(shape[i].range==0) return 0;
cumsizes[i]=s;
2024-03-21 23:24:21 +01:00
switch(shape[i].symmetry)
{
case 0:
2024-04-02 17:55:07 +02:00
s *= longpow(shape[i].range,shape[i].number);
2024-03-21 23:24:21 +01:00
break;
case 1:
2024-04-02 17:55:07 +02:00
s *= simplicial(shape[i].number,shape[i].range);
2024-03-21 23:24:21 +01:00
break;
case -1:
2024-04-02 17:55:07 +02:00
s *= simplicial(shape[i].number,shape[i].range-shape[i].number+1);
2024-03-21 23:24:21 +01:00
break;
default:
laerror("illegal index group symmetry");
break;
}
}
return s;
}
}//namespace
#endif