Loading TOC...

MarkLogic 12 EA 1 Product Documentation
vec.dotProduct

vec.dotProduct(
   vector1 as vec.vector,
   vector2 as vec.vector
) as Number

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

  const vec1 = vec.normalize(vec.vector([3.14,1.59,2.65]))
  const vec2 = vec.normalize(vec.vector([3.58,9.79,3.23]))

  vec.dotProduct(vec1,vec2)

  => 0.735591769218445

Example

  const vec1 = vec.vector(xdmp.toJSON(fn.doc('pronethalol.json')).xpath('/data/array-node{embedding}'))
  const vec2 = vec.vector(fn.head(fn.doc('cell_renewal.json')).xpath('/data/array-node{embedding}'))

  vec.dotProduct(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.

Stack Overflow iconStack Overflow: Get the most useful answers to questions from the MarkLogic community, or ask your own question.