13 #ifndef BLACKCAT_TENSORS_TENSOR_ACCESSOR_H_ 14 #define BLACKCAT_TENSORS_TENSOR_ACCESSOR_H_ 22 template<
int TensorDimension,
class ExpressionTemplate>
27 throw std::invalid_argument(
"[] not supported for scalars");
31 throw std::invalid_argument(
"() not supported for scalars");
36 template<
class ExpressionTemplate>
38 ExpressionTemplate::tensor_dim == 0,
41 ExpressionTemplate::tensor_dim,
44 template<
int TensorDimension,
class Derived>
48 static constexpr
int tensor_dim = TensorDimension;
49 Derived& derived() {
return static_cast<Derived&
>(*this); }
50 const Derived& derived()
const {
return static_cast<const Derived&
>(*this); }
63 return slice(range[0], range[1]);
67 return slice(range[0], range[1]);
72 BC_ASSERT(i >= 0 && i < derived().outer_dim(),
73 "slice index must be between 0 and outer_dim()");
74 return make_tensor(exprs::make_slice(derived(), i));
78 BC_ASSERT(i >= 0 && i < derived().outer_dim(),
79 "slice index must be between 0 and outer_dim()");
80 return make_tensor(exprs::make_slice(derived(), i));
85 BC_ASSERT(from >= 0 && to <= derived().outer_dim(),
86 "slice `from` must be between 0 and outer_dim()");
87 BC_ASSERT(to > from && to <= derived().outer_dim(),
88 "slice `to` must be between `from` and outer_dim()");
89 return make_tensor(exprs::make_ranged_slice(derived(), from, to));
94 BC_ASSERT(from >= 0 && to <= derived().outer_dim(),
95 "slice `from` must be between 0 and outer_dim()");
96 BC_ASSERT(to > from && to <= derived().outer_dim(),
97 "slice `to` must be between `from` and outer_dim()");
98 return make_tensor(exprs::make_ranged_slice(derived(), from, to));
103 BC_ASSERT(i >= 0 && i < derived().size(),
104 "Scalar index must be between 0 and size()");
105 return make_tensor(exprs::make_scalar(derived(), i));
110 BC_ASSERT(i >= 0 && i < derived().size(),
111 "Scalar index must be between 0 and size()");
112 return make_tensor(exprs::make_scalar(derived(), i));
117 static_assert(tensor_dim == 2,
118 "diagnol method is only available to matrices");
119 BC_ASSERT(index > -derived().rows() && index < derived().rows(),
120 "diagnol `index` must be -rows() and rows())");
126 static_assert(tensor_dim == 2,
127 "diagnol method is only available to matrices");
128 BC_ASSERT(index > -derived().rows() && index < derived().rows(),
129 "diagnol `index` must be -rows() and rows())");
136 return make_tensor(exprs::make_view(derived(), derived().get_shape()));
141 return make_tensor(exprs::make_view(derived(), derived().get_shape()));
146 static_assert(tensor_dim == 2,
147 "MATRIX COL ONLY AVAILABLE TO MATRICES OF ORDER 2");
153 static_assert(tensor_dim == 2,
154 "MATRIX COL ONLY AVAILABLE TO MATRICES OF ORDER 2");
160 static_assert(tensor_dim == 2,
161 "MATRIX ROW ONLY AVAILABLE TO MATRICES OF ORDER 2");
162 BC_ASSERT(index >= 0 && index < derived().rows(),
163 "Row index must be between 0 and rows()");
169 static_assert(tensor_dim == 2,
170 "MATRIX ROW ONLY AVAILABLE TO MATRICES OF ORDER 2");
171 BC_ASSERT(index >= 0 && index < derived().rows(),
172 "Row index must be between 0 and rows()");
179 "Index + Shape must be less parent shape");
180 BC_ASSERT(((index>=0).all() && (shape>=0).all()),
181 "Shape and Index must be greater than 0");
188 "Index + Shape must be less parent shape");
189 BC_ASSERT(((index>=0).all() && (shape>=0).all()),
190 "Shape and Index must be greater than 0");
196 return subblock(std::get<0>(index_shape), std::get<1>(index_shape));
201 return subblock(std::get<0>(index_shape), std::get<1>(index_shape));
210 static_assert(Derived::tensor_iterator_dim <= 1,
211 "Reshape is only available to continuous tensors");
214 "Reshape requires the new and old shape be same sizes");
216 return make_tensor(exprs::make_view(derived(), shape));
222 static_assert(Derived::tensor_iterator_dim <= 1,
223 "Reshape is only available to continuous tensors");
226 "Reshape requires the new and old shape be same sizes");
228 return make_tensor(exprs::make_view(derived(), shape));
231 template<
class... Integers>
233 return reshaped(
bc::dim(ints...));
236 template<
class... Integers>
238 return reshaped(
bc::dim(ints...));
242 return this->reshaped(derived().size());
246 return this->reshaped(derived().size());
auto shallow_copy()
Definition: tensor_accessor.h:134
auto slice(bc::size_t i)
Definition: tensor_accessor.h:70
Definition: tensor_accessor.h:25
auto subblock(Dim< tensor_dim > index, Dim< tensor_dim > shape)
Definition: tensor_accessor.h:176
auto flattened()
Definition: tensor_accessor.h:241
auto reshaped(Integers... ints) const
Definition: tensor_accessor.h:237
const auto flattened() const
Definition: tensor_accessor.h:245
auto slice(bc::size_t from, bc::size_t to) const
Definition: tensor_accessor.h:92
auto make_diagnol(Parent &parent, bc::size_t diagnol_index)
Definition: array_slice.h:98
auto make_chunk(Parent &parent, bc::Dim< Parent::tensor_dim > index_points, ShapeLike shape)
Definition: array_slice.h:203
BCINLINE auto dim(const Integers &... ints)
Definition: dim.h:336
auto scalar(bc::size_t i)
Definition: tensor_accessor.h:101
Definition: tensor_accessor.h:23
auto subblock(Dim< tensor_dim > index, Dim< tensor_dim > shape) const
Definition: tensor_accessor.h:185
auto reshaped(Integers... ints)
Definition: tensor_accessor.h:232
auto make_row(Parent &parent, bc::size_t index)
Definition: array_slice.h:86
BCINLINE value_type size() const
Definition: dim.h:28
void operator[](int index) const
Definition: tensor_accessor.h:26
BCINLINE Dim reversed() const
Definition: dim.h:257
int size_t
Definition: common.h:283
auto diagnol(bc::size_t index=0) const
Definition: tensor_accessor.h:124
auto row(bc::size_t index) const
Definition: tensor_accessor.h:167
std::conditional_t< ExpressionTemplate::tensor_dim==0, Scalar_Accessor, Tensor_Accessor_Base< ExpressionTemplate::tensor_dim, Tensor_Base< ExpressionTemplate > >> Tensor_Accessor
Definition: tensor_accessor.h:42
auto row(bc::size_t index)
Definition: tensor_accessor.h:158
auto scalar(bc::size_t i) const
Definition: tensor_accessor.h:108
auto reshaped(bc::Dim< X > shape)
Definition: tensor_accessor.h:208
auto slice(bc::size_t from, bc::size_t to)
Definition: tensor_accessor.h:83
auto reshaped(bc::Dim< X > shape) const
Definition: tensor_accessor.h:220
auto range(T begin, T end=T())
Definition: io.h:46
#define BC_ASSERT(condition, message)
Definition: common.h:185
BCINLINE auto shape(Integers... ints)
Definition: shape.h:264
auto slice(bc::size_t i) const
Definition: tensor_accessor.h:77
auto diagnol(bc::size_t index=0)
Definition: tensor_accessor.h:115
auto col(bc::size_t i) const
Definition: tensor_accessor.h:151
auto shallow_copy() const
Definition: tensor_accessor.h:139
auto make_tensor(ExpressionTemplate expression)
Definition: common.h:29
auto col(bc::size_t i)
Definition: tensor_accessor.h:144
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22
void operator()(int index) const
Definition: tensor_accessor.h:30