org.lenskit.util.math

## Class Vectors

• public final class Vectors
extends Object

Utility methods for vector arithmetic.

• ### Method Summary

All Methods
Modifier and Type Method and Description
static Long2DoubleMap addScalar(Long2DoubleMap vec, double val)
Add a scalar to each element of a vector.
static Long2DoubleMap combine(Long2DoubleMap x, Long2DoubleFunction y, double sy, double o)
Add a vector to another (scaled) vector and a scalar.
static double dotProduct(Long2DoubleMap v1, Long2DoubleMap v2)
Compute the dot product of two maps.
static double dotProduct(Long2DoubleSortedArrayMap v1, Long2DoubleSortedArrayMap v2)
Compute the dot product of two maps (optimized).
static double euclideanNorm(Long2DoubleMap v)
Compute the Euclidean norm of the values of the map.
static Iterable<Long2DoubleMap.Entry> fastEntries(Long2DoubleMap map)
static Iterator<Long2DoubleMap.Entry> fastEntryIterator(Long2DoubleMap map)
Get an iterator over the entries of the map.
static org.apache.commons.math3.linear.RealVector matrixRow(org.apache.commons.math3.linear.RealMatrix mat, int row)
Create a flyweight row view of a matrix.
static double mean(Long2DoubleMap vec)
Compute the arithmetic mean of a vector’s values.
static Long2DoubleMap multiplyScalar(Long2DoubleMap vector, double value)
Multiply each element of a vector by a scalar.
static double sum(Long2DoubleMap v)
Compute the sum of the elements of a map.
static double sumAbs(Long2DoubleMap v)
Compute the sum of the elements of a map.
static double sumOfSquares(Long2DoubleMap v)
Compute the sum of the squares of elements of a map.
static double sumOfSquares(Long2DoubleSortedArrayMap v)
Compute the sum of the squares of elements of a map (optimized).
static Long2DoubleMap transform(Long2DoubleMap input, java.util.function.DoubleUnaryOperator function)
Transform the values of a vector.
static Long2DoubleMap unitVector(Long2DoubleMap v)
Convert a vector to a unit vector.
• ### Methods inherited from class java.lang.Object

clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
• ### Method Detail

• #### fastEntryIterator

public static Iterator<Long2DoubleMap.Entry> fastEntryIterator(Long2DoubleMap map)

Get an iterator over the entries of the map. If possible, this is a fast iterator.

Parameters:
map - The map.
Returns:
An iterator over the map’s entries.
• #### fastEntries

public static Iterable<Long2DoubleMap.Entry> fastEntries(Long2DoubleMap map)
• #### sum

public static double sum(Long2DoubleMap v)

Compute the sum of the elements of a map.

Parameters:
v - The vector
Returns:
The sum of the values of v.
• #### sumAbs

public static double sumAbs(Long2DoubleMap v)

Compute the sum of the elements of a map.

Parameters:
v - The vector
Returns:
The sum of the values of v.
• #### sumOfSquares

public static double sumOfSquares(Long2DoubleMap v)

Compute the sum of the squares of elements of a map.

Parameters:
v - The vector
Returns:
The sum of the squares of the values of v.
• #### sumOfSquares

public static double sumOfSquares(Long2DoubleSortedArrayMap v)

Compute the sum of the squares of elements of a map (optimized).

Parameters:
v - The vector
Returns:
The sum of the squares of the values of v.
• #### euclideanNorm

public static double euclideanNorm(Long2DoubleMap v)

Compute the Euclidean norm of the values of the map. This is the square root of the sum of squares.

Parameters:
v - The vector.
Returns:
The Euclidean norm of the vector.
• #### unitVector

public static Long2DoubleMap unitVector(Long2DoubleMap v)

Convert a vector to a unit vector.

Parameters:
v - The vector.
Returns:
A vector with Euclidean norm of 1.
• #### dotProduct

public static double dotProduct(Long2DoubleMap v1,
Long2DoubleMap v2)

Compute the dot product of two maps. This method assumes any value missing in one map is 0, so it is the dot product of the values of common keys.

Parameters:
v1 - The first vector.
v2 - The second vector.
Returns:
The sum of the products of corresponding values in the two vectors.
• #### dotProduct

public static double dotProduct(Long2DoubleSortedArrayMap v1,
Long2DoubleSortedArrayMap v2)

Compute the dot product of two maps (optimized). This method assumes any value missing in one map is 0, so it is the dot product of the values of common keys.

Parameters:
v1 - The first vector.
v2 - The second vector.
Returns:
The sum of the products of corresponding values in the two vectors.
• #### mean

public static double mean(Long2DoubleMap vec)

Compute the arithmetic mean of a vector’s values.

Parameters:
vec - The vector.
Returns:
The arithmetic mean.
• #### combine

public static Long2DoubleMap combine(Long2DoubleMap x,
Long2DoubleFunction y,
double sy,
double o)

Add a vector to another (scaled) vector and a scalar. The result is $$x_i + s_y y_i + o$$.

Parameters:
x - The source vector.
y - The addition vector. Long2DoubleFunction.defaultReturnValue() is assumed for missing values.
sy - The scale by which elements of y are multipled.
o - The offset to add.
Returns:
A vector with the same keys as x, transformed by the specified linear formula.

public static Long2DoubleMap addScalar(Long2DoubleMap vec,
double val)

Add a scalar to each element of a vector.

Parameters:
vec - The vector to rescale.
val - The value to add.
Returns:
A new map with every value in vec increased by val.
• #### multiplyScalar

@Nonnull
public static Long2DoubleMap multiplyScalar(Long2DoubleMap vector,
double value)

Multiply each element of a vector by a scalar.

Parameters:
vector - The vector.
value - The scalar to multiply.
Returns:
A new vector consisting of the same keys as vector, with value multipled by each.
• #### transform

public static Long2DoubleMap transform(Long2DoubleMap input,
java.util.function.DoubleUnaryOperator function)

Transform the values of a vector.

Parameters:
input - The vector to transform.
function - The transformation to apply.
Returns:
A new vector that is the result of applying function to each value in input.
• #### matrixRow

public static org.apache.commons.math3.linear.RealVector matrixRow(org.apache.commons.math3.linear.RealMatrix mat,
int row)

Create a flyweight row view of a matrix.

Parameters:
mat - The matrix.
row - The row number.
Returns:
A vector that is a read-only flyweight view of the matrix row.