GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
__dsearchn__.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2007-2017 David Bateman
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include <iostream>
28 #include <fstream>
29 #include <string>
30 
31 #include "lo-math.h"
32 
33 #include "defun.h"
34 #include "error.h"
35 #include "ovl.h"
36 
37 DEFUN (__dsearchn__, args, ,
38  doc: /* -*- texinfo -*-
39 @deftypefn {} {[@var{idx}, @var{d}] =} dsearch (@var{x}, @var{xi})
40 Undocumented internal function.
41 @end deftypefn */)
42 {
43  if (args.length () != 2)
44  print_usage ();
45 
46  Matrix x = args(0).matrix_value ().transpose ();
47  Matrix xi = args(1).matrix_value ().transpose ();
48 
49  if (x.rows () != xi.rows () || x.columns () < 1)
50  error ("__dsearch__: number of rows of X and XI must match");
51 
52  octave_idx_type n = x.rows ();
53  octave_idx_type nx = x.columns ();
54  octave_idx_type nxi = xi.columns ();
55 
56  ColumnVector idx (nxi);
57  double *pidx = idx.fortran_vec ();
58  ColumnVector dist (nxi);
59  double *pdist = dist.fortran_vec ();
60 
61 #define DIST(dd, y, yi, m) \
62  dd = 0.0; \
63  for (octave_idx_type k = 0; k < m; k++) \
64  { \
65  double yd = y[k] - yi[k]; \
66  dd += yd * yd; \
67  } \
68  dd = sqrt (dd)
69 
70  const double *pxi = xi.fortran_vec ();
71  for (octave_idx_type i = 0; i < nxi; i++)
72  {
73  double d0;
74  const double *px = x.fortran_vec ();
75  DIST(d0, px, pxi, n);
76  *pidx = 1.;
77  for (octave_idx_type j = 1; j < nx; j++)
78  {
79  px += n;
80  double d;
81  DIST (d, px, pxi, n);
82  if (d < d0)
83  {
84  d0 = d;
85  *pidx = static_cast<double>(j + 1);
86  }
88  }
89 
90  *pdist++ = d0;
91  pidx++;
92  pxi += n;
93  }
94 
95  return ovl (idx, dist);
96 }
97 
98 /*
99 ## No test needed for internal helper function.
100 %!assert (1)
101 */
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).is_integer_type())
OCTINTERP_API void print_usage(void)
Definition: defun.cc:52
#define DEFUN(name, args_name, nargout_name, doc)
Definition: defun.h:46
void error(const char *fmt,...)
Definition: error.cc:570
octave_idx_type rows(void) const
Definition: Array.h:401
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define DIST(dd, y, yi, m)
JNIEnv void * args
Definition: ov-java.cc:67
Definition: dMatrix.h:37
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
const T * fortran_vec(void) const
Definition: Array.h:584
#define OCTAVE_QUIT
Definition: quit.h:212
static const double xi[33]
Definition: quadcc.cc:55
octave_idx_type columns(void) const
Definition: Array.h:410
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x