Learn R Programming

capr (version 0.2.0)

cosine_similarity: Cosine similarity between numeric vectors

Description

Computes the cosine of the angle between two numeric vectors. Both vectors must have equal length and non-zero Euclidean norms.

Usage

cosine_similarity(a, b, eps = 1e-12)

Value

A scalar double value in [-1, 1] representing the cosine similarity between a and b.

Arguments

a, b

Numeric vectors of equal length.

eps

Non-negative numeric tolerance used to guard against division by zero. Defaults to 1e-12.

Examples

Run this code
cosine_similarity(c(1, 2, 3), c(1, 2, 3))
cosine_similarity(c(1, 0), c(0, 1))
cosine_similarity(c(1, 2), c(-1, -2))

Run the code above in your browser using DataLab