MarkLogic 12 EA 1 Product Documentation
vec:dot-product

vec:dot-product(
   $vector1 as vec:vector,
   $vector2 as vec:vector
) as xs:double

Summary

Returns the dot product between two vectors. The vectors must be of the same dimension. Use this function to calculate similarity between vectors if you are certain they are both of magnitude 1.

Parameters
vector1 The vector from which to calculate the dot product with vector2.
vector2 The vector from which to calculate the dot product with vector1.

Example

  xquery version "1.0-ml";
  let $vec1 := vec:normalize(vec:vector((3.14,1.59,2.65)))
  let $vec2 := vec:normalize(vec:vector((3.58,9.79,3.23)))

  return vec:dot-product($vec1,$vec2);

  => 0.735591769218445

Example

  xquery version "1.0-ml";

  let $vec1 := vec:vector(fn:head(fn:doc('pronethalol.json'))/data/array-node{embedding})
  let $vec2 := vec:vector(fn:head(fn:doc('cell_renewal.json'))/data/array-node{embedding})

  return vec:dot-product($vec1,$vec2)

  => The dot product between vectors in JSON arrays named 'embedding'
      in documents 'pronethalol.json' and 'cell_renewal.json'
    Can be used instead of vec:cosine-similarity() if you are sure the
      vectors are normalized to magnitude 1.
Powered by MarkLogic Server | Terms of Use | Privacy Policy