LORENE
zero_list.C
1/*
2 * Function zero_list
3 * Locates approximatively all the zeros of a function in a given interval
4 *
5 * (see file utilitaires.h for documentation)
6 *
7 */
8
9
10/*
11 * Copyright (c) 2003 Eric Gourgoulhon
12 *
13 * This file is part of LORENE.
14 *
15 * LORENE is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * LORENE is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with LORENE; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 *
29 */
30
31
32
33
34/*
35 * $Id: zero_list.C,v 1.3 2016/12/05 16:18:11 j_novak Exp $
36 * $Log: zero_list.C,v $
37 * Revision 1.3 2016/12/05 16:18:11 j_novak
38 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
39 *
40 * Revision 1.2 2014/10/13 08:53:32 j_novak
41 * Lorene classes and functions now belong to the namespace Lorene.
42 *
43 * Revision 1.1 2003/09/08 20:22:02 e_gourgoulhon
44 * First version
45 *
46 *
47 * $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Utilities/zero_list.C,v 1.3 2016/12/05 16:18:11 j_novak Exp $
48 *
49 */
50
51
52// Headers Lorene
53#include "param.h"
54#include "tbl.h"
55
56//****************************************************************************
57
58namespace Lorene {
59
60void zero_list( double (*f)(double, const Param&), const Param& par,
61 double xmin, double xmax, int nsub,
62 Tbl*& az, Tbl*& bz ) {
63
64 int nzero = 0 ;
65
66 double dx = (xmax-xmin) / double(nsub) ;
67 double f1 = f(xmin, par) ;
68 double x1 = xmin ;
69 double x2 = xmin + dx ;
70
71 double* borne_inf = new double[nsub] ; // At maximum nsub zeros
72 double* borne_sup = new double[nsub] ;
73
74 for (int i=0; i<nsub; i++) {
75 double f2 = f(x2, par) ;
76 if (f1*f2 < 0.) { // A zero has been found
77 borne_inf[nzero] = x1 ;
78 borne_sup[nzero] = x2 ;
79 nzero += 1 ;
80 }
81 // Next sub-interval :
82 x1 = x2 ;
83 f1 = f2 ;
84 x2 += dx ;
85 }
86
87 // Result:
88
89 az = new Tbl(nzero) ;
90 bz = new Tbl(nzero) ;
91
92 if (nzero > 0) {
93
94 az->set_etat_qcq() ;
95 bz->set_etat_qcq() ;
96
97 for (int i=0; i<nzero; i++) {
98 az->set(i) = borne_inf[i] ;
99 bz->set(i) = borne_sup[i] ;
100 }
101 }
102
103 delete [] borne_inf ;
104 delete [] borne_sup ;
105
106}
107}
Parameter storage.
Definition param.h:125
Basic array class.
Definition tbl.h:161
void set_etat_qcq()
Sets the logical state to ETATQCQ (ordinary state).
Definition tbl.C:364
double & set(int i)
Read/write of a particular element (index i) (1D case).
Definition tbl.h:281
void zero_list(double(*f)(double, const Param &), const Param &par, double xmin, double xmax, int nsub, Tbl *&az, Tbl *&bz)
Locates approximatively all the zeros of a function in a given interval.
Definition zero_list.C:60
Lorene prototypes.
Definition app_hor.h:67