OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
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 "gsl/gsl_sf_gamma.h"
31#include "gsl/gsl_sf_pow_int.h"
32
34
35namespace endfieldmodel {
36
37std::vector< std::vector< std::vector<int> > > Tanh::_tdi;
38
39Tanh::Tanh(double x0, double lambda, int max_index) : _x0(x0), _lambda(lambda) {
40 setTanhDiffIndices(max_index);
41}
42
44
45Tanh* Tanh::clone() const {
46 return new Tanh(*this);
47}
48
49
50double Tanh::getTanh(double x, int n) const {
51 if (n == 0) return tanh((x+_x0)/_lambda);
52 double t = 0;
53 double lam_n = gsl_sf_pow_int(_lambda, n);
54 double tanh_x = tanh((x+_x0)/_lambda);
55 for (size_t i = 0; i < _tdi[n].size(); i++)
56 t += 1./lam_n*static_cast<double>(_tdi[n][i][0])
57 *gsl_sf_pow_int(tanh_x, _tdi[n][i][1]);
58 return t;
59}
60
61double Tanh::getNegTanh(double x, int n) const {
62 if (n == 0) return tanh((x-_x0)/_lambda);
63 double t = 0;
64 double lam_n = gsl_sf_pow_int(_lambda, n);
65 double tanh_x = tanh((x-_x0)/_lambda);
66 for (size_t i = 0; i < _tdi[n].size(); i++)
67 t += 1./lam_n*static_cast<double>(_tdi[n][i][0])
68 *gsl_sf_pow_int(tanh_x, _tdi[n][i][1]);
69 return t;
70}
71
72double Tanh::function(double x, int n) const {
73 return (getTanh(x, n)-getNegTanh(x, n))/2.;
74}
75
78}
79
80
82 _tdi.reserve(n+1);
83 if (_tdi.size() == 0) {
84 _tdi.push_back(std::vector< std::vector<int> >(1, std::vector<int>(2)));
85 _tdi[0][0][0] = 1; // 1*tanh(x) - third index is redundant
86 _tdi[0][0][1] = 1;
87 }
88 for (size_t i = _tdi.size(); i < n+1; ++i) {
89 _tdi.push_back(std::vector< std::vector<int> >());
90 for (size_t j = 0; j < _tdi[i-1].size(); ++j) {
91 int value = _tdi[i-1][j][1];
92 if (value != 0) {
93 std::vector<int> new_vec(_tdi[i-1][j]);
94 new_vec[0] *= value;
95 new_vec[1] -= 1;
96 _tdi[i].push_back(new_vec);
97 std::vector<int> new_vec2(_tdi[i-1][j]);
98 new_vec2[0] *= -value;
99 new_vec2[1] += 1;
100 _tdi[i].push_back(new_vec2);
101 }
102 }
103 _tdi[i] = CompactVector(_tdi[i]);
104 }
105}
106
107std::vector< std::vector<int> > Tanh::getTanhDiffIndices(size_t n) {
109 return _tdi[n];
110}
111
112void Tanh::rescale(double scaleFactor) {
113 _x0 *= scaleFactor;
114 _lambda *= scaleFactor;
115}
116
117
118
119std::ostream& Tanh::print(std::ostream& out) const {
120 out << "Tanh model with centre length: " << _x0
121 << " end length: " << _lambda;
122 return out;
123}
124}
125
Tps< T > tanh(const Tps< T > &x)
Hyperbolic tangent.
Definition: TpsMath.h:240
std::vector< std::vector< int > > CompactVector(std::vector< std::vector< int > > vec)
virtual void setMaximumDerivative(size_t n)
Definition: Tanh.cpp:76
static void setTanhDiffIndices(size_t n)
Definition: Tanh.cpp:81
double function(double x, int n) const
Definition: Tanh.cpp:72
double getNegTanh(double x, int n) const
Definition: Tanh.cpp:61
double _lambda
Definition: Tanh.h:118
std::ostream & print(std::ostream &out) const
Definition: Tanh.cpp:119
static std::vector< std::vector< int > > getTanhDiffIndices(size_t n)
Definition: Tanh.cpp:107
void rescale(double scaleFactor)
Definition: Tanh.cpp:112
Tanh * clone() const
Definition: Tanh.cpp:45
static std::vector< std::vector< std::vector< int > > > _tdi
Definition: Tanh.h:124
double getTanh(double x, int n) const
Definition: Tanh.cpp:50