Tenncor
constant.hpp
Go to the documentation of this file.
1 
9 #include "tag/prop.hpp"
10 
11 #include "eteq/ileaf.hpp"
12 #include "eteq/inode.hpp"
13 
14 #ifndef ETEQ_CONSTANT_HPP
15 #define ETEQ_CONSTANT_HPP
16 
17 namespace eteq
18 {
19 
20 static const size_t label_limit = 5;
21 
23 template <typename T>
24 struct Constant final : public iLeaf<T>
25 {
28  static Constant<T>* get (T* data, teq::Shape shape);
29 
31  static Constant<T>* get_scalar (T scalar, teq::Shape shape)
32  {
33  size_t n = shape.n_elems();
34  T buffer[n];
35  std::fill(buffer, buffer + n, scalar);
36  return Constant<T>::get(buffer, shape);
37  }
38 
39  Constant (const Constant<T>& other) = delete;
40 
41  Constant (Constant<T>&& other) = delete;
42 
43  Constant<T>& operator = (const Constant<T>& other) = delete;
44 
45  Constant<T>& operator = (Constant<T>&& other) = delete;
46 
48  std::string to_string (void) const override
49  {
50  const T* data = this->data_.data();
51  if (is_scalar())
52  {
53  if (0 == data[0]) // prevent -0
54  {
55  return "0";
56  }
57  return fmts::to_string(data[0]);
58  }
59  size_t nelems = this->shape_.n_elems();
60  auto out = fmts::to_string(data,
61  data + std::min(label_limit, nelems));
62  if (nelems > label_limit)
63  {
64  out += "...";
65  }
66  return out;
67  }
68 
70  bool is_const (void) const override
71  {
72  return true;
73  }
74 
76  bool is_scalar (void) const
77  {
78  const T* data = this->data_.data();
79  size_t nelems = this->shape_.n_elems();
80  return std::all_of(data + 1, data + nelems,
81  [&](const T& e) { return e == data[0]; });
82  }
83 
84 private:
86  iLeaf<T>(data, shape) {}
87 };
88 
90 template <typename T>
91 struct ConstantNode final : public iNode<T>
92 {
93  ConstantNode (std::shared_ptr<Constant<T>> cst) : cst_(cst) {}
94 
96  ConstantNode<T>* clone (void) const
97  {
98  return static_cast<ConstantNode<T>*>(clone_impl());
99  }
100 
102  T* data (void) override
103  {
104  return (T*) cst_->data();
105  }
106 
108  void update (void) override {}
109 
111  teq::TensptrT get_tensor (void) const override
112  {
113  return cst_;
114  }
115 
116 protected:
117  iNode<T>* clone_impl (void) const override
118  {
119  teq::Shape shape = cst_->shape();
120  const T* d = (const T*) cst_->data();
121  std::vector<T> cpy(d, d + shape.n_elems());
122  return new ConstantNode(std::shared_ptr<Constant<T>>(
123  Constant<T>::get(cpy.data(), shape)));
124  }
125 
126 private:
127  std::shared_ptr<Constant<T>> cst_;
128 };
129 
130 template <typename T>
132 {
133  static bool registered = register_builder<Constant<T>,T>(
134  [](teq::TensptrT tens)
135  {
136  return std::make_shared<ConstantNode<T>>(
137  std::static_pointer_cast<Constant<T>>(tens));
138  });
139  assert(registered);
140 
141  return new Constant(data, shape);
142 }
143 
145 template <typename T>
147 {
148  auto out = std::make_shared<ConstantNode<T>>(
149  std::shared_ptr<Constant<T>>(Constant<T>::get_scalar(scalar, shape))
150  );
152  return out;
153 }
154 
156 template <typename T>
158 {
159  auto out = std::make_shared<ConstantNode<T>>(
160  std::shared_ptr<Constant<T>>(Constant<T>::get(data, shape))
161  );
163  return out;
164 }
165 
166 }
167 
168 #endif // ETEQ_CONSTANT_HPP
T * data(void) override
Implementation of iNode<T>
Definition: constant.hpp:102
static Constant< T > * get(T *data, teq::Shape shape)
Definition: constant.hpp:131
NodeptrT< T > make_constant_scalar(T scalar, teq::Shape shape)
Return constant node given scalar and shape.
Definition: constant.hpp:146
bool is_scalar(void) const
Return true if constant data values are all the same, otherwise false.
Definition: constant.hpp:76
const teq::Shape & shape(void) const override
Implementation of iTensor.
Definition: ileaf.hpp:26
ConstantNode(std::shared_ptr< Constant< T >> cst)
Definition: constant.hpp:93
Definition: constant.hpp:17
TensorT< T > data_
Data Source.
Definition: ileaf.hpp:67
std::shared_ptr< Constant< T > > cst_
Definition: constant.hpp:127
Definition: shape.hpp:62
Interface node for wrapping typed tensor.
Definition: inode.hpp:23
static Constant< T > * get_scalar(T scalar, teq::Shape shape)
Return Constant tensor containing scalar expanded to fill shape.
Definition: constant.hpp:31
Constant(T *data, teq::Shape shape)
Definition: constant.hpp:85
Constant&#39;s node wrapper.
Definition: constant.hpp:91
teq::Shape shape_
Shape utility to avoid excessive conversion between data_.dimensions()
Definition: ileaf.hpp:70
teq::Shape shape(void)
Return shape of internal tensor.
Definition: inode.hpp:37
void property_tag(teq::TensrefT tens, std::string property)
Associate property with tensor.
Definition: prop.hpp:57
Constant(const Constant< T > &other)=delete
bool is_const(void) const override
Implementation of iLeaf.
Definition: constant.hpp:70
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
std::string to_string(teq::CoordptrT c)
Return brief hashable string representation of coordinate mapper.
Constant implementation of Eigen leaf tensor.
Definition: constant.hpp:24
std::string to_string(void) const override
Implementation of iTensor.
Definition: constant.hpp:48
EigenptrT< T > min(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:939
PropertyRegistry & get_property_reg(void)
Return reference to global property registry.
const std::string immutable_tag
Identifier for immutable property.
Definition: prop.hpp:20
std::shared_ptr< iNode< T > > NodeptrT
Smart pointer of node.
Definition: inode.hpp:63
void update(void) override
Implementation of iNode<T>
Definition: constant.hpp:108
Constant< T > & operator=(const Constant< T > &other)=delete
iLeaf extension of TEQ iLeaf containing Eigen data objects
Definition: ileaf.hpp:21
static const size_t label_limit
Definition: constant.hpp:20
NodeptrT< T > make_constant(T *data, teq::Shape shape)
Return constant node given raw array and shape.
Definition: constant.hpp:157
teq::TensptrT get_tensor(void) const override
Implementation of iNode<T>
Definition: constant.hpp:111
NElemT n_elems(void) const
Return the total number of elements represented by the shape.
Definition: shape.hpp:118
ConstantNode< T > * clone(void) const
Return deep copy of this instance (with a copied constant)
Definition: constant.hpp:96
void * data(void) override
Implementation of iData.
Definition: ileaf.hpp:32
iNode< T > * clone_impl(void) const override
Definition: constant.hpp:117