summaryrefslogtreecommitdiff
path: root/src/stk_dist_matrixy.c
blob: ec23115ee1fc47c47f42b9ab4aa2ce422e70d729 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*****************************************************************************
 *                                                                           *
 *                  Small (Matlab/Octave) Toolbox for Kriging                *
 *                                                                           *
 * Copyright Notice                                                          *
 *                                                                           *
 *    Copyright (C) 2015 CentraleSupelec                                     *
 *    Copyright (C) 2011, 2012 SUPELEC                                       *
 *                                                                           *
 *    Authors:  Julien Bect       <julien.bect@centralesupelec.fr>           *
 *              Emmanuel Vazquez  <emmanuel.vazquez@centralesupelec.fr>      *
 *                                                                           *
 * Copying Permission Statement                                              *
 *                                                                           *
 *    This file is part of                                                   *
 *                                                                           *
 *            STK: a Small (Matlab/Octave) Toolbox for Kriging               *
 *               (https://github.com/stk-kriging/stk/)                   *
 *                                                                           *
 *    STK is free software: you can redistribute it and/or modify it under   *
 *    the terms of the GNU General Public License as published by the Free   *
 *    Software Foundation,  either version 3  of the License, or  (at your   *
 *    option) any later version.                                             *
 *                                                                           *
 *    STK is distributed  in the hope that it will  be useful, but WITHOUT   *
 *    ANY WARRANTY;  without even the implied  warranty of MERCHANTABILITY   *
 *    or FITNESS  FOR A  PARTICULAR PURPOSE.  See  the GNU  General Public   *
 *    License for more details.                                              *
 *                                                                           *
 *    You should  have received a copy  of the GNU  General Public License   *
 *    along with STK.  If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                           *
 ****************************************************************************/

#include "stk_mex.h"


static void distance2(double* x, double* y, double* h, size_t nx, size_t ny, size_t dim)
{
  size_t i, j, kx, ky;
  double diff, lambda;

  for (i = 0; i < nx; i++) {
    for (j = 0; j < ny; j++) {

      /* compute distance between x[i,:] and y[j,:] */
      lambda = 0.0;
      for (kx = i, ky = j; kx < dim * nx; kx += nx, ky += ny)
      {
        diff = x[kx] - y[ky];
        lambda += diff * diff;
      }

      /* store the result in h */
      h[i+nx*j] = sqrt(lambda);

    }
  }
}


mxArray* compute_distance_xy(const mxArray* x, const mxArray* y)
{
  size_t d, mx, my;
  mxArray* h;

  if((!stk_is_realmatrix(x)) || (!stk_is_realmatrix(y)))
    mexErrMsgTxt("Input arguments should be real-valued double-precision array.");

  /* Check that the input arguments have the same number of columns */
  if (mxGetN(y) != (d = mxGetN(x)))
    mexErrMsgTxt("Both input arguments should have the same number of columns.");

  /* Read the size (number of lines) of each input argument */
  mx = mxGetM(x);
  my = mxGetM(y);

  /* Create a matrix for the return argument */
  h = mxCreateDoubleMatrix(mx, my, mxREAL);

  /* Do the actual computations in a subroutine */
  distance2(mxGetPr(x), mxGetPr(y), mxGetPr(h), mx, my, d);

  return h;
}


void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[])
{
  if (nlhs > 1)  /* Check number of output arguments */
    mexErrMsgTxt("Too many output arguments.");

  if (nrhs != 2)  /* Check number of input arguments */
    mexErrMsgTxt("Incorrect number of input arguments.");

  plhs[0] = compute_distance_xy(prhs[0], prhs[1]);
}