MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
ActivationFunction.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libann/ActivationFunction.c,v 1.12 2017/01/12 14:43:23 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 /*
32  * Copyright (C) 2008
33  *
34  * Mattia Mattaboni <mattaboni@aero.polimi.it>
35  */
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <math.h>
40 #include "ActivationFunction.h"
41 #include "matrix.h"
42 
43 /* ACTIVATION FUNCTION 1: hyperbolic tangent */
44 
45 int
46 w_tanh_init(void **privp)
47 {
48  *privp = malloc(sizeof(w_tanh_t));
49 
50  return (*privp == NULL);
51 }
52 
53 int
54 w_tanh_destroy(void *priv)
55 {
56 #if 0
57  w_tanh_t *data = (w_tanh_t *)priv;
58 #endif
59 
60  free(priv);
61 
62  return 0;
63 }
64 
65 int
66 w_tanh_read(void *priv, FILE *fh, unsigned flags)
67 {
68  w_tanh_t *data = (w_tanh_t *)priv;
69 
70  fscanf(fh, "%lf", &data->alpha);
71  fscanf(fh, "%lf", &data->beta);
72 
73  return 0;
74 }
75 
76 int
77 w_tanh_write(void *priv, FILE *fh, unsigned flags)
78 {
79  w_tanh_t *data = (w_tanh_t *)priv;
80 
81  if (flags & W_F_BIN) {
82  fprintf(fh, "%d\n%e %e",
83  1,data->alpha, data->beta);
84 
85  } else if (flags & W_F_TEXT) {
86  fprintf(fh, "tanh alpha=%e beta=%e\n",
87  data->alpha, data->beta);
88  }
89 
90  return 0;
91 }
92 
93 int
94 w_tanh_eval(void *priv, double in, int order, double *outp)
95 {
96  w_tanh_t *data = (w_tanh_t *)priv;
97  double y;
98 
99  switch (order) {
100  case 0:
101  y = data->alpha*tanh(data->beta*in);
102  break;
103 
104  case 1:
105  y = tanh(data->beta*in);
106  y = data->alpha*data->beta*(1. - y*y);
107  break;
108 
109  default:
110  return 1;
111  }
112 
113  *outp = y;
114 
115  return 0;
116 }
117 
118 /* Linear Activation Function */
119 int
120 w_linear_init(void **privp)
121 {
122  *privp = malloc(sizeof(w_linear_t));
123 
124  return (*privp == NULL);
125 }
126 
127 int
128 w_linear_destroy(void *priv)
129 {
130 #if 0
131  w_tanh_t *data = (w_tanh_t *)priv;
132 #endif
133 
134  free(priv);
135 
136  return 0;
137 }
138 
139 int
140 w_linear_read(void *priv, FILE *fh, unsigned flags)
141 {
142  w_linear_t *data = (w_linear_t *)priv;
143 
144  fscanf(fh, "%lf", &data->m);
145  fscanf(fh, "%lf", &data->q);
146 
147  return 0;
148 }
149 
150 int
151 w_linear_write(void *priv, FILE *fh, unsigned flags)
152 {
153  w_linear_t *data = (w_linear_t *)priv;
154 
155  if (flags & W_F_BIN) {
156  fprintf(fh, "%d\n%e %e",
157  2,data->m, data->q);
158 
159  } else if (flags & W_F_TEXT) {
160  fprintf(fh, "linear m=%e q=%e\n",
161  data->m, data->q);
162  }
163 
164  return 0;
165 }
166 
167 int
168 w_linear_eval(void *priv, double in, int order, double *outp)
169 {
170  w_linear_t *data = (w_linear_t *)priv;
171  double y;
172 
173  switch (order) {
174  case 0:
175  y = data->m*in + data->q;
176  break;
177 
178  case 1:
179  y = data->m;
180  break;
181 
182  default:
183  return 1;
184  }
185 
186  *outp = y;
187 
188  return 0;
189 }
190 
GradientExpression< UnaryExpr< FuncTanh, Expr > > tanh(const GradientExpression< Expr > &u)
Definition: gradient.h:2982
int w_linear_write(void *priv, FILE *fh, unsigned flags)
int w_tanh_eval(void *priv, double in, int order, double *outp)
int w_linear_init(void **privp)
int w_tanh_read(void *priv, FILE *fh, unsigned flags)
int w_tanh_write(void *priv, FILE *fh, unsigned flags)
int w_tanh_init(void **privp)
enum @55 order
int w_linear_eval(void *priv, double in, int order, double *outp)
int w_tanh_destroy(void *priv)
#define W_F_BIN
int w_linear_read(void *priv, FILE *fh, unsigned flags)
#define W_F_TEXT
int w_linear_destroy(void *priv)