numerical_differentiation.cpp
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// N U M E R I C A L D I F F E R E N T I A T I O N C L A S S
5//
6// Artificial Intelligence Techniques SL
7// artelnics@artelnics.com
8
9#include "numerical_differentiation.h"
10
11namespace OpenNN
12{
13
16
18{
20}
21
22
24
26{
27
28}
29
31
33{
34 return precision_digits;
35}
36
37
39
41{
42 return display;
43}
44
47
48void NumericalDifferentiation::set_display(const bool& new_display)
49{
50 display = new_display;
51}
52
53
56
57void NumericalDifferentiation::set_precision_digits(const Index& new_precision_digits)
58{
59 precision_digits = new_precision_digits;
60}
61
62
69
71{
73
74 display = true;
75}
76
77
78type NumericalDifferentiation::calculate_eta() const
79{
80 return pow(static_cast<type>(10.0), static_cast<type>(-1.0*precision_digits));
81}
82
83
86
88{
89 const type eta = calculate_eta();
90
91 return sqrt(eta)*(static_cast<type>(1.0) + abs(x));
92}
93
94
97
98Tensor<type, 1> NumericalDifferentiation::calculate_h(const Tensor<type, 1>& x) const
99{
100 const type eta = calculate_eta();
101
102 const Index n = x.size();
103
104 Tensor<type, 1> h(n);
105
106 for(Index i = 0; i < n; i++)
107 {
108 h(i) = sqrt(eta)*(type(1) + abs(x(i)));
109 }
110
111 return h;
112}
113
114
117
118Tensor<type, 2> NumericalDifferentiation::calculate_h(const Tensor<type, 2>& x) const
119{
120 const type eta = calculate_eta();
121
122 const Index n = x.size();
123
124 const auto& dimensions = x.dimensions();
125
126 Tensor<type, 2> h(dimensions);
127
128 Tensor<type, 2> y = x.abs();
129
130 for(Index i = 0; i < n; i++)
131 {
132 h(i) = sqrt(eta)*(type(1) + y(i));
133 }
134
135 return h;
136}
137
138
139Tensor<type, 4> NumericalDifferentiation::calculate_h(const Tensor<type, 4>& x) const
140{
141 const type eta = calculate_eta();
142
143 const Index n = x.size();
144
145 const auto& dimensions = x.dimensions();
146
147 Tensor<type, 4> h(dimensions);
148
149 Tensor<type, 4> y = x.abs();
150
151 for(Index i = 0; i < n; i++)
152 {
153 h(i) = sqrt(eta)*(type(1) + y(i));
154 }
155
156 return h;
157}
158
159}
160
161// OpenNN: Open Neural Networks Library.
162// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
163//
164// This library is free software; you can redistribute it and/or
165// modify it under the terms of the GNU Lesser General Public
166// License as published by the Free Software Foundation; either
167// version 2.1 of the License, or any later version.
168//
169// This library is distributed in the hope that it will be useful,
170// but WITHOUT ANY WARRANTY; without even the implied warranty of
171// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
172// Lesser General Public License for more details.
173
174// You should have received a copy of the GNU Lesser General Public
175// License along with this library; if not, write to the Free Software
176// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
const Index & get_precision_digits() const
Enumeration of available methods for numerical differentiation.
const bool & get_display() const
Returns the flag used by this class for displaying or not displaying warnings.
bool display
Flag for displaying warning messages from this class.
Index precision_digits
Number of precision digits.
HALF_CONSTEXPR half abs(half arg)
Definition: half.hpp:2735
half pow(half x, half y)
Definition: half.hpp:3427