BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
expression_unary.h
Go to the documentation of this file.
1 /* Project: BlackCat_Tensors
2  * Author: JosephJaspers
3  * Copyright 2018
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 
9 #ifndef BC_EXPRESSION_TEMPLATES_EXPRESSION_UNARY_H_
10 #define BC_EXPRESSION_TEMPLATES_EXPRESSION_UNARY_H_
11 
13 
14 namespace bc {
15 namespace tensors {
16 namespace exprs {
17 
18 template<class Operation, class ArrayType>
19 struct Un_Op:
20  Expression_Base<Un_Op<Operation, ArrayType>>,
21  Operation {
22 
23  using return_type = decltype(std::declval<Operation>()(
24  std::declval<typename ArrayType::value_type>()));
25 
26  using value_type = std::decay_t<return_type>;
27  using system_tag = typename ArrayType::system_tag;
28 
29  static constexpr int tensor_dim = ArrayType::tensor_dim;
30  static constexpr int tensor_iterator_dim = ArrayType::tensor_iterator_dim;
31 
32  ArrayType array;
33 
34  BCINLINE
35  const Operation& get_operation() const {
36  return static_cast<const Operation&>(*this);
37  }
38 
39  template<class... args> BCINLINE
40  Un_Op(ArrayType v, const args&... args_):
41  Operation(args_...),
42  array(v) {}
43 
44  template<class... integers> BCINLINE
45  value_type operator ()(integers... index) const {
46  return Operation::operator()(array(index...));
47  }
48 
49  BCINLINE
50  value_type operator [](int index) const {
51  return Operation::operator()(array[index]);
52  }
53 
54  template<class... integers> BCINLINE
55  value_type operator ()(integers... index) {
56  return Operation::operator()(array(index...));
57  }
58 
59  BCINLINE
60  value_type operator [](int index) {
61  return Operation::operator()(array[index]);
62  }
63 
64  BCINLINE const auto inner_shape() const { return array.inner_shape(); }
65  BCINLINE bc::size_t size() const { return array.size(); }
66  BCINLINE bc::size_t rows() const { return array.rows(); }
67  BCINLINE bc::size_t cols() const { return array.cols(); }
68  BCINLINE bc::size_t dim(int i) const { return array.dim(i); }
69 };
70 
71 
72 template<class Operation, class Expression> BCHOT
73 auto make_un_expr(Expression expression, Operation operation=Operation()) {
74  return Un_Op<Operation, Expression>(expression, operation);
75 }
76 
77 } //ns BC
78 } //ns exprs
79 } //ns tensors
80 
81 
82 #endif /* EXPRESSION_UNARY_POINTWISE_CU_ */
#define BCINLINE
Definition: common.h:96
BCINLINE bc::size_t dim(int i) const
Definition: expression_unary.h:68
BCHOT auto make_un_expr(Expression expression, Operation operation=Operation())
Definition: expression_unary.h:73
BCINLINE const Operation & get_operation() const
Definition: expression_unary.h:35
std::decay_t< return_type > value_type
Definition: expression_unary.h:26
decltype(std::declval< Operation >()(std::declval< typename ArrayType::value_type >())) return_type
Definition: expression_unary.h:24
BCINLINE Un_Op(ArrayType v, const args &... args_)
Definition: expression_unary.h:40
BCINLINE const auto inner_shape() const
Definition: expression_unary.h:64
BCINLINE value_type operator()(integers... index) const
Definition: expression_unary.h:45
BCINLINE bc::size_t rows() const
Definition: expression_unary.h:66
BCINLINE value_type operator[](int index) const
Definition: expression_unary.h:50
ArrayType array
Definition: expression_unary.h:32
int size_t
Definition: common.h:283
static constexpr int tensor_iterator_dim
Definition: expression_unary.h:30
static constexpr int tensor_dim
Definition: expression_unary.h:29
typename ArrayType::system_tag system_tag
Definition: expression_unary.h:27
BCINLINE bc::size_t cols() const
Definition: expression_unary.h:67
BCINLINE bc::size_t size() const
Definition: expression_unary.h:65
#define BCHOT
Definition: common.h:97
Definition: expression_template_traits.h:19
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22