MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
ann_sf.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/ann_sf.c,v 1.12 2017/01/12 15:10:27 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 #include <stdio.h>
33 #include <stdlib.h>
34 #include <math.h>
35 #include <getopt.h>
36 #include <string.h>
37 
38 #include "ann.h"
39 
40 static int TRAINING_MODE = 1;
41 
42 void
44 {
45  fprintf(stdout, "\nUSAGE OPTIONS:\n"
46  " -u, --usage\n"
47  " print usage\n"
48  " -f, --file\n"
49  " input file name\n"
50  " -m, --mode\n"
51  " scaling mode: 1 mean-variance (default)\n"
52  " 2 min-max\n"
53  " -M, --mean\n"
54  " mean value (defaul 0.)\n"
55  " -V, --variance"
56  " variance value (defaul 1.)\n"
57  " -N, --min\n"
58  " minimum value (defaul -1.)\n"
59  " -X, --max\n"
60  " maximum value (defaul 1.)\n"
61  );
62  exit(0);
63 }
64 
65 
66 static int MODE = 1;
67 static float MEAN = 0.;
68 static float VARIANCE = 1.;
69 static float MIN = -1.;
70 static float MAX = 1.;
71 static char *filename = "data.dat";
72 
73 int
74 main(int argc, char *argv[])
75 {
76  int opt;
77  extern char *optarg;
78  matrix MAT, SF;
79  int N_sample, i;
80  double mean, var, min, max;
81 
82  /* 0. Training options */
83  do {
84 #ifdef HAVE_GETOPT_LONG
85  static struct option options[] = {
86  { "usage", 0, 0, 'u' },
87  { "mode", 1, 0, 'm' },
88  { "mean", 1, 0, 'M' },
89  { "variance", 1, 0, 'V' },
90  { "min", 1, 0, 'N' },
91  { "max", 1, 0, 'X' },
92  { "file", 1, 0, 'f' }
93  };
94  opt = getopt_long(argc, argv, "uf:m:M:V:N:X:", options, NULL);
95 #else /* ! HAVE_GETOPT_LONG */
96  opt = getopt(argc, argv, "uf:m:M:V:N:X:");
97 #endif /* ! HAVE_GETOPT_LONG */
98 
99  switch (opt) {
100  case 'u': print_usage();
101  break;
102  case 'f': filename = optarg;
103  break;
104  case 'm': MODE = atoi(optarg);
105  break;
106  case 'M': MEAN = atof(optarg);
107  break;
108  case 'V': VARIANCE = atof(optarg);
109  break;
110  case 'N': MIN = atof(optarg);
111  break;
112  case 'X': MAX = atof(optarg);
113  break;
114  default: break;
115  }
116  } while (opt >= 0);
117 
118  N_sample = 0;
119 
120  if (ANN_DataRead(&MAT, &N_sample, filename)) {
121  fprintf(stderr, "Error in Input data acquisition\n");
122  return 1;
123  }
124 
125  matrix_init(&SF, MAT.Ncolumn, 2);
126 
127  switch (MODE) {
128  case 1:
129  for (i = 0; i < MAT.Ncolumn; i++) {
130  mean = mean_value(&MAT, i);
131  var = variance(&MAT, i);
132  SF.mat[i][0] = sqrt(VARIANCE/var);
133  SF.mat[i][1] = MEAN - SF.mat[i][0]*mean;
134  }
135  break;
136 
137  case 2:
138  for (i = 0; i < MAT.Ncolumn; i++) {
139  min = minimum(&MAT, i);
140  max = maximum(&MAT, i);
141  printf("AAAA %lf %lf\n", min, max);
142  SF.mat[i][0] = (MAX - MIN)/(max - min);
143  SF.mat[i][1] = (max*MIN-MAX*min)/(max-min);
144  }
145  break;
146  }
147 
148  matrix_write(&SF, stdout, W_M_TEXT);
149 
150  return 0;
151 }
mat_res_t matrix_write(matrix *MAT, FILE *fh, unsigned flags)
Definition: matrix.c:467
static float MAX
Definition: ann_sf.c:70
double variance(matrix *MAT, int column)
Definition: matrix.c:598
#define W_M_TEXT
Definition: matrix.h:47
static char * filename
Definition: ann_sf.c:71
unsigned Ncolumn
Definition: matrix.h:64
ann_res_t ANN_DataRead(matrix *MAT, int *N_sample, char *FileName)
Definition: ann.c:475
static int MODE
Definition: ann_sf.c:66
Definition: matrix.h:61
static float VARIANCE
Definition: ann_sf.c:68
double mean_value(matrix *MAT, int column)
Definition: matrix.c:586
void print_usage(void)
Definition: ann_sf.c:43
static float MEAN
Definition: ann_sf.c:67
GradientExpression< UnaryExpr< FuncSqrt, Expr > > sqrt(const GradientExpression< Expr > &u)
Definition: gradient.h:2974
double maximum(matrix *MAT, int column)
Definition: matrix.c:612
double ** mat
Definition: matrix.h:62
static float MIN
Definition: ann_sf.c:69
int main(int argc, char *argv[])
Definition: ann_sf.c:74
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
static int TRAINING_MODE
Definition: ann_sf.c:40
char * optarg
Definition: getopt.c:74
mat_res_t matrix_init(matrix *MAT, unsigned Nrow, unsigned Ncolumn)
Definition: matrix.c:43
double minimum(matrix *MAT, int column)
Definition: matrix.c:628