Inter-particle distances

1. Introduction

This module computes the distances between the points of two sets, and the quantities derived from them that the wave function needs: the squared distance, the distance, and the rescaled distance Cij=(1−e−κrij)/κ with its gradient and Laplacian.

All four functions take a context, the two transpose flags, the sizes m and n, the arrays and their leading dimensions; the two rescaled ones also take rescale_factor_kappa. With 'N', the three coordinates of a point are consecutive, so A is [m][3] in C and (3,m) in Fortran. With 'T', they are stored by component: [3][m] in C and (m,3) in Fortran. The same rule applies to B with n. The flags are case-insensitive. The context stores its points the transposed way, and that is also the layout these functions are fastest on.

2. Squared distance

2.1. qmckl_distance_sq

qmckl_distance_sq computes the matrix of squared Euclidean distances between the points of two sets. It is the quantity that qmckl_distance returns, without the square root:

Cij=∑k=13(Ak,i−Bk,j)2

where A and B hold the coordinates of the two sets and C is the result.

Variable Type In/Out Description
context qmckl_context in Global state
transa char in Array A is 'N': Normal, 'T': Transposed
transb char in Array B is 'N': Normal, 'T': Transposed
m int64_t in Number of points in the first set
n int64_t in Number of points in the second set
A double[][lda] in Array containing the m×3 matrix A
lda int64_t in Leading dimension of array A
B double[][ldb] in Array containing the n×3 matrix B
ldb int64_t in Leading dimension of array B
C double[n][ldc] out Array containing the m×n matrix C
ldc int64_t in Leading dimension of array C

Requirements:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • lda >= 3 if transa == 'N'
  • lda >= m if transa == 'T'
  • ldb >= 3 if transb == 'N'
  • ldb >= n if transb == 'T'
  • ldc >= m
  • A is allocated with at least 3×m×8 bytes
  • B is allocated with at least 3×n×8 bytes
  • C is allocated with at least m×n×8 bytes
C
qmckl_exit_code qmckl_distance_sq (
          const qmckl_context context,
          const char transa,
          const char transb,
          const int64_t m,
          const int64_t n,
          const double* A,
          const int64_t lda,
          const double* B,
          const int64_t ldb,
          double* const C,
          const int64_t ldc ); 
Fortran
function qmckl_distance_sq(context, transa, transb, m, n, &
     A, LDA, B, LDB, C, LDC) &
     bind(C) result(info)

  use qmckl_constants
  implicit none

  integer (qmckl_context) , intent(in)  , value :: context
  character(c_char)   , intent(in)  , value :: transa
  character(c_char)   , intent(in)  , value :: transb
  integer (c_int64_t) , intent(in)  , value :: m
  integer (c_int64_t) , intent(in)  , value :: n
  integer (c_int64_t) , intent(in)  , value :: lda
  integer (c_int64_t) , intent(in)  , value :: ldb
  integer (c_int64_t) , intent(in)  , value :: ldc
  real    (c_double ) , intent(in)          :: A(lda,*)
  real    (c_double ) , intent(in)          :: B(ldb,*)
  real    (c_double ) , intent(out)         :: C(ldc,n)

  integer(qmckl_exit_code) :: info

  integer*8 :: i,j
  real*8    :: x, y, z
  integer   :: transab

  info = QMCKL_SUCCESS

  if (context == QMCKL_NULL_CONTEXT) then
     info = QMCKL_INVALID_CONTEXT
     return
  endif

  if (m <= 0_8) then
     info = QMCKL_INVALID_ARG_4
     return
  endif

  if (n <= 0_8) then
     info = QMCKL_INVALID_ARG_5
     return
  endif

  if (transa == 'N' .or. transa == 'n') then
     transab = 0
  else if (transa == 'T' .or. transa == 't') then
     transab = 1
  else
     transab = -100
  endif

  if (transb == 'N' .or. transb == 'n') then
     continue
  else if (transb == 'T' .or. transb == 't') then
     transab = transab + 2
  else
     transab = -100
  endif

  if (transab < 0) then
     info = QMCKL_INVALID_ARG_1
     return
  endif

  if (iand(transab,1) == 0 .and. LDA < 3) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  if (iand(transab,1) == 1 .and. LDA < m) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  if (iand(transab,2) == 0 .and. LDB < 3) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  if (iand(transab,2) == 2 .and. LDB < n) then
     info = QMCKL_INVALID_ARG_7
     return
  endif


  select case (transab)

  case(0)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(1,j)
           y = A(2,i) - B(2,j)
           z = A(3,i) - B(3,j)
           C(i,j) = x*x + y*y + z*z
        end do
     end do

  case(1)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(1,j)
           y = A(i,2) - B(2,j)
           z = A(i,3) - B(3,j)
           C(i,j) = x*x + y*y + z*z
        end do
     end do

  case(2)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(j,1)
           y = A(2,i) - B(j,2)
           z = A(3,i) - B(j,3)
           C(i,j) = x*x + y*y + z*z
        end do
     end do

  case(3)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(j,1)
           y = A(i,2) - B(j,2)
           z = A(i,3) - B(j,3)
           C(i,j) = x*x + y*y + z*z
        end do
     end do

  end select

end function qmckl_distance_sq

2.1.1. Performance

This function is more efficient when A and B are transposed.

3. Distance

3.1. qmckl_distance

qmckl_distance computes the matrix of the Euclidean distances between the points of two sets:

Cij=∑k=13(Ak,i−Bk,j)2

Variable Type In/Out Description
context qmckl_context in Global state
transa char in Array A is 'N': Normal, 'T': Transposed
transb char in Array B is 'N': Normal, 'T': Transposed
m int64_t in Number of points in the first set
n int64_t in Number of points in the second set
A double[][lda] in Array containing the m×3 matrix A
lda int64_t in Leading dimension of array A
B double[][ldb] in Array containing the n×3 matrix B
ldb int64_t in Leading dimension of array B
C double[n][ldc] out Array containing the m×n matrix C
ldc int64_t in Leading dimension of array C

3.1.1. Requirements

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • lda >= 3 if transa == 'N'
  • lda >= m if transa == 'T'
  • ldb >= 3 if transb == 'N'
  • ldb >= n if transb == 'T'
  • ldc >= m
  • A is allocated with at least 3×m×8 bytes
  • B is allocated with at least 3×n×8 bytes
  • C is allocated with at least m×n×8 bytes

3.1.2. C header

C
qmckl_exit_code qmckl_distance (
          const qmckl_context context,
          const char transa,
          const char transb,
          const int64_t m,
          const int64_t n,
          const double* A,
          const int64_t lda,
          const double* B,
          const int64_t ldb,
          double* const C,
          const int64_t ldc ); 

3.1.3. Source

Fortran
function qmckl_distance(context, transa, transb, m, n, &
     A, LDA, B, LDB, C, LDC) &
     bind(C) result(info)
  use qmckl_constants
  implicit none
  integer(qmckl_context), intent(in), value  :: context
  character(c_char)   , intent(in)  , value :: transa
  character(c_char)   , intent(in)  , value :: transb
  integer (c_int64_t) , intent(in)  , value :: m
  integer (c_int64_t) , intent(in)  , value :: n
  integer (c_int64_t) , intent(in)  , value :: lda
  integer (c_int64_t) , intent(in)  , value :: ldb
  integer (c_int64_t) , intent(in)  , value :: ldc
  real    (c_double ) , intent(in)          :: A(lda,*)
  real    (c_double ) , intent(in)          :: B(ldb,*)
  real    (c_double ) , intent(out)         :: C(ldc,n)
  integer (qmckl_exit_code) :: info

  integer*8 :: i,j
  real*8    :: x, y, z
  integer   :: transab

  info = QMCKL_SUCCESS

  if (context == QMCKL_NULL_CONTEXT) then
     info = QMCKL_INVALID_CONTEXT
     return
  endif

  if (m <= 0_8) then
     info = QMCKL_INVALID_ARG_4
     return
  endif

  if (n <= 0_8) then
     info = QMCKL_INVALID_ARG_5
     return
  endif

  if (transa == 'N' .or. transa == 'n') then
     transab = 0
  else if (transa == 'T' .or. transa == 't') then
     transab = 1
  else
     transab = -100
  endif

  if (transb == 'N' .or. transb == 'n') then
     continue
  else if (transb == 'T' .or. transb == 't') then
     transab = transab + 2
  else
     transab = -100
  endif

  if (transab < 0) then
     info = QMCKL_INVALID_ARG_1
     return
  endif

  ! check for LDA
  if (iand(transab,1) == 0 .and. LDA < 3) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  if (iand(transab,1) == 1 .and. LDA < m) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  ! check for LDB
  if (iand(transab,1) == 0 .and. LDB < 3) then
     info = QMCKL_INVALID_ARG_9
     return
  endif

  if (iand(transab,1) == 1 .and. LDB < n) then
     info = QMCKL_INVALID_ARG_9
     return
  endif

  ! check for LDC
  if (LDC < m) then
     info = QMCKL_INVALID_ARG_11
     return
  endif


  select case (transab)

  case(0)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(1,j)
           y = A(2,i) - B(2,j)
           z = A(3,i) - B(3,j)
           C(i,j) = x*x + y*y + z*z
        end do
        C(:,j) = dsqrt(C(:,j))
     end do

  case(1)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(1,j)
           y = A(i,2) - B(2,j)
           z = A(i,3) - B(3,j)
           C(i,j) = x*x + y*y + z*z
        end do
        C(:,j) = dsqrt(C(:,j))
     end do

  case(2)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(j,1)
           y = A(2,i) - B(j,2)
           z = A(3,i) - B(j,3)
           C(i,j) = x*x + y*y + z*z
        end do
        C(:,j) = dsqrt(C(:,j))
     end do

  case(3)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(j,1)
           y = A(i,2) - B(j,2)
           z = A(i,3) - B(j,3)
           C(i,j) = x*x + y*y + z*z
        end do
        C(:,j) = dsqrt(C(:,j))
     end do

  end select

end function qmckl_distance

3.1.4. Performance

This function is more efficient when A and B are transposed.

4. Rescaled Distance

4.1. qmckl_distance_rescaled

qmckl_distance_rescaled computes the matrix of the rescaled distances between the points of two sets:

Cij=1−e−κrijκ

Variable Type In/Out Description
context qmckl_context in Global state
transa char in Array A is 'N': Normal, 'T': Transposed
transb char in Array B is 'N': Normal, 'T': Transposed
m int64_t in Number of points in the first set
n int64_t in Number of points in the second set
A double[][lda] in Array containing the m×3 matrix A
lda int64_t in Leading dimension of array A
B double[][ldb] in Array containing the n×3 matrix B
ldb int64_t in Leading dimension of array B
C double[n][ldc] out Array containing the m×n matrix C
ldc int64_t in Leading dimension of array C
rescale_factor_kappa double in Factor for calculating rescaled distances

4.1.1. Requirements

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • lda >= 3 if transa == 'N'
  • lda >= m if transa == 'T'
  • ldb >= 3 if transb == 'N'
  • ldb >= n if transb == 'T'
  • ldc >= m
  • A is allocated with at least 3×m×8 bytes
  • B is allocated with at least 3×n×8 bytes
  • C is allocated with at least m×n×8 bytes

4.1.2. C header

C
qmckl_exit_code qmckl_distance_rescaled (
          const qmckl_context context,
          const char transa,
          const char transb,
          const int64_t m,
          const int64_t n,
          const double* A,
          const int64_t lda,
          const double* B,
          const int64_t ldb,
          double* const C,
          const int64_t ldc,
          const double rescale_factor_kappa ); 

4.1.3. Source

Fortran
function qmckl_distance_rescaled(context, transa, transb, m, n, &
     A, LDA, B, LDB, C, LDC, rescale_factor_kappa) &
     bind(C) result(info)
  use qmckl_constants
  implicit none
  integer (qmckl_context), intent(in)  , value :: context
  character(c_char  ) , intent(in)  , value :: transa
  character(c_char  ) , intent(in)  , value :: transb
  integer (c_int64_t) , intent(in)  , value :: m
  integer (c_int64_t) , intent(in)  , value :: n
  integer (c_int64_t) , intent(in)  , value :: lda
  integer (c_int64_t) , intent(in)  , value :: ldb
  integer (c_int64_t) , intent(in)  , value :: ldc
  real    (c_double ) , intent(in)  , value :: rescale_factor_kappa
  real    (c_double ) , intent(in)          :: A(lda,*)
  real    (c_double ) , intent(in)          :: B(ldb,*)
  real    (c_double ) , intent(out)         :: C(ldc,n)
  integer(qmckl_exit_code) :: info

  integer*8 :: i,j
  real*8    :: x, y, z, dist, rescale_factor_kappa_inv
  integer   :: transab

  rescale_factor_kappa_inv = 1.0d0/rescale_factor_kappa;

  info = QMCKL_SUCCESS

  if (context == QMCKL_NULL_CONTEXT) then
     info = QMCKL_INVALID_CONTEXT
     return
  endif

  if (m <= 0_8) then
     info = QMCKL_INVALID_ARG_4
     return
  endif

  if (n <= 0_8) then
     info = QMCKL_INVALID_ARG_5
     return
  endif

  if (transa == 'N' .or. transa == 'n') then
     transab = 0
  else if (transa == 'T' .or. transa == 't') then
     transab = 1
  else
     transab = -100
  endif

  if (transb == 'N' .or. transb == 'n') then
     continue
  else if (transb == 'T' .or. transb == 't') then
     transab = transab + 2
  else
     transab = -100
  endif

  ! check for LDA
  if (transab < 0) then
     info = QMCKL_INVALID_ARG_1
     return
  endif

  if (iand(transab,1) == 0 .and. LDA < 3) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  if (iand(transab,1) == 1 .and. LDA < m) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  ! check for LDB
  if (iand(transab,2) == 0 .and. LDB < 3) then
     info = QMCKL_INVALID_ARG_9
     return
  endif

  if (iand(transab,2) == 2 .and. LDB < n) then
     info = QMCKL_INVALID_ARG_9
     return
  endif

  ! check for LDC
  if (LDC < m) then
     info = QMCKL_INVALID_ARG_11
     return
  endif


  select case (transab)

  case(0)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(1,j)
           y = A(2,i) - B(2,j)
           z = A(3,i) - B(3,j)
           dist = dsqrt(x*x + y*y + z*z)
           C(i,j) = (1.0d0 - dexp(-rescale_factor_kappa * dist)) * rescale_factor_kappa_inv
        end do
     end do

  case(1)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(1,j)
           y = A(i,2) - B(2,j)
           z = A(i,3) - B(3,j)
           dist = dsqrt(x*x + y*y + z*z)
           C(i,j) = (1.0d0 - dexp(-rescale_factor_kappa * dist)) * rescale_factor_kappa_inv
        end do
     end do

  case(2)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(j,1)
           y = A(2,i) - B(j,2)
           z = A(3,i) - B(j,3)
           dist = dsqrt(x*x + y*y + z*z)
           C(i,j) = (1.0d0 - dexp(-rescale_factor_kappa * dist)) * rescale_factor_kappa_inv
        end do
     end do

  case(3)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(j,1)
           y = A(i,2) - B(j,2)
           z = A(i,3) - B(j,3)
           dist = dsqrt(x*x + y*y + z*z)
           C(i,j) = (1.0d0 - dexp(-rescale_factor_kappa * dist)) * rescale_factor_kappa_inv
        end do
     end do

  end select

end function qmckl_distance_rescaled

4.1.4. Performance

This function is more efficient when A and B are transposed.

5. Rescaled Distance Derivatives

5.1. qmckl_distance_rescaled_gl

qmckl_distance_rescaled_gl computes the gradient and the Laplacian of the rescaled distance with respect to the coordinates of the first set. Each pair contributes four numbers, stored contiguously: the three components of the gradient, then the Laplacian. The output is declared double C[n][ldc][4] and indexed as C[j][i][c], where i numbers the points of the first set and j those of the second.

C(rij)=(1−exp(−κrij))/κ

Here the gradient is defined as follows:

∇iC(rij)=(∂C(rij)∂xi,∂C(rij)∂yi,∂C(rij)∂zi)
and the Laplacian is defined as follows:

ΔiC(rij)=∂2C∂xi2+∂2C∂yi2+∂2C∂zi2

Using the above three formulas, the expression for the gradient and Laplacian is:

∂C(rij)∂xi=xi−xjrijexp(−κrij)

ΔiC(rij)=[2rij−κ]exp(−κrij)

The distance is floored at 1.d-20 before the division, so two coincident points give a zero gradient instead of an undefined one.

Variable Type In/Out Description
context qmckl_context in Global state
transa char in Array A is 'N': Normal, 'T': Transposed
transb char in Array B is 'N': Normal, 'T': Transposed
m int64_t in Number of points in the first set
n int64_t in Number of points in the second set
A double[][lda] in Array containing the m×3 matrix A
lda int64_t in Leading dimension of array A
B double[][ldb] in Array containing the n×3 matrix B
ldb int64_t in Leading dimension of array B
C double[n][ldc][4] out Array containing the 4×m×n matrix C
ldc int64_t in Leading dimension of array C
rescale_factor_kappa double in Factor for calculating rescaled distances derivatives

Requirements:

  • context is not QMCKL_NULL_CONTEXT
  • m > 0
  • n > 0
  • lda >= 3 if transa == 'N'
  • lda >= m if transa == 'T'
  • ldb >= 3 if transb == 'N'
  • ldb >= n if transb == 'T'
  • ldc >= m
  • A is allocated with at least 3×m×8 bytes
  • B is allocated with at least 3×n×8 bytes
  • C is allocated with at least 4×m×n×8 bytes
C
qmckl_exit_code qmckl_distance_rescaled_gl (
          const qmckl_context context,
          const char transa,
          const char transb,
          const int64_t m,
          const int64_t n,
          const double* A,
          const int64_t lda,
          const double* B,
          const int64_t ldb,
          double* const C,
          const int64_t ldc,
          const double rescale_factor_kappa ); 
Fortran
function qmckl_distance_rescaled_gl(context, transa, transb, m, n, &
     A, LDA, B, LDB, C, LDC, rescale_factor_kappa) &
     bind(C) result(info)
  use qmckl_constants
  implicit none

  integer(qmckl_exit_code) :: info
  integer (qmckl_context), intent(in)  , value :: context
  character(c_char  ) , intent(in)  , value :: transa
  character(c_char  ) , intent(in)  , value :: transb
  integer (c_int64_t) , intent(in)  , value :: m
  integer (c_int64_t) , intent(in)  , value :: n
  integer (c_int64_t) , intent(in)  , value :: lda
  integer (c_int64_t) , intent(in)  , value :: ldb
  integer (c_int64_t) , intent(in)  , value :: ldc
  real    (c_double ) , intent(in)  , value :: rescale_factor_kappa
  real    (c_double ) , intent(in)          :: A(lda,*)
  real    (c_double ) , intent(in)          :: B(ldb,*)
  real    (c_double ) , intent(out)         :: C(4,ldc,n)

  integer*8 :: i,j
  real*8    :: x, y, z, dist, dist_inv
  real*8    :: rij
  integer   :: transab

  info = QMCKL_SUCCESS

  if (context == QMCKL_NULL_CONTEXT) then
     info = QMCKL_INVALID_CONTEXT
     return
  endif

  if (m <= 0_8) then
     info = QMCKL_INVALID_ARG_4
     return
  endif

  if (n <= 0_8) then
     info = QMCKL_INVALID_ARG_5
     return
  endif

  if (transa == 'N' .or. transa == 'n') then
     transab = 0
  else if (transa == 'T' .or. transa == 't') then
     transab = 1
  else
     transab = -100
  endif

  if (transb == 'N' .or. transb == 'n') then
     continue
  else if (transb == 'T' .or. transb == 't') then
     transab = transab + 2
  else
     transab = -100
  endif

  ! check for LDA
  if (transab < 0) then
     info = QMCKL_INVALID_ARG_1
     return
  endif

  if (iand(transab,1) == 0 .and. LDA < 3) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  if (iand(transab,1) == 1 .and. LDA < m) then
     info = QMCKL_INVALID_ARG_7
     return
  endif

  ! check for LDB
  if (iand(transab,2) == 0 .and. LDB < 3) then
     info = QMCKL_INVALID_ARG_9
     return
  endif

  if (iand(transab,2) == 2 .and. LDB < n) then
     info = QMCKL_INVALID_ARG_9
     return
  endif

  ! check for LDC
  if (LDC < m) then
     info = QMCKL_INVALID_ARG_11
     return
  endif


  select case (transab)

  case(0)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(1,j)
           y = A(2,i) - B(2,j)
           z = A(3,i) - B(3,j)
           dist = max(1.d-20, dsqrt(x*x + y*y + z*z))
           dist_inv = 1.0d0/dist
           rij = dexp(-rescale_factor_kappa * dist)
           C(1,i,j) = x * dist_inv * rij
           C(2,i,j) = y * dist_inv * rij
           C(3,i,j) = z * dist_inv * rij
           C(4,i,j) = (2.0d0 * dist_inv - rescale_factor_kappa) * rij
        end do
     end do

  case(1)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(1,j)
           y = A(i,2) - B(2,j)
           z = A(i,3) - B(3,j)
           dist = max(1.d-20, dsqrt(x*x + y*y + z*z))
           dist_inv = 1.0d0/dist
           rij = dexp(-rescale_factor_kappa * dist)
           C(1,i,j) = x * dist_inv * rij
           C(2,i,j) = y * dist_inv * rij
           C(3,i,j) = z * dist_inv * rij
           C(4,i,j) = (2.0d0 * dist_inv - rescale_factor_kappa) * rij
        end do
     end do

  case(2)

     do j=1,n
        do i=1,m
           x = A(1,i) - B(j,1)
           y = A(2,i) - B(j,2)
           z = A(3,i) - B(j,3)
           dist = max(1.d-20, dsqrt(x*x + y*y + z*z))
           dist_inv = 1.0d0/dist
           rij = dexp(-rescale_factor_kappa * dist)
           C(1,i,j) = x * dist_inv * rij
           C(2,i,j) = y * dist_inv * rij
           C(3,i,j) = z * dist_inv * rij
           C(4,i,j) = (2.0d0 * dist_inv - rescale_factor_kappa) * rij
        end do
     end do

  case(3)

     do j=1,n
        do i=1,m
           x = A(i,1) - B(j,1)
           y = A(i,2) - B(j,2)
           z = A(i,3) - B(j,3)
           dist = max(1.d-20, dsqrt(x*x + y*y + z*z))
           dist_inv = 1.0d0/dist
           rij = dexp(-rescale_factor_kappa * dist)
           C(1,i,j) = x * dist_inv * rij
           C(2,i,j) = y * dist_inv * rij
           C(3,i,j) = z * dist_inv * rij
           C(4,i,j) = (2.0d0 * dist_inv - rescale_factor_kappa) * rij
        end do
     end do

  end select

end function qmckl_distance_rescaled_gl

This function is more efficient when A and B are transposed.