MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
bisec.h
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbmath/bisec.h,v 1.12 2017/01/12 14:43:53 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 #ifndef BISEC_H
33 #define BISEC_H
34 
35 #ifdef __cplusplus
36 
37 /*
38  * algoritmo di bisezione per ricerca efficiente degli angoli
39  *
40  * v: array dei valori
41  * val: valore da cercare
42  * lb: indice inferiore
43  * ub: indice superiore
44  *
45  * restituisce l'indice corrispondente al valore di v
46  * che approssima per difetto val;
47  * se val > v[ub] restituisce ub;
48  * se val < v[lb] restituisce lb - 1.
49  * quindi v[ret] <= val < v[ret + 1]
50  *
51  * Nota: ovviamente si presuppone che i valori di v siano ordinati in modo
52  * crescente e strettamente monotono, ovvero v[i] < v[i + 1].
53  */
54 template <class T>
55 int
56 bisec(const T *const v, const T& val, int lb, int ub)
57 {
58  if (val < v[lb]) {
59  return lb - 1;
60  }
61 
62  if (val > v[ub]) {
63  return ub;
64  }
65 
66  while (ub > lb + 1) {
67  int b = (lb + ub)/2;
68  if (v[b] > val) {
69  ub = b;
70  } else if (v[b] < val) {
71  lb = b;
72  } else {
73  return b;
74  }
75  }
76 
77  return lb;
78 }
79 
80 #endif /* __cplusplus */
81 
82 #ifdef __cplusplus
83 extern "C"
84 #endif
85 int bisec_d(doublereal *v, doublereal val, int lb, int ub);
86 
87 #endif /* BISEC_H */
int bisec_d(doublereal *v, doublereal val, int lb, int ub)
Definition: bisec.cc:57
double doublereal
Definition: colamd.c:52