summaryrefslogtreecommitdiff
path: root/inst/misc/dist/stk_dist.m
blob: b6e553fa596affd64ad456656dc316ff5a6b084c (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
% STK_DIST computes a matrix of (Euclidean) distances
%
% CALL: D = stk_dist(X, Y)
%
%    computes the matrix of distances between X and Y. More precisely, if
%    X is an nX x d matrix, and Y an nY x d matrix, the D is an nX x nY
%    matrix with
%
%       D_{i,j} = norm(X(i,:) - Y(j,:)),
%
%    where norm(.) denotes the Euclidean norm in R^d.
%
% See also: stk_mindist, stk_filldist, norm

% Copyright Notice
%
%    Copyright (C) 2018 CentraleSupelec
%    Copyright (C) 2012, 2013 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/>.

function D = stk_dist(x, y, pairwise)

% read argument #1
x = double(x);

% read argument #2
if nargin < 2,
    y = [];
else
    y = double(y);
end

% read argument #3
if nargin < 3,
    pairwise = false;
else
    if ~islogical(pairwise)
        errmsg = 'Argument ''pairwise'' should be either true or false.';
        stk_error(errmsg, 'TypeMismatch');
    end
end

if pairwise,
    if isempty(y),
        D = zeros(size(x, 1), 1);
    else
        D = __stk_dist_pairwise__ (x, y);
        %D = sqrt (sum ((x - y) .^ 2, 2));  % The MEX-file is usually faster
    end
else
    if isempty(y),
        D = __stk_dist_matrixx__(x);
    else
        D = __stk_dist_matrixy__(x, y);
    end
end

end % function

%%
% Check that an error is raised in nargin is neither 1 nor 2

%!error stk_dist();
%!error stk_dist(0, 0, 0);
%!error stk_dist(0, 0, 0, 0);

%%
% Check that an error is raised when the number of columns differs

%!error stk_dist(0, ones(1, 2));
%!error stk_dist(eye(3), ones(1, 2));
%!error stk_dist(ones(2, 1), zeros(2));

%%
% Test with some simple matrices

%!shared x, y, z
%! x = zeros(11, 5);
%! y = zeros(13, 5);
%! z = ones(7, 5);

%!test
%! Dx = stk_dist(x);
%! assert(isequal(Dx, zeros(11)));

%!test
%! Dxx = stk_dist(x, x);
%! assert(isequal(Dxx, zeros(11)));

%!test
%! Dxy = stk_dist(x, y);
%! assert(isequal(Dxy, zeros(11, 13)));

%!test
%! Dzz = stk_dist(z, z);
%! assert(isequal(Dzz, zeros(7)));

%!test
%! Dxz = stk_dist(x, z);
%! assert(stk_isequal_tolabs(Dxz, sqrt(5)*ones(11, 7)));

%!test
%! x = randn(5,3);
%! y = randn(5,3);
%! D1 = stk_dist(x, y, true); % pairwise
%! D2 = stk_dist(x, y);
%! assert(stk_isequal_tolabs(D1, diag(D2)));

%!test
%! x = randn(5,3);
%! D1 = stk_dist(x, [], true); % pairwise
%! assert(stk_isequal_tolabs(D1, zeros(5, 1)));
%! D1 = stk_dist(x, x, true); % pairwise
%! assert(stk_isequal_tolabs(D1, zeros(5, 1)));