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