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 |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
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 |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #include <iostream> |
|
25 #include <fstream> |
|
26 #include <cmath> |
|
27 #include <string> |
|
28 |
|
29 #ifdef HAVE_CONFIG_H |
|
30 #include <config.h> |
|
31 #endif |
|
32 |
|
33 #include "oct.h" |
|
34 |
|
35 DEFUN_DLD (__dsearchn__, args, , |
|
36 "-*- texinfo -*-\n\ |
|
37 @deftypefn {Loadable Function} {[@var{idx}, @var{d}] =} dsearch (@var{x}, @var{xi})\n\ |
6945
|
38 Undocumented internal function.\n\ |
6823
|
39 @end deftypefn") |
|
40 { |
|
41 int nargin = args.length(); |
|
42 octave_value_list retval; |
|
43 |
|
44 if (nargin != 2) |
|
45 { |
|
46 print_usage (); |
|
47 return retval; |
|
48 } |
|
49 |
|
50 Matrix x = args(0).matrix_value().transpose (); |
|
51 Matrix xi = args(1).matrix_value().transpose (); |
|
52 |
|
53 if (! error_state) |
|
54 { |
|
55 if (x.rows() != xi.rows() || x.columns() < 1) |
6945
|
56 error ("__dsearch__: dimensional mismatch"); |
6823
|
57 else |
|
58 { |
|
59 octave_idx_type n = x.rows(); |
|
60 octave_idx_type nx = x.columns(); |
|
61 octave_idx_type nxi = xi.columns(); |
|
62 |
|
63 ColumnVector idx (nxi); |
|
64 double *pidx = idx.fortran_vec (); |
|
65 ColumnVector dist (nxi); |
|
66 double *pdist = dist.fortran_vec (); |
|
67 |
|
68 #define DIST(dd, y, yi, m) \ |
|
69 dd = 0.; \ |
|
70 for (octave_idx_type k = 0; k < m; k++) \ |
|
71 { \ |
|
72 double yd = y[k] - yi[k]; \ |
|
73 dd += yd * yd; \ |
|
74 } \ |
|
75 dd = sqrt (dd); |
|
76 |
|
77 const double *pxi = xi.fortran_vec (); |
|
78 for (octave_idx_type i = 0; i < nxi; i++) |
|
79 { |
|
80 double d0; |
|
81 const double *px = x.fortran_vec (); |
|
82 DIST(d0, px, pxi, n); |
|
83 *pidx = 1.; |
|
84 for (octave_idx_type j = 1; j < nx; j++) |
|
85 { |
|
86 px += n; |
|
87 double d; |
|
88 DIST (d, px, pxi, n); |
|
89 if (d < d0) |
|
90 { |
|
91 d0 = d; |
|
92 *pidx = static_cast<double>(j + 1); |
|
93 } |
|
94 OCTAVE_QUIT; |
|
95 } |
|
96 |
|
97 *pdist++ = d0; |
|
98 pidx++; |
|
99 pxi += n; |
|
100 } |
|
101 |
|
102 retval(1) = dist; |
|
103 retval(0) = idx; |
|
104 } |
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |