1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Ceres Solver - A fast non-linear least squares minimizer
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// http://code.google.com/p/ceres-solver/
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Redistribution and use in source and binary forms, with or without
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// modification, are permitted provided that the following conditions are met:
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// * Redistributions of source code must retain the above copyright notice,
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//   this list of conditions and the following disclaimer.
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// * Redistributions in binary form must reproduce the above copyright notice,
115e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)//   this list of conditions and the following disclaimer in the documentation
125e3f23d412006dc4db4e659864679f29341e113fTorne (Richard Coles)//   and/or other materials provided with the distribution.
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// * Neither the name of Google Inc. nor the names of its contributors may be
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//   used to endorse or promote products derived from this software without
15558790d6acca3451cf3a6b497803a5f07d0bec58Ben Murdoch//   specific prior written permission.
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)//
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// POSSIBILITY OF SUCH DAMAGE.
28a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)//
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Author: keir@google.com (Keir Mierle)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//         sameeragarwal@google.com (Sameer Agarwal)
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
32a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#ifndef CERES_INTERNAL_RANDOM_H_
33a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#define CERES_INTERNAL_RANDOM_H_
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <cmath>
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include <cstdlib>
37a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "ceres/internal/port.h"
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)namespace ceres {
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)inline void SetRandomState(int state) {
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  srand(state);
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)inline int Uniform(int n) {
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return rand() % n;
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)inline double RandDouble() {
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  double r = static_cast<double>(rand());
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return r / RAND_MAX;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Box-Muller algorithm for normal random number generation.
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// http://en.wikipedia.org/wiki/Box-Muller_transform
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)inline double RandNormal() {
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  double x1, x2, w;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  do {
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    x1 = 2.0 * RandDouble() - 1.0;
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    x2 = 2.0 * RandDouble() - 1.0;
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    w = x1 * x1 + x2 * x2;
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } while ( w >= 1.0 || w == 0.0 );
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  w = sqrt((-2.0 * log(w)) / w);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return x1 * w;
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace ceres
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif  // CERES_INTERNAL_RANDOM_H_
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch