37 GaussianProcessTimer::GaussianProcessTimer()
39 _interpolationCount = 0;
48 void GaussianProcessTimer::reset()
50 _interpolationCount = 0;
59 void GaussianProcessTimer::start()
65 void GaussianProcessTimer::addToEigen()
69 _eigenTime += after - _before;
73 void GaussianProcessTimer::addToVariance()
77 _varianceTime += after - _before;
81 void GaussianProcessTimer::addToSearch()
85 _searchTime += after - _before;
89 void GaussianProcessTimer::addToIteration()
93 _iterationTime += after - _before;
97 void GaussianProcessTimer::addToTotal(
int i)
101 _totalTime += after - _beginning;
102 _interpolationCount += i;
105 void GaussianProcessTimer::display(){
106 std::cout <<
"\nSearch time " << _searchTime <<
"\n";
107 std::cout <<
"Eigen time " << _eigenTime <<
"\n";
108 std::cout <<
"Variance time " << _varianceTime <<
"\n";
109 std::cout <<
"Iteration time " << _iterationTime <<
"\n";
110 std::cout <<
"Total time " << _totalTime <<
"\n";
111 std::cout <<
"Number of interpolations " << _interpolationCount <<
"\n";
114 template <
typename T >
119 _npts = dt.template getSize < 0 > ();
120 _dimensions = dt.template getSize < 1 > ();
123 _inn = allocate(ndarray::makeVector(_npts));
128 _data = allocate(ndarray::makeVector(_room,_dimensions));
132 _tree = allocate(ndarray::makeVector(_room,4));
134 for (i = 0 ; i < _npts ; i++ ) {
138 _organize(_inn,_npts, -1, -1);
143 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
144 "Failed to properly initialize KdTree\n");
149 template <
typename T >
151 ndarray::Array < double,1,1 > dd,
152 ndarray::Array < const T,1,1 >
const &v,
157 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
158 "Asked for more neighbors than kd tree contains\n");
162 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
163 "Asked for zero or a negative number of neighbors\n");
166 if(neighdex.getNumElements() != n_nn){
167 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
168 "Size of neighdex does not equal n_nn in KdTree.findNeighbors\n");
171 if(dd.getNumElements() != n_nn){
172 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
173 "Size of dd does not equal n_nn in KdTree.findNeighbors\n");
178 _neighborCandidates = allocate(ndarray::makeVector(n_nn));
179 _neighborDistances = allocate(ndarray::makeVector(n_nn));
181 _neighborsWanted = n_nn;
183 for (i = 0; i < n_nn; i++ ) _neighborDistances[i] = -1.0;
185 start = _findNode(v);
187 _neighborDistances[0] = _distance(v,_data[start]);
188 _neighborCandidates[0] = start;
192 for (i = 1; i < 4; i++ ) {
193 if(_tree[start][i] >= 0){
194 _lookForNeighbors(v,_tree[start][i], start);
198 for (i = 0 ; i < n_nn ; i++ ) {
199 neighdex[i] = _neighborCandidates[i];
200 dd[i] = _neighborDistances[i];
204 template <
typename T >
207 if(ipt >= _npts || ipt < 0){
208 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
209 "Asked for point that does not exist in KdTree\n");
212 if(idim >= _dimensions || idim < 0){
213 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
214 "KdTree does not have points of that many dimensions\n");
217 return _data[ipt][idim];
220 template <
typename T >
223 if(ipt >= _npts || ipt<0){
224 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
225 "Asked for point that does not exist in KdTree\n");
231 template <
typename T >
235 if(v.getNumElements() != _dimensions){
236 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
237 "You are trying to add a point of the incorrect dimensionality to KdTree\n");
240 int i,j,node,dim,dir;
243 dim = _tree[node][DIMENSION] + 1;
244 if(dim == _dimensions)dim = 0;
248 ndarray::Array < T,2,2 > dbuff = allocate(ndarray::makeVector(_npts, _dimensions));
249 ndarray::Array < int,2,2 > tbuff = allocate(ndarray::makeVector(_npts, 4));
251 dbuff.deep() = _data;
252 tbuff.deep() = _tree;
256 _tree = allocate(ndarray::makeVector(_room, 4));
257 _data = allocate(ndarray::makeVector(_room, _dimensions));
260 for (i = 0; i < _npts; i++ ) {
261 for (j = 0; j < _dimensions; j++ ) _data[i][j] = dbuff[i][j];
262 for (j = 0; j < 4; j++ ) _tree[i][j] = tbuff[i][j];
266 _tree[_npts][DIMENSION] = dim;
267 _tree[_npts][
PARENT] = node;
268 i = _tree[node][DIMENSION];
270 if(_data[node][i] > v[i]){
271 if(_tree[node][LT] >= 0){
272 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
273 "Trying to add to KdTree in a node that is already occupied\n");
275 _tree[node][LT] = _npts;
279 if(_tree[node][GEQ] >= 0){
280 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
281 "Trying to add to KdTree in a node that is already occupied\n");
283 _tree[node][GEQ] = _npts;
286 _tree[_npts][LT] = -1;
287 _tree[_npts][GEQ] = -1;
288 for (i = 0; i < _dimensions; i++ ) {
289 _data[_npts][i] = v[i];
295 i = _walkUpTree(_tree[_npts-1][
PARENT], dir, _npts-1);
296 if (i != _masterParent){
297 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
298 "Adding to KdTree failed\n");
303 template <
typename T >
309 template <
typename T >
313 if(dex < 0 || dex >= _npts){
314 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
315 "Asked for tree information on point that does not exist\n");
318 if(v.getNumElements() != 4){
319 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
320 "Need to pass a 4-element ndarray into KdTree.getTreeNode()\n");
323 v[0] = _tree[dex][DIMENSION];
324 v[1] = _tree[dex][LT];
325 v[2] = _tree[dex][GEQ];
326 v[3] = _tree[dex][
PARENT];
329 template <
typename T >
332 if(_npts == 1 && _tree[0][
PARENT] < 0 && _masterParent==0){
338 std::vector < int > isparent;
342 for (i = 0; i < _npts; i++ ) isparent.push_back(0);
345 for (i = 0; i < _npts; i++ ) {
346 if(_tree[i][
PARENT] < 0)j++;
349 std::cout <<
"_tree FAILURE " << j <<
" _masterParents\n";
353 for (i = 0; i < _npts; i++ ) {
354 if(_tree[i][
PARENT] >= 0){
355 isparent[_tree[i][
PARENT]]++;
359 for (i = 0; i < _npts; i++ ) {
361 std::cout <<
"_tree FAILURE " << i <<
" is parent to " << isparent[i] <<
"\n";
367 for (i = 0; i < _npts; i++ ) {
368 if(_tree[i][
PARENT] >= 0) {
369 if(_tree[_tree[i][
PARENT]][LT] == i)j = LT;
371 output = _walkUpTree(_tree[i][
PARENT], j, i);
373 if(output != _masterParent){
379 if(output != _masterParent)
return 0;
383 template <
typename T >
391 int i,j,k,l,idim,daughter;
395 std::vector < std::vector < T > > toSort;
396 std::vector < T > toSortElement;
404 for (i = 0; i < _dimensions; i++ ) {
407 for (j = 0; j < ct; j++ ) {
408 mean += _data[use[j]][i];
409 var += _data[use[j]][i]*_data[use[j]][i];
411 mean = mean/double(ct);
412 var = var/double(ct) - mean*mean;
413 if(i == 0 || var > varbest ||
414 (var == varbest && parent >= 0 && i > _tree[parent][DIMENSION])) {
428 toSortElement.push_back(_data[use[0]][idim]);
429 toSortElement.push_back(T(use[0]));
430 toSort.push_back(toSortElement);
431 for(i = 1; i < ct; i++ ) {
432 toSortElement[0] = _data[use[i]][idim];
433 toSortElement[1] = T(use[i]);
434 toSort.push_back(toSortElement);
437 std::stable_sort(toSort.begin(), toSort.end());
441 while(k > 0 && toSort[k][0] == toSort[k - 1][0]) k--;
443 while(l < ct - 1 && toSort[l][0] == toSort[ct/2][0]) l++;
445 if((ct/2 - k) < (l - ct/2) || l == ct - 1) j = k;
448 for(i = 0; i < ct; i++ ) {
449 use[i] = int(toSort[i][1]);
453 if(parent >= 0)_tree[parent][dir] = daughter;
454 _tree[daughter][DIMENSION] = idim;
455 _tree[daughter][
PARENT] = parent;
458 _organize(use[ndarray::view(j + 1,use.getSize < 0 > ())], ct - j - 1, daughter, GEQ);
460 else _tree[daughter][GEQ] = -1;
463 _organize(use, j, daughter, LT);
465 else _tree[daughter][LT] = -1;
471 if(parent >= 0) _tree[parent][dir] = daughter;
473 idim = _tree[parent][DIMENSION] + 1;
475 if(idim >= _dimensions)idim = 0;
477 _tree[daughter][DIMENSION] = idim;
478 _tree[daughter][LT] = - 1;
479 _tree[daughter][GEQ] = - 1;
480 _tree[daughter][
PARENT] = parent;
485 _masterParent = daughter;
490 template <
typename T >
491 int KdTree < T > ::_findNode(ndarray::Array < const T,1,1 >
const &v)
const
493 int consider,next,dim;
495 dim = _tree[_masterParent][DIMENSION];
497 if(v[dim] < _data[_masterParent][dim]) consider = _tree[_masterParent][LT];
498 else consider = _tree[_masterParent][GEQ];
506 dim = _tree[consider][DIMENSION];
507 if(v[dim] < _data[consider][dim]) next = _tree[consider][LT];
508 else next = _tree[consider][GEQ];
515 template <
typename T >
516 void KdTree < T > ::_lookForNeighbors(ndarray::Array < const T,1,1 >
const &v,
524 dd = _distance(v, _data[consider]);
526 if(_neighborsFound < _neighborsWanted || dd < _neighborDistances[_neighborsWanted -1]) {
528 for(j = 0; j < _neighborsFound && _neighborDistances[j] < dd; j++ );
530 for(i = _neighborsWanted - 1; i > j; i-- ){
531 _neighborDistances[i] = _neighborDistances[i - 1];
532 _neighborCandidates[i] = _neighborCandidates[i - 1];
535 _neighborDistances[j] = dd;
536 _neighborCandidates[j] = consider;
538 if(_neighborsFound < _neighborsWanted) _neighborsFound++;
541 if(_tree[consider][
PARENT] == from){
544 i = _tree[consider][DIMENSION];
545 dd = v[i] - _data[consider][i];
546 if((dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted)
547 && _tree[consider][LT] >= 0){
549 _lookForNeighbors(v, _tree[consider][LT], consider);
552 dd = _data[consider][i] - v[i];
553 if((dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted)
554 && _tree[consider][GEQ] >= 0){
556 _lookForNeighbors(v, _tree[consider][GEQ], consider);
563 if(_tree[consider][LT] == from){
570 j = _tree[consider][going];
573 i = _tree[consider][DIMENSION];
575 if(going == 1) dd = v[i] - _data[consider][i];
576 else dd = _data[consider][i] - v[i];
578 if(dd <= _neighborDistances[_neighborsFound - 1] || _neighborsFound < _neighborsWanted) {
579 _lookForNeighbors(v, j, consider);
584 if(_tree[consider][
PARENT] >= 0) {
585 _lookForNeighbors(v, _tree[consider][
PARENT], consider);
590 template <
typename T >
604 if(_data[root][_tree[target][DIMENSION]] >= _data[target][_tree[target][DIMENSION]]){
609 if(_data[root][_tree[target][DIMENSION]] < _data[target][_tree[target][DIMENSION]]) {
614 if(_tree[target][
PARENT] >= 0){
615 if(_tree[_tree[target][
PARENT]][LT] == target)i = LT;
617 output = output*_walkUpTree(_tree[target][
PARENT],i,root);
621 output = output*target;
629 template <
typename T >
633 if(target < 0 || target >= _npts){
634 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
635 "You are trying to remove a point that doesn't exist from KdTree\n");
639 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
640 "There is only one point left in this KdTree. You cannot call removePoint\n");
643 int nl,nr,i,j,k,side;
648 if(_tree[target][LT] >= 0){
650 _count(_tree[target][LT], &nl);
653 if(_tree[target][GEQ] >= 0){
655 _count(_tree[target][GEQ], &nr);
658 if(nl == 0 && nr == 0){
660 k = _tree[target][
PARENT];
662 if(_tree[k][LT] == target) _tree[k][LT] = - 1;
663 else if(_tree[k][GEQ] == target) _tree[k][GEQ] = - 1;
666 else if((nl == 0 && nr > 0) || (nr == 0 && nl > 0)){
668 if(nl == 0) side = GEQ;
671 k = _tree[target][
PARENT];
674 if(_tree[k][LT] == target){
675 _tree[k][LT] = _tree[target][side];
676 _tree[_tree[k][LT]][
PARENT] = k;
680 _tree[k][GEQ] = _tree[target][side];
681 _tree[_tree[k][GEQ]][
PARENT] = k;
685 _masterParent = _tree[target][side];
686 _tree[_tree[target][side]][
PARENT] = - 1;
691 if(nl > nr)side = LT;
694 k = _tree[target][
PARENT];
696 _masterParent = _tree[target][side];
697 _tree[_masterParent][
PARENT] = - 1;
700 if(_tree[k][LT] == target){
701 _tree[k][LT] = _tree[target][side];
702 _tree[_tree[k][LT]][
PARENT] = k;
705 _tree[k][GEQ] = _tree[target][side];
706 _tree[_tree[k][GEQ]][
PARENT] = k;
710 root = _tree[target][3 - side];
715 if(target < _npts - 1){
716 for(i = target + 1;i < _npts;i++ ){
717 for(j = 0; j < 4; j++ ) _tree[i - 1][j] = _tree[i][j];
719 for(j = 0; j < _dimensions; j++ ) _data[i - 1][j] = _data[i][j];
722 for(i = 0; i < _npts; i++ ){
723 for(j = 1; j < 4; j++ ){
724 if(_tree[i][j] > target) _tree[i][j]--;
728 if(_masterParent > target)_masterParent-- ;
734 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
735 "Subtracting from KdTree failed\n");
741 template <
typename T >
746 if(_tree[where][LT] >= 0) {
748 _count(_tree[where][LT], ct);
750 if(_tree[where][GEQ] >= 0) {
752 _count(_tree[where][GEQ], ct);
756 template <
typename T >
762 where = _masterParent;
763 if(_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]]) dir = LT;
766 k = _tree[where][dir];
769 if(_data[target][_tree[where][DIMENSION]] < _data[where][_tree[where][DIMENSION]]) dir = LT;
771 k = _tree[where][dir];
774 _tree[where][dir] = target;
775 _tree[target][
PARENT] = where;
776 _tree[target][LT] = - 1;
777 _tree[target][GEQ] = - 1;
778 _tree[target][DIMENSION] = _tree[where][DIMENSION] + 1;
780 if(_tree[target][DIMENSION] == _dimensions) _tree[target][DIMENSION] = 0;
783 template <
typename T >
787 if(_tree[root][LT] >= 0) _descend(_tree[root][LT]);
788 if(_tree[root][GEQ] >= 0) _descend(_tree[root][GEQ]);
793 template <
typename T >
794 double KdTree < T > ::_distance(ndarray::Array < const T,1,1 >
const &p1,
795 ndarray::Array < const T,1,1 >
const &p2)
const
801 dd = p1.template getSize< 0 >();
803 for(i = 0;i < dd;i++ ) ans += (p1[i] - p2[i])*(p1[i] - p2[i]);
810 template <
typename T >
812 ndarray::Array < T,1,1 >
const &ff,
818 _covariogram = covarIn;
820 _npts = dataIn.template getSize < 0 > ();
821 _dimensions = dataIn.template getSize < 1 > ();
827 _function = allocate(ndarray::makeVector(_npts,1));
829 if(ff.getNumElements() != _npts){
830 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
831 "You did not pass in the same number of data points as function values\n");
834 for(i = 0; i < _npts; i++ ) _function[i][0] = ff[i];
836 _krigingParameter = T(1.0);
842 _kdTree.Initialize(dataIn);
844 _npts = _kdTree.getNPoints();
848 template <
typename T >
850 ndarray::Array < T,1,1 >
const &mn,
851 ndarray::Array < T,1,1 >
const &mx,
852 ndarray::Array < T,1,1 >
const &ff,
858 ndarray::Array < T,2,2 > normalizedData;
860 _covariogram = covarIn;
862 _npts = dataIn.template getSize < 0 > ();
863 _dimensions = dataIn.template getSize < 1 > ();
867 if(ff.getNumElements() != _npts){
868 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
869 "You did not pass in the same number of data points as function values\n");
872 if(mn.getNumElements() != _dimensions || mx.getNumElements() != _dimensions){
873 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
874 "Your min/max values have different dimensionality than your data points\n");
877 _krigingParameter = T(1.0);
880 _krigingParameter = T(1.0);
882 _max = allocate(ndarray::makeVector(_dimensions));
883 _min = allocate(ndarray::makeVector(_dimensions));
887 normalizedData = allocate(ndarray::makeVector(_npts, _dimensions));
888 for(i = 0; i < _npts; i++ ) {
889 for(j = 0; j < _dimensions; j++ ) {
890 normalizedData[i][j] = (dataIn[i][j] - _min[j])/(_max[j] - _min[j]);
895 _kdTree.Initialize(normalizedData);
897 _npts = _kdTree.getNPoints();
899 _function = allocate(ndarray::makeVector(_npts, 1));
900 for(i = 0; i < _npts; i++ )_function[i][0] = ff[i];
903 template <
typename T >
905 ndarray::Array < T,2,2 >
const &ff,
909 _covariogram = covarIn;
911 _npts = dataIn.template getSize < 0 > ();
912 _dimensions = dataIn.template getSize < 1 > ();
917 if(ff.template getSize < 0 > () != _npts){
918 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
919 "You did not pass in the same number of data points as function values\n");
922 _nFunctions = ff.template getSize < 1 > ();
923 _function = allocate(ndarray::makeVector(_npts, _nFunctions));
924 _function.deep() = ff;
926 _krigingParameter = T(1.0);
932 _kdTree.Initialize(dataIn);
934 _npts = _kdTree.getNPoints();
937 template <
typename T >
939 ndarray::Array < T,1,1 >
const &mn,
940 ndarray::Array < T,1,1 >
const &mx,
941 ndarray::Array < T,2,2 >
const &ff,
947 ndarray::Array < T,2,2 > normalizedData;
949 _covariogram = covarIn;
951 _npts = dataIn.template getSize < 0 > ();
952 _dimensions = dataIn.template getSize < 1 > ();
957 if(ff.template getSize < 0 > () != _npts){
958 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
959 "You did not pass in the same number of data points as function values\n");
962 if(mn.getNumElements() != _dimensions || mx.getNumElements() != _dimensions){
963 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
964 "Your min/max values have different dimensionality than your data points\n");
967 _krigingParameter = T(1.0);
970 _krigingParameter = T(1.0);
972 _max = allocate(ndarray::makeVector(_dimensions));
973 _min = allocate(ndarray::makeVector(_dimensions));
977 normalizedData = allocate(ndarray::makeVector(_npts,_dimensions));
978 for(i = 0; i < _npts; i++ ) {
979 for(j = 0; j < _dimensions; j++ ) {
980 normalizedData[i][j] = (dataIn[i][j] - _min[j])/(_max[j] - _min[j]);
985 _kdTree.Initialize(normalizedData);
986 _npts = _kdTree.getNPoints();
987 _nFunctions = ff.template getSize < 1 > ();
988 _function = allocate(ndarray::makeVector(_npts, _nFunctions));
989 _function.deep() = ff;
993 template <
typename T >
999 template <
typename T >
1004 template <
typename T >
1006 ndarray::Array <int, 1, 1 > indices)
const{
1008 if(_nFunctions != 1){
1009 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1010 "Your function value array does not have enough room for all of the functions "
1011 "in your GaussianProcess.\n");
1014 if(pts.template getSize < 1 > () != _dimensions){
1015 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1016 "Your pts array is constructed for points of the wrong dimensionality.\n");
1019 if(pts.template getSize < 0 > () != indices.getNumElements()){
1020 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1021 "You did not put enough room in your pts array to fit all the points "
1022 "you asked for in your indices array.\n");
1025 if(fn.template getSize < 0 > () != indices.getNumElements()){
1026 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1027 "You did not provide enough room in your function value array "
1028 "for all of the points you requested in your indices array.\n");
1032 for(i = 0; i < indices.template getSize < 0 > (); i++){
1033 pts[i] = _kdTree.getData(indices[i]);
1035 fn[i] = _function[indices[i]][0];
1036 if(_useMaxMin == 1){
1037 for(j = 0; j < _dimensions; j ++){
1038 pts[i][j] *= (_max[j] - _min[j]);
1039 pts[i][j] += _min[j];
1047 template <
typename T >
1049 ndarray::Array <int, 1, 1 > indices)
const{
1051 if(fn.template getSize < 1 > () != _nFunctions){
1052 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1053 "Your function value array does not have enough room for all of the functions "
1054 "in your GaussianProcess.\n");
1057 if(pts.template getSize < 1 > () != _dimensions){
1058 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1059 "Your pts array is constructed for points of the wrong dimensionality.\n");
1062 if(pts.template getSize < 0 > () != indices.getNumElements()){
1063 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1064 "You did not put enough room in your pts array to fit all the points "
1065 "you asked for in your indices array.\n");
1068 if(fn.template getSize < 0 > () != indices.getNumElements()){
1069 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1070 "You did not provide enough room in your function value array "
1071 "for all of the points you requested in your indices array.\n");
1075 for(i = 0; i < indices.template getSize < 0 > (); i++){
1076 pts[i] = _kdTree.getData(indices[i]);
1078 for(j = 0; j < _nFunctions; j ++){
1079 fn[i][j] = _function[indices[i]][j];
1081 if(_useMaxMin == 1){
1082 for(j = 0; j < _dimensions; j ++){
1083 pts[i][j] *= (_max[j] - _min[j]);
1084 pts[i][j] += _min[j];
1092 template <
typename T >
1094 ndarray::Array < T,1,1 >
const &vin,
1095 int numberOfNeighbors)
const
1098 if(_nFunctions > 1){
1099 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1100 "You need to call the version of GaussianProcess.interpolate() "
1101 "that accepts mu and variance arrays (which it populates with results). "
1102 "You are interpolating more than one function.");
1105 if(numberOfNeighbors <= 0){
1106 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1107 "Asked for zero or negative number of neighbors\n");
1110 if(numberOfNeighbors > _kdTree.getNPoints()){
1111 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1112 "Asked for more neighbors than you have data points\n");
1115 if(variance.getNumElements() != _nFunctions){
1116 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1117 "Your variance array is the incorrect size for the number "
1118 "of functions you are trying to interpolate\n");
1121 if(vin.getNumElements() != _dimensions){
1122 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1123 "You are interpolating at a point with different dimensionality than you data\n");
1129 ndarray::Array < T,1,1 > covarianceTestPoint;
1130 ndarray::Array < int,1,1 > neighbors;
1131 ndarray::Array < double,1,1 > neighborDistances,vv;
1133 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1134 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1139 bb.resize(numberOfNeighbors, 1);
1140 xx.resize(numberOfNeighbors, 1);
1142 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1144 covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1146 neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1148 neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1150 vv = allocate(ndarray::makeVector(_dimensions));
1151 if(_useMaxMin == 1){
1157 for(i = 0; i < _dimensions; i++ ) vv[i] = (vin[i] - _min[i])/(_max[i] - _min[i]);
1164 _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1166 _timer.addToSearch();
1169 for(i = 0; i < numberOfNeighbors; i++ )fbar += _function[neighbors[i]][0];
1170 fbar = fbar/double(numberOfNeighbors);
1172 for(i = 0; i < numberOfNeighbors; i++ ){
1173 covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.getData(neighbors[i]));
1175 covariance(i,i) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i]))
1178 for(j = i + 1; j < numberOfNeighbors; j++ ){
1179 covariance(i,j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1180 _kdTree.getData(neighbors[j]));
1181 covariance(j,i) = covariance(i, j);
1185 _timer.addToIteration();
1188 ldlt.compute(covariance);
1190 for(i = 0; i < numberOfNeighbors; i++) bb(i,0) = _function[neighbors[i]][0] - fbar;
1192 xx = ldlt.solve(bb);
1193 _timer.addToEigen();
1197 for(i = 0; i < numberOfNeighbors; i++ ){
1198 mu += covarianceTestPoint[i]*xx(i, 0);
1201 _timer.addToIteration();
1203 variance(0) = (*_covariogram)(vv, vv) + _lambda;
1205 for(i = 0; i < numberOfNeighbors; i++ ) bb(i) = covarianceTestPoint[i];
1207 xx = ldlt.solve(bb);
1209 for(i = 0; i < numberOfNeighbors; i++ ){
1210 variance(0) -= covarianceTestPoint[i]*xx(i, 0);
1213 variance(0) = variance(0)*_krigingParameter;
1215 _timer.addToVariance();
1216 _timer.addToTotal(1);
1221 template <
typename T >
1223 ndarray::Array < T,1,1 > variance,
1224 ndarray::Array < T,1,1 >
const &vin,
1225 int numberOfNeighbors)
const
1229 if(numberOfNeighbors <= 0){
1230 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1231 "Asked for zero or negative number of neighbors\n");
1234 if(numberOfNeighbors > _kdTree.getNPoints()){
1235 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1236 "Asked for more neighbors than you have data points\n");
1239 if(vin.getNumElements() != _dimensions){
1240 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1241 "You are interpolating at a point with different dimensionality than you data\n");
1244 if(mu.getNumElements() != _nFunctions || variance.getNumElements() != _nFunctions){
1245 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1246 "Your mu and/or var arrays are improperly sized for the number of functions "
1247 "you are interpolating\n");
1253 ndarray::Array < T,1,1 > covarianceTestPoint;
1254 ndarray::Array < int,1,1 > neighbors;
1255 ndarray::Array < double,1,1 > neighborDistances,vv;
1257 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1258 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1263 bb.resize(numberOfNeighbors,1);
1264 xx.resize(numberOfNeighbors,1);
1265 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1266 covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1267 neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1268 neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1270 vv = allocate(ndarray::makeVector(_dimensions));
1273 if(_useMaxMin == 1) {
1279 for(i = 0; i < _dimensions; i++ )vv[i] = (vin[i] - _min[i])/(_max[i] - _min[i]);
1286 _kdTree.findNeighbors(neighbors, neighborDistances, vv, numberOfNeighbors);
1288 _timer.addToSearch();
1290 for(i = 0; i < numberOfNeighbors; i++ ) {
1291 covarianceTestPoint[i] = (*_covariogram)(vv, _kdTree.getData(neighbors[i]));
1293 covariance(i,i) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i]))
1296 for(j = i + 1; j < numberOfNeighbors; j++ ){
1297 covariance(i,j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1298 _kdTree.getData(neighbors[j]));
1299 covariance(j,i) = covariance(i, j);
1303 _timer.addToIteration();
1306 ldlt.compute(covariance);
1308 for(ii = 0; ii < _nFunctions; ii++ ) {
1311 for(i = 0; i < numberOfNeighbors; i++ )fbar += _function[neighbors[i]][ii];
1312 fbar = fbar/double(numberOfNeighbors);
1314 for(i = 0; i < numberOfNeighbors; i++ )bb(i,0) = _function[neighbors[i]][ii] - fbar;
1315 xx = ldlt.solve(bb);
1319 for(i = 0; i < numberOfNeighbors; i++ ) {
1320 mu[ii] += covarianceTestPoint[i]*xx(i, 0);
1325 _timer.addToEigen();
1327 variance[0] = (*_covariogram)(vv, vv) + _lambda;
1329 for(i = 0; i < numberOfNeighbors; i++ )bb(i) = covarianceTestPoint[i];
1331 xx = ldlt.solve(bb);
1333 for(i = 0; i < numberOfNeighbors; i++ ) {
1334 variance[0] -= covarianceTestPoint[i]*xx(i, 0);
1336 variance[0] = variance[0]*_krigingParameter;
1339 for(i = 1; i < _nFunctions; i++ )variance[i] = variance[0];
1341 _timer.addToVariance();
1342 _timer.addToTotal(1);
1346 template <
typename T >
1349 int numberOfNeighbors)
const
1352 if(_nFunctions > 1){
1353 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1354 "You need to call the version of GaussianProcess.selfInterpolate() "
1355 "that accepts mu and variance arrays (which it populates with results). "
1356 "You are interpolating more than one function.");
1359 if(variance.getNumElements() != _nFunctions){
1360 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1361 "Your variance array is the incorrect size for the number "
1362 "of functions you are trying to interpolate\n");
1365 if(numberOfNeighbors <= 0){
1366 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1367 "Asked for zero or negative number of neighbors\n");
1370 if(numberOfNeighbors + 1 > _kdTree.getNPoints()){
1371 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1372 "Asked for more neighbors than you have data points\n");
1375 if(dex < 0 || dex >=_kdTree.getNPoints()){
1376 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1377 "Asked to self interpolate on a point that does not exist\n");
1383 ndarray::Array < T,1,1 > covarianceTestPoint;
1384 ndarray::Array < int,1,1 > selfNeighbors;
1385 ndarray::Array < double,1,1 > selfDistances;
1386 ndarray::Array < int,1,1 > neighbors;
1387 ndarray::Array < double,1,1 > neighborDistances;
1389 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1390 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1394 bb.resize(numberOfNeighbors, 1);
1395 xx.resize(numberOfNeighbors, 1);
1396 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1397 covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1398 neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1399 neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1401 selfNeighbors = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1402 selfDistances = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1407 _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.getData(dex),
1408 numberOfNeighbors + 1);
1410 _timer.addToSearch();
1412 if(selfNeighbors[0]!= dex) {
1413 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1414 "Nearest neighbor search in selfInterpolate did not find self\n");
1422 for(i = 0; i < numberOfNeighbors; i++ ){
1423 neighbors[i] = selfNeighbors[i + 1];
1424 neighborDistances[i] = selfDistances[i + 1];
1428 for(i = 0; i < numberOfNeighbors; i++ ) fbar += _function[neighbors[i]][0];
1429 fbar = fbar/double(numberOfNeighbors);
1431 for(i = 0; i < numberOfNeighbors; i++ ){
1432 covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex),
1433 _kdTree.getData(neighbors[i]));
1435 covariance(i, i) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1436 _kdTree.getData(neighbors[i])) + _lambda;
1438 for(j = i + 1; j < numberOfNeighbors; j++ ) {
1439 covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1440 _kdTree.getData(neighbors[j]));
1441 covariance(j, i) = covariance(i ,j);
1444 _timer.addToIteration();
1447 ldlt.compute(covariance);
1450 for(i = 0; i < numberOfNeighbors;i++ ) bb(i, 0) = _function[neighbors[i]][0] - fbar;
1451 xx = ldlt.solve(bb);
1452 _timer.addToEigen();
1456 for(i = 0; i < numberOfNeighbors; i++ ) {
1457 mu += covarianceTestPoint[i]*xx(i, 0);
1461 variance(0) = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1463 for(i = 0; i < numberOfNeighbors; i++ )bb(i) = covarianceTestPoint[i];
1465 xx = ldlt.solve(bb);
1467 for(i = 0; i < numberOfNeighbors; i++ ){
1468 variance(0) -= covarianceTestPoint[i]*xx(i,0);
1471 variance(0) = variance(0)*_krigingParameter;
1472 _timer.addToVariance();
1473 _timer.addToTotal(1);
1478 template <
typename T >
1480 ndarray::Array < T,1,1 > variance,
1482 int numberOfNeighbors)
const{
1484 if(mu.getNumElements() != _nFunctions || variance.getNumElements() != _nFunctions){
1485 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1486 "Your mu and/or var arrays are improperly sized for the number of functions "
1487 "you are interpolating\n");
1490 if(numberOfNeighbors <= 0){
1491 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1492 "Asked for zero or negative number of neighbors\n");
1495 if(numberOfNeighbors + 1 > _kdTree.getNPoints()){
1496 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1497 "Asked for more neighbors than you have data points\n");
1500 if(dex < 0 || dex >=_kdTree.getNPoints()){
1501 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1502 "Asked to self interpolate on a point that does not exist\n");
1508 ndarray::Array < T,1,1 > covarianceTestPoint;
1509 ndarray::Array < int,1,1 > selfNeighbors;
1510 ndarray::Array < double,1,1 > selfDistances;
1511 ndarray::Array < int,1,1 > neighbors;
1512 ndarray::Array < double,1,1 > neighborDistances;
1514 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > covariance,bb,xx;
1515 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1519 bb.resize(numberOfNeighbors, 1);
1520 xx.resize(numberOfNeighbors, 1);
1521 covariance.resize(numberOfNeighbors,numberOfNeighbors);
1522 covarianceTestPoint = allocate(ndarray::makeVector(numberOfNeighbors));
1523 neighbors = allocate(ndarray::makeVector(numberOfNeighbors));
1524 neighborDistances = allocate(ndarray::makeVector(numberOfNeighbors));
1526 selfNeighbors = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1527 selfDistances = allocate(ndarray::makeVector(numberOfNeighbors + 1));
1532 _kdTree.findNeighbors(selfNeighbors, selfDistances, _kdTree.getData(dex),
1533 numberOfNeighbors + 1);
1535 _timer.addToSearch();
1537 if(selfNeighbors[0]!= dex) {
1539 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1540 "Nearest neighbor search in selfInterpolate did not find self\n");
1548 for(i = 0; i < numberOfNeighbors; i++ ) {
1549 neighbors[i] = selfNeighbors[i + 1];
1550 neighborDistances[i] = selfDistances[i + 1];
1555 for(i = 0; i < numberOfNeighbors; i++ ) {
1556 covarianceTestPoint[i] = (*_covariogram)(_kdTree.getData(dex),_kdTree.getData(neighbors[i]));
1558 covariance(i, i) = (*_covariogram)(_kdTree.getData(neighbors[i]), _kdTree.getData(neighbors[i]))
1561 for(j = i + 1; j < numberOfNeighbors; j++ ) {
1562 covariance(i, j) = (*_covariogram)(_kdTree.getData(neighbors[i]),
1563 _kdTree.getData(neighbors[j]));
1564 covariance(j, i) = covariance(i, j);
1567 _timer.addToIteration();
1571 ldlt.compute(covariance);
1573 for(ii = 0; ii < _nFunctions; ii++ ) {
1576 for(i = 0; i < numberOfNeighbors; i++ )fbar += _function[neighbors[i]][ii];
1577 fbar = fbar/double(numberOfNeighbors);
1579 for(i = 0; i < numberOfNeighbors; i++ )bb(i,0) = _function[neighbors[i]][ii] - fbar;
1580 xx = ldlt.solve(bb);
1584 for(i = 0; i < numberOfNeighbors; i++ ){
1585 mu[ii] += covarianceTestPoint[i]*xx(i,0);
1589 _timer.addToEigen();
1591 variance[0] = (*_covariogram)(_kdTree.getData(dex), _kdTree.getData(dex)) + _lambda;
1593 for(i = 0; i < numberOfNeighbors; i++ )bb(i) = covarianceTestPoint[i];
1595 xx = ldlt.solve(bb);
1597 for(i = 0; i < numberOfNeighbors; i++ ) {
1598 variance[0] -= covarianceTestPoint[i]*xx(i,0);
1601 variance[0] = variance[0]*_krigingParameter;
1603 for(i = 1; i < _nFunctions; i++ )variance[i] = variance[0];
1605 _timer.addToVariance();
1606 _timer.addToTotal(1);
1610 template <
typename T >
1612 ndarray:: Array < T,1,1 > variance,
1613 ndarray::Array < T,2,2 >
const &queries)
const
1616 int i,j,ii,nQueries;
1618 nQueries = queries.template getSize < 0 > ();
1620 if(_nFunctions != 1){
1621 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1622 "Your mu and variance arrays do not have room for all of the functions "
1623 "as you are trying to interpolate\n");
1626 if(mu.getNumElements() != nQueries || variance.getNumElements() != nQueries){
1627 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1628 "Your mu and variance arrays do not have room for all of the points "
1629 "at which you are trying to interpolate your function.\n");
1632 if(queries.template getSize < 1 > () != _dimensions){
1633 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1634 "The points you passed to batchInterpolate are of the wrong "
1635 "dimensionality for your Gaussian Process\n");
1639 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1640 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1641 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1643 ndarray::Array < T,1,1 > v1;
1648 v1 = allocate(ndarray::makeVector(_dimensions));
1649 batchbb.resize(_npts, 1);
1650 batchxx.resize(_npts, 1);
1651 batchCovariance.resize(_npts, _npts);
1652 queryCovariance.resize(_npts, 1);
1655 for(i = 0; i < _npts; i++ ) {
1657 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1658 for(j = i + 1; j < _npts; j++ ) {
1659 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1660 batchCovariance(j, i) = batchCovariance(i, j);
1663 _timer.addToIteration();
1665 ldlt.compute(batchCovariance);
1668 for(i = 0; i < _npts; i++ ) {
1669 fbar += _function[i][0];
1671 fbar = fbar/T(_npts);
1673 for(i = 0; i < _npts; i++ ){
1674 batchbb(i, 0) = _function[i][0] - fbar;
1676 batchxx = ldlt.solve(batchbb);
1677 _timer.addToEigen();
1680 for(ii = 0; ii < nQueries; ii++ ) {
1681 for(i = 0; i < _dimensions; i++ )v1[i] = queries[ii][i];
1682 if(_useMaxMin == 1) {
1683 for(i = 0; i < _dimensions; i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1686 for(i = 0; i < _npts; i++ ){
1687 mu(ii) += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1690 _timer.addToIteration();
1692 for(ii = 0; ii < nQueries; ii++ ) {
1693 for(i = 0; i < _dimensions; i++ )v1[i] = queries[ii][i];
1694 if(_useMaxMin == 1) {
1695 for(i = 0; i < _dimensions; i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1698 for(i = 0; i < _npts; i++ ) {
1699 batchbb(i, 0) = (*_covariogram)(v1, _kdTree.getData(i));
1700 queryCovariance(i, 0) = batchbb(i, 0);
1702 batchxx = ldlt.solve(batchbb);
1704 variance[ii] = (*_covariogram)(v1, v1) + _lambda;
1706 for(i = 0; i < _npts; i++ ){
1707 variance[ii] -= queryCovariance(i, 0)*batchxx(i);
1710 variance[ii] = variance[ii]*_krigingParameter;
1714 _timer.addToVariance();
1715 _timer.addToTotal(nQueries);
1718 template <
typename T >
1720 ndarray:: Array < T,2,2 > variance,
1721 ndarray::Array < T,2,2 >
const &queries)
const
1724 int i,j,ii,nQueries,ifn;
1726 nQueries = queries.template getSize < 0 > ();
1728 if(mu.template getSize < 0 > () != nQueries || variance.template getSize < 0 > () != nQueries){
1729 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1730 "Your output arrays do not have room for all of the points at which "
1731 "you are interpolating your functions.\n");
1734 if(mu.template getSize < 1 > () != _nFunctions || variance.template getSize < 1 > () != _nFunctions){
1735 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1736 "Your output arrays do not have room for all of the functions you are "
1740 if(queries.template getSize < 1 > () != _dimensions){
1741 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1742 "The points at which you are interpolating your functions have the "
1743 "wrong dimensionality.\n");
1747 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1748 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1749 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1751 ndarray::Array < T,1,1 > v1;
1755 v1 = allocate(ndarray::makeVector(_dimensions));
1756 batchbb.resize(_npts, 1);
1757 batchxx.resize(_npts, 1);
1758 batchCovariance.resize(_npts, _npts);
1759 queryCovariance.resize(_npts, 1);
1761 for(i = 0; i < _npts; i++ ){
1762 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1763 for(j = i + 1; j < _npts; j++ ) {
1764 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1765 batchCovariance(j, i) = batchCovariance(i, j);
1769 _timer.addToIteration();
1771 ldlt.compute(batchCovariance);
1773 _timer.addToEigen();
1775 for(ifn = 0; ifn < _nFunctions; ifn++ ) {
1778 for(i = 0; i < _npts; i++ ){
1779 fbar += _function[i][ifn];
1781 fbar = fbar/T(_npts);
1782 _timer.addToIteration();
1784 for(i = 0; i < _npts; i++ ){
1785 batchbb(i,0) = _function[i][ifn] - fbar;
1787 batchxx = ldlt.solve(batchbb);
1788 _timer.addToEigen();
1791 for(ii = 0; ii < nQueries; ii++ ){
1792 for(i = 0; i < _dimensions; i++ ) v1[i] = queries[ii][i];
1793 if(_useMaxMin == 1) {
1794 for(i = 0; i < _dimensions; i++ ) v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1797 for(i = 0; i < _npts; i++ ){
1798 mu[ii][ifn] += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1804 _timer.addToIteration();
1805 for(ii = 0; ii < nQueries; ii++ ){
1806 for(i = 0; i < _dimensions; i++ ) v1[i] = queries[ii][i];
1807 if(_useMaxMin == 1){
1808 for(i = 0; i < _dimensions; i++ ) v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1811 for(i = 0;i < _npts;i++ ) {
1812 batchbb(i,0) = (*_covariogram)(v1,_kdTree.getData(i));
1813 queryCovariance(i,0) = batchbb(i,0);
1815 batchxx = ldlt.solve(batchbb);
1817 variance[ii][0] = (*_covariogram)(v1, v1) + _lambda;
1819 for(i = 0; i < _npts; i++ ) {
1820 variance[ii][0] -= queryCovariance(i, 0)*batchxx(i);
1823 variance[ii][0] = variance[ii][0]*_krigingParameter;
1824 for(i = 1; i < _nFunctions; i++ ) variance[ii][i] = variance[ii][0];
1827 _timer.addToVariance();
1828 _timer.addToTotal(nQueries);
1831 template <
typename T >
1833 ndarray::Array < T,2,2 >
const &queries)
const
1836 int i,j,ii,nQueries;
1838 nQueries = queries.template getSize < 0 > ();
1840 if(_nFunctions != 1){
1841 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1842 "Your output array does not have enough room for all of the functions "
1843 "you are trying to interpolate.\n");
1846 if(queries.template getSize <1> () != _dimensions){
1847 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1848 "The points at which you are trying to interpolate your function are "
1849 "of the wrong dimensionality.\n");
1852 if(mu.getNumElements() != nQueries){
1853 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1854 "Your output array does not have enough room for all of the points "
1855 "at which you are trying to interpolate your function.\n");
1859 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1860 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1861 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1863 ndarray::Array < T,1,1 > v1;
1867 v1 = allocate(ndarray::makeVector(_dimensions));
1869 batchbb.resize(_npts, 1);
1870 batchxx.resize(_npts, 1);
1871 batchCovariance.resize(_npts, _npts);
1872 queryCovariance.resize(_npts, 1);
1875 for(i = 0; i < _npts; i++ ) {
1876 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1877 for(j = i + 1; j < _npts; j++ ) {
1878 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1879 batchCovariance(j, i) = batchCovariance(i, j);
1882 _timer.addToIteration();
1884 ldlt.compute(batchCovariance);
1887 for(i = 0; i < _npts; i++ ) {
1888 fbar += _function[i][0];
1890 fbar = fbar/T(_npts);
1892 for(i = 0; i < _npts; i++ ) {
1893 batchbb(i, 0) = _function[i][0] - fbar;
1895 batchxx = ldlt.solve(batchbb);
1896 _timer.addToEigen();
1898 for(ii = 0; ii < nQueries; ii++ ) {
1900 for(i = 0; i < _dimensions; i++ ) v1[i] = queries[ii][i];
1901 if(_useMaxMin == 1) {
1902 for(i = 0; i < _dimensions; i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1906 for(i = 0; i < _npts; i++ ) {
1907 mu(ii) += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
1910 _timer.addToIteration();
1911 _timer.addToTotal(nQueries);
1915 template <
typename T >
1917 ndarray::Array < T,2,2 >
const &queries)
const
1920 int i,j,ii,nQueries,ifn;
1922 nQueries = queries.template getSize < 0 > ();
1924 if(mu.template getSize < 0 > () != nQueries){
1925 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1926 "Your output array does not have enough room for all of the points "
1927 "at which you want to interpolate your functions.\n");
1930 if(mu.template getSize < 1 > () != _nFunctions){
1931 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1932 "Your output array does not have enough room for all of the functions "
1933 "you are trying to interpolate.\n");
1936 if(queries.template getSize < 1 > () != _dimensions){
1937 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
1938 "The points at which you are interpolating your functions do not "
1939 "have the correct dimensionality.\n");
1943 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > batchCovariance,batchbb,batchxx;
1944 Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > queryCovariance;
1945 Eigen::LDLT < Eigen::Matrix < T,Eigen::Dynamic,Eigen::Dynamic > > ldlt;
1947 ndarray::Array < T,1,1 > v1;
1951 v1 = allocate(ndarray::makeVector(_dimensions));
1953 batchbb.resize(_npts, 1);
1954 batchxx.resize(_npts, 1);
1955 batchCovariance.resize(_npts, _npts);
1956 queryCovariance.resize(_npts, 1);
1959 for(i = 0; i < _npts; i++ ){
1960 batchCovariance(i, i) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(i)) + _lambda;
1961 for(j = i + 1; j < _npts; j++ ){
1962 batchCovariance(i, j) = (*_covariogram)(_kdTree.getData(i), _kdTree.getData(j));
1963 batchCovariance(j, i) = batchCovariance(i, j);
1967 _timer.addToIteration();
1969 ldlt.compute(batchCovariance);
1971 _timer.addToEigen();
1973 for(ifn = 0; ifn < _nFunctions; ifn++ ){
1976 for(i = 0; i < _npts; i++ ){
1977 fbar += _function[i][ifn];
1979 fbar = fbar/T(_npts);
1981 _timer.addToIteration();
1983 for(i = 0; i < _npts; i++ ){
1984 batchbb(i, 0) = _function[i][ifn] - fbar;
1986 batchxx = ldlt.solve(batchbb);
1987 _timer.addToEigen();
1990 for(ii = 0; ii < nQueries; ii++ ){
1991 for(i = 0; i < _dimensions; i++ )v1[i] = queries[ii][i];
1992 if(_useMaxMin == 1){
1993 for(i = 0;i < _dimensions;i++ )v1[i] = (v1[i] - _min[i])/(_max[i] - _min[i]);
1997 for(i = 0; i < _npts; i++ ){
1998 mu[ii][ifn] += batchxx(i)*(*_covariogram)(v1, _kdTree.getData(i));
2005 _timer.addToTotal(nQueries);
2010 template <
typename T >
2016 if(_nFunctions!= 1){
2018 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
2019 "You are calling the wrong addPoint; you need a vector of functions\n");
2023 if(vin.getNumElements() != _dimensions){
2024 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
2025 "You are trying to add a point of the wrong dimensionality to "
2026 "your GaussianProcess.\n");
2029 ndarray::Array < T,1,1 > v;
2030 v = allocate(ndarray::makeVector(_dimensions));
2032 for(i = 0; i < _dimensions; i++ ){
2034 if(_useMaxMin == 1){
2035 v[i] = (v[i] - _min[i])/(_max[i] - _min[i]);
2040 ndarray::Array < T,2,2 > buff;
2041 buff = allocate(ndarray::makeVector(_npts, _nFunctions));
2042 buff.deep() = _function;
2045 _function = allocate(ndarray::makeVector(_room, _nFunctions));
2046 for(i = 0; i < _npts; i++ ) {
2048 for(j = 0; j < _nFunctions; j++ ) {
2049 _function[i][j] = buff[i][j];
2055 _function[_npts][0] = f;
2057 _kdTree.addPoint(v);
2058 _npts = _kdTree.getNPoints();
2062 template <
typename T >
2064 ndarray::Array < T,1,1 >
const &f)
2067 if(vin.getNumElements() != _dimensions){
2068 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
2069 "You are trying to add a point of the wrong dimensionality to "
2070 "your GaussianProcess.\n");
2073 if(f.template getSize < 0 > () != _nFunctions){
2074 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
2075 "You are not adding the correct number of function values to "
2076 "your GaussianProcess.\n");
2081 ndarray::Array < T,1,1 > v;
2082 v = allocate(ndarray::makeVector(_dimensions));
2084 for(i = 0; i < _dimensions; i++ ) {
2086 if(_useMaxMin == 1) {
2087 v[i] = (v[i] - _min[i])/(_max[i] - _min[i]);
2091 if(_npts == _room) {
2092 ndarray::Array < T,2,2 > buff;
2093 buff = allocate(ndarray::makeVector(_npts, _nFunctions));
2094 buff.deep() = _function;
2097 _function = allocate(ndarray::makeVector(_room, _nFunctions));
2098 for(i = 0; i < _npts; i++ ) {
2099 for(j = 0; j < _nFunctions; j++ ) {
2100 _function[i][j] = buff[i][j];
2105 for(i = 0; i < _nFunctions; i++ )_function[_npts][i] = f[i];
2107 _kdTree.addPoint(v);
2108 _npts = _kdTree.getNPoints();
2113 template <
typename T >
2121 for(i = dex; i < _npts; i++ ) {
2122 for(j = 0; j < _nFunctions; j++ ) {
2123 _function[i][j] = _function[i + 1][j];
2126 _npts = _kdTree.getNPoints();
2129 template <
typename T >
2132 _krigingParameter = kk;
2135 template <
typename T >
2137 _covariogram = covar;
2140 template <
typename T >
2146 template <
typename T >
2152 template <
typename T >
2155 template <
typename T >
2157 ndarray::Array < const T,1,1 >
const &p2)
const
2159 std::cout <<
"by the way, you are calling the wrong operator\n";
2164 template <
typename T >
2167 template <
typename T >
2173 template <
typename T >
2176 _ellSquared = ellSquared;
2179 template <
typename T >
2181 ndarray::Array < const T,1,1 >
const &p2)
const
2186 for(i = 0; i < p1.template getSize < 0 > (); i++ ){
2187 d += (p1[i] - p2[i])*(p1[i] - p2[i]);
2191 return T(exp( - 0.5*d));
2194 template <
typename T >
2197 template <
typename T >
2204 template <
typename T >
2206 ndarray::Array < const T,1,1 >
const &p2
2210 double num,denom1,denom2,arg;
2212 dim = p1.template getSize < 0 > ();
2215 denom1 = 1.0 + 2.0*_sigma0;
2216 denom2 = 1.0 + 2.0*_sigma0;
2218 for(i = 0; i < dim; i++ ) {
2219 num += 2.0*p1[i]*p2[i]*_sigma1;
2220 denom1 += 2.0*p1[i]*p1[i]*_sigma1;
2221 denom2 += 2.0*p2[i]*p2[i]*_sigma1;
2224 arg = num/::sqrt(denom1*denom2);
2225 return T(2.0*(::asin(arg))/3.141592654);
2229 template <
typename T >
2235 template <
typename T >
2243 #define gpn lsst::afw::math
2245 #define INSTANTIATEGP(T) \
2246 template class gpn::KdTree < T > ; \
2247 template class gpn::GaussianProcess < T > ; \
2248 template class gpn::Covariogram < T > ; \
2249 template class gpn::SquaredExpCovariogram < T > ;\
2250 template class gpn::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 falls off as the negative exponent of the square of the distance between the point...
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
#define LSST_EXCEPT(type,...)
Create an exception with a given type and message and optionally other arguments (dependent on the ty...
afw::table::Key< double > sigma1
void removePoint(int dex)
This will remove a point from the data set.
static DateTime now(void)
Return current time as a DateTime.