OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 <math.h>
29 
30 #include "gsl/gsl_sf_gamma.h"
31 #include "gsl/gsl_sf_pow_int.h"
32 
34 
35 namespace endfieldmodel {
36 
37 std::vector< std::vector< std::vector<int> > > Tanh::_tdi;
38 
39 Tanh::Tanh(double x0, double lambda, int max_index) : _x0(x0), _lambda(lambda) {
40  setTanhDiffIndices(max_index);
41 }
42 
44 
46  return new Tanh(*this);
47 }
48 
49 
50 double 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 
61 double 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 
72 double Tanh::function(double x, int n) const {
73  return (getTanh(x, n)-getNegTanh(x, n))/2.;
74 }
75 
77  if (_tdi.size() == 0) {
78  _tdi.push_back(std::vector< std::vector<int> >(1, std::vector<int>(2)));
79  _tdi[0][0][0] = 1; // 1*tanh(x) - third index is redundant
80  _tdi[0][0][1] = 1;
81  }
82  for (size_t i = _tdi.size(); i < n+1; ++i) {
83  _tdi.push_back(std::vector< std::vector<int> >());
84  for (size_t j = 0; j < _tdi[i-1].size(); ++j) {
85  if (_tdi[i-1][j][1] != 0) {
86  std::vector<int> new_vec(_tdi[i-1][j]);
87  new_vec[0] *= _tdi[i-1][j][1];
88  new_vec[1] -= 1;
89  _tdi[i].push_back(new_vec);
90  }
91  if (_tdi[i-1][j][1] != 0) {
92  std::vector<int> new_vec(_tdi[i-1][j]);
93  new_vec[0] *= -_tdi[i-1][j][1];
94  new_vec[1] += 1;
95  _tdi[i].push_back(new_vec);
96  }
97  }
98  _tdi[i] = CompactVector(_tdi[i]);
99  }
100 }
101 
102 std::vector< std::vector<int> > Tanh::getTanhDiffIndices(size_t n) {
104  return _tdi[n];
105 }
106 
107 
108 std::ostream& Tanh::print(std::ostream& out) const {
109  out << "Tanh model with centre length: " << _x0
110  << " end length: " << _lambda;
111  return out;
112 }
113 }
114 
static void setTanhDiffIndices(size_t n)
Definition: Tanh.cpp:76
EndFieldModel * clone() const
Definition: Tanh.cpp:45
static std::vector< std::vector< std::vector< int > > > _tdi
Definition: Tanh.h:112
double _lambda
Definition: Tanh.h:106
double function(double x, int n) const
Definition: Tanh.cpp:72
std::vector< std::vector< int > > CompactVector(std::vector< std::vector< int > > vec)
std::ostream & print(std::ostream &out) const
Definition: Tanh.cpp:108
double getNegTanh(double x, int n) const
Definition: Tanh.cpp:61
double getTanh(double x, int n) const
Definition: Tanh.cpp:50
static std::vector< std::vector< int > > getTanhDiffIndices(size_t n)
Definition: Tanh.cpp:102
Tps< T > tanh(const Tps< T > &x)
Hyperbolic tangent.
Definition: TpsMath.h:240