aboutsummaryrefslogtreecommitdiffhomepage
path: root/unsupported/test/cxx11_tensor_math.cpp
blob: 82a1a26d85e1f2c57c61cb4f8744e085d559d02a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2015 Benoit Steiner <benoit.steiner.goog@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include "main.h"

#include <Eigen/CXX11/Tensor>

using Eigen::Tensor;
using Eigen::RowMajor;

static void test_tanh()
{
  Tensor<float, 1> vec1(6);
  vec1.setRandom();

  Tensor<float, 1> vec2 = vec1.tanh();

  for (int i = 0; i < 6; ++i) {
    VERIFY_IS_APPROX(vec2(i), tanhf(vec1(i)));
  }
}

static void test_sigmoid()
{
  Tensor<float, 1> vec1(6);
  vec1.setRandom();

  Tensor<float, 1> vec2 = vec1.sigmoid();

  for (int i = 0; i < 6; ++i) {
    VERIFY_IS_APPROX(vec2(i), 1.0f / (1.0f + std::exp(-vec1(i))));
  }
}


EIGEN_DECLARE_TEST(cxx11_tensor_math)
{
  CALL_SUBTEST(test_tanh());
  CALL_SUBTEST(test_sigmoid());
}