VectorExtensions
Helpers & Utilities · AiDotNet.Extensions
Provides extension methods for vector operations commonly used in AI and machine learning applications.
For Beginners
A vector in AI is simply a list of numbers that can represent data points, features, or weights in a model. These extension methods provide ways to manipulate and perform calculations on these lists of numbers.
Methods
| Method | Summary |
|---|---|
List<Vector<T>> ToVectorList(IEnumerable<int> indices) |
Converts a collection of integer indices to a list of single-element vectors. |
List<int> ToIntList(IEnumerable<Vector<T>> vectors) |
Converts a collection of vectors to a list of integers. |
Matrix<T> CreateDiagonal(Vector<T> vector) |
Creates a diagonal matrix from a vector. |
Matrix<T> OuterProduct(Vector<T> leftVector, Vector<T> rightVector) |
Computes the outer product of two vectors, resulting in a matrix. |
Matrix<T> Reshape(Vector<T> vector, int rows, int columns) |
Reshapes a vector into a two-dimensional matrix with the specified number of rows and columns. |
Matrix<T> ToColumnMatrix(Vector<T> vector) |
Converts the vector to a column matrix (n x 1). |
Matrix<T> ToDiagonalMatrix(Vector<T> vector) |
Converts the vector to a diagonal matrix. |
Matrix<T> ToRowMatrix(Vector<T> vector) |
Converts the vector to a row matrix (1 x n). |
T AbsoluteMaximum(Vector<T> vector) |
Finds the element with the largest absolute value in the vector. |
T Average(Vector<T> vector) |
Calculates the average (mean) of all elements in the vector. |
T DotProduct(Vector<T> left, Vector<T> right) |
Calculates the dot product (scalar product) of two vectors. |
T EuclideanDistance(Vector<T> v1, Vector<T> v2) |
Calculates the Euclidean distance between two vectors. |
T Magnitude(Vector<T> vector) |
Calculates the magnitude (length) of a vector. |
T Max(Vector<T> vector) |
Finds the maximum value in the vector. |
T Median(Vector<T> vector) |
Calculates the median value of the elements in the vector. |
T Min(Vector<T> vector) |
Finds the minimum value in the vector. |
T Minimum(Vector<T> vector) |
Finds the minimum element value in the vector. |
T Norm(Vector<T> vector) |
Calculates the Euclidean norm (magnitude or length) of the vector. |
T StandardDeviation(Vector<T> vector) |
Calculates the standard deviation of the elements in the vector. |
T Sum(Vector<T> vector) |
Calculates the sum of all elements in the vector. |
Vector<T> Add(Vector<T> left, Vector<T> right) |
Adds a scalar value to each element of the vector. |
Vector<T> Add(Vector<T> vector, T scalar) |
Adds a scalar value to each element of the vector. |
Vector<T> Divide(Vector<T> vector, T scalar) |
Divides each element of the vector by a scalar value. |
Vector<T> Extract(Vector<T> vector, int length) |
Creates a new vector containing the first specified number of elements from the original vector. |
Vector<T> Multiply(Vector<T> vector, Matrix<T> matrix) |
Multiplies a vector by a matrix, performing a vector-matrix multiplication. |
Vector<T> Multiply(Vector<T> vector, T scalar) |
Multiplies a vector by a matrix, performing a vector-matrix multiplication. |
Vector<T> PointwiseAbs(Vector<T> vector) |
Creates a new vector where each element is the absolute value of the corresponding element in the input vector. |
Vector<T> PointwiseDivide(Vector<T> left, Vector<T> right) |
Divides each element of the left vector by the corresponding element of the right vector. |
Vector<T> PointwiseMultiply(Vector<T> left, Vector<T> right) |
Multiplies corresponding elements of two vectors together (Hadamard product). |
Vector<T> Repeat(Vector<T> vector, int count) |
Creates a new vector by repeating the original vector a specified number of times. |
Vector<T> Slice(Vector<T> vector, int start, int length) |
Creates a new vector containing a subset of elements from the original vector. |
Vector<T> SubVector(Vector<T> vector, int startIndex, int count) |
Creates a new vector containing a subset of elements from the original vector. |
Vector<T> SubVector(Vector<T> vector, int[] indices) |
Creates a new vector by selecting elements from the original vector at specified indices. |
Vector<T> Subtract(Vector<T> left, Vector<T> right) |
Subtracts a scalar value from each element of the vector. |
Vector<T> Subtract(Vector<T> vector, T scalar) |
Subtracts a scalar value from each element of the vector. |
Vector<T> Subvector(Vector<T> vector, int[] indices) |
Creates a subvector from the given vector using the specified indices. |
Vector<T> ToRealVector(Vector<Complex<T>> vector) |
Extracts the real parts from a vector of complex numbers. |
Vector<T> Transform(Vector<T> vector, Func<T, T> function) |
Applies a function to each element of the vector and returns a new vector with the results, allowing for a change in the element type. |
VectorBase<T> Maximum(Vector<T> vector, T scalar) |
Creates a new vector where each element is the maximum of the corresponding element in the input vector and a scalar value. |
VectorBase<T> PointwiseExp(Vector<T> vector) |
Applies the exponential function to each element of the vector. |
VectorBase<T> PointwiseLog(Vector<T> vector) |
Applies the natural logarithm function to each element of the vector. |
VectorBase<T> PointwiseSign(Vector<T> vector) |
Returns a new vector where each element is the sign of the corresponding element in the input vector. |
VectorBase<T> PointwiseSqrt(Vector<T> vector) |
Creates a new vector where each element is the square root of the corresponding element in the input vector. |
VectorBase<TResult> Transform(Vector<T> vector, Func<T, TResult> function) |
Applies a function to each element of the vector and returns a new vector with the results, allowing for a change in the element type. |
int MaxIndex(Vector<T> vector) |
Finds the index of the maximum value in the vector. |
int MinIndex(Vector<T> vector) |
Finds the index of the minimum value in the vector. |
int[] Argsort(Vector<T> vector) |
Returns the indices that would sort the vector in ascending order. |
void PointwiseMultiplyInPlace(Vector<T> left, Vector<T> right) |
Multiplies each element of the left vector by the corresponding element of the right vector and stores the result in the left vector. |