| 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
 | #include <eigen3/Eigen/Dense>
#include <random>
#include <getopt.h>
#include "pcg-cpp/include/pcg_random.hpp"
#include "randutils/randutils.hpp"
template <class Real>
using Vector = Eigen::Matrix<Real, Eigen::Dynamic, 1>;
template <class Real>
using Matrix = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>;
template <class Real>
class Model {
private:
  Matrix<Real> A;
  Vector<Real> b;
public:
  template <class Generator>
  Model(Real σ, unsigned N, unsigned M, Generator& r) : A(M, N), b(M) {
    std::normal_distribution<Real> aDistribution(0, 1);
    for (unsigned i = 0; i < M; i++) {
      for (unsigned j =0; j < N; j++) {
        A(i, j) = aDistribution(r);
      }
    }
    std::normal_distribution<Real> bDistribution(0, σ);
    for (unsigned i = 0; i < M; i++) {
      b(i) = bDistribution(r);
    }
  }
  const unsigned N() {
    return A.cols();
  }
  const unsigned M() {
    return A.rows();
  }
  const Vector<Real> V(const Vector<Real>& x) {
    return A * x + b;
  }
  const Matrix<Real> dV(const Vector<Real>& x) {
    return A;
  }
//  const Real ddV(const Vector<Real>& x) {
//    return Matrix::Zero(;
//  }
  const Real H(const Vector<Real>& x) {
    return V(x).squaredNorm();
  }
  const Vector<Real> dH(const Vector<Real>& x) {
    return dV(x).transpose() * V(x);
  }
  const Matrix<Real> ddH(const Vector<Real>& x) {
    return dV(x).transpose() * dV(x);
  }
  const Vector<Real> ∇H(const Vector<Real>& x){
    return dH(x) - dH(x).dot(x) * x / x.squaredNorm();
  }
  const Matrix<Real> HessH(const Vector<Real>& x) {
    Matrix<Real> hess = ddH(x) - x.dot(dH(x)) * Matrix<Real>::Identity(N(), N());
    return hess - (hess * x) * x.transpose() / x.squaredNorm();
  }
};
using Rng = randutils::random_generator<pcg32>;
using Real = double;
int main(int argc, char* argv[]) {
  unsigned N = 10;
  unsigned M = 10;
  Real σ = 1;
  int opt;
  while ((opt = getopt(argc, argv, "N:M:s:")) != -1) {
    switch (opt) {
    case 'N':
      N = (unsigned)atof(optarg);
      break;
    case 'M':
      M = (unsigned)atof(optarg);
      break;
    case 's':
      σ = atof(optarg);
      break;
    default:
      exit(1);
    }
  }
  Rng r;
  Model<Real> leastSquares(σ, N, M, r.engine());
  Vector<Real> x = Vector<Real>::Zero(N);
  x(0) = N;
  std::cout << leastSquares.H(x) << std::endl;
  std::cout << leastSquares.∇H(x) << std::endl;
  std::cout << leastSquares.HessH(x) << std::endl;
  return 0;
}
 |