OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Tanh.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017, Chris Rogers
3 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 * 2. Redistributions in binary form must reproduce the above copyright notice,
9 * this list of conditions and the following disclaimer in the documentation
10 * and/or other materials provided with the distribution.
11 * 3. Neither the name of STFC nor the names of its contributors may be used to
12 * endorse or promote products derived from this software without specific
13 * prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <cmath>
29
30#include "Utilities/GSLCompat.h"
31
33
34namespace endfieldmodel {
35
36 std::vector<std::vector<std::vector<int> > > Tanh::_tdi;
37
38 Tanh::Tanh(double x0, double lambda, int max_index) : _x0(x0), _lambda(lambda) {
39 setTanhDiffIndices(max_index);
40 }
41
43
44 Tanh* Tanh::clone() const { return new Tanh(*this); }
45
46 double Tanh::getTanh(double x, int n) const {
47 if (n == 0) return tanh((x + _x0) / _lambda);
48 double t = 0;
49 double lam_n = gsl_sf_pow_int(_lambda, n);
50 double tanh_x = tanh((x + _x0) / _lambda);
51 for (size_t i = 0; i < _tdi[n].size(); i++)
52 t += 1. / lam_n * static_cast<double>(_tdi[n][i][0])
53 * gsl_sf_pow_int(tanh_x, _tdi[n][i][1]);
54 return t;
55 }
56
57 double Tanh::getNegTanh(double x, int n) const {
58 if (n == 0) return tanh((x - _x0) / _lambda);
59 double t = 0;
60 double lam_n = gsl_sf_pow_int(_lambda, n);
61 double tanh_x = tanh((x - _x0) / _lambda);
62 for (size_t i = 0; i < _tdi[n].size(); i++)
63 t += 1. / lam_n * static_cast<double>(_tdi[n][i][0])
64 * gsl_sf_pow_int(tanh_x, _tdi[n][i][1]);
65 return t;
66 }
67
68 double Tanh::function(double x, int n) const { return (getTanh(x, n) - getNegTanh(x, n)) / 2.; }
69
71
72 void Tanh::setTanhDiffIndices(size_t n) {
73 _tdi.reserve(n + 1);
74 if (_tdi.size() == 0) {
75 _tdi.push_back(std::vector<std::vector<int> >(1, std::vector<int>(2)));
76 _tdi[0][0][0] = 1; // 1*tanh(x) - third index is redundant
77 _tdi[0][0][1] = 1;
78 }
79 for (size_t i = _tdi.size(); i < n + 1; ++i) {
80 _tdi.push_back(std::vector<std::vector<int> >());
81 for (size_t j = 0; j < _tdi[i - 1].size(); ++j) {
82 int value = _tdi[i - 1][j][1];
83 if (value != 0) {
84 std::vector<int> new_vec(_tdi[i - 1][j]);
85 new_vec[0] *= value;
86 new_vec[1] -= 1;
87 _tdi[i].push_back(new_vec);
88 std::vector<int> new_vec2(_tdi[i - 1][j]);
89 new_vec2[0] *= -value;
90 new_vec2[1] += 1;
91 _tdi[i].push_back(new_vec2);
92 }
93 }
94 _tdi[i] = CompactVector(_tdi[i]);
95 }
96 }
97
98 std::vector<std::vector<int> > Tanh::getTanhDiffIndices(size_t n) {
100 return _tdi[n];
101 }
102
103 void Tanh::rescale(double scaleFactor) {
104 _x0 *= scaleFactor;
105 _lambda *= scaleFactor;
106 }
107
108 std::ostream& Tanh::print(std::ostream& out) const {
109 out << "Tanh model with centre length: " << _x0 << " end length: " << _lambda;
110 return out;
111 }
112} // namespace endfieldmodel
double gsl_sf_pow_int(double x, int n)
Integer power .
Definition GSLCompat.h:57
virtual void setMaximumDerivative(size_t n)
Definition Tanh.cpp:70
static void setTanhDiffIndices(size_t n)
Definition Tanh.cpp:72
double function(double x, int n) const
Definition Tanh.cpp:68
double getNegTanh(double x, int n) const
Definition Tanh.cpp:57
std::ostream & print(std::ostream &out) const
Definition Tanh.cpp:108
static std::vector< std::vector< int > > getTanhDiffIndices(size_t n)
Definition Tanh.cpp:98
void rescale(double scaleFactor)
Definition Tanh.cpp:103
Tanh * clone() const
Definition Tanh.cpp:44
static std::vector< std::vector< std::vector< int > > > _tdi
Definition Tanh.h:125
double getTanh(double x, int n) const
Definition Tanh.cpp:46
std::vector< std::vector< int > > CompactVector(std::vector< std::vector< int > > vec)