tensor.hpp 5.93 KB
Newer Older
1
/** ExaTN::Numerics: Abstract Tensor
2
REVISION: 2019/06/05
3

4
5
Copyright (C) 2018-2019 Dmitry I. Lyakh (Liakh)
Copyright (C) 2018-2019 Oak Ridge National Laboratory (UT-Battelle) **/
6

7
8
9
/** NOTES:
 Tensor specification requires:
 (a) Symbolic tensor name;
10
11
 (b) Tensor rank (number of tensor dimensions) and
     tensor shape (extents of all tensor dimensions);
12
 (c) Optional tensor signature (space/subspace identifier for all tensor dimensions).
13

14
 Tensor signature identifies a full tensor or its slice. Tensor signature
15
16
17
 requires providing a pair<SpaceId,SubspaceId> for each tensor dimension.
 It has two alternative specifications:
 (a) SpaceId = SOME_SPACE: In this case, SubspaceId is the lower bound
18
     of the specified tensor slice (0 is the min lower bound). The upper
19
     bound is computed by adding the dimension extent to the lower bound - 1.
20
     The defining vector space (SOME_SPACE) is an abstract anonymous vector space.
21
22
 (b) SpaceId != SOME_SPACE: In this case, SpaceId refers to a registered
     vector space and the SubspaceId refers to a registered subspace of
23
24
25
     this vector space. The subspaces will carry lower/upper bounds of
     the specified tensor slice. SubspaceId = 0 refers to the full space,
     which is automatically registered when the space is registered.
26
27
     Although tensor dimension extents cannot exceed the dimensions
     of the corresponding registered subspaces from the tensor signature,
28
     they in general can be smaller than the latter (low-rank representation).
29
30
**/

31
32
#ifndef EXATN_NUMERICS_TENSOR_HPP_
#define EXATN_NUMERICS_TENSOR_HPP_
33
34
35

#include "tensor_basic.hpp"
#include "tensor_shape.hpp"
36
#include "tensor_signature.hpp"
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

#include <assert.h>

#include <iostream>
#include <type_traits>
#include <string>
#include <initializer_list>
#include <vector>

namespace exatn{

namespace numerics{

class Tensor{
public:

 /** Create a tensor by providing its name, shape and signature. **/
 Tensor(const std::string & name,           //tensor name
        const TensorShape & shape,          //tensor shape
        const TensorSignature & signature); //tensor signature
 /** Create a tensor by providing its name and shape.
     The signature defaults to SOME_SPACE spaces and lbound=0 subspaces. **/
 Tensor(const std::string & name,           //tensor name
        const TensorShape & shape);         //tensor shape
 /** Create a tensor by providing its name, shape and signature from scratch. **/
 template<typename T>
 Tensor(const std::string & name,                                        //tensor name
        std::initializer_list<T> extents,                                //tensor dimension extents
        std::initializer_list<std::pair<SpaceId,SubspaceId>> subspaces); //tensor dimension defining subspaces
 template<typename T>
 Tensor(const std::string & name,                                      //tensor name
        const std::vector<T> & extents,                                //tensor dimension extents
        const std::vector<std::pair<SpaceId,SubspaceId>> & subspaces); //tensor dimension defining subspaces
 /** Create a tensor by providing its name and shape from scratch.
     The signature defaults to SOME_SPACE spaces and lbound=0 subspaces. **/
 template<typename T>
 Tensor(const std::string & name,          //tensor name
        std::initializer_list<T> extents); //tensor dimension extents
 template<typename T>
 Tensor(const std::string & name,        //tensor name
        const std::vector<T> & extents); //tensor dimension extents

 Tensor(const Tensor & tensor) = default;
 Tensor & operator=(const Tensor & tensor) = default;
81
82
 Tensor(Tensor && tensor) noexcept = default;
 Tensor & operator=(Tensor && tensor) noexcept = default;
83
84
85
86
87
 virtual ~Tensor() = default;

 /** Print. **/
 void printIt() const;

88
89
 /** Get the tensor rank (order). **/
 unsigned int getRank() const;
90
91
92
93
 /** Get the tensor shape. **/
 const TensorShape & getShape() const;
 /** Get the tensor signature. **/
 const TensorSignature & getSignature() const;
94
95
96

 /** Get the extent of a specific tensor dimension. **/
 DimExtent getDimExtent(unsigned int dim_id) const;
97
 /** Get the extents of all tensor dimensions. **/
98
 const std::vector<DimExtent> & getDimExtents() const;
99
100
101
102
103
104

 /** Get the space/subspace id for a specific tensor dimension. **/
 SpaceId getDimSpaceId(unsigned int dim_id) const;
 SubspaceId getDimSubspaceId(unsigned int dim_id) const;
 std::pair<SpaceId,SubspaceId> getDimSpaceAttr(unsigned int dim_id) const;

105
106
107
 /** Get the unique integer tensor id. **/
 std::size_t getTensorId() const;

108
109
private:

110
111
112
 std::string name_;          //tensor name
 TensorShape shape_;         //tensor shape
 TensorSignature signature_; //tensor signature
113
114
115
116
117
118
119
120
121
122
};


//TEMPLATES:
template<typename T>
Tensor::Tensor(const std::string & name,
               std::initializer_list<T> extents,
               std::initializer_list<std::pair<SpaceId,SubspaceId>> subspaces):
name_(name), shape_(extents), signature_(subspaces)
{
123
 //DEBUG:
124
125
126
127
128
129
130
131
132
133
 if(signature_.getRank() != shape_.getRank()) std::cout << "ERROR(Tensor::Tensor): Signature/Shape size mismatch!" << std::endl;
 assert(signature_.getRank() == shape_.getRank());
}

template<typename T>
Tensor::Tensor(const std::string & name,
               const std::vector<T> & extents,
               const std::vector<std::pair<SpaceId,SubspaceId>> & subspaces):
name_(name), shape_(extents), signature_(subspaces)
{
134
 //DEBUG:
135
136
137
138
139
140
141
 if(signature_.getRank() != shape_.getRank()) std::cout << "ERROR(Tensor::Tensor): Signature/Shape size mismatch!" << std::endl;
 assert(signature_.getRank() == shape_.getRank());
}

template<typename T>
Tensor::Tensor(const std::string & name,
               std::initializer_list<T> extents):
142
name_(name), shape_(extents), signature_(static_cast<unsigned int>(extents.size()))
143
144
145
146
{
}

template<typename T>
Dmitry I. Lyakh's avatar
Dmitry I. Lyakh committed
147
148
Tensor::Tensor(const std::string & name,
               const std::vector<T> & extents):
149
name_(name), shape_(extents), signature_(static_cast<unsigned int>(extents.size()))
150
151
152
153
154
155
156
{
}

} //namespace numerics

} //namespace exatn

157
#endif //EXATN_NUMERICS_TENSOR_HPP_