GNU Octave 7.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
__dsearchn__.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2007-2022 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if defined (HAVE_CONFIG_H)
27# include "config.h"
28#endif
29
30#include <cmath>
31
32#include <string>
33
34#include "defun.h"
35#include "error.h"
36#include "ovl.h"
37
38OCTAVE_NAMESPACE_BEGIN
39
40DEFUN (__dsearchn__, args, ,
41 doc: /* -*- texinfo -*-
42@deftypefn {} {[@var{idx}, @var{d}] =} dsearch (@var{x}, @var{xi})
43Undocumented internal function.
44@end deftypefn */)
45{
46 if (args.length () != 2)
47 print_usage ();
48
49 Matrix x = args(0).matrix_value ().transpose ();
50 Matrix xi = args(1).matrix_value ().transpose ();
51
52 if (x.rows () != xi.rows () || x.columns () < 1)
53 error ("__dsearchn__: number of rows of X and XI must match");
54
55 octave_idx_type n = x.rows ();
56 octave_idx_type nx = x.columns ();
57 octave_idx_type nxi = xi.columns ();
58
59 ColumnVector idx (nxi);
60 double *pidx = idx.fortran_vec ();
61 ColumnVector dist (nxi);
62 double *pdist = dist.fortran_vec ();
63
64#define DIST(dd, y, yi, m) \
65 dd = 0.0; \
66 for (octave_idx_type k = 0; k < m; k++) \
67 { \
68 double yd = y[k] - yi[k]; \
69 dd += yd * yd; \
70 } \
71 dd = sqrt (dd)
72
73 const double *pxi = xi.data ();
74 for (octave_idx_type i = 0; i < nxi; i++)
75 {
76 double d0;
77 const double *px = x.data ();
78 DIST(d0, px, pxi, n);
79 *pidx = 1.;
80 for (octave_idx_type j = 1; j < nx; j++)
81 {
82 px += n;
83 double d;
84 DIST (d, px, pxi, n);
85 if (d < d0)
86 {
87 d0 = d;
88 *pidx = static_cast<double> (j + 1);
89 }
90 octave_quit ();
91 }
92
93 *pdist++ = d0;
94 pidx++;
95 pxi += n;
96 }
97
98 return ovl (idx, dist);
99}
100
101/*
102## No test needed for internal helper function.
103%!assert (1)
104*/
105
106OCTAVE_NAMESPACE_END
#define DIST(dd, y, yi, m)
OCTARRAY_API T * fortran_vec(void)
Size of the specified dimension.
Definition: Array.cc:1744
Definition: dMatrix.h:42
OCTINTERP_API void print_usage(void)
Definition: defun-int.h:72
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
void error(const char *fmt,...)
Definition: error.cc:980
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
static const double xi[33]
Definition: quadcc.cc:68