File: findroot_demo.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (170 lines) | stat: -rw-r--r-- 5,371 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* Boost example/findroot_demo.cpp
 * find zero points of some function by dichotomy
 *
 * Copyright 2000 Jens Maurer
 * Copyright 2002-2003 Guillaume Melquiond
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE_1_0.txt or
 * copy at http://www.boost.org/LICENSE_1_0.txt)
 *
 * The idea and the 2D function are based on RVInterval,
 * which contains the following copyright notice:

        This file is copyrighted 1996 by Ronald Van Iwaarden.

        Permission is hereby granted, without written agreement and
        without license or royalty fees, to use, copy, modify, and
        distribute this software and its documentation for any
        purpose, subject to the following conditions:
        
        The above license notice and this permission notice shall
        appear in all copies or substantial portions of this software.
        
        The name "RVInterval" cannot be used for any modified form of
        this software that does not originate from the authors.
        Nevertheless, the name "RVInterval" may and should be used to
        designate the optimization software implemented and described
        in this package, even if embedded in any other system, as long
        as any portion of this code remains.
        
        The authors specifically disclaim any warranties, including,
        but not limited to, the implied warranties of merchantability
        and fitness for a particular purpose.  The software provided
        hereunder is on an "as is" basis, and the authors have no
        obligation to provide maintenance, support, updates,
        enhancements, or modifications.  In no event shall the authors
        be liable to any party for direct, indirect, special,
        incidental, or consequential damages arising out of the use of
        this software and its documentation.      
*/

#include <boost/numeric/interval.hpp>    // must be first for <limits> workaround
#include <list>
#include <deque>
#include <vector>
#include <fstream>
#include <iostream>


template<class T>
struct test_func2d
{
  T operator()(T x, T y) const
  {
    return sin(x)*cos(y) - exp(x*y)/45.0 * (pow(x+y, 2)+100.0) - 
      cos(sin(y))*y/4.0;
  }
};

template <class T>
struct test_func1d
{
  T operator()(T x) const
  {
    return sin(x)/(x*x+1.0);
  }
};

template<class T>
struct test_func1d_2
{
  T operator()(T x) const
  {
    using std::sqrt;
    return sqrt(x*x-1.0);
  }
};

template<class Function, class I>
void find_zeros(std::ostream & os, Function f, I searchrange)
{
  std::list<I> l, done;
  l.push_back(searchrange);
  while(!l.empty()) {
    I range = l.front();
    l.pop_front();
    I val = f(range);
    if (zero_in(val)) {
      if(width(range) < 1e-6) {
        os << range << '\n';
        continue;
      }
      // there's still a solution hidden somewhere
      std::pair<I,I> p = bisect(range);
      l.push_back(p.first);
      l.push_back(p.second);
    }
  }
}

template<class T>
std::ostream &operator<<(std::ostream &os, const std::pair<T, T> &x) {
  os << "(" << x.first << ", " << x.second << ")";
  return os;
}

template<class T, class Policies>
std::ostream &operator<<(std::ostream &os,
                         const boost::numeric::interval<T, Policies> &x) {
  os << "[" << x.lower() << ", " << x.upper() << "]";
  return os;
}

static const double epsilon = 5e-3;

template<class Function, class I>
void find_zeros(std::ostream & os, Function f, I rx, I ry)
{
  typedef std::pair<I, I> rectangle;
  typedef std::deque<rectangle> container;
  container l, done;
  // l.reserve(50);
  l.push_back(std::make_pair(rx, ry));
  for(int i = 1; !l.empty(); ++i) {
    rectangle rect = l.front();
    l.pop_front();
    I val = f(rect.first, rect.second);
    if (zero_in(val)) {
      if(width(rect.first) < epsilon && width(rect.second) < epsilon) {
        os << median(rect.first) << " " << median(rect.second) << " "
           << lower(rect.first) << " " << upper(rect.first) << " "
           << lower(rect.second) << " " << upper(rect.second) 
           << '\n';
      } else {
        if(width(rect.first) > width(rect.second)) {
          std::pair<I,I> p = bisect(rect.first);
          l.push_back(std::make_pair(p.first, rect.second));
          l.push_back(std::make_pair(p.second, rect.second));
        } else {
          std::pair<I,I> p = bisect(rect.second);
          l.push_back(std::make_pair(rect.first, p.first));
          l.push_back(std::make_pair(rect.first, p.second));
        }
      }
    }
    if(i % 10000 == 0)
      std::cerr << "\rIteration " << i << ", l.size() = " << l.size();
  }
  std::cerr << '\n';
}

int main()
{
  using namespace boost;
  using namespace numeric;
  using namespace interval_lib;

  typedef interval<double,
                   policies<save_state<rounded_transc_opp<double> >,
                            checking_base<double> > > I;

  std::cout << "Zero points of sin(x)/(x*x+1)\n";
  find_zeros(std::cout, test_func1d<I>(), I(-11, 10));
  std::cout << "Zero points of sqrt(x*x-1)\n";
  find_zeros(std::cout, test_func1d_2<I>(), I(-5, 6));
  std::cout << "Zero points of Van Iwaarden's 2D function\n";
  std::ofstream f("func2d.data");
  find_zeros(f, test_func2d<I>(), I(-20, 20), I(-20, 20));
  std::cout << "Use gnuplot, command 'plot \"func2d.data\" with dots'   to plot\n";
}