Stan  1.0
probability, sampling & optimization
util.hpp
Go to the documentation of this file.
1 #ifndef __STAN__MATH__UTIL_HPP__
2 #define __STAN__MATH__UTIL_HPP__
3 
4 #include <cmath>
5 #include <vector>
6 #include <cstring>
7 
8 namespace stan {
9 
10  namespace math {
11 
12  inline double max(double a, double b) {
13  return a > b ? a : b;
14  }
15 
16  inline double max(std::vector<double>& x) {
17  double max = x[0];
18  for (size_t i = 1; i < x.size(); ++i)
19  if (x[i] > max)
20  max = x[i];
21  return max;
22  }
23 
24 
25  inline double min(double a, double b) {
26  return a < b ? a : b;
27  }
28 
29  // x' * y
30  inline double dot(std::vector<double>& x,
31  std::vector<double>& y) {
32  double sum = 0.0;
33  for (size_t i = 0; i < x.size(); ++i)
34  sum += x[i] * y[i];
35  return sum;
36  }
37 
38  // x' * x
39  inline double dot_self(std::vector<double>& x) {
40  double sum = 0.0;
41  for (size_t i = 0; i < x.size(); ++i)
42  sum += x[i] * x[i];
43  return sum;
44  }
45 
46  // x <- x + lambda * y
47  inline void scaled_add(std::vector<double>& x,
48  std::vector<double>& y,
49  double lambda) {
50  for (size_t i = 0; i < x.size(); ++i)
51  x[i] += lambda * y[i];
52  }
53 
54  inline void sub(std::vector<double>& x, std::vector<double>& y,
55  std::vector<double>& result) {
56  result.resize(x.size());
57  for (size_t i = 0; i < x.size(); ++i)
58  result[i] = x[i] - y[i];
59  }
60 
61  inline double dist(const std::vector<double>& x, const std::vector<double>& y) {
62  using std::sqrt;
63  double result = 0;
64  for (size_t i = 0; i < x.size(); ++i) {
65  double diff = x[i] - y[i];
66  result += diff * diff;
67  }
68  return sqrt(result);
69  }
70 
71  inline double sum(std::vector<double>& x) {
72  double sum = x[0];
73  for (size_t i = 1; i < x.size(); ++i)
74  sum += x[i];
75  return sum;
76  }
77 
78 
79  }
80 }
81 
82 #endif
var sqrt(const var &a)
Return the square root of the specified variable (cmath).
Definition: agrad.hpp:1761
T sum(const std::vector< T > &xs)
Return the sum of the values in the specified standard vector.
Definition: matrix.hpp:1131
int min(const std::vector< int > &x)
Returns the minimum coefficient in the specified column vector.
Definition: matrix.hpp:917
double dot(std::vector< double > &x, std::vector< double > &y)
Definition: util.hpp:30
int max(const std::vector< int > &x)
Returns the maximum coefficient in the specified column vector.
Definition: matrix.hpp:968
void scaled_add(std::vector< double > &x, std::vector< double > &y, double lambda)
Definition: util.hpp:47
void sub(std::vector< double > &x, std::vector< double > &y, std::vector< double > &result)
Definition: util.hpp:54
double dot_self(const Eigen::Matrix< double, R, C > &v)
Returns the dot product of the specified vector with itself.
Definition: matrix.hpp:846
double dist(const std::vector< double > &x, const std::vector< double > &y)
Definition: util.hpp:61
Probability, optimization and sampling library.
Definition: agrad.cpp:6

     [ Stan Home Page ] © 2011–2012, Stan Development Team.