123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #pragma once
- #include <functional>
- namespace WwiseGTE
- {
- template <typename Real>
- class RootsBisection
- {
- public:
-
- static unsigned int Find(std::function<Real(Real)> const& F, Real t0,
- Real t1, unsigned int maxIterations, Real& root)
- {
-
-
- root = t0;
- if (t0 < t1)
- {
-
- Real f0 = F(t0);
- if (f0 == (Real)0)
- {
- root = t0;
- return 1;
- }
- Real f1 = F(t1);
- if (f1 == (Real)0)
- {
- root = t1;
- return 1;
- }
- if (f0 * f1 > (Real)0)
- {
-
- return 0;
- }
- unsigned int i;
- for (i = 2; i <= maxIterations; ++i)
- {
- root = (Real)0.5 * (t0 + t1);
- if (root == t0 || root == t1)
- {
-
-
- break;
- }
- Real fm = F(root);
- Real product = fm * f0;
- if (product < (Real)0)
- {
- t1 = root;
- f1 = fm;
- }
- else if (product > (Real)0)
- {
- t0 = root;
- f0 = fm;
- }
- else
- {
- break;
- }
- }
- return i;
- }
- else
- {
-
- return 0;
- }
- }
-
-
-
-
- static unsigned int Find(std::function<Real(Real)> const& F, Real t0,
- Real t1, Real f0, Real f1, unsigned int maxIterations, Real& root)
- {
-
-
- root = t0;
- if (t0 < t1)
- {
-
- if (f0 == (Real)0)
- {
- root = t0;
- return 1;
- }
- if (f1 == (Real)0)
- {
- root = t1;
- return 1;
- }
- if (f0 * f1 > (Real)0)
- {
-
- return 0;
- }
- unsigned int i;
- root = t0;
- for (i = 2; i <= maxIterations; ++i)
- {
- root = (Real)0.5 * (t0 + t1);
- if (root == t0 || root == t1)
- {
-
-
- break;
- }
- Real fm = F(root);
- Real product = fm * f0;
- if (product < (Real)0)
- {
- t1 = root;
- f1 = fm;
- }
- else if (product > (Real)0)
- {
- t0 = root;
- f0 = fm;
- }
- else
- {
- break;
- }
- }
- return i;
- }
- else
- {
-
- return 0;
- }
- }
- };
- }
|