# Vector Operations¶

PostgresML adds optimized vector operations that can be used inside SQL queries. Vector operations are particularly useful for dealing with embeddings that have been generated from other machine learning algorithms, and can provide functions like nearest neighbor calculations using various distance functions.

Embeddings can be a relatively efficient mechanism to leverage the power of deep learning, without the runtime inference costs. These functions are fast with the most expensive distance functions computing upwards of ~100k per second for a memory resident dataset on modern hardware.

The PostgreSQL planner will also automatically parallelize evaluation on larger datasets, if configured to take advantage of multiple CPU cores when available.

Vector operations are implemented in Rust using `ndarray`

and BLAS, for maximum performance.

## Element-wise Arithmetic with Constants¶

#### Addition¶

#### Subtraction¶

#### Multiplication¶

#### Division¶

## Pairwise arithmetic with Vectors¶

#### Addition¶

#### Subtraction¶

#### Multiplication¶

#### Division¶

## Norms¶

#### Dimensions not at origin¶

#### Manhattan distance from origin¶

#### Euclidean distance from origin¶

#### Absolute value of largest element¶

## Normalization¶

#### Unit Vector¶

#### Squared Unit Vector¶

#### -1:1 values¶

## Distances¶

#### Manhattan¶

#### Euclidean¶

#### Projection¶

#### Direction¶

## Nearest Neighbor Example¶

If we had precalculated the embeddings for a set of user and product data, we could find the 100 best products for a user with a similarity search.

```
SELECT
products.id,
pgml.cosine_similarity(
users.embedding,
products.embedding
) AS distance
FROM users
JOIN products
WHERE users.id = 123
ORDER BY distance ASC
LIMIT 100;
```