6823
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 David Bateman <dbateman@free.fr> |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
6823
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
6823
|
20 |
|
21 */ |
|
22 |
|
23 #include <iostream> |
|
24 #include <fstream> |
|
25 #include <cmath> |
|
26 #include <string> |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include "oct.h" |
|
33 |
|
34 DEFUN_DLD (__dsearchn__, args, , |
|
35 "-*- texinfo -*-\n\ |
|
36 @deftypefn {Loadable Function} {[@var{idx}, @var{d}] =} dsearch (@var{x}, @var{xi})\n\ |
6945
|
37 Undocumented internal function.\n\ |
6823
|
38 @end deftypefn") |
|
39 { |
|
40 int nargin = args.length(); |
|
41 octave_value_list retval; |
|
42 |
|
43 if (nargin != 2) |
|
44 { |
|
45 print_usage (); |
|
46 return retval; |
|
47 } |
|
48 |
|
49 Matrix x = args(0).matrix_value().transpose (); |
|
50 Matrix xi = args(1).matrix_value().transpose (); |
|
51 |
|
52 if (! error_state) |
|
53 { |
|
54 if (x.rows() != xi.rows() || x.columns() < 1) |
6945
|
55 error ("__dsearch__: dimensional mismatch"); |
6823
|
56 else |
|
57 { |
|
58 octave_idx_type n = x.rows(); |
|
59 octave_idx_type nx = x.columns(); |
|
60 octave_idx_type nxi = xi.columns(); |
|
61 |
|
62 ColumnVector idx (nxi); |
|
63 double *pidx = idx.fortran_vec (); |
|
64 ColumnVector dist (nxi); |
|
65 double *pdist = dist.fortran_vec (); |
|
66 |
|
67 #define DIST(dd, y, yi, m) \ |
|
68 dd = 0.; \ |
|
69 for (octave_idx_type k = 0; k < m; k++) \ |
|
70 { \ |
|
71 double yd = y[k] - yi[k]; \ |
|
72 dd += yd * yd; \ |
|
73 } \ |
|
74 dd = sqrt (dd); |
|
75 |
|
76 const double *pxi = xi.fortran_vec (); |
|
77 for (octave_idx_type i = 0; i < nxi; i++) |
|
78 { |
|
79 double d0; |
|
80 const double *px = x.fortran_vec (); |
|
81 DIST(d0, px, pxi, n); |
|
82 *pidx = 1.; |
|
83 for (octave_idx_type j = 1; j < nx; j++) |
|
84 { |
|
85 px += n; |
|
86 double d; |
|
87 DIST (d, px, pxi, n); |
|
88 if (d < d0) |
|
89 { |
|
90 d0 = d; |
|
91 *pidx = static_cast<double>(j + 1); |
|
92 } |
|
93 OCTAVE_QUIT; |
|
94 } |
|
95 |
|
96 *pdist++ = d0; |
|
97 pidx++; |
|
98 pxi += n; |
|
99 } |
|
100 |
|
101 retval(1) = dist; |
|
102 retval(0) = idx; |
|
103 } |
|
104 } |
|
105 |
|
106 return retval; |
|
107 } |