36 GaussianProcessTimer::GaussianProcessTimer() {
37 _interpolationCount = 0;
45 void GaussianProcessTimer::reset() {
46 _interpolationCount = 0;
54 void GaussianProcessTimer::start() {
55 _before = double(lsst::daf::base::DateTime::now().
get() * 24 * 60 * 60);
59 void GaussianProcessTimer::addToEigen() {
61 after = double(lsst::daf::base::DateTime::now().
get() * 24 * 60 * 60);
62 _eigenTime += after - _before;
66 void GaussianProcessTimer::addToVariance() {
68 after = double(lsst::daf::base::DateTime::now().
get() * 24 * 60 * 60);
69 _varianceTime += after - _before;
73 void GaussianProcessTimer::addToSearch() {
75 after = double(lsst::daf::base::DateTime::now().
get() * 24 * 60 * 60);
76 _searchTime += after - _before;
80 void GaussianProcessTimer::addToIteration() {
82 after = double(lsst::daf::base::DateTime::now().
get() * 24 * 60 * 60);
83 _iterationTime += after - _before;
87 void GaussianProcessTimer::addToTotal(
int i) {
89 after = double(lsst::daf::base::DateTime::now().
get() * 24 * 60 * 60);
90 _totalTime += after - _beginning;
91 _interpolationCount += i;
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";
103 template <
typename T>
107 _npts = dt.template getSize<0>();
108 _dimensions = dt.template getSize<1>();
111 _inn = allocate(ndarray::makeVector(_npts));
116 _data = allocate(ndarray::makeVector(_room, _dimensions));
120 _tree = allocate(ndarray::makeVector(_room, 4));
122 for (i = 0; i < _npts; i++) {
126 _organize(_inn, _npts, -1, -1);
130 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Failed to properly initialize KdTree\n");
134 template <
typename T>
136 ndarray::Array<const T, 1, 1>
const &v,
int n_nn)
const {
138 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
139 "Asked for more neighbors than kd tree contains\n");
143 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
144 "Asked for zero or a negative number of neighbors\n");
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");
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");
159 _neighborCandidates = allocate(ndarray::makeVector(n_nn));
160 _neighborDistances = allocate(ndarray::makeVector(n_nn));
162 _neighborsWanted = n_nn;
164 for (i = 0; i < n_nn; i++) _neighborDistances[i] = -1.0;
166 start = _findNode(v);
168 _neighborDistances[0] = _distance(v, _data[start]);
169 _neighborCandidates[0] = start;
172 for (i = 1; i < 4; i++) {
173 if (_tree[start][i] >= 0) {
174 _lookForNeighbors(v, _tree[start][i], start);
178 for (i = 0; i < n_nn; i++) {
179 neighdex[i] = _neighborCandidates[i];
180 dd[i] = _neighborDistances[i];
184 template <
typename T>
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");
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");
196 return _data[ipt][idim];
199 template <
typename T>
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");
209 template <
typename T>
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");
216 int i, j, node, dim, dir;
219 dim = _tree[node][DIMENSION] + 1;
220 if (dim == _dimensions) dim = 0;
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));
226 dbuff.deep() = _data;
227 tbuff.deep() = _tree;
231 _tree = allocate(ndarray::makeVector(_room, 4));
232 _data = allocate(ndarray::makeVector(_room, _dimensions));
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];
240 _tree[_npts][DIMENSION] = dim;
241 _tree[_npts][
PARENT] = node;
242 i = _tree[node][DIMENSION];
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");
249 _tree[node][LT] = _npts;
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");
256 _tree[node][GEQ] = _npts;
259 _tree[_npts][LT] = -1;
260 _tree[_npts][GEQ] = -1;
261 for (i = 0; i < _dimensions; i++) {
262 _data[_npts][i] = v[i];
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");
273 template <
typename T>
278 template <
typename T>
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");
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");
290 v[0] = _tree[dex][DIMENSION];
291 v[1] = _tree[dex][LT];
292 v[2] = _tree[dex][GEQ];
293 v[3] = _tree[dex][
PARENT];
296 template <
typename T>
298 if (_npts == 1 && _tree[0][
PARENT] < 0 && _masterParent == 0) {
304 std::vector<int> isparent;
308 for (i = 0; i < _npts; i++) isparent.push_back(0);
311 for (i = 0; i < _npts; i++) {
312 if (_tree[i][
PARENT] < 0) j++;
315 std::cout <<
"_tree FAILURE " << j <<
" _masterParents\n";
319 for (i = 0; i < _npts; i++) {
320 if (_tree[i][
PARENT] >= 0) {
321 isparent[_tree[i][
PARENT]]++;
325 for (i = 0; i < _npts; i++) {
326 if (isparent[i] > 2) {
327 std::cout <<
"_tree FAILURE " << i <<
" is parent to " << isparent[i] <<
"\n";
332 for (i = 0; i < _npts; i++) {
333 if (_tree[i][
PARENT] >= 0) {
334 if (_tree[_tree[i][
PARENT]][LT] == i)
338 output = _walkUpTree(_tree[i][
PARENT], j, i);
340 if (output != _masterParent) {
346 if (output != _masterParent)
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;
357 std::vector<std::vector<T> > toSort;
358 std::vector<T> toSortElement;
366 for (i = 0; i < _dimensions; i++) {
369 for (j = 0; j < ct; j++) {
370 mean += _data[use[j]][i];
371 var += _data[use[j]][i] * _data[use[j]][i];
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])) {
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);
398 std::stable_sort(toSort.begin(), toSort.end());
402 while (k > 0 && toSort[k][0] == toSort[k - 1][0]) k--;
404 while (l < ct - 1 && toSort[l][0] == toSort[ct / 2][0]) l++;
406 if ((ct / 2 - k) < (l - ct / 2) || l == ct - 1)
411 for (i = 0; i < ct; i++) {
412 use[i] = int(toSort[i][1]);
416 if (parent >= 0) _tree[parent][dir] = daughter;
417 _tree[daughter][DIMENSION] = idim;
418 _tree[daughter][
PARENT] = parent;
421 _organize(use[ndarray::view(j + 1, use.getSize<0>())], ct - j - 1, daughter, GEQ);
423 _tree[daughter][GEQ] = -1;
426 _organize(use, j, daughter, LT);
428 _tree[daughter][LT] = -1;
434 if (parent >= 0) _tree[parent][dir] = daughter;
436 idim = _tree[parent][DIMENSION] + 1;
438 if (idim >= _dimensions) idim = 0;
440 _tree[daughter][DIMENSION] = idim;
441 _tree[daughter][LT] = -1;
442 _tree[daughter][GEQ] = -1;
443 _tree[daughter][
PARENT] = parent;
447 _masterParent = daughter;
451 template <
typename T>
452 int KdTree<T>::_findNode(ndarray::Array<const T, 1, 1>
const &v)
const {
453 int consider, next, dim;
455 dim = _tree[_masterParent][DIMENSION];
457 if (v[dim] < _data[_masterParent][dim])
458 consider = _tree[_masterParent][LT];
460 consider = _tree[_masterParent][GEQ];
467 dim = _tree[consider][DIMENSION];
468 if (v[dim] < _data[consider][dim])
469 next = _tree[consider][LT];
471 next = _tree[consider][GEQ];
477 template <
typename T>
478 void KdTree<T>::_lookForNeighbors(ndarray::Array<const T, 1, 1>
const &v,
int consider,
int from)
const {
482 dd = _distance(v, _data[consider]);
484 if (_neighborsFound < _neighborsWanted || dd < _neighborDistances[_neighborsWanted - 1]) {
485 for (j = 0; j < _neighborsFound && _neighborDistances[j] < dd; j++)
488 for (i = _neighborsWanted - 1; i > j; i--) {
489 _neighborDistances[i] = _neighborDistances[i - 1];
490 _neighborCandidates[i] = _neighborCandidates[i - 1];
493 _neighborDistances[j] = dd;
494 _neighborCandidates[j] = consider;
496 if (_neighborsFound < _neighborsWanted) _neighborsFound++;
499 if (_tree[consider][
PARENT] == from) {
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);
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);
518 if (_tree[consider][LT] == from) {
524 j = _tree[consider][going];
527 i = _tree[consider][DIMENSION];
530 dd = v[i] - _data[consider][i];
532 dd = _data[consider][i] - v[i];
534 if (dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted) {
535 _lookForNeighbors(v, j, consider);
540 if (_tree[consider][
PARENT] >= 0) {
541 _lookForNeighbors(v, _tree[consider][
PARENT], consider);
546 template <
typename T>
547 int KdTree<T>::_walkUpTree(
int target,
int dir,
int root)
const {
557 if (_data[root][_tree[target][DIMENSION]] >= _data[target][_tree[target][DIMENSION]]) {
561 if (_data[root][_tree[target][DIMENSION]] < _data[target][_tree[target][DIMENSION]]) {
566 if (_tree[target][
PARENT] >= 0) {
567 if (_tree[_tree[target][
PARENT]][LT] == target)
571 output = output * _walkUpTree(_tree[target][
PARENT], i, root);
574 output = output * target;
581 template <
typename T>
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");
589 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
590 "There is only one point left in this KdTree. You cannot call removePoint\n");
593 int nl, nr, i, j, k, side;
598 if (_tree[target][LT] >= 0) {
600 _count(_tree[target][LT], &nl);
603 if (_tree[target][GEQ] >= 0) {
605 _count(_tree[target][GEQ], &nr);
608 if (nl == 0 && nr == 0) {
609 k = _tree[target][
PARENT];
611 if (_tree[k][LT] == target)
613 else if (_tree[k][GEQ] == target)
617 else if ((nl == 0 && nr > 0) || (nr == 0 && nl > 0)) {
623 k = _tree[target][
PARENT];
625 if (_tree[k][LT] == target) {
626 _tree[k][LT] = _tree[target][side];
627 _tree[_tree[k][LT]][
PARENT] = k;
630 _tree[k][GEQ] = _tree[target][side];
631 _tree[_tree[k][GEQ]][
PARENT] = k;
634 _masterParent = _tree[target][side];
635 _tree[_tree[target][side]][
PARENT] = -1;
644 k = _tree[target][
PARENT];
646 _masterParent = _tree[target][side];
647 _tree[_masterParent][
PARENT] = -1;
649 if (_tree[k][LT] == target) {
650 _tree[k][LT] = _tree[target][side];
651 _tree[_tree[k][LT]][
PARENT] = k;
653 _tree[k][GEQ] = _tree[target][side];
654 _tree[_tree[k][GEQ]][
PARENT] = k;
658 root = _tree[target][3 - side];
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];
667 for (j = 0; j < _dimensions; j++) _data[i - 1][j] = _data[i][j];
670 for (i = 0; i < _npts; i++) {
671 for (j = 1; j < 4; j++) {
672 if (_tree[i][j] > target) _tree[i][j]--;
676 if (_masterParent > target) _masterParent--;
682 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Subtracting from KdTree failed\n");
686 template <
typename T>
690 if (_tree[where][LT] >= 0) {
692 _count(_tree[where][LT], ct);
694 if (_tree[where][GEQ] >= 0) {
696 _count(_tree[where][GEQ], ct);
700 template <
typename T>
701 void KdTree<T>::_reassign(
int target) {
704 where = _masterParent;
705 if (_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]])
710 k = _tree[where][dir];
713 if (_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]])
717 k = _tree[where][dir];
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;
726 if (_tree[target][DIMENSION] == _dimensions) _tree[target][DIMENSION] = 0;
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]);
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 {
743 dd = p1.template getSize<0>();
745 for (i = 0; i < dd; i++) ans += (p1[i] - p2[i]) * (p1[i] - p2[i]);
750 template <
typename T>
757 _covariogram = covarIn;
759 _npts = dataIn.template getSize<0>();
760 _dimensions = dataIn.template getSize<1>();
766 _function = allocate(ndarray::makeVector(_npts, 1));
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");
773 for (i = 0; i < _npts; i++) _function[i][0] = ff[i];
775 _krigingParameter = T(1.0);
780 _kdTree.Initialize(dataIn);
782 _npts = _kdTree.getNPoints();
785 template <
typename T>
787 ndarray::Array<T, 1, 1>
const &mx, ndarray::Array<T, 1, 1>
const &ff,
790 ndarray::Array<T, 2, 2> normalizedData;
792 _covariogram = covarIn;
794 _npts = dataIn.template getSize<0>();
795 _dimensions = dataIn.template getSize<1>();
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");
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");
809 _krigingParameter = T(1.0);
812 _krigingParameter = T(1.0);
814 _max = allocate(ndarray::makeVector(_dimensions));
815 _min = allocate(ndarray::makeVector(_dimensions));
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]);
827 _kdTree.Initialize(normalizedData);
829 _npts = _kdTree.getNPoints();
831 _function = allocate(ndarray::makeVector(_npts, 1));
832 for (i = 0; i < _npts; i++) _function[i][0] = ff[i];
835 template <
typename T>
838 _covariogram = covarIn;
840 _npts = dataIn.template getSize<0>();
841 _dimensions = dataIn.template getSize<1>();
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");
851 _nFunctions = ff.template getSize<1>();
852 _function = allocate(ndarray::makeVector(_npts, _nFunctions));
853 _function.deep() = ff;
855 _krigingParameter = T(1.0);
861 _kdTree.Initialize(dataIn);
863 _npts = _kdTree.getNPoints();
866 template <
typename T>
868 ndarray::Array<T, 1, 1>
const &mx, ndarray::Array<T, 2, 2>
const &ff,
871 ndarray::Array<T, 2, 2> normalizedData;
873 _covariogram = covarIn;
875 _npts = dataIn.template getSize<0>();
876 _dimensions = dataIn.template getSize<1>();
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");
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");
891 _krigingParameter = T(1.0);
894 _krigingParameter = T(1.0);
896 _max = allocate(ndarray::makeVector(_dimensions));
897 _min = allocate(ndarray::makeVector(_dimensions));
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]);
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;
916 template <
typename T>
921 template <
typename T>
926 template <
typename T>
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");
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");
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");
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");
953 for (i = 0; i < indices.template getSize<0>(); i++) {
954 pts[i] = _kdTree.getData(indices[i]);
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];
966 template <
typename T>
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");
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");
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");
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");
993 for (i = 0; i < indices.template getSize<0>(); i++) {
994 pts[i] = _kdTree.getData(indices[i]);
996 for (j = 0; j < _nFunctions; j++) {
997 fn[i][j] = _function[indices[i]][j];
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];
1008 template <
typename T>
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.");
1018 if (numberOfNeighbors <= 0) {
1019 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1020 "Asked for zero or negative number of neighbors\n");
1023 if (numberOfNeighbors > _kdTree.getNPoints()) {
1024 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1025 "Asked for more neighbors than you have data points\n");
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");
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");
1042 ndarray::Array<T, 1, 1> covarianceTestPoint;
1043 ndarray::Array<int, 1, 1> neighbors;
1044 ndarray::Array<double, 1, 1> neighborDistances, vv;
1046 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1047 Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
1051 bb.resize(numberOfNeighbors, 1);
1052 xx.resize(numberOfNeighbors, 1);
1054 covariance.resize(numberOfNeighbors, numberOfNeighbors);
1056 covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1058 neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1060 neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1062 vv = allocate(ndarray::makeVector(_dimensions));
1063 if (_useMaxMin == 1) {
1069 for (i = 0; i < _dimensions; i++) vv[i] = (vin[i] - _min[i]) / (_max[i] - _min[i]);
1074 _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1076 _timer.addToSearch();
1079 for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][0];
1080 fbar = fbar / double(numberOfNeighbors);
1082 for (i = 0; i < numberOfNeighbors; i++) {
1083 covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.getData(neighbors[i]));
1086 (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
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);
1094 _timer.addToIteration();
1097 ldlt.compute(covariance);
1099 for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][0] - fbar;
1101 xx = ldlt.solve(bb);
1102 _timer.addToEigen();
1106 for (i = 0; i < numberOfNeighbors; i++) {
1107 mu += covarianceTestPoint[i] * xx(i, 0);
1110 _timer.addToIteration();
1112 variance(0) = (*_covariogram)(vv, vv) + _lambda;
1114 for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1116 xx = ldlt.solve(bb);
1118 for (i = 0; i < numberOfNeighbors; i++) {
1119 variance(0) -= covarianceTestPoint[i] * xx(i, 0);
1124 _timer.addToVariance();
1125 _timer.addToTotal(1);
1130 template <
typename T>
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");
1138 if (numberOfNeighbors > _kdTree.getNPoints()) {
1139 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1140 "Asked for more neighbors than you have data points\n");
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");
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");
1157 ndarray::Array<T, 1, 1> covarianceTestPoint;
1158 ndarray::Array<int, 1, 1> neighbors;
1159 ndarray::Array<double, 1, 1> neighborDistances, vv;
1161 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1162 Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
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));
1173 vv = allocate(ndarray::makeVector(_dimensions));
1175 if (_useMaxMin == 1) {
1181 for (i = 0; i < _dimensions; i++) vv[i] = (vin[i] - _min[i]) / (_max[i] - _min[i]);
1186 _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1188 _timer.addToSearch();
1190 for (i = 0; i < numberOfNeighbors; i++) {
1191 covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.getData(neighbors[i]));
1194 (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
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);
1202 _timer.addToIteration();
1205 ldlt.compute(covariance);
1207 for (ii = 0; ii < _nFunctions; ii++) {
1209 for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][ii];
1210 fbar = fbar / double(numberOfNeighbors);
1212 for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][ii] - fbar;
1213 xx = ldlt.solve(bb);
1217 for (i = 0; i < numberOfNeighbors; i++) {
1218 mu[ii] += covarianceTestPoint[i] * xx(i, 0);
1223 _timer.addToEigen();
1225 variance[0] = (*_covariogram)(vv, vv) + _lambda;
1227 for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1229 xx = ldlt.solve(bb);
1231 for (i = 0; i < numberOfNeighbors; i++) {
1232 variance[0] -= covarianceTestPoint[i] * xx(i, 0);
1234 variance[0] = variance[0] * _krigingParameter;
1236 for (i = 1; i < _nFunctions; i++) variance[i] = variance[0];
1238 _timer.addToVariance();
1239 _timer.addToTotal(1);
1242 template <
typename T>
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.");
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");
1258 if (numberOfNeighbors <= 0) {
1259 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1260 "Asked for zero or negative number of neighbors\n");
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");
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");
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;
1282 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1283 Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
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));
1294 selfNeighbors = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1295 selfDistances = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1299 _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.getData(dex), numberOfNeighbors + 1);
1301 _timer.addToSearch();
1303 if (selfNeighbors[0] != dex) {
1304 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1305 "Nearest neighbor search in selfInterpolate did not find self\n");
1313 for (i = 0; i < numberOfNeighbors; i++) {
1314 neighbors[i] = selfNeighbors[i + 1];
1315 neighborDistances[i] = selfDistances[i + 1];
1319 for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][0];
1320 fbar = fbar / double(numberOfNeighbors);
1322 for (i = 0; i < numberOfNeighbors; i++) {
1323 covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(neighbors[i]));
1326 (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
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);
1333 _timer.addToIteration();
1336 ldlt.compute(covariance);
1338 for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][0] - fbar;
1339 xx = ldlt.solve(bb);
1340 _timer.addToEigen();
1344 for (i = 0; i < numberOfNeighbors; i++) {
1345 mu += covarianceTestPoint[i] * xx(i, 0);
1348 variance(0) = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1350 for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1352 xx = ldlt.solve(bb);
1354 for (i = 0; i < numberOfNeighbors; i++) {
1355 variance(0) -= covarianceTestPoint[i] * xx(i, 0);
1359 _timer.addToVariance();
1360 _timer.addToTotal(1);
1365 template <
typename T>
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");
1374 if (numberOfNeighbors <= 0) {
1375 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1376 "Asked for zero or negative number of neighbors\n");
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");
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");
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;
1398 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> covariance, bb, xx;
1399 Eigen::LDLT<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > ldlt;
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));
1410 selfNeighbors = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1411 selfDistances = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1415 _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.getData(dex), numberOfNeighbors + 1);
1417 _timer.addToSearch();
1419 if (selfNeighbors[0] != dex) {
1420 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1421 "Nearest neighbor search in selfInterpolate did not find self\n");
1429 for (i = 0; i < numberOfNeighbors; i++) {
1430 neighbors[i] = selfNeighbors[i + 1];
1431 neighborDistances[i] = selfDistances[i + 1];
1434 for (i = 0; i < numberOfNeighbors; i++) {
1435 covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(neighbors[i]));
1438 (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i])) + _lambda;
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);
1445 _timer.addToIteration();
1448 ldlt.compute(covariance);
1450 for (ii = 0; ii < _nFunctions; ii++) {
1452 for (i = 0; i < numberOfNeighbors; i++) fbar += _function[neighbors[i]][ii];
1453 fbar = fbar / double(numberOfNeighbors);
1455 for (i = 0; i < numberOfNeighbors; i++) bb(i, 0) = _function[neighbors[i]][ii] - fbar;
1456 xx = ldlt.solve(bb);
1460 for (i = 0; i < numberOfNeighbors; i++) {
1461 mu[ii] += covarianceTestPoint[i] * xx(i, 0);
1465 _timer.addToEigen();
1467 variance[0] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1469 for (i = 0; i < numberOfNeighbors; i++) bb(i) = covarianceTestPoint[i];
1471 xx = ldlt.solve(bb);
1473 for (i = 0; i < numberOfNeighbors; i++) {
1474 variance[0] -= covarianceTestPoint[i] * xx(i, 0);
1477 variance[0] = variance[0] * _krigingParameter;
1479 for (i = 1; i < _nFunctions; i++) variance[i] = variance[0];
1481 _timer.addToVariance();
1482 _timer.addToTotal(1);
1485 template <
typename T>
1487 ndarray::Array<T, 2, 2>
const &queries)
const {
1488 int i, j, ii, nQueries;
1490 nQueries = queries.template getSize<0>();
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");
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");
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");
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;
1515 ndarray::Array<T, 1, 1> v1;
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);
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);
1532 _timer.addToIteration();
1534 ldlt.compute(batchCovariance);
1537 for (i = 0; i < _npts; i++) {
1538 fbar += _function[i][0];
1540 fbar = fbar / T(_npts);
1542 for (i = 0; i < _npts; i++) {
1543 batchbb(i, 0) = _function[i][0] - fbar;
1545 batchxx = ldlt.solve(batchbb);
1546 _timer.addToEigen();
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]);
1554 for (i = 0; i < _npts; i++) {
1555 mu(ii) += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1558 _timer.addToIteration();
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]);
1566 for (i = 0; i < _npts; i++) {
1567 batchbb(i, 0) = (*_covariogram)(v1, _kdTree.getData(i));
1568 queryCovariance(i, 0) = batchbb(i, 0);
1570 batchxx = ldlt.solve(batchbb);
1572 variance[ii] = (*_covariogram)(v1, v1) + _lambda;
1574 for (i = 0; i < _npts; i++) {
1575 variance[ii] -= queryCovariance(i, 0) * batchxx(i);
1578 variance[ii] = variance[ii] * _krigingParameter;
1581 _timer.addToVariance();
1582 _timer.addToTotal(nQueries);
1585 template <
typename T>
1587 ndarray::Array<T, 2, 2>
const &queries)
const {
1588 int i, j, ii, nQueries, ifn;
1590 nQueries = queries.template getSize<0>();
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");
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 "
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");
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;
1615 ndarray::Array<T, 1, 1> v1;
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);
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);
1633 _timer.addToIteration();
1635 ldlt.compute(batchCovariance);
1637 _timer.addToEigen();
1639 for (ifn = 0; ifn < _nFunctions; ifn++) {
1641 for (i = 0; i < _npts; i++) {
1642 fbar += _function[i][ifn];
1644 fbar = fbar / T(_npts);
1645 _timer.addToIteration();
1647 for (i = 0; i < _npts; i++) {
1648 batchbb(i, 0) = _function[i][ifn] - fbar;
1650 batchxx = ldlt.solve(batchbb);
1651 _timer.addToEigen();
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]);
1659 for (i = 0; i < _npts; i++) {
1660 mu[ii][ifn] += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
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]);
1673 for (i = 0; i < _npts; i++) {
1674 batchbb(i, 0) = (*_covariogram)(v1, _kdTree.getData(i));
1675 queryCovariance(i, 0) = batchbb(i, 0);
1677 batchxx = ldlt.solve(batchbb);
1679 variance[ii][0] = (*_covariogram)(v1, v1) + _lambda;
1681 for (i = 0; i < _npts; i++) {
1682 variance[ii][0] -= queryCovariance(i, 0) * batchxx(i);
1685 variance[ii][0] = variance[ii][0] * _krigingParameter;
1686 for (i = 1; i < _nFunctions; i++) variance[ii][i] = variance[ii][0];
1688 _timer.addToVariance();
1689 _timer.addToTotal(nQueries);
1692 template <
typename T>
1694 ndarray::Array<T, 2, 2>
const &queries)
const {
1695 int i, j, ii, nQueries;
1697 nQueries = queries.template getSize<0>();
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");
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");
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");
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;
1722 ndarray::Array<T, 1, 1> v1;
1726 v1 = allocate(ndarray::makeVector(_dimensions));
1728 batchbb.resize(_npts, 1);
1729 batchxx.resize(_npts, 1);
1730 batchCovariance.resize(_npts, _npts);
1731 queryCovariance.resize(_npts, 1);
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);
1740 _timer.addToIteration();
1742 ldlt.compute(batchCovariance);
1745 for (i = 0; i < _npts; i++) {
1746 fbar += _function[i][0];
1748 fbar = fbar / T(_npts);
1750 for (i = 0; i < _npts; i++) {
1751 batchbb(i, 0) = _function[i][0] - fbar;
1753 batchxx = ldlt.solve(batchbb);
1754 _timer.addToEigen();
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]);
1763 for (i = 0; i < _npts; i++) {
1764 mu(ii) += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1767 _timer.addToIteration();
1768 _timer.addToTotal(nQueries);
1771 template <
typename T>
1773 ndarray::Array<T, 2, 2>
const &queries)
const {
1774 int i, j, ii, nQueries, ifn;
1776 nQueries = queries.template getSize<0>();
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");
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");
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");
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;
1801 ndarray::Array<T, 1, 1> v1;
1805 v1 = allocate(ndarray::makeVector(_dimensions));
1807 batchbb.resize(_npts, 1);
1808 batchxx.resize(_npts, 1);
1809 batchCovariance.resize(_npts, _npts);
1810 queryCovariance.resize(_npts, 1);
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);
1820 _timer.addToIteration();
1822 ldlt.compute(batchCovariance);
1824 _timer.addToEigen();
1826 for (ifn = 0; ifn < _nFunctions; ifn++) {
1828 for (i = 0; i < _npts; i++) {
1829 fbar += _function[i][ifn];
1831 fbar = fbar / T(_npts);
1833 _timer.addToIteration();
1835 for (i = 0; i < _npts; i++) {
1836 batchbb(i, 0) = _function[i][ifn] - fbar;
1838 batchxx = ldlt.solve(batchbb);
1839 _timer.addToEigen();
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]);
1848 for (i = 0; i < _npts; i++) {
1849 mu[ii][ifn] += batchxx(i) * (*_covariogram)(v1, _kdTree.getData(i));
1855 _timer.addToTotal(nQueries);
1858 template <
typename T>
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");
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");
1873 ndarray::Array<T, 1, 1> v;
1874 v = allocate(ndarray::makeVector(_dimensions));
1876 for (i = 0; i < _dimensions; i++) {
1878 if (_useMaxMin == 1) {
1879 v[i] = (v[i] - _min[i]) / (_max[i] - _min[i]);
1883 if (_npts == _room) {
1884 ndarray::Array<T, 2, 2> buff;
1885 buff = allocate(ndarray::makeVector(_npts, _nFunctions));
1886 buff.deep() = _function;
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];
1897 _function[_npts][0] = f;
1899 _kdTree.addPoint(v);
1900 _npts = _kdTree.getNPoints();
1903 template <
typename T>
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");
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");
1919 ndarray::Array<T, 1, 1> v;
1920 v = allocate(ndarray::makeVector(_dimensions));
1922 for (i = 0; i < _dimensions; i++) {
1924 if (_useMaxMin == 1) {
1925 v[i] = (v[i] - _min[i]) / (_max[i] - _min[i]);
1929 if (_npts == _room) {
1930 ndarray::Array<T, 2, 2> buff;
1931 buff = allocate(ndarray::makeVector(_npts, _nFunctions));
1932 buff.deep() = _function;
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];
1942 for (i = 0; i < _nFunctions; i++) _function[_npts][i] = f[i];
1944 _kdTree.addPoint(v);
1945 _npts = _kdTree.getNPoints();
1948 template <
typename T>
1954 for (i = dex; i < _npts; i++) {
1955 for (j = 0; j < _nFunctions; j++) {
1956 _function[i][j] = _function[i + 1][j];
1959 _npts = _kdTree.getNPoints();
1962 template <
typename T>
1964 _krigingParameter = kk;
1967 template <
typename T>
1969 _covariogram = covar;
1972 template <
typename T>
1977 template <
typename T>
1982 template <
typename T>
1985 template <
typename T>
1987 ndarray::Array<const T, 1, 1>
const &p2)
const {
1988 std::cout <<
"by the way, you are calling the wrong operator\n";
1993 template <
typename T>
1996 template <
typename T>
2001 template <
typename T>
2003 _ellSquared = ellSquared;
2006 template <
typename T>
2008 ndarray::Array<const T, 1, 1>
const &p2)
const {
2012 for (i = 0; i < p1.template getSize<0>(); i++) {
2013 d += (p1[i] - p2[i]) * (p1[i] - p2[i]);
2016 d = d / _ellSquared;
2017 return T(exp(-0.5 * d));
2020 template <
typename T>
2023 template <
typename T>
2029 template <
typename T>
2031 ndarray::Array<const T, 1, 1>
const &p2)
const {
2033 double num, denom1, denom2, arg;
2035 dim = p1.template getSize<0>();
2037 num = 2.0 * _sigma0;
2038 denom1 = 1.0 + 2.0 * _sigma0;
2039 denom2 = 1.0 + 2.0 * _sigma0;
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;
2047 arg = num / ::sqrt(denom1 * denom2);
2048 return T(2.0 * (::asin(arg)) / 3.141592654);
2051 template <
typename T>
2056 template <
typename T>
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>;
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
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.