Generate and return the (squared) Euclidean distance of a (set of) point(s) in ndim
-dimensions from a reference point (possibly origin), optionally robustly without underflow or overflow.
- Parameters
-
[out] | distance | : The output object of,
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
containing the requested Euclidean (squared) distance(s).
The rank and shape of the output distance follows that of the interfaces illustrated below.
|
[in] | point | : The input contiguous vector of shape (1:ndim) or matrix of shape (1:ndim, 1:npnt) of the same type and kind as the output distance , containing a (set of npnt ) point(s) in the ndim -dimensional Euclidean space whose distances with respect to the input reference point ref must be returned.
|
[in] | ref | : The input contiguous vector of shape (1:ndim) or matrix of shape (1:ndim, 1:nref) of the same type and kind as point , containing the (set of nref ) reference point(s) from which the distance(s) of point must be computed.
(optional, default = [(0., i = 1, size(point, 1))] .) |
[in] | method | : The input scalar that can be,
-
the constant euclid or an object of type euclid_type, implying that all distance calculations must be done without undue numerical overflow.
This option is computationally the most expensive method.
-
the constant euclidu or an object of type euclidu_type, implying that all distance calculations must be without runtime checks for numerical overflow.
This option is computationally faster than the euclid method.
-
the constant euclidv or an object of type euclidv_type, implying that the volumes corresponding to all Euclidean distances must be returned without runtime checks for numerical overflow.
This option is computationally the fastest approach to computing the distances because it avoid costly sqrt() operations and runtime overflow
-
the constant euclidsq or an object of type euclidsq_type implying that the squared values of all distance calculations must be returned without runtime checks for numerical overflow.
This option is computationally the fastest approach to computing the distances because it avoid costly sqrt() operations and runtime overflow checks.
(optional, default = euclid) |
Possible calling interfaces ⛓
call setDisEuclid(distance(
1:npnt), point(
1:ndim,
1:npnt), method)
call setDisEuclid(distance, point(
1:ndim), ref(
1:ndim), method)
call setDisEuclid(distance(
1:nref), point(
1:ndim), ref(
1:ndim,
1:nref), method)
call setDisEuclid(distance(
1:npnt), point(
1:ndim,
1:npnt), ref(
1:ndim), method)
call setDisEuclid(distance(
1:npnt,
1:nref), point(
1:npnt), ref(
1:nref), method)
call setDisEuclid(distance(
1:npnt,
1:nref), point(
1:ndim,
1:npnt), ref(
1:ndim,
1:nref), method)
!
Generate and return the (squared) Euclidean distance of a (set of) point(s) in ndim-dimensions from a...
This module contains procedures and generic interfaces for computing the Euclidean norm of a single p...
- Warning
- The shapes of
distance
, point
, and ref
must be consistent as in the above interface at all times.
The condition size(point, 1) == size(ref, 1) .or. rank(distance) == 2
must hold for the corresponding input arguments.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1
.
-
The
pure
procedure(s) documented herein become impure
when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1
.
By default, these procedures are pure
in release
build and impure
in debug
and testing
builds.
- BLAS/LAPACK equivalent:
- The procedures under discussion combine, modernize, and extend the interface and functionalities of Version 3.11 of BLAS/LAPACK routine(s):
dlapy3
- See also
- getDisEuclid
setDisEuclid
getDisMatEuclid
setDisMatEuclid
Intrinsic Fortran procedure hypot(x, y)
(robust)
Intrinsic Fortran procedure norm2(x(:))
(unsafe)
Example usage ⛓
13 type(display_type) :: disp
17 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
18 call disp%show(
"! Compute the distance of a point with respect to a reference in arbitrary dimensions without undue overflow/underflow.")
19 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
24 real(TKG),
allocatable :: point(:), ref(:)
26 call disp%show(
"point = getUnifRand(1, 10, 3_IK)")
30 call disp%show(
"ref = getUnifRand(1, 10, 3_IK)")
34 call disp%show(
"call setDisEuclid(distance, point, ref, euclidsq)")
36 call disp%show(
"[norm2(point - ref), sqrt(distance), distance]")
37 call disp%show( [
norm2(point
- ref),
sqrt(distance), distance] )
42 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
43 call disp%show(
"! Compute the asymmetric matrix of (squared) distances of a set of points from a set of reference points with or without undue overflow/underflow.")
44 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
48 real(TKG),
allocatable :: point(:,:), ref(:,:), distance(:,:)
49 integer(IK) :: ndim, npnt, nref
51 call disp%show(
"ndim = getUnifRand(1, 3); npnt = getUnifRand(1, 4); nref = getUnifRand(1, 3)")
55 call disp%show(
"point = getUnifRand(1, 10, ndim, npnt)")
59 call disp%show(
"ref = getUnifRand(1, 10, ndim, nref)")
63 call disp%show(
"call setResized(distance, [npnt, nref])")
65 call disp%show(
"call setDisEuclid(distance, point, ref, euclidsq)")
69 call disp%show(
"call setDisEuclid(distance(1,:), point(:,1), ref, euclidsq)")
73 call disp%show(
"call setDisEuclid(distance(:,1), point, ref(:,1), euclidsq)")
77 call disp%show(
"call setDisEuclid(distance(1,1), point(:,1), ref(:,1), euclidsq)")
81 call disp%show(
"call setDisEuclid(distance(1,1), point(:,1), euclidsq) ! `ref` is the origin.")
85 call disp%show(
"call setDisEuclid(distance(:,1), point, euclidsq) ! `ref` is the origin.")
89 call disp%show(
"call setDisEuclid(distance, point(1,:), ref(1,:), euclid) ! 1D point and ref (faster than below).")
93 call disp%show(
"call setDisEuclid(distance, point(1:1,:), ref(1:1,:), euclid) ! For comparison with the above.")
97 call disp%show(
"call setDisEuclid(distance, point(1,:), ref(1,:), euclidsq) ! 1D point and ref (faster than below).")
101 call disp%show(
"call setDisEuclid(distance, point(1:1,:), ref(1:1,:), euclidsq) ! For comparison with the above.")
105 call disp%show(
"call setDisEuclid(distance, point(1,:), ref(1,:), euclidv) ! volume.")
109 call disp%show(
"call setDisEuclid(distance, point(1,:), ref(1,:), euclid) ! reference volume.")
111 call disp%show(
"getVolUnitBall(1._TKG) * distance")
114 call disp%show(
"call setDisEuclid(distance, point, ref, euclidv) ! volume")
118 call disp%show(
"call setDisEuclid(distance, point, ref, euclid) ! reference volume")
120 call disp%show(
"getVolUnitBall(real(ndim, TKG)) * distance**ndim")
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
Generate and return the natural logarithm of the volume of an -dimensional ball of unit-radius.
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains classes and procedures for computing various statistical quantities related to t...
type(euclidu_type), parameter euclidu
This is a scalar parameter object of type euclidu_typethat is exclusively used to request unsafe meth...
type(euclid_type), parameter euclid
This is a scalar parameter object of type euclid_type that is exclusively used to request safe method...
type(euclidv_type), parameter euclidv
This is a scalar parameter object of type euclidv_typethat is exclusively used to request computing E...
type(euclidsq_type), parameter euclidsq
This is a scalar parameter object of type euclidsq_typethat is exclusively used to request computing ...
This module contains classes and procedures for setting up and computing the properties of the hyper-...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
integer, parameter RKH
The scalar integer constant of intrinsic default kind, representing the highest-precision real kind t...
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
9+4.00000000,
+10.0000000,
+5.00000000
12+8.00000000,
+3.00000000,
+8.00000000
14[
norm2(point
- ref),
sqrt(distance), distance]
15+8.60232544,
+8.60232544,
+74.0000000
28+3.00000000,
+8.00000000
29+5.00000000,
+8.00000000
32+2.00000000,
+9.00000000,
+3.00000000
33+2.00000000,
+8.00000000,
+4.00000000
37+10.0000000,
+45.0000000,
+1.00000000
38+72.0000000,
+1.00000000,
+41.0000000
41+10.0000000,
+45.0000000,
+1.00000000
44+10.0000000,
+72.0000000
53+34.0000000,
+128.000000
56+1.00000000,
+6.00000000,
+0.00000000
57+6.00000000,
+1.00000000,
+5.00000000
60+1.00000000,
+6.00000000,
+0.00000000
61+6.00000000,
+1.00000000,
+5.00000000
64+1.00000000,
+36.0000000,
+0.00000000
65+36.0000000,
+1.00000000,
+25.0000000
68+1.00000000,
+36.0000000,
+0.00000000
69+36.0000000,
+1.00000000,
+25.0000000
72+2.00000000,
+12.0000000,
+0.00000000
73+12.0000000,
+2.00000000,
+10.0000000
76+2.00000000,
+12.0000000,
+0.00000000
77+12.0000000,
+2.00000000,
+10.0000000
81+31.4159279,
+141.371674,
+3.14159274
82+226.194672,
+3.14159274,
+128.805298
85+31.4159279,
+141.371689,
+3.14159274
86+226.194656,
+3.14159274,
+128.805298
- Test:
- test_pm_distanceEuclid
- Internal naming convention:
- The following illustrates the internal naming convention used for the procedures within this generic interface.
setDE_MEQ_D1_D1_D2_RK5()
|| ||| || || || |||
|| ||| || || || The type and kind parameters.
|| ||| || || The dimension of `ref` array: D1 => vector, D2 => matrix, XX => `ref` missing.
|| ||| || The dimension of `point` array: D1 => vector, D2 => matrix.
|| ||| The dimension of the output `distance`: D0 => scalar, D1 => vector, D2 => matrix.
|| The Method of Euclidean distance computation: MED
=> euclid_type(default
/safe), MEU
=> eulidu_type(unsafe), MEV
=> eulidv_type(volume), MEQ
=> eulidsq_type(squared)
The abbreviation for DisEuclid to shorten procedure names.
This is a concrete derived type whose instances are exclusively used to request safe method of comput...
- Todo:
- Normal Priority: A benchmark comparison with the equivalent compiler implementations would be informative.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, September 1, 2017, 12:00 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas at Austin
Definition at line 1065 of file pm_distanceEuclid.F90.