lsst.afw  13.0-49-g6f23dda
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GaussianProcess.cc
Go to the documentation of this file.
1 // - * - LSST - C++ - * -
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see < http://www.lsstcorp.org/LegalNotices/ > .
23  */
24 
25 #include <iostream>
26 #include <cmath>
27 
29 
30 using namespace std;
31 
32 namespace lsst {
33 namespace afw {
34 namespace math {
35 
36 GaussianProcessTimer::GaussianProcessTimer() {
37  _interpolationCount = 0;
38  _iterationTime = 0.0;
39  _eigenTime = 0.0;
40  _searchTime = 0.0;
41  _varianceTime = 0.0;
42  _totalTime = 0.0;
43 }
44 
45 void GaussianProcessTimer::reset() {
46  _interpolationCount = 0;
47  _iterationTime = 0.0;
48  _eigenTime = 0.0;
49  _searchTime = 0.0;
50  _varianceTime = 0.0;
51  _totalTime = 0.0;
52 }
53 
54 void GaussianProcessTimer::start() {
55  _before = double(lsst::daf::base::DateTime::now().get() * 24 * 60 * 60);
56  _beginning = _before;
57 }
58 
59 void GaussianProcessTimer::addToEigen() {
60  double after;
61  after = double(lsst::daf::base::DateTime::now().get() * 24 * 60 * 60);
62  _eigenTime += after - _before;
63  _before = after;
64 }
65 
66 void GaussianProcessTimer::addToVariance() {
67  double after;
68  after = double(lsst::daf::base::DateTime::now().get() * 24 * 60 * 60);
69  _varianceTime += after - _before;
70  _before = after;
71 }
72 
73 void GaussianProcessTimer::addToSearch() {
74  double after;
75  after = double(lsst::daf::base::DateTime::now().get() * 24 * 60 * 60);
76  _searchTime += after - _before;
77  _before = after;
78 }
79 
80 void GaussianProcessTimer::addToIteration() {
81  double after;
82  after = double(lsst::daf::base::DateTime::now().get() * 24 * 60 * 60);
83  _iterationTime += after - _before;
84  _before = after;
85 }
86 
87 void GaussianProcessTimer::addToTotal(int i) {
88  double after;
89  after = double(lsst::daf::base::DateTime::now().get() * 24 * 60 * 60);
90  _totalTime += after - _beginning;
91  _interpolationCount += i;
92 }
93 
95  std::cout << "\nSearch time " << _searchTime << "\n";
96  std::cout << "Eigen time " << _eigenTime << "\n";
97  std::cout << "Variance time " << _varianceTime << "\n";
98  std::cout << "Iteration time " << _iterationTime << "\n";
99  std::cout << "Total time " << _totalTime << "\n";
100  std::cout << "Number of interpolations " << _interpolationCount << "\n";
101 }
102 
103 template <typename T>
104 void KdTree<T>::Initialize(ndarray::Array<T, 2, 2> const &dt) {
105  int i;
106 
107  _npts = dt.template getSize<0>();
108  _dimensions = dt.template getSize<1>();
109 
110  // a buffer to use when first building the tree
111  _inn = allocate(ndarray::makeVector(_npts));
112 
113  _roomStep = 5000;
114  _room = _npts;
115 
116  _data = allocate(ndarray::makeVector(_room, _dimensions));
117 
118  _data.deep() = dt;
119 
120  _tree = allocate(ndarray::makeVector(_room, 4));
121 
122  for (i = 0; i < _npts; i++) {
123  _inn[i] = i;
124  }
125 
126  _organize(_inn, _npts, -1, -1);
127 
128  i = _testTree();
129  if (i == 0) {
130  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Failed to properly initialize KdTree\n");
131  }
132 }
133 
134 template <typename T>
135 void KdTree<T>::findNeighbors(ndarray::Array<int, 1, 1> neighdex, ndarray::Array<double, 1, 1> dd,
136  ndarray::Array<const T, 1, 1> const &v, int n_nn) const {
137  if (n_nn > _npts) {
138  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
139  "Asked for more neighbors than kd tree contains\n");
140  }
141 
142  if (n_nn <= 0) {
143  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
144  "Asked for zero or a negative number of neighbors\n");
145  }
146 
147  if (neighdex.getNumElements() != n_nn) {
148  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
149  "Size of neighdex does not equal n_nn in KdTree.findNeighbors\n");
150  }
151 
152  if (dd.getNumElements() != n_nn) {
153  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
154  "Size of dd does not equal n_nn in KdTree.findNeighbors\n");
155  }
156 
157  int i, start;
158 
159  _neighborCandidates = allocate(ndarray::makeVector(n_nn));
160  _neighborDistances = allocate(ndarray::makeVector(n_nn));
161  _neighborsFound = 0;
162  _neighborsWanted = n_nn;
163 
164  for (i = 0; i < n_nn; i++) _neighborDistances[i] = -1.0;
165 
166  start = _findNode(v);
167 
168  _neighborDistances[0] = _distance(v, _data[start]);
169  _neighborCandidates[0] = start;
170  _neighborsFound = 1;
171 
172  for (i = 1; i < 4; i++) {
173  if (_tree[start][i] >= 0) {
174  _lookForNeighbors(v, _tree[start][i], start);
175  }
176  }
177 
178  for (i = 0; i < n_nn; i++) {
179  neighdex[i] = _neighborCandidates[i];
180  dd[i] = _neighborDistances[i];
181  }
182 }
183 
184 template <typename T>
185 T KdTree<T>::getData(int ipt, int idim) const {
186  if (ipt >= _npts || ipt < 0) {
187  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
188  "Asked for point that does not exist in KdTree\n");
189  }
190 
191  if (idim >= _dimensions || idim < 0) {
192  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
193  "KdTree does not have points of that many dimensions\n");
194  }
195 
196  return _data[ipt][idim];
197 }
198 
199 template <typename T>
200 ndarray::Array<T, 1, 1> KdTree<T>::getData(int ipt) const {
201  if (ipt >= _npts || ipt < 0) {
202  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
203  "Asked for point that does not exist in KdTree\n");
204  }
205 
206  return _data[ipt];
207 }
208 
209 template <typename T>
210 void KdTree<T>::addPoint(ndarray::Array<const T, 1, 1> const &v) {
211  if (v.getNumElements() != _dimensions) {
212  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
213  "You are trying to add a point of the incorrect dimensionality to KdTree\n");
214  }
215 
216  int i, j, node, dim, dir;
217 
218  node = _findNode(v);
219  dim = _tree[node][DIMENSION] + 1;
220  if (dim == _dimensions) dim = 0;
221 
222  if (_npts == _room) {
223  ndarray::Array<T, 2, 2> dbuff = allocate(ndarray::makeVector(_npts, _dimensions));
224  ndarray::Array<int, 2, 2> tbuff = allocate(ndarray::makeVector(_npts, 4));
225 
226  dbuff.deep() = _data;
227  tbuff.deep() = _tree;
228 
229  _room += _roomStep;
230 
231  _tree = allocate(ndarray::makeVector(_room, 4));
232  _data = allocate(ndarray::makeVector(_room, _dimensions));
233 
234  for (i = 0; i < _npts; i++) {
235  for (j = 0; j < _dimensions; j++) _data[i][j] = dbuff[i][j];
236  for (j = 0; j < 4; j++) _tree[i][j] = tbuff[i][j];
237  }
238  }
239 
240  _tree[_npts][DIMENSION] = dim;
241  _tree[_npts][PARENT] = node;
242  i = _tree[node][DIMENSION];
243 
244  if (_data[node][i] > v[i]) {
245  if (_tree[node][LT] >= 0) {
246  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
247  "Trying to add to KdTree in a node that is already occupied\n");
248  }
249  _tree[node][LT] = _npts;
250  dir = LT;
251  } else {
252  if (_tree[node][GEQ] >= 0) {
253  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
254  "Trying to add to KdTree in a node that is already occupied\n");
255  }
256  _tree[node][GEQ] = _npts;
257  dir = GEQ;
258  }
259  _tree[_npts][LT] = -1;
260  _tree[_npts][GEQ] = -1;
261  for (i = 0; i < _dimensions; i++) {
262  _data[_npts][i] = v[i];
263  }
264 
265  _npts++;
266 
267  i = _walkUpTree(_tree[_npts - 1][PARENT], dir, _npts - 1);
268  if (i != _masterParent) {
269  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Adding to KdTree failed\n");
270  }
271 }
272 
273 template <typename T>
275  return _npts;
276 }
277 
278 template <typename T>
279 void KdTree<T>::getTreeNode(ndarray::Array<int, 1, 1> const &v, int dex) const {
280  if (dex < 0 || dex >= _npts) {
281  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
282  "Asked for tree information on point that does not exist\n");
283  }
284 
285  if (v.getNumElements() != 4) {
286  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
287  "Need to pass a 4-element ndarray into KdTree.getTreeNode()\n");
288  }
289 
290  v[0] = _tree[dex][DIMENSION];
291  v[1] = _tree[dex][LT];
292  v[2] = _tree[dex][GEQ];
293  v[3] = _tree[dex][PARENT];
294 }
295 
296 template <typename T>
297 int KdTree<T>::_testTree() const {
298  if (_npts == 1 && _tree[0][PARENT] < 0 && _masterParent == 0) {
299  // there is only one point in the tree
300  return 1;
301  }
302 
303  int i, j, output;
304  std::vector<int> isparent;
305 
306  output = 1;
307 
308  for (i = 0; i < _npts; i++) isparent.push_back(0);
309 
310  j = 0;
311  for (i = 0; i < _npts; i++) {
312  if (_tree[i][PARENT] < 0) j++;
313  }
314  if (j != 1) {
315  std::cout << "_tree FAILURE " << j << " _masterParents\n";
316  return 0;
317  }
318 
319  for (i = 0; i < _npts; i++) {
320  if (_tree[i][PARENT] >= 0) {
321  isparent[_tree[i][PARENT]]++;
322  }
323  }
324 
325  for (i = 0; i < _npts; i++) {
326  if (isparent[i] > 2) {
327  std::cout << "_tree FAILURE " << i << " is parent to " << isparent[i] << "\n";
328  return 0;
329  }
330  }
331 
332  for (i = 0; i < _npts; i++) {
333  if (_tree[i][PARENT] >= 0) {
334  if (_tree[_tree[i][PARENT]][LT] == i)
335  j = LT;
336  else
337  j = GEQ;
338  output = _walkUpTree(_tree[i][PARENT], j, i);
339 
340  if (output != _masterParent) {
341  return 0;
342  }
343  }
344  }
345 
346  if (output != _masterParent)
347  return 0;
348  else
349  return 1;
350 }
351 
352 template <typename T>
353 void KdTree<T>::_organize(ndarray::Array<int, 1, 1> const &use, int ct, int parent, int dir) {
354  int i, j, k, l, idim, daughter;
355  T mean, var, varbest;
356 
357  std::vector<std::vector<T> > toSort;
358  std::vector<T> toSortElement;
359 
360  if (ct > 1) {
361  // below is code to choose the dimension on which the available points
362  // have the greates variance. This will be the dimension on which
363  // the daughter node splits the data
364  idim = 0;
365  varbest = -1.0;
366  for (i = 0; i < _dimensions; i++) {
367  mean = 0.0;
368  var = 0.0;
369  for (j = 0; j < ct; j++) {
370  mean += _data[use[j]][i];
371  var += _data[use[j]][i] * _data[use[j]][i];
372  }
373  mean = mean / double(ct);
374  var = var / double(ct) - mean * mean;
375  if (i == 0 || var > varbest || (var == varbest && parent >= 0 && i > _tree[parent][DIMENSION])) {
376  idim = i;
377  varbest = var;
378  }
379  } // for(i = 0;i < _dimensions;i++ )
380 
381  // we need to sort the available data points according to their idim - th element
382  // but we need to keep track of their original indices, so we can refer back to
383  // their positions in _data. Therefore, the code constructs a 2 - dimensional
384  // std::vector. The first dimension contains the element to be sorted.
385  // The second dimension contains the original index information.
386  // After sorting, the (rearranged) index information will be stored back in the
387  // ndarray use[]
388 
389  toSortElement.push_back(_data[use[0]][idim]);
390  toSortElement.push_back(T(use[0]));
391  toSort.push_back(toSortElement);
392  for (i = 1; i < ct; i++) {
393  toSortElement[0] = _data[use[i]][idim];
394  toSortElement[1] = T(use[i]);
395  toSort.push_back(toSortElement);
396  }
397 
398  std::stable_sort(toSort.begin(), toSort.end());
399 
400  k = ct / 2;
401  l = ct / 2;
402  while (k > 0 && toSort[k][0] == toSort[k - 1][0]) k--;
403 
404  while (l < ct - 1 && toSort[l][0] == toSort[ct / 2][0]) l++;
405 
406  if ((ct / 2 - k) < (l - ct / 2) || l == ct - 1)
407  j = k;
408  else
409  j = l;
410 
411  for (i = 0; i < ct; i++) {
412  use[i] = int(toSort[i][1]);
413  }
414  daughter = use[j];
415 
416  if (parent >= 0) _tree[parent][dir] = daughter;
417  _tree[daughter][DIMENSION] = idim;
418  _tree[daughter][PARENT] = parent;
419 
420  if (j < ct - 1) {
421  _organize(use[ndarray::view(j + 1, use.getSize<0>())], ct - j - 1, daughter, GEQ);
422  } else
423  _tree[daughter][GEQ] = -1;
424 
425  if (j > 0) {
426  _organize(use, j, daughter, LT);
427  } else
428  _tree[daughter][LT] = -1;
429 
430  } // if(ct > 1)
431  else {
432  daughter = use[0];
433 
434  if (parent >= 0) _tree[parent][dir] = daughter;
435 
436  idim = _tree[parent][DIMENSION] + 1;
437 
438  if (idim >= _dimensions) idim = 0;
439 
440  _tree[daughter][DIMENSION] = idim;
441  _tree[daughter][LT] = -1;
442  _tree[daughter][GEQ] = -1;
443  _tree[daughter][PARENT] = parent;
444  }
445 
446  if (parent == -1) {
447  _masterParent = daughter;
448  }
449 }
450 
451 template <typename T>
452 int KdTree<T>::_findNode(ndarray::Array<const T, 1, 1> const &v) const {
453  int consider, next, dim;
454 
455  dim = _tree[_masterParent][DIMENSION];
456 
457  if (v[dim] < _data[_masterParent][dim])
458  consider = _tree[_masterParent][LT];
459  else
460  consider = _tree[_masterParent][GEQ];
461 
462  next = consider;
463 
464  while (next >= 0) {
465  consider = next;
466 
467  dim = _tree[consider][DIMENSION];
468  if (v[dim] < _data[consider][dim])
469  next = _tree[consider][LT];
470  else
471  next = _tree[consider][GEQ];
472  }
473 
474  return consider;
475 }
476 
477 template <typename T>
478 void KdTree<T>::_lookForNeighbors(ndarray::Array<const T, 1, 1> const &v, int consider, int from) const {
479  int i, j, going;
480  double dd;
481 
482  dd = _distance(v, _data[consider]);
483 
484  if (_neighborsFound < _neighborsWanted || dd < _neighborDistances[_neighborsWanted - 1]) {
485  for (j = 0; j < _neighborsFound && _neighborDistances[j] < dd; j++)
486  ;
487 
488  for (i = _neighborsWanted - 1; i > j; i--) {
489  _neighborDistances[i] = _neighborDistances[i - 1];
490  _neighborCandidates[i] = _neighborCandidates[i - 1];
491  }
492 
493  _neighborDistances[j] = dd;
494  _neighborCandidates[j] = consider;
495 
496  if (_neighborsFound < _neighborsWanted) _neighborsFound++;
497  }
498 
499  if (_tree[consider][PARENT] == from) {
500  // you came here from the parent
501 
502  i = _tree[consider][DIMENSION];
503  dd = v[i] - _data[consider][i];
504  if ((dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted) &&
505  _tree[consider][LT] >= 0) {
506  _lookForNeighbors(v, _tree[consider][LT], consider);
507  }
508 
509  dd = _data[consider][i] - v[i];
510  if ((dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted) &&
511  _tree[consider][GEQ] >= 0) {
512  _lookForNeighbors(v, _tree[consider][GEQ], consider);
513  }
514  } else {
515  // you came here from one of the branches
516 
517  // descend the other branch
518  if (_tree[consider][LT] == from) {
519  going = GEQ;
520  } else {
521  going = LT;
522  }
523 
524  j = _tree[consider][going];
525 
526  if (j >= 0) {
527  i = _tree[consider][DIMENSION];
528 
529  if (going == 1)
530  dd = v[i] - _data[consider][i];
531  else
532  dd = _data[consider][i] - v[i];
533 
534  if (dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted) {
535  _lookForNeighbors(v, j, consider);
536  }
537  }
538 
539  // ascend to the parent
540  if (_tree[consider][PARENT] >= 0) {
541  _lookForNeighbors(v, _tree[consider][PARENT], consider);
542  }
543  }
544 }
545 
546 template <typename T>
547 int KdTree<T>::_walkUpTree(int target, int dir, int root) const {
548  // target is the node that you are examining now
549  // dir is where you came from
550  // root is the ultimate point from which you started
551 
552  int i, output;
553 
554  output = 1;
555 
556  if (dir == LT) {
557  if (_data[root][_tree[target][DIMENSION]] >= _data[target][_tree[target][DIMENSION]]) {
558  return 0;
559  }
560  } else {
561  if (_data[root][_tree[target][DIMENSION]] < _data[target][_tree[target][DIMENSION]]) {
562  return 0;
563  }
564  }
565 
566  if (_tree[target][PARENT] >= 0) {
567  if (_tree[_tree[target][PARENT]][LT] == target)
568  i = LT;
569  else
570  i = GEQ;
571  output = output * _walkUpTree(_tree[target][PARENT], i, root);
572 
573  } else {
574  output = output * target;
575  // so that it will return _masterParent
576  // make sure everything is connected to _masterParent
577  }
578  return output;
579 }
580 
581 template <typename T>
582 void KdTree<T>::removePoint(int target) {
583  if (target < 0 || target >= _npts) {
584  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
585  "You are trying to remove a point that doesn't exist from KdTree\n");
586  }
587 
588  if (_npts == 1) {
589  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
590  "There is only one point left in this KdTree. You cannot call removePoint\n");
591  }
592 
593  int nl, nr, i, j, k, side;
594  int root;
595 
596  nl = 0;
597  nr = 0;
598  if (_tree[target][LT] >= 0) {
599  nl++;
600  _count(_tree[target][LT], &nl);
601  }
602 
603  if (_tree[target][GEQ] >= 0) {
604  nr++;
605  _count(_tree[target][GEQ], &nr);
606  }
607 
608  if (nl == 0 && nr == 0) {
609  k = _tree[target][PARENT];
610 
611  if (_tree[k][LT] == target)
612  _tree[k][LT] = -1;
613  else if (_tree[k][GEQ] == target)
614  _tree[k][GEQ] = -1;
615 
616  } // if target is terminal
617  else if ((nl == 0 && nr > 0) || (nr == 0 && nl > 0)) {
618  if (nl == 0)
619  side = GEQ;
620  else
621  side = LT;
622 
623  k = _tree[target][PARENT];
624  if (k >= 0) {
625  if (_tree[k][LT] == target) {
626  _tree[k][LT] = _tree[target][side];
627  _tree[_tree[k][LT]][PARENT] = k;
628 
629  } else {
630  _tree[k][GEQ] = _tree[target][side];
631  _tree[_tree[k][GEQ]][PARENT] = k;
632  }
633  } else {
634  _masterParent = _tree[target][side];
635  _tree[_tree[target][side]][PARENT] = -1;
636  }
637  } // if only one side is populated
638  else {
639  if (nl > nr)
640  side = LT;
641  else
642  side = GEQ;
643 
644  k = _tree[target][PARENT];
645  if (k < 0) {
646  _masterParent = _tree[target][side];
647  _tree[_masterParent][PARENT] = -1;
648  } else {
649  if (_tree[k][LT] == target) {
650  _tree[k][LT] = _tree[target][side];
651  _tree[_tree[k][LT]][PARENT] = k;
652  } else {
653  _tree[k][GEQ] = _tree[target][side];
654  _tree[_tree[k][GEQ]][PARENT] = k;
655  }
656  }
657 
658  root = _tree[target][3 - side];
659 
660  _descend(root);
661  } // if both sides are populated
662 
663  if (target < _npts - 1) {
664  for (i = target + 1; i < _npts; i++) {
665  for (j = 0; j < 4; j++) _tree[i - 1][j] = _tree[i][j];
666 
667  for (j = 0; j < _dimensions; j++) _data[i - 1][j] = _data[i][j];
668  }
669 
670  for (i = 0; i < _npts; i++) {
671  for (j = 1; j < 4; j++) {
672  if (_tree[i][j] > target) _tree[i][j]--;
673  }
674  }
675 
676  if (_masterParent > target) _masterParent--;
677  }
678  _npts--;
679 
680  i = _testTree();
681  if (i == 0) {
682  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Subtracting from KdTree failed\n");
683  }
684 }
685 
686 template <typename T>
687 void KdTree<T>::_count(int where, int *ct) const {
688  // a way to count the number of vital elements on a given branch
689 
690  if (_tree[where][LT] >= 0) {
691  ct[0]++;
692  _count(_tree[where][LT], ct);
693  }
694  if (_tree[where][GEQ] >= 0) {
695  ct[0]++;
696  _count(_tree[where][GEQ], ct);
697  }
698 }
699 
700 template <typename T>
701 void KdTree<T>::_reassign(int target) {
702  int where, dir, k;
703 
704  where = _masterParent;
705  if (_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]])
706  dir = LT;
707  else
708  dir = GEQ;
709 
710  k = _tree[where][dir];
711  while (k >= 0) {
712  where = k;
713  if (_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]])
714  dir = LT;
715  else
716  dir = GEQ;
717  k = _tree[where][dir];
718  }
719 
720  _tree[where][dir] = target;
721  _tree[target][PARENT] = where;
722  _tree[target][LT] = -1;
723  _tree[target][GEQ] = -1;
724  _tree[target][DIMENSION] = _tree[where][DIMENSION] + 1;
725 
726  if (_tree[target][DIMENSION] == _dimensions) _tree[target][DIMENSION] = 0;
727 }
728 
729 template <typename T>
730 void KdTree<T>::_descend(int root) {
731  if (_tree[root][LT] >= 0) _descend(_tree[root][LT]);
732  if (_tree[root][GEQ] >= 0) _descend(_tree[root][GEQ]);
733 
734  _reassign(root);
735 }
736 
737 template <typename T>
738 double KdTree<T>::_distance(ndarray::Array<const T, 1, 1> const &p1,
739  ndarray::Array<const T, 1, 1> const &p2) const {
740  int i, dd;
741  double ans;
742  ans = 0.0;
743  dd = p1.template getSize<0>();
744 
745  for (i = 0; i < dd; i++) ans += (p1[i] - p2[i]) * (p1[i] - p2[i]);
746 
747  return ::sqrt(ans);
748 }
749 
750 template <typename T>
751 GaussianProcess<T>::GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &ff,
752  std::shared_ptr<Covariogram<T> > const &covarIn)
753 
754 {
755  int i;
756 
757  _covariogram = covarIn;
758 
759  _npts = dataIn.template getSize<0>();
760  _dimensions = dataIn.template getSize<1>();
761 
762  _room = _npts;
763  _roomStep = 5000;
764 
765  _nFunctions = 1;
766  _function = allocate(ndarray::makeVector(_npts, 1));
767 
768  if (ff.getNumElements() != _npts) {
769  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
770  "You did not pass in the same number of data points as function values\n");
771  }
772 
773  for (i = 0; i < _npts; i++) _function[i][0] = ff[i];
774 
775  _krigingParameter = T(1.0);
776  _lambda = T(1.0e-5);
777 
778  _useMaxMin = 0;
779 
780  _kdTree.Initialize(dataIn);
781 
782  _npts = _kdTree.getNPoints();
783 }
784 
785 template <typename T>
786 GaussianProcess<T>::GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
787  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 1, 1> const &ff,
788  std::shared_ptr<Covariogram<T> > const &covarIn) {
789  int i, j;
790  ndarray::Array<T, 2, 2> normalizedData;
791 
792  _covariogram = covarIn;
793 
794  _npts = dataIn.template getSize<0>();
795  _dimensions = dataIn.template getSize<1>();
796  _room = _npts;
797  _roomStep = 5000;
798 
799  if (ff.getNumElements() != _npts) {
800  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
801  "You did not pass in the same number of data points as function values\n");
802  }
803 
804  if (mn.getNumElements() != _dimensions || mx.getNumElements() != _dimensions) {
805  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
806  "Your min/max values have different dimensionality than your data points\n");
807  }
808 
809  _krigingParameter = T(1.0);
810 
811  _lambda = T(1.0e-5);
812  _krigingParameter = T(1.0);
813 
814  _max = allocate(ndarray::makeVector(_dimensions));
815  _min = allocate(ndarray::makeVector(_dimensions));
816  _max.deep() = mx;
817  _min.deep() = mn;
818  _useMaxMin = 1;
819  normalizedData = allocate(ndarray::makeVector(_npts, _dimensions));
820  for (i = 0; i < _npts; i++) {
821  for (j = 0; j < _dimensions; j++) {
822  normalizedData[i][j] = (dataIn[i][j] - _min[j]) / (_max[j] - _min[j]);
823  // note the normalization by _max - _min in each dimension
824  }
825  }
826 
827  _kdTree.Initialize(normalizedData);
828 
829  _npts = _kdTree.getNPoints();
830  _nFunctions = 1;
831  _function = allocate(ndarray::makeVector(_npts, 1));
832  for (i = 0; i < _npts; i++) _function[i][0] = ff[i];
833 }
834 
835 template <typename T>
836 GaussianProcess<T>::GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 2, 2> const &ff,
837  std::shared_ptr<Covariogram<T> > const &covarIn) {
838  _covariogram = covarIn;
839 
840  _npts = dataIn.template getSize<0>();
841  _dimensions = dataIn.template getSize<1>();
842 
843  _room = _npts;
844  _roomStep = 5000;
845 
846  if (ff.template getSize<0>() != _npts) {
847  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
848  "You did not pass in the same number of data points as function values\n");
849  }
850 
851  _nFunctions = ff.template getSize<1>();
852  _function = allocate(ndarray::makeVector(_npts, _nFunctions));
853  _function.deep() = ff;
854 
855  _krigingParameter = T(1.0);
856 
857  _lambda = T(1.0e-5);
858 
859  _useMaxMin = 0;
860 
861  _kdTree.Initialize(dataIn);
862 
863  _npts = _kdTree.getNPoints();
864 }
865 
866 template <typename T>
867 GaussianProcess<T>::GaussianProcess(ndarray::Array<T, 2, 2> const &dataIn, ndarray::Array<T, 1, 1> const &mn,
868  ndarray::Array<T, 1, 1> const &mx, ndarray::Array<T, 2, 2> const &ff,
869  std::shared_ptr<Covariogram<T> > const &covarIn) {
870  int i, j;
871  ndarray::Array<T, 2, 2> normalizedData;
872 
873  _covariogram = covarIn;
874 
875  _npts = dataIn.template getSize<0>();
876  _dimensions = dataIn.template getSize<1>();
877 
878  _room = _npts;
879  _roomStep = 5000;
880 
881  if (ff.template getSize<0>() != _npts) {
882  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
883  "You did not pass in the same number of data points as function values\n");
884  }
885 
886  if (mn.getNumElements() != _dimensions || mx.getNumElements() != _dimensions) {
887  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
888  "Your min/max values have different dimensionality than your data points\n");
889  }
890 
891  _krigingParameter = T(1.0);
892 
893  _lambda = T(1.0e-5);
894  _krigingParameter = T(1.0);
895 
896  _max = allocate(ndarray::makeVector(_dimensions));
897  _min = allocate(ndarray::makeVector(_dimensions));
898  _max.deep() = mx;
899  _min.deep() = mn;
900  _useMaxMin = 1;
901  normalizedData = allocate(ndarray::makeVector(_npts, _dimensions));
902  for (i = 0; i < _npts; i++) {
903  for (j = 0; j < _dimensions; j++) {
904  normalizedData[i][j] = (dataIn[i][j] - _min[j]) / (_max[j] - _min[j]);
905  // note the normalization by _max - _min in each dimension
906  }
907  }
908 
909  _kdTree.Initialize(normalizedData);
910  _npts = _kdTree.getNPoints();
911  _nFunctions = ff.template getSize<1>();
912  _function = allocate(ndarray::makeVector(_npts, _nFunctions));
913  _function.deep() = ff;
914 }
915 
916 template <typename T>
918  return _npts;
919 }
920 
921 template <typename T>
923  return _dimensions;
924 }
925 
926 template <typename T>
927 void GaussianProcess<T>::getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 1, 1> fn,
928  ndarray::Array<int, 1, 1> indices) const {
929  if (_nFunctions != 1) {
930  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
931  "Your function value array does not have enough room for all of the functions "
932  "in your GaussianProcess.\n");
933  }
934 
935  if (pts.template getSize<1>() != _dimensions) {
936  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
937  "Your pts array is constructed for points of the wrong dimensionality.\n");
938  }
939 
940  if (pts.template getSize<0>() != indices.getNumElements()) {
941  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
942  "You did not put enough room in your pts array to fit all the points "
943  "you asked for in your indices array.\n");
944  }
945 
946  if (fn.template getSize<0>() != indices.getNumElements()) {
947  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
948  "You did not provide enough room in your function value array "
949  "for all of the points you requested in your indices array.\n");
950  }
951 
952  int i, j;
953  for (i = 0; i < indices.template getSize<0>(); i++) {
954  pts[i] = _kdTree.getData(indices[i]); // do this first in case one of the indices is invalid.
955  // _kdTree.getData() will raise an exception in that case
956  fn[i] = _function[indices[i]][0];
957  if (_useMaxMin == 1) {
958  for (j = 0; j < _dimensions; j++) {
959  pts[i][j] *= (_max[j] - _min[j]);
960  pts[i][j] += _min[j];
961  }
962  }
963  }
964 }
965 
966 template <typename T>
967 void GaussianProcess<T>::getData(ndarray::Array<T, 2, 2> pts, ndarray::Array<T, 2, 2> fn,
968  ndarray::Array<int, 1, 1> indices) const {
969  if (fn.template getSize<1>() != _nFunctions) {
970  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
971  "Your function value array does not have enough room for all of the functions "
972  "in your GaussianProcess.\n");
973  }
974 
975  if (pts.template getSize<1>() != _dimensions) {
976  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
977  "Your pts array is constructed for points of the wrong dimensionality.\n");
978  }
979 
980  if (pts.template getSize<0>() != indices.getNumElements()) {
981  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
982  "You did not put enough room in your pts array to fit all the points "
983  "you asked for in your indices array.\n");
984  }
985 
986  if (fn.template getSize<0>() != indices.getNumElements()) {
987  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
988  "You did not provide enough room in your function value array "
989  "for all of the points you requested in your indices array.\n");
990  }
991 
992  int i, j;
993  for (i = 0; i < indices.template getSize<0>(); i++) {
994  pts[i] = _kdTree.getData(indices[i]); // do this first in case one of the indices is invalid.
995  // _kdTree.getData() will raise an exception in that case
996  for (j = 0; j < _nFunctions; j++) {
997  fn[i][j] = _function[indices[i]][j];
998  }
999  if (_useMaxMin == 1) {
1000  for (j = 0; j < _dimensions; j++) {
1001  pts[i][j] *= (_max[j] - _min[j]);
1002  pts[i][j] += _min[j];
1003  }
1004  }
1005  }
1006 }
1007 
1008 template <typename T>
1009 T GaussianProcess<T>::interpolate(ndarray::Array<T, 1, 1> variance, ndarray::Array<T, 1, 1> const &vin,
1010  int numberOfNeighbors) const {
1011  if (_nFunctions > 1) {
1012  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1013  "You need to call the version of GaussianProcess.interpolate() "
1014  "that accepts mu and variance arrays (which it populates with results). "
1015  "You are interpolating more than one function.");
1016  }
1017 
1018  if (numberOfNeighbors <= 0) {
1019  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1020  "Asked for zero or negative number of neighbors\n");
1021  }
1022 
1023  if (numberOfNeighbors > _kdTree.getNPoints()) {
1024  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1025  "Asked for more neighbors than you have data points\n");
1026  }
1027 
1028  if (variance.getNumElements() != _nFunctions) {
1029  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1030  "Your variance array is the incorrect size for the number "
1031  "of functions you are trying to interpolate\n");
1032  }
1033 
1034  if (vin.getNumElements() != _dimensions) {
1035  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1036  "You are interpolating at a point with different dimensionality than you data\n");
1037  }
1038 
1039  int i, j;
1040  T fbar, mu;
1041 
1042  ndarray::Array<T, 1, 1> covarianceTestPoint;
1043  ndarray::Array<int, 1, 1> neighbors;
1044  ndarray::Array<double, 1, 1> neighborDistances, vv;
1045 
1046  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1047  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1048 
1049  _timer.start();
1050 
1051  bb.resize(numberOfNeighbors, 1);
1052  xx.resize(numberOfNeighbors, 1);
1053 
1054  covariance.resize(numberOfNeighbors, numberOfNeighbors);
1055 
1056  covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1057 
1058  neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1059 
1060  neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1061 
1062  vv = allocate(ndarray::makeVector(_dimensions));
1063  if (_useMaxMin == 1) {
1064  // if you constructed this Gaussian process with minimum and maximum
1065  // values for the dimensions of your parameter space,
1066  // the point you are interpolating must be scaled to match the data so
1067  // that the selected nearest neighbors are appropriate
1068 
1069  for (i = 0; i < _dimensions; i++) vv[i] = (vin[i] - _min[i]) / (_max[i] - _min[i]);
1070  } else {
1071  vv = vin;
1072  }
1073 
1074  _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1075 
1076  _timer.addToSearch();
1077 
1078  fbar = 0.0;
1079  for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][0];
1080  fbar = fbar / double(numberOfNeighbors);
1081 
1082  for (i = 0; i < numberOfNeighbors; i++) {
1083  covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.getData(neighbors[i]));
1084 
1085  covariance(i, i) =
1086  (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
1087 
1088  for (j = i + 1; j < numberOfNeighbors; j++) {
1089  covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[j]));
1090  covariance(j, i) = covariance(i, j);
1091  }
1092  }
1093 
1094  _timer.addToIteration();
1095 
1096  // use Eigen's ldlt solver in place of matrix inversion (for speed purposes)
1097  ldlt.compute(covariance);
1098 
1099  for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][0] - fbar;
1100 
1101  xx = ldlt.solve(bb);
1102  _timer.addToEigen();
1103 
1104  mu = fbar;
1105 
1106  for (i = 0; i < numberOfNeighbors; i++) {
1107  mu += covarianceTestPoint[i] * xx(i, 0);
1108  }
1109 
1110  _timer.addToIteration();
1111 
1112  variance(0) = (*_covariogram)(vv, vv) + _lambda;
1113 
1114  for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1115 
1116  xx = ldlt.solve(bb);
1117 
1118  for (i = 0; i < numberOfNeighbors; i++) {
1119  variance(0) -= covarianceTestPoint[i] * xx(i, 0);
1120  }
1121 
1122  variance(0) = variance(0) * _krigingParameter;
1123 
1124  _timer.addToVariance();
1125  _timer.addToTotal(1);
1126 
1127  return mu;
1128 }
1129 
1130 template <typename T>
1131 void GaussianProcess<T>::interpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
1132  ndarray::Array<T, 1, 1> const &vin, int numberOfNeighbors) const {
1133  if (numberOfNeighbors <= 0) {
1134  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1135  "Asked for zero or negative number of neighbors\n");
1136  }
1137 
1138  if (numberOfNeighbors > _kdTree.getNPoints()) {
1139  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1140  "Asked for more neighbors than you have data points\n");
1141  }
1142 
1143  if (vin.getNumElements() != _dimensions) {
1144  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1145  "You are interpolating at a point with different dimensionality than you data\n");
1146  }
1147 
1148  if (mu.getNumElements() != _nFunctions || variance.getNumElements() != _nFunctions) {
1149  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1150  "Your mu and/or var arrays are improperly sized for the number of functions "
1151  "you are interpolating\n");
1152  }
1153 
1154  int i, j, ii;
1155  T fbar;
1156 
1157  ndarray::Array<T, 1, 1> covarianceTestPoint;
1158  ndarray::Array<int, 1, 1> neighbors;
1159  ndarray::Array<double, 1, 1> neighborDistances, vv;
1160 
1161  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1162  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1163 
1164  _timer.start();
1165 
1166  bb.resize(numberOfNeighbors, 1);
1167  xx.resize(numberOfNeighbors, 1);
1168  covariance.resize(numberOfNeighbors, numberOfNeighbors);
1169  covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1170  neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1171  neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1172 
1173  vv = allocate(ndarray::makeVector(_dimensions));
1174 
1175  if (_useMaxMin == 1) {
1176  // if you constructed this Gaussian process with minimum and maximum
1177  // values for the dimensions of your parameter space,
1178  // the point you are interpolating must be scaled to match the data so
1179  // that the selected nearest neighbors are appropriate
1180 
1181  for (i = 0; i < _dimensions; i++) vv[i] = (vin[i] - _min[i]) / (_max[i] - _min[i]);
1182  } else {
1183  vv = vin;
1184  }
1185 
1186  _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1187 
1188  _timer.addToSearch();
1189 
1190  for (i = 0; i < numberOfNeighbors; i++) {
1191  covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.getData(neighbors[i]));
1192 
1193  covariance(i, i) =
1194  (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
1195 
1196  for (j = i + 1; j < numberOfNeighbors; j++) {
1197  covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[j]));
1198  covariance(j, i) = covariance(i, j);
1199  }
1200  }
1201 
1202  _timer.addToIteration();
1203 
1204  // use Eigen's ldlt solver in place of matrix inversion (for speed purposes)
1205  ldlt.compute(covariance);
1206 
1207  for (ii = 0; ii < _nFunctions; ii++) {
1208  fbar = 0.0;
1209  for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][ii];
1210  fbar = fbar / double(numberOfNeighbors);
1211 
1212  for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][ii] - fbar;
1213  xx = ldlt.solve(bb);
1214 
1215  mu[ii] = fbar;
1216 
1217  for (i = 0; i < numberOfNeighbors; i++) {
1218  mu[ii] += covarianceTestPoint[i] * xx(i, 0);
1219  }
1220 
1221  } // ii = 0 through _nFunctions
1222 
1223  _timer.addToEigen();
1224 
1225  variance[0] = (*_covariogram)(vv, vv) + _lambda;
1226 
1227  for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1228 
1229  xx = ldlt.solve(bb);
1230 
1231  for (i = 0; i < numberOfNeighbors; i++) {
1232  variance[0] -= covarianceTestPoint[i] * xx(i, 0);
1233  }
1234  variance[0] = variance[0] * _krigingParameter;
1235 
1236  for (i = 1; i < _nFunctions; i++) variance[i] = variance[0];
1237 
1238  _timer.addToVariance();
1239  _timer.addToTotal(1);
1240 }
1241 
1242 template <typename T>
1243 T GaussianProcess<T>::selfInterpolate(ndarray::Array<T, 1, 1> variance, int dex,
1244  int numberOfNeighbors) const {
1245  if (_nFunctions > 1) {
1246  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1247  "You need to call the version of GaussianProcess.selfInterpolate() "
1248  "that accepts mu and variance arrays (which it populates with results). "
1249  "You are interpolating more than one function.");
1250  }
1251 
1252  if (variance.getNumElements() != _nFunctions) {
1253  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1254  "Your variance array is the incorrect size for the number "
1255  "of functions you are trying to interpolate\n");
1256  }
1257 
1258  if (numberOfNeighbors <= 0) {
1259  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1260  "Asked for zero or negative number of neighbors\n");
1261  }
1262 
1263  if (numberOfNeighbors + 1 > _kdTree.getNPoints()) {
1264  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1265  "Asked for more neighbors than you have data points\n");
1266  }
1267 
1268  if (dex < 0 || dex >= _kdTree.getNPoints()) {
1269  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1270  "Asked to self interpolate on a point that does not exist\n");
1271  }
1272 
1273  int i, j;
1274  T fbar, mu;
1275 
1276  ndarray::Array<T, 1, 1> covarianceTestPoint;
1277  ndarray::Array<int, 1, 1> selfNeighbors;
1278  ndarray::Array<double, 1, 1> selfDistances;
1279  ndarray::Array<int, 1, 1> neighbors;
1280  ndarray::Array<double, 1, 1> neighborDistances;
1281 
1282  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1283  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1284 
1285  _timer.start();
1286 
1287  bb.resize(numberOfNeighbors, 1);
1288  xx.resize(numberOfNeighbors, 1);
1289  covariance.resize(numberOfNeighbors, numberOfNeighbors);
1290  covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1291  neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1292  neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1293 
1294  selfNeighbors = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1295  selfDistances = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1296 
1297  // we don't use _useMaxMin because the data has already been normalized
1298 
1299  _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.getData(dex), numberOfNeighbors + 1);
1300 
1301  _timer.addToSearch();
1302 
1303  if (selfNeighbors[0] != dex) {
1304  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1305  "Nearest neighbor search in selfInterpolate did not find self\n");
1306  }
1307 
1308  // SelfNeighbors[0] will be the point itself (it is its own nearest neighbor)
1309  // We discard that for the interpolation calculation
1310  //
1311  // If you do not wish to do this, simply call the usual ::interpolate() method instead of
1312  //::selfInterpolate()
1313  for (i = 0; i < numberOfNeighbors; i++) {
1314  neighbors[i] = selfNeighbors[i + 1];
1315  neighborDistances[i] = selfDistances[i + 1];
1316  }
1317 
1318  fbar = 0.0;
1319  for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][0];
1320  fbar = fbar / double(numberOfNeighbors);
1321 
1322  for (i = 0; i < numberOfNeighbors; i++) {
1323  covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(neighbors[i]));
1324 
1325  covariance(i, i) =
1326  (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
1327 
1328  for (j = i + 1; j < numberOfNeighbors; j++) {
1329  covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[j]));
1330  covariance(j, i) = covariance(i, j);
1331  }
1332  }
1333  _timer.addToIteration();
1334 
1335  // use Eigen's ldlt solver in place of matrix inversion (for speed purposes)
1336  ldlt.compute(covariance);
1337 
1338  for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][0] - fbar;
1339  xx = ldlt.solve(bb);
1340  _timer.addToEigen();
1341 
1342  mu = fbar;
1343 
1344  for (i = 0; i < numberOfNeighbors; i++) {
1345  mu += covarianceTestPoint[i] * xx(i, 0);
1346  }
1347 
1348  variance(0) = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1349 
1350  for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1351 
1352  xx = ldlt.solve(bb);
1353 
1354  for (i = 0; i < numberOfNeighbors; i++) {
1355  variance(0) -= covarianceTestPoint[i] * xx(i, 0);
1356  }
1357 
1358  variance(0) = variance(0) * _krigingParameter;
1359  _timer.addToVariance();
1360  _timer.addToTotal(1);
1361 
1362  return mu;
1363 }
1364 
1365 template <typename T>
1366 void GaussianProcess<T>::selfInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
1367  int dex, int numberOfNeighbors) const {
1368  if (mu.getNumElements() != _nFunctions || variance.getNumElements() != _nFunctions) {
1369  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1370  "Your mu and/or var arrays are improperly sized for the number of functions "
1371  "you are interpolating\n");
1372  }
1373 
1374  if (numberOfNeighbors <= 0) {
1375  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1376  "Asked for zero or negative number of neighbors\n");
1377  }
1378 
1379  if (numberOfNeighbors + 1 > _kdTree.getNPoints()) {
1380  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1381  "Asked for more neighbors than you have data points\n");
1382  }
1383 
1384  if (dex < 0 || dex >= _kdTree.getNPoints()) {
1385  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1386  "Asked to self interpolate on a point that does not exist\n");
1387  }
1388 
1389  int i, j, ii;
1390  T fbar;
1391 
1392  ndarray::Array<T, 1, 1> covarianceTestPoint;
1393  ndarray::Array<int, 1, 1> selfNeighbors;
1394  ndarray::Array<double, 1, 1> selfDistances;
1395  ndarray::Array<int, 1, 1> neighbors;
1396  ndarray::Array<double, 1, 1> neighborDistances;
1397 
1398  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1399  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1400 
1401  _timer.start();
1402 
1403  bb.resize(numberOfNeighbors, 1);
1404  xx.resize(numberOfNeighbors, 1);
1405  covariance.resize(numberOfNeighbors, numberOfNeighbors);
1406  covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1407  neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1408  neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1409 
1410  selfNeighbors = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1411  selfDistances = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1412 
1413  // we don't use _useMaxMin because the data has already been normalized
1414 
1415  _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.getData(dex), numberOfNeighbors + 1);
1416 
1417  _timer.addToSearch();
1418 
1419  if (selfNeighbors[0] != dex) {
1420  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1421  "Nearest neighbor search in selfInterpolate did not find self\n");
1422  }
1423 
1424  // SelfNeighbors[0] will be the point itself (it is its own nearest neighbor)
1425  // We discard that for the interpolation calculation
1426  //
1427  // If you do not wish to do this, simply call the usual ::interpolate() method instead of
1428  //::selfInterpolate()
1429  for (i = 0; i < numberOfNeighbors; i++) {
1430  neighbors[i] = selfNeighbors[i + 1];
1431  neighborDistances[i] = selfDistances[i + 1];
1432  }
1433 
1434  for (i = 0; i < numberOfNeighbors; i++) {
1435  covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(neighbors[i]));
1436 
1437  covariance(i, i) =
1438  (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
1439 
1440  for (j = i + 1; j < numberOfNeighbors; j++) {
1441  covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[j]));
1442  covariance(j, i) = covariance(i, j);
1443  }
1444  }
1445  _timer.addToIteration();
1446 
1447  // use Eigen's ldlt solver in place of matrix inversion (for speed purposes)
1448  ldlt.compute(covariance);
1449 
1450  for (ii = 0; ii < _nFunctions; ii++) {
1451  fbar = 0.0;
1452  for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][ii];
1453  fbar = fbar / double(numberOfNeighbors);
1454 
1455  for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][ii] - fbar;
1456  xx = ldlt.solve(bb);
1457 
1458  mu[ii] = fbar;
1459 
1460  for (i = 0; i < numberOfNeighbors; i++) {
1461  mu[ii] += covarianceTestPoint[i] * xx(i, 0);
1462  }
1463  } // ii = 0 through _nFunctions
1464 
1465  _timer.addToEigen();
1466 
1467  variance[0] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1468 
1469  for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1470 
1471  xx = ldlt.solve(bb);
1472 
1473  for (i = 0; i < numberOfNeighbors; i++) {
1474  variance[0] -= covarianceTestPoint[i] * xx(i, 0);
1475  }
1476 
1477  variance[0] = variance[0] * _krigingParameter;
1478 
1479  for (i = 1; i < _nFunctions; i++) variance[i] = variance[0];
1480 
1481  _timer.addToVariance();
1482  _timer.addToTotal(1);
1483 }
1484 
1485 template <typename T>
1486 void GaussianProcess<T>::batchInterpolate(ndarray::Array<T, 1, 1> mu, ndarray::Array<T, 1, 1> variance,
1487  ndarray::Array<T, 2, 2> const &queries) const {
1488  int i, j, ii, nQueries;
1489 
1490  nQueries = queries.template getSize<0>();
1491 
1492  if (_nFunctions != 1) {
1493  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1494  "Your mu and variance arrays do not have room for all of the functions "
1495  "as you are trying to interpolate\n");
1496  }
1497 
1498  if (mu.getNumElements() != nQueries || variance.getNumElements() != nQueries) {
1499  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1500  "Your mu and variance arrays do not have room for all of the points "
1501  "at which you are trying to interpolate your function.\n");
1502  }
1503 
1504  if (queries.template getSize<1>() != _dimensions) {
1505  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1506  "The points you passed to batchInterpolate are of the wrong "
1507  "dimensionality for your Gaussian Process\n");
1508  }
1509 
1510  T fbar;
1511  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> batchCovariance, batchbb, batchxx;
1512  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> queryCovariance;
1513  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1514 
1515  ndarray::Array<T, 1, 1> v1;
1516 
1517  _timer.start();
1518 
1519  v1 = allocate(ndarray::makeVector(_dimensions));
1520  batchbb.resize(_npts, 1);
1521  batchxx.resize(_npts, 1);
1522  batchCovariance.resize(_npts, _npts);
1523  queryCovariance.resize(_npts, 1);
1524 
1525  for (i = 0; i < _npts; i++) {
1526  batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1527  for (j = i + 1; j < _npts; j++) {
1528  batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1529  batchCovariance(j, i) = batchCovariance(i, j);
1530  }
1531  }
1532  _timer.addToIteration();
1533 
1534  ldlt.compute(batchCovariance);
1535 
1536  fbar = 0.0;
1537  for (i = 0; i < _npts; i++) {
1538  fbar += _function[i][0];
1539  }
1540  fbar = fbar / T(_npts);
1541 
1542  for (i = 0; i < _npts; i++) {
1543  batchbb(i, 0) = _function[i][0] - fbar;
1544  }
1545  batchxx = ldlt.solve(batchbb);
1546  _timer.addToEigen();
1547 
1548  for (ii = 0; ii < nQueries; ii++) {
1549  for (i = 0; i < _dimensions; i++) v1[i] = queries[ii][i];
1550  if (_useMaxMin == 1) {
1551  for (i = 0; i < _dimensions; i++) v1[i] = (v1[i] - _min[i]) / (_max[i] - _min[i]);
1552  }
1553  mu(ii) = fbar;
1554  for (i = 0; i < _npts; i++) {
1555  mu(ii) += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1556  }
1557  }
1558  _timer.addToIteration();
1559 
1560  for (ii = 0; ii < nQueries; ii++) {
1561  for (i = 0; i < _dimensions; i++) v1[i] = queries[ii][i];
1562  if (_useMaxMin == 1) {
1563  for (i = 0; i < _dimensions; i++) v1[i] = (v1[i] - _min[i]) / (_max[i] - _min[i]);
1564  }
1565 
1566  for (i = 0; i < _npts; i++) {
1567  batchbb(i, 0) = (*_covariogram)(v1, _kdTree.getData(i));
1568  queryCovariance(i, 0) = batchbb(i, 0);
1569  }
1570  batchxx = ldlt.solve(batchbb);
1571 
1572  variance[ii] = (*_covariogram)(v1, v1) + _lambda;
1573 
1574  for (i = 0; i < _npts; i++) {
1575  variance[ii] -= queryCovariance(i, 0) * batchxx(i);
1576  }
1577 
1578  variance[ii] = variance[ii] * _krigingParameter;
1579  }
1580 
1581  _timer.addToVariance();
1582  _timer.addToTotal(nQueries);
1583 }
1584 
1585 template <typename T>
1586 void GaussianProcess<T>::batchInterpolate(ndarray::Array<T, 2, 2> mu, ndarray::Array<T, 2, 2> variance,
1587  ndarray::Array<T, 2, 2> const &queries) const {
1588  int i, j, ii, nQueries, ifn;
1589 
1590  nQueries = queries.template getSize<0>();
1591 
1592  if (mu.template getSize<0>() != nQueries || variance.template getSize<0>() != nQueries) {
1593  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1594  "Your output arrays do not have room for all of the points at which "
1595  "you are interpolating your functions.\n");
1596  }
1597 
1598  if (mu.template getSize<1>() != _nFunctions || variance.template getSize<1>() != _nFunctions) {
1599  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1600  "Your output arrays do not have room for all of the functions you are "
1601  "interpolating\n");
1602  }
1603 
1604  if (queries.template getSize<1>() != _dimensions) {
1605  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1606  "The points at which you are interpolating your functions have the "
1607  "wrong dimensionality.\n");
1608  }
1609 
1610  T fbar;
1611  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> batchCovariance, batchbb, batchxx;
1612  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> queryCovariance;
1613  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1614 
1615  ndarray::Array<T, 1, 1> v1;
1616 
1617  _timer.start();
1618 
1619  v1 = allocate(ndarray::makeVector(_dimensions));
1620  batchbb.resize(_npts, 1);
1621  batchxx.resize(_npts, 1);
1622  batchCovariance.resize(_npts, _npts);
1623  queryCovariance.resize(_npts, 1);
1624 
1625  for (i = 0; i < _npts; i++) {
1626  batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1627  for (j = i + 1; j < _npts; j++) {
1628  batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1629  batchCovariance(j, i) = batchCovariance(i, j);
1630  }
1631  }
1632 
1633  _timer.addToIteration();
1634 
1635  ldlt.compute(batchCovariance);
1636 
1637  _timer.addToEigen();
1638 
1639  for (ifn = 0; ifn < _nFunctions; ifn++) {
1640  fbar = 0.0;
1641  for (i = 0; i < _npts; i++) {
1642  fbar += _function[i][ifn];
1643  }
1644  fbar = fbar / T(_npts);
1645  _timer.addToIteration();
1646 
1647  for (i = 0; i < _npts; i++) {
1648  batchbb(i, 0) = _function[i][ifn] - fbar;
1649  }
1650  batchxx = ldlt.solve(batchbb);
1651  _timer.addToEigen();
1652 
1653  for (ii = 0; ii < nQueries; ii++) {
1654  for (i = 0; i < _dimensions; i++) v1[i] = queries[ii][i];
1655  if (_useMaxMin == 1) {
1656  for (i = 0; i < _dimensions; i++) v1[i] = (v1[i] - _min[i]) / (_max[i] - _min[i]);
1657  }
1658  mu[ii][ifn] = fbar;
1659  for (i = 0; i < _npts; i++) {
1660  mu[ii][ifn] += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1661  }
1662  }
1663 
1664  } // ifn = 0 to _nFunctions
1665 
1666  _timer.addToIteration();
1667  for (ii = 0; ii < nQueries; ii++) {
1668  for (i = 0; i < _dimensions; i++) v1[i] = queries[ii][i];
1669  if (_useMaxMin == 1) {
1670  for (i = 0; i < _dimensions; i++) v1[i] = (v1[i] - _min[i]) / (_max[i] - _min[i]);
1671  }
1672 
1673  for (i = 0; i < _npts; i++) {
1674  batchbb(i, 0) = (*_covariogram)(v1, _kdTree.getData(i));
1675  queryCovariance(i, 0) = batchbb(i, 0);
1676  }
1677  batchxx = ldlt.solve(batchbb);
1678 
1679  variance[ii][0] = (*_covariogram)(v1, v1) + _lambda;
1680 
1681  for (i = 0; i < _npts; i++) {
1682  variance[ii][0] -= queryCovariance(i, 0) * batchxx(i);
1683  }
1684 
1685  variance[ii][0] = variance[ii][0] * _krigingParameter;
1686  for (i = 1; i < _nFunctions; i++) variance[ii][i] = variance[ii][0];
1687  }
1688  _timer.addToVariance();
1689  _timer.addToTotal(nQueries);
1690 }
1691 
1692 template <typename T>
1693 void GaussianProcess<T>::batchInterpolate(ndarray::Array<T, 1, 1> mu,
1694  ndarray::Array<T, 2, 2> const &queries) const {
1695  int i, j, ii, nQueries;
1696 
1697  nQueries = queries.template getSize<0>();
1698 
1699  if (_nFunctions != 1) {
1700  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1701  "Your output array does not have enough room for all of the functions "
1702  "you are trying to interpolate.\n");
1703  }
1704 
1705  if (queries.template getSize<1>() != _dimensions) {
1706  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1707  "The points at which you are trying to interpolate your function are "
1708  "of the wrong dimensionality.\n");
1709  }
1710 
1711  if (mu.getNumElements() != nQueries) {
1712  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1713  "Your output array does not have enough room for all of the points "
1714  "at which you are trying to interpolate your function.\n");
1715  }
1716 
1717  T fbar;
1718  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> batchCovariance, batchbb, batchxx;
1719  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> queryCovariance;
1720  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1721 
1722  ndarray::Array<T, 1, 1> v1;
1723 
1724  _timer.start();
1725 
1726  v1 = allocate(ndarray::makeVector(_dimensions));
1727 
1728  batchbb.resize(_npts, 1);
1729  batchxx.resize(_npts, 1);
1730  batchCovariance.resize(_npts, _npts);
1731  queryCovariance.resize(_npts, 1);
1732 
1733  for (i = 0; i < _npts; i++) {
1734  batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1735  for (j = i + 1; j < _npts; j++) {
1736  batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1737  batchCovariance(j, i) = batchCovariance(i, j);
1738  }
1739  }
1740  _timer.addToIteration();
1741 
1742  ldlt.compute(batchCovariance);
1743 
1744  fbar = 0.0;
1745  for (i = 0; i < _npts; i++) {
1746  fbar += _function[i][0];
1747  }
1748  fbar = fbar / T(_npts);
1749 
1750  for (i = 0; i < _npts; i++) {
1751  batchbb(i, 0) = _function[i][0] - fbar;
1752  }
1753  batchxx = ldlt.solve(batchbb);
1754  _timer.addToEigen();
1755 
1756  for (ii = 0; ii < nQueries; ii++) {
1757  for (i = 0; i < _dimensions; i++) v1[i] = queries[ii][i];
1758  if (_useMaxMin == 1) {
1759  for (i = 0; i < _dimensions; i++) v1[i] = (v1[i] - _min[i]) / (_max[i] - _min[i]);
1760  }
1761 
1762  mu(ii) = fbar;
1763  for (i = 0; i < _npts; i++) {
1764  mu(ii) += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1765  }
1766  }
1767  _timer.addToIteration();
1768  _timer.addToTotal(nQueries);
1769 }
1770 
1771 template <typename T>
1772 void GaussianProcess<T>::batchInterpolate(ndarray::Array<T, 2, 2> mu,
1773  ndarray::Array<T, 2, 2> const &queries) const {
1774  int i, j, ii, nQueries, ifn;
1775 
1776  nQueries = queries.template getSize<0>();
1777 
1778  if (mu.template getSize<0>() != nQueries) {
1779  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1780  "Your output array does not have enough room for all of the points "
1781  "at which you want to interpolate your functions.\n");
1782  }
1783 
1784  if (mu.template getSize<1>() != _nFunctions) {
1785  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1786  "Your output array does not have enough room for all of the functions "
1787  "you are trying to interpolate.\n");
1788  }
1789 
1790  if (queries.template getSize<1>() != _dimensions) {
1791  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1792  "The points at which you are interpolating your functions do not "
1793  "have the correct dimensionality.\n");
1794  }
1795 
1796  T fbar;
1797  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> batchCovariance, batchbb, batchxx;
1798  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> queryCovariance;
1799  Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1800 
1801  ndarray::Array<T, 1, 1> v1;
1802 
1803  _timer.start();
1804 
1805  v1 = allocate(ndarray::makeVector(_dimensions));
1806 
1807  batchbb.resize(_npts, 1);
1808  batchxx.resize(_npts, 1);
1809  batchCovariance.resize(_npts, _npts);
1810  queryCovariance.resize(_npts, 1);
1811 
1812  for (i = 0; i < _npts; i++) {
1813  batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1814  for (j = i + 1; j < _npts; j++) {
1815  batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1816  batchCovariance(j, i) = batchCovariance(i, j);
1817  }
1818  }
1819 
1820  _timer.addToIteration();
1821 
1822  ldlt.compute(batchCovariance);
1823 
1824  _timer.addToEigen();
1825 
1826  for (ifn = 0; ifn < _nFunctions; ifn++) {
1827  fbar = 0.0;
1828  for (i = 0; i < _npts; i++) {
1829  fbar += _function[i][ifn];
1830  }
1831  fbar = fbar / T(_npts);
1832 
1833  _timer.addToIteration();
1834 
1835  for (i = 0; i < _npts; i++) {
1836  batchbb(i, 0) = _function[i][ifn] - fbar;
1837  }
1838  batchxx = ldlt.solve(batchbb);
1839  _timer.addToEigen();
1840 
1841  for (ii = 0; ii < nQueries; ii++) {
1842  for (i = 0; i < _dimensions; i++) v1[i] = queries[ii][i];
1843  if (_useMaxMin == 1) {
1844  for (i = 0; i < _dimensions; i++) v1[i] = (v1[i] - _min[i]) / (_max[i] - _min[i]);
1845  }
1846 
1847  mu[ii][ifn] = fbar;
1848  for (i = 0; i < _npts; i++) {
1849  mu[ii][ifn] += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1850  }
1851  }
1852 
1853  } // ifn = 0 through _nFunctions
1854 
1855  _timer.addToTotal(nQueries);
1856 }
1857 
1858 template <typename T>
1859 void GaussianProcess<T>::addPoint(ndarray::Array<T, 1, 1> const &vin, T f) {
1860  int i, j;
1861 
1862  if (_nFunctions != 1) {
1863  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1864  "You are calling the wrong addPoint; you need a vector of functions\n");
1865  }
1866 
1867  if (vin.getNumElements() != _dimensions) {
1868  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1869  "You are trying to add a point of the wrong dimensionality to "
1870  "your GaussianProcess.\n");
1871  }
1872 
1873  ndarray::Array<T, 1, 1> v;
1874  v = allocate(ndarray::makeVector(_dimensions));
1875 
1876  for (i = 0; i < _dimensions; i++) {
1877  v[i] = vin[i];
1878  if (_useMaxMin == 1) {
1879  v[i] = (v[i] - _min[i]) / (_max[i] - _min[i]);
1880  }
1881  }
1882 
1883  if (_npts == _room) {
1884  ndarray::Array<T, 2, 2> buff;
1885  buff = allocate(ndarray::makeVector(_npts, _nFunctions));
1886  buff.deep() = _function;
1887 
1888  _room += _roomStep;
1889  _function = allocate(ndarray::makeVector(_room, _nFunctions));
1890  for (i = 0; i < _npts; i++) {
1891  for (j = 0; j < _nFunctions; j++) {
1892  _function[i][j] = buff[i][j];
1893  }
1894  }
1895  }
1896 
1897  _function[_npts][0] = f;
1898 
1899  _kdTree.addPoint(v);
1900  _npts = _kdTree.getNPoints();
1901 }
1902 
1903 template <typename T>
1904 void GaussianProcess<T>::addPoint(ndarray::Array<T, 1, 1> const &vin, ndarray::Array<T, 1, 1> const &f) {
1905  if (vin.getNumElements() != _dimensions) {
1906  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1907  "You are trying to add a point of the wrong dimensionality to "
1908  "your GaussianProcess.\n");
1909  }
1910 
1911  if (f.template getSize<0>() != _nFunctions) {
1912  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1913  "You are not adding the correct number of function values to "
1914  "your GaussianProcess.\n");
1915  }
1916 
1917  int i, j;
1918 
1919  ndarray::Array<T, 1, 1> v;
1920  v = allocate(ndarray::makeVector(_dimensions));
1921 
1922  for (i = 0; i < _dimensions; i++) {
1923  v[i] = vin[i];
1924  if (_useMaxMin == 1) {
1925  v[i] = (v[i] - _min[i]) / (_max[i] - _min[i]);
1926  }
1927  }
1928 
1929  if (_npts == _room) {
1930  ndarray::Array<T, 2, 2> buff;
1931  buff = allocate(ndarray::makeVector(_npts, _nFunctions));
1932  buff.deep() = _function;
1933 
1934  _room += _roomStep;
1935  _function = allocate(ndarray::makeVector(_room, _nFunctions));
1936  for (i = 0; i < _npts; i++) {
1937  for (j = 0; j < _nFunctions; j++) {
1938  _function[i][j] = buff[i][j];
1939  }
1940  }
1941  }
1942  for (i = 0; i < _nFunctions; i++) _function[_npts][i] = f[i];
1943 
1944  _kdTree.addPoint(v);
1945  _npts = _kdTree.getNPoints();
1946 }
1947 
1948 template <typename T>
1950  int i, j;
1951 
1952  _kdTree.removePoint(dex);
1953 
1954  for (i = dex; i < _npts; i++) {
1955  for (j = 0; j < _nFunctions; j++) {
1956  _function[i][j] = _function[i + 1][j];
1957  }
1958  }
1959  _npts = _kdTree.getNPoints();
1960 }
1961 
1962 template <typename T>
1964  _krigingParameter = kk;
1965 }
1966 
1967 template <typename T>
1968 void GaussianProcess<T>::setCovariogram(std::shared_ptr<Covariogram<T> > const &covar) {
1969  _covariogram = covar;
1970 }
1971 
1972 template <typename T>
1974  _lambda = lambda;
1975 }
1976 
1977 template <typename T>
1979  return _timer;
1980 }
1981 
1982 template <typename T>
1984 
1985 template <typename T>
1986 T Covariogram<T>::operator()(ndarray::Array<const T, 1, 1> const &p1,
1987  ndarray::Array<const T, 1, 1> const &p2) const {
1988  std::cout << "by the way, you are calling the wrong operator\n";
1989  exit(1);
1990  return T(1.0);
1991 }
1992 
1993 template <typename T>
1995 
1996 template <typename T>
1998  _ellSquared = 1.0;
1999 }
2000 
2001 template <typename T>
2003  _ellSquared = ellSquared;
2004 }
2005 
2006 template <typename T>
2007 T SquaredExpCovariogram<T>::operator()(ndarray::Array<const T, 1, 1> const &p1,
2008  ndarray::Array<const T, 1, 1> const &p2) const {
2009  int i;
2010  T d;
2011  d = 0.0;
2012  for (i = 0; i < p1.template getSize<0>(); i++) {
2013  d += (p1[i] - p2[i]) * (p1[i] - p2[i]);
2014  }
2015 
2016  d = d / _ellSquared;
2017  return T(exp(-0.5 * d));
2018 }
2019 
2020 template <typename T>
2022 
2023 template <typename T>
2025  _sigma0 = 1.0;
2026  _sigma1 = 1.0;
2027 }
2028 
2029 template <typename T>
2030 T NeuralNetCovariogram<T>::operator()(ndarray::Array<const T, 1, 1> const &p1,
2031  ndarray::Array<const T, 1, 1> const &p2) const {
2032  int i, dim;
2033  double num, denom1, denom2, arg;
2034 
2035  dim = p1.template getSize<0>();
2036 
2037  num = 2.0 * _sigma0;
2038  denom1 = 1.0 + 2.0 * _sigma0;
2039  denom2 = 1.0 + 2.0 * _sigma0;
2040 
2041  for (i = 0; i < dim; i++) {
2042  num += 2.0 * p1[i] * p2[i] * _sigma1;
2043  denom1 += 2.0 * p1[i] * p1[i] * _sigma1;
2044  denom2 += 2.0 * p2[i] * p2[i] * _sigma1;
2045  }
2046 
2047  arg = num / ::sqrt(denom1 * denom2);
2048  return T(2.0 * (::asin(arg)) / 3.141592654);
2049 }
2050 
2051 template <typename T>
2053  _sigma0 = sigma0;
2054 }
2055 
2056 template <typename T>
2058  _sigma1 = sigma1;
2059 }
2060 
2061 #define INSTANTIATEGP(T) \
2062  template class KdTree<T>; \
2063  template class GaussianProcess<T>; \
2064  template class Covariogram<T>; \
2065  template class SquaredExpCovariogram<T>; \
2066  template class NeuralNetCovariogram<T>;
2067 
2068 INSTANTIATEGP(double);
2069 }
2070 }
2071 }
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
afw::table::Key< afw::table::Array< VariancePixelT > > variance
This is a structure for keeping track of how long the interpolation methods spend on different parts ...
The parent class of covariogram functions for use in Gaussian Process interpolation.
The data for GaussianProcess is stored in a KD tree to facilitate nearest-neighbor searches...
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
void removePoint(int dex)
This will remove a point from the data set.
#define INSTANTIATEGP(T)