Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
functions.hpp
Go to the documentation of this file.
1// Copyright 2025 Eric Hermosis
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15
16#ifndef FUNCTIONS_HPP
17#define FUNCTIONS_HPP
18
47#include "concepts.hpp"
48#include "traits.hpp"
49#include "shape.hpp"
50#include "strides.hpp"
51#include "tensor.hpp"
52
53namespace tannic::function {
64template<Functional Functor, Expression Argument>
65class Function {
66public:
67 Functor functor;
69
78 {}
79
89 constexpr type dtype() const {
90 return argument.dtype();
91 }
92
100 constexpr Shape const& shape() const {
101 return argument.shape();
102 }
103
112 constexpr Strides const& strides() const {
113 return argument.strides();
114 }
115
123 auto offset() const {
124 return argument.offset();
125 }
126
127 Tensor forward() const {
128 Tensor source = argument.forward();
129 Tensor target(dtype(), shape(), strides(), offset());
130 functor(source, target);
131 return target;
132 }
133};
134
135
140struct Log {
141 void operator()(Tensor const&, Tensor&) const;
142};
143
148struct Exp {
149 void operator()(Tensor const&, Tensor&) const;
150};
151
156struct Sqrt {
157 void operator()(Tensor const&, Tensor&) const;
158};
159
164struct Rsqrt {
165 float epsilon = 0.0f;
166 void operator()(Tensor const&, Tensor&) const;
167};
168
169
174struct Abs {
175 void operator()(Tensor const&, Tensor&) const;
176};
177
182struct Sin {
183 void operator()(Tensor const&, Tensor&) const;
184};
185
190struct Cos {
191 void operator()(Tensor const&, Tensor&) const;
192};
193
198struct Tan {
199 void operator()(Tensor const&, Tensor&) const;
200};
201
206struct Sinh {
207 void operator()(Tensor const&, Tensor&) const;
208};
209
214struct Cosh {
215 void operator()(Tensor const&, Tensor&) const;
216};
217
222struct Tanh {
223 void operator()(Tensor const&, Tensor&) const;
224};
225
234template<Expression Operand>
235constexpr auto log(Operand&& operand) {
236 return Function<Log, Operand>({}, std::forward<Operand>(operand));
237}
238
246template<Expression Operand>
247constexpr auto exp(Operand&& operand) {
248 return Function<Exp, Operand>({}, std::forward<Operand>(operand));
249}
250
259template<Expression Operand>
260constexpr auto sqrt(Operand&& operand) {
261 return Function<Sqrt, Operand>({}, std::forward<Operand>(operand));
262}
263
272template<Expression Operand>
273constexpr auto rsqrt(Operand&& operand, float epsilon = 0.0f) {
274 return Function<Rsqrt, Operand>({epsilon}, std::forward<Operand>(operand));
275}
276
284template<Expression Operand>
285constexpr auto abs(Operand&& operand) {
286 return Function<Abs, Operand>({}, std::forward<Operand>(operand));
287}
288
296template<Expression Operand>
297constexpr auto sin(Operand&& operand) {
298 return Function<Sin, Operand>({}, std::forward<Operand>(operand));
299}
300
308template<Expression Operand>
309constexpr auto cos(Operand&& operand) {
310 return Function<Cos, Operand>({}, std::forward<Operand>(operand));
311}
312
321template<Expression Operand>
322constexpr auto tan(Operand&& operand) {
323 return Function<Tan, Operand>({}, std::forward<Operand>(operand));
324}
325
333template<Expression Operand>
334constexpr auto sinh(Operand&& operand) {
335 return Function<Sinh, Operand>({}, std::forward<Operand>(operand));
336}
337
345template<Expression Operand>
346constexpr auto cosh(Operand&& operand) {
347 return Function<Cosh, Operand>({}, std::forward<Operand>(operand));
348}
349
357template<Expression Operand>
358constexpr auto tanh(Operand&& operand) {
359 return Function<Tanh, Operand>({}, std::forward<Operand>(operand));
360}
361
362} namespace tannic {
363
364using function::log;
365using function::exp;
366using function::sqrt;
367using function::rsqrt;
368using function::abs;
369using function::sin;
370using function::cos;
371using function::tan;
372using function::sinh;
373using function::cosh;
374using function::tanh;
375
376} //namespace tannic
377
378#endif // FUNCTIONS_HPP
Represents the shape (dimensions) of an tensor-like expression.
Definition: shape.hpp:79
Represents the memory strides associated with a tensor shape.
Definition: strides.hpp:87
A multidimensional, strided tensor data structure.
Definition: tensor.hpp:99
Expression template for mathematical function operations.
Definition: functions.hpp:65
constexpr Shape const & shape() const
Returns the shape of the result.
Definition: functions.hpp:100
constexpr Function(Functor functor, typename Trait< Argument >::Reference argument)
Constructs a Function expression.
Definition: functions.hpp:75
Tensor forward() const
Definition: functions.hpp:127
constexpr type dtype() const
Returns the data type of the result.
Definition: functions.hpp:89
Trait< Argument >::Reference argument
Definition: functions.hpp:68
Functor functor
Definition: functions.hpp:67
auto offset() const
Returns the offset of the result.
Definition: functions.hpp:123
constexpr Strides const & strides() const
Returns the strides of the result.
Definition: functions.hpp:112
Definition: functions.hpp:53
constexpr auto exp(Operand &&operand)
Creates a lazy-evaluated exponential function expression.
Definition: functions.hpp:247
constexpr auto tanh(Operand &&operand)
Creates a lazy-evaluated hyperbolic tangent expression.
Definition: functions.hpp:358
constexpr auto cos(Operand &&operand)
Creates a lazy-evaluated cosine function expression.
Definition: functions.hpp:309
constexpr auto sqrt(Operand &&operand)
Creates a lazy-evaluated square root expression.
Definition: functions.hpp:260
constexpr auto tan(Operand &&operand)
Creates a lazy-evaluated tangent function expression.
Definition: functions.hpp:322
constexpr auto sin(Operand &&operand)
Creates a lazy-evaluated sine function expression.
Definition: functions.hpp:297
constexpr auto cosh(Operand &&operand)
Creates a lazy-evaluated hyperbolic cosine expression.
Definition: functions.hpp:346
constexpr auto log(Operand &&operand)
Creates a lazy-evaluated natural logarithm expression.
Definition: functions.hpp:235
constexpr auto abs(Operand &&operand)
Creates a lazy-evaluated absolute value expression.
Definition: functions.hpp:285
constexpr auto rsqrt(Operand &&operand, float epsilon=0.0f)
Creates a lazy-evaluated inverse square root expression.
Definition: functions.hpp:273
constexpr auto sinh(Operand &&operand)
Creates a lazy-evaluated hyperbolic sine expression.
Definition: functions.hpp:334
Definition: buffer.hpp:41
std::decay_t< T > Reference
Definition: traits.hpp:28
Functor absolute value (|x|) Applies element-wise absolute value to tensor elements.
Definition: functions.hpp:174
void operator()(Tensor const &, Tensor &) const
Functor cosine Applies element-wise cosine to tensor elements (radians)
Definition: functions.hpp:190
void operator()(Tensor const &, Tensor &) const
Functor hyperbolic cosine Applies element-wise hyperbolic cosine to tensor elements.
Definition: functions.hpp:214
void operator()(Tensor const &, Tensor &) const
Functor exponential (e^x) Applies element-wise exponential to tensor elements.
Definition: functions.hpp:148
void operator()(Tensor const &, Tensor &) const
Functor natural logarithm (ln(x)) Applies element-wise natural logarithm to tensor elements.
Definition: functions.hpp:140
void operator()(Tensor const &, Tensor &) const
Functor inverse square root (1/√x) Applies element-wise inverse square root to tensor elements.
Definition: functions.hpp:164
void operator()(Tensor const &, Tensor &) const
float epsilon
Definition: functions.hpp:165
Functor sine Applies element-wise sine to tensor elements (radians)
Definition: functions.hpp:182
void operator()(Tensor const &, Tensor &) const
Functor hyperbolic sine Applies element-wise hyperbolic sine to tensor elements.
Definition: functions.hpp:206
void operator()(Tensor const &, Tensor &) const
Functor square root (√x) Applies element-wise square root to tensor elements.
Definition: functions.hpp:156
void operator()(Tensor const &, Tensor &) const
Functor tangent Applies element-wise tangent to tensor elements (radians)
Definition: functions.hpp:198
void operator()(Tensor const &, Tensor &) const
Functor hyperbolic tangent Applies element-wise hyperbolic tangent to tensor elements.
Definition: functions.hpp:222
void operator()(Tensor const &, Tensor &) const