| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : MatQuery.tol |
|---|
| 4 | // PURPOSE: Defines KNN related classes |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 8 | Text _.autodoc.member.KNN.Neighbourhood = |
|---|
| 9 | "Result of K-Nearest Neighbor Searching "; |
|---|
| 10 | Class @KNN.Neighbourhood |
|---|
| 11 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 12 | { |
|---|
| 13 | //Number of searched neigbours |
|---|
| 14 | Real _.k = 0; |
|---|
| 15 | //Number of queried points |
|---|
| 16 | Real _.q = 0; |
|---|
| 17 | //Query points by rows: Rows(_.Q)==_.q && Columns(_.Q)==_.k |
|---|
| 18 | Matrix _.Q = Rand(0,0,0,0); |
|---|
| 19 | //Indexes of neighbours in the searcher engine |
|---|
| 20 | Matrix _.neighbour = Rand(0,0,0,0); |
|---|
| 21 | //Distance from queried points to its found neighbours |
|---|
| 22 | Matrix _.distance2 = Rand(0,0,0,0); |
|---|
| 23 | |
|---|
| 24 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 25 | Static @KNN.Neighbourhood Default(Real void) |
|---|
| 26 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 27 | { |
|---|
| 28 | @KNN.Neighbourhood new |
|---|
| 29 | }; |
|---|
| 30 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 31 | Real set.data( |
|---|
| 32 | Matrix Q, |
|---|
| 33 | Matrix neighbour, |
|---|
| 34 | Matrix distance2) |
|---|
| 35 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 36 | { |
|---|
| 37 | Real _.k := Columns(Q); |
|---|
| 38 | Real _.q := Rows(Q); |
|---|
| 39 | Matrix _.Q := Q; |
|---|
| 40 | Matrix _.neighbour := neighbour; |
|---|
| 41 | Matrix _.distance2 := distance2; |
|---|
| 42 | True |
|---|
| 43 | }; |
|---|
| 44 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 45 | Static @KNN.Neighbourhood New( |
|---|
| 46 | Matrix Q, |
|---|
| 47 | Matrix neighbour, |
|---|
| 48 | Matrix distance2) |
|---|
| 49 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 50 | { |
|---|
| 51 | @KNN.Neighbourhood new; |
|---|
| 52 | Real new::set.data(Q,neighbour,distance2); |
|---|
| 53 | new |
|---|
| 54 | } |
|---|
| 55 | }; |
|---|
| 56 | |
|---|
| 57 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 58 | Text _.autodoc.member.KNN = |
|---|
| 59 | "Abstract class to inherite K-Nearest Neighbor Searching engines"; |
|---|
| 60 | Class @KNN |
|---|
| 61 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 62 | { |
|---|
| 63 | //Dimension of points |
|---|
| 64 | Real _.n = 0; |
|---|
| 65 | //Number of reference points |
|---|
| 66 | Real _.m = 0; |
|---|
| 67 | //Reference points by rows: Rows(_.X)==_.m && Columns(_.X)==_.n |
|---|
| 68 | Matrix _.X = Rand(0,0,0,0); |
|---|
| 69 | |
|---|
| 70 | //Searches |
|---|
| 71 | @KNN.Neighbourhood search(Matrix Q, Real k); |
|---|
| 72 | Real update.search(Matrix Q, Real k, @KNN.Neighbourhood ngbh) |
|---|
| 73 | }; |
|---|
| 74 | |
|---|
| 75 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 76 | Text _.autodoc.member.ANN.KDTree = |
|---|
| 77 | "This class is part of the TOL-OOP-API for ANN: A Library for Approximate " |
|---|
| 78 | "Nearest Neighbor Searching "; |
|---|
| 79 | Class @ANN.KDTree : @KNN |
|---|
| 80 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 81 | { |
|---|
| 82 | //ANN identifier of internal KDTree |
|---|
| 83 | Real _id = ?; |
|---|
| 84 | |
|---|
| 85 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 86 | Static @ANN.KDTree Default(Real void) |
|---|
| 87 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 88 | { |
|---|
| 89 | @ANN.KDTree new |
|---|
| 90 | }; |
|---|
| 91 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 92 | Real set.data(Matrix X) |
|---|
| 93 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 94 | { |
|---|
| 95 | Real __destroy(0); |
|---|
| 96 | Real _.n := Columns(X); |
|---|
| 97 | Real _.m := Rows(X); |
|---|
| 98 | Matrix _.X := X; |
|---|
| 99 | Real _id := ANN.KDTree.New(_.X) |
|---|
| 100 | }; |
|---|
| 101 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 102 | Static @ANN.KDTree New(Matrix X) |
|---|
| 103 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 104 | { |
|---|
| 105 | @ANN.KDTree new; |
|---|
| 106 | Real set.data(X); |
|---|
| 107 | new |
|---|
| 108 | }; |
|---|
| 109 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 110 | Real __destroy(Real void) |
|---|
| 111 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 112 | { |
|---|
| 113 | If(!IsUnknown(_id) & _id, ANN.KDTree.Delete(_id)); |
|---|
| 114 | Real _id := 0; |
|---|
| 115 | True |
|---|
| 116 | }; |
|---|
| 117 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 118 | @KNN.Neighbourhood search(Matrix Q, Real k) |
|---|
| 119 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 120 | { |
|---|
| 121 | Set aux=ANN.KDTree.KSearch(_id,Q,k); |
|---|
| 122 | @KNN.Neighbourhood::New(Q,aux[1],aux[2]) |
|---|
| 123 | }; |
|---|
| 124 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 125 | Real update.search(Matrix Q, Real k, @KNN.Neighbourhood ngbh) |
|---|
| 126 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 127 | { |
|---|
| 128 | Set aux=ANN.KDTree.KSearch(_id,Q,k); |
|---|
| 129 | ngbh::set.data(Q,aux[1],aux[2]) |
|---|
| 130 | } |
|---|
| 131 | }; |
|---|