GNU Octave  6.2.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-2021 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 
38 DEFUN (__dsearchn__, args, ,
39  doc: /* -*- texinfo -*-
40 @deftypefn {} {[@var{idx}, @var{d}] =} dsearch (@var{x}, @var{xi})
41 Undocumented internal function.
42 @end deftypefn */)
43 {
44  if (args.length () != 2)
45  print_usage ();
46 
47  Matrix x = args(0).matrix_value ().transpose ();
48  Matrix xi = args(1).matrix_value ().transpose ();
49 
50  if (x.rows () != xi.rows () || x.columns () < 1)
51  error ("__dsearch__: number of rows of X and XI must match");
52 
53  octave_idx_type n = x.rows ();
54  octave_idx_type nx = x.columns ();
55  octave_idx_type nxi = xi.columns ();
56 
57  ColumnVector idx (nxi);
58  double *pidx = idx.fortran_vec ();
59  ColumnVector dist (nxi);
60  double *pdist = dist.fortran_vec ();
61 
62 #define DIST(dd, y, yi, m) \
63  dd = 0.0; \
64  for (octave_idx_type k = 0; k < m; k++) \
65  { \
66  double yd = y[k] - yi[k]; \
67  dd += yd * yd; \
68  } \
69  dd = sqrt (dd)
70 
71  const double *pxi = xi.fortran_vec ();
72  for (octave_idx_type i = 0; i < nxi; i++)
73  {
74  double d0;
75  const double *px = x.fortran_vec ();
76  DIST(d0, px, pxi, n);
77  *pidx = 1.;
78  for (octave_idx_type j = 1; j < nx; j++)
79  {
80  px += n;
81  double d;
82  DIST (d, px, pxi, n);
83  if (d < d0)
84  {
85  d0 = d;
86  *pidx = static_cast<double> (j + 1);
87  }
88  octave_quit ();
89  }
90 
91  *pdist++ = d0;
92  pidx++;
93  pxi += n;
94  }
95 
96  return ovl (idx, dist);
97 }
98 
99 /*
100 ## No test needed for internal helper function.
101 %!assert (1)
102 */
#define DIST(dd, y, yi, m)
const T * fortran_vec(void) const
Size of the specified dimension.
Definition: Array.h:583
Definition: dMatrix.h:42
OCTINTERP_API void print_usage(void)
Definition: defun.cc:53
#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:968
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
F77_RET_T const F77_DBLE * x
octave_idx_type n
Definition: mx-inlines.cc:753
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:66