User Documentation
 All Files Functions Groups
linalg.sql_in File Reference

SQL functions for linear algebra. More...

Go to the source code of this file.

Functions

float8 norm1 (float8[] x)
 1-norm of a vector More...
 
float8 norm2 (float8[] x)
 2-norm of a vector More...
 
float8 dist_norm1 (float8[] x, float8[] y)
 1-norm of the difference between two vectors More...
 
float8 dist_norm2 (float8[] x, float8[] y)
 2-norm of the difference between two vectors More...
 
float8 squared_dist_norm2 (float8[] x, float8[] y)
 Squared 2-norm of the difference between two vectors. More...
 
float8 dist_angle (float8[] x, float8[] y)
 Angle between two vectors. More...
 
float8 dist_tanimoto (float8[] x, float8[] y)
 Tanimoto distance between two vectors. More...
 
closest_column_result closest_column (float8[][] m, float8[] x, regproc dist="squared_dist_norm2")
 Given matrix \( M \) and vector \( \vec x \) compute the column of \( M \) that is closest to \( \vec x \). More...
 
closest_columns_result closest_columns (float8[][] m, float8[] x, integer num, regproc dist="squared_dist_norm2")
 Given matrix \( M \) and vector \( \vec x \) compute the columns of \( M \) that are closest to \( \vec x \). More...
 
aggregate float8[] avg (float8[] x)
 Compute the average of vectors. More...
 
aggregate float8[] normalized_avg (float8[] x)
 Compute the normalized average of vectors. More...
 
aggregate float8[] matrix_agg (float8[] x)
 Combine vectors to a matrix. More...
 
float8[] matrix_column (float8[][] matrix, integer col)
 Return the column of a matrix. More...
 

Detailed Description

See Also
For an overview of linear-algebra functions, see the module description Linear-Algebra Operations.

Definition in file linalg.sql_in.

Function Documentation

aggregate float8 [] avg ( float8[]  x)

Given vectors \( x_1, \dots, x_n \), compute the average \( \frac 1n \sum_{i=1}^n x_i \).

Parameters
xPoint \( x_i \)
Returns
Average \( \frac 1n \sum_{i=1}^n x_i \)

Definition at line 301 of file linalg.sql_in.

closest_column_result closest_column ( float8  m[][],
float8[]  x,
regproc  dist = "squared_dist_norm2" 
)
Parameters
MMatrix \( M = (\vec{m_0} \dots \vec{m_{l-1}}) \in \mathbb{R}^{k \times l} \)
xVector \( \vec x \in \mathbb R^k \)
distThe metric \( \operatorname{dist} \). This needs to be a function with signature DOUBLE PRECISION[] x DOUBLE PRECISION[] -> DOUBLE PRECISION.
Returns
A composite value:
  • columns_id INTEGER - The 0-based index of the column of \( M \) that is closest to \( x \). In case of ties, the first such index is returned. That is, columns_id is the minimum element in the set \( \arg\min_{i=0,\dots,l-1} \operatorname{dist}(\vec{m_i}, \vec x) \).
  • distance DOUBLE PRECISION - The minimum distance between any column of \( M \) and \( x \). That is, \( \min_{i=0,\dots,l-1} \operatorname{dist}(\vec{m_i}, \vec x) \).

Definition at line 196 of file linalg.sql_in.

closest_columns_result closest_columns ( float8  m[][],
float8[]  x,
integer  num,
regproc  dist = "squared_dist_norm2" 
)

This function does essentially the same as closest_column(), except that it allows to specify the number of closest columns to return. The return value is a composite value:

  • columns_ids INTEGER[] - The 0-based indices of the num columns of \( M \) that are closest to \( x \). In case of ties, the first such indices are returned.
  • distances DOUBLE PRECISION[] - The distances between the columns of \( M \) with indices in columns_ids and \( x \). That is, distances[i] contains \( \operatorname{dist}(\vec{m_j}, \vec x) \), where \( j = \) columns_ids[i].

Definition at line 242 of file linalg.sql_in.

float8 dist_angle ( float8[]  x,
float8[]  y 
)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
yVector \( \vec y = (y_1, \dots, y_n) \)
Returns
\( \arccos\left(\frac{\langle \vec x, \vec y \rangle} {\| \vec x \| \cdot \| \vec y \|}\right) \)

Definition at line 141 of file linalg.sql_in.

float8 dist_norm1 ( float8[]  x,
float8[]  y 
)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
yVector \( \vec y = (y_1, \dots, y_n) \)
Returns
\( \| x - y \|_1 = \sum_{i=1}^n |x_i - y_i| \)

Definition at line 92 of file linalg.sql_in.

float8 dist_norm2 ( float8[]  x,
float8[]  y 
)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
yVector \( \vec y = (y_1, \dots, y_n) \)
Returns
\( \| x - y \|_2 = \sqrt{\sum_{i=1}^n (x_i - y_i)^2} \)

Definition at line 108 of file linalg.sql_in.

float8 dist_tanimoto ( float8[]  x,
float8[]  y 
)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
yVector \( \vec y = (y_1, \dots, y_n) \)
Returns
\( 1 - \frac{\langle \vec x, \vec y \rangle} {\| \vec x \|^2 \cdot \| \vec y \|^2 - \langle \vec x, \vec y \rangle} \)

Definition at line 159 of file linalg.sql_in.

aggregate float8 [] matrix_agg ( float8[]  x)

Given vectors \( \vec x_1, \dots, \vec x_n \in \mathbb R^m \), return matrix \( ( \vec x_1 \dots \vec x_n ) \in \mathbb R^{m \times n}\).

Parameters
xVector \( x_i \)
Returns
Matrix with columns \( x_1, \dots, x_n \)

Definition at line 375 of file linalg.sql_in.

float8 [] matrix_column ( float8  matrix[][],
integer  col 
)
Parameters
matrixTwo-dimensional matrix
colColumn of the matrix to return (0-based index)

Definition at line 392 of file linalg.sql_in.

float8 norm1 ( float8[]  x)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
Returns
\( \| x \|_1 = \sum_{i=1}^n |x_i| \)

Definition at line 63 of file linalg.sql_in.

float8 norm2 ( float8[]  x)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
Returns
\( \| x \|_2 = \sqrt{\sum_{i=1}^n x_i^2} \)

Definition at line 77 of file linalg.sql_in.

aggregate float8 [] normalized_avg ( float8[]  x)

Given vectors \( x_1, \dots, x_n \), define \( \widetilde{x} := \frac 1n \sum_{i=1}^n \frac{x_i}{\| x_i \|} \), and compute the normalized average \( \frac{\widetilde{x}}{\| \widetilde{x} \|} \).

Parameters
xPoint \( x_i \)
Returns
Normalized average \( \frac{\widetilde{x}}{\| \widetilde{x} \|} \)

Definition at line 339 of file linalg.sql_in.

float8 squared_dist_norm2 ( float8[]  x,
float8[]  y 
)
Parameters
xVector \( \vec x = (x_1, \dots, x_n) \)
yVector \( \vec y = (y_1, \dots, y_n) \)
Returns
\( \| x - y \|_2^2 = \sum_{i=1}^n (x_i - y_i)^2 \)

Definition at line 124 of file linalg.sql_in.