__dsearchn__.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 David Bateman
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <iostream>
00028 #include <fstream>
00029 #include <string>
00030 
00031 #include "lo-math.h"
00032 
00033 #include "defun-dld.h"
00034 #include "error.h"
00035 #include "oct-obj.h"
00036 
00037 DEFUN_DLD (__dsearchn__, args, ,
00038         "-*- texinfo -*-\n\
00039 @deftypefn {Loadable Function} {[@var{idx}, @var{d}] =} dsearch (@var{x}, @var{xi})\n\
00040 Undocumented internal function.\n\
00041 @end deftypefn")
00042 {
00043   int nargin = args.length();
00044   octave_value_list retval;
00045 
00046   if (nargin != 2)
00047     {
00048       print_usage ();
00049       return retval;
00050     }
00051 
00052   Matrix x = args(0).matrix_value().transpose ();
00053   Matrix xi = args(1).matrix_value().transpose ();
00054 
00055   if (! error_state)
00056     {
00057       if (x.rows() != xi.rows() || x.columns() < 1)
00058         error ("__dsearch__: number of rows of X and XI must match");
00059       else
00060         {
00061           octave_idx_type n = x.rows();
00062           octave_idx_type nx = x.columns();
00063           octave_idx_type nxi = xi.columns();
00064 
00065           ColumnVector idx (nxi);
00066           double *pidx = idx.fortran_vec ();
00067           ColumnVector dist (nxi);
00068           double *pdist = dist.fortran_vec ();
00069 
00070 #define DIST(dd, y, yi, m) \
00071   dd = 0.; \
00072   for (octave_idx_type k = 0; k < m; k++) \
00073    { \
00074      double yd = y[k] - yi[k]; \
00075      dd += yd * yd; \
00076    } \
00077   dd = sqrt (dd);
00078 
00079           const double *pxi = xi.fortran_vec ();
00080           for (octave_idx_type i = 0; i < nxi; i++)
00081             {
00082               double d0;
00083               const double *px = x.fortran_vec ();
00084               DIST(d0, px, pxi, n);
00085               *pidx = 1.;
00086               for (octave_idx_type j = 1; j < nx; j++)
00087                 {
00088                   px += n;
00089                   double d;
00090                   DIST (d, px, pxi, n);
00091                   if (d < d0)
00092                     {
00093                       d0 = d;
00094                       *pidx = static_cast<double>(j + 1);
00095                     }
00096                   OCTAVE_QUIT;
00097                 }
00098 
00099               *pdist++ = d0;
00100               pidx++;
00101               pxi += n;
00102             }
00103 
00104           retval(1) = dist;
00105           retval(0) = idx;
00106         }
00107     }
00108 
00109   return retval;
00110 }
00111 
00112 /*
00113 
00114 ## No test needed for internal helper function.
00115 %!assert (1)
00116 
00117 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines