Class VectorExtensions
- Namespace
- AiDotNet.Extensions
- Assembly
- AiDotNet.dll
Provides extension methods for vector operations commonly used in AI and machine learning applications.
public static class VectorExtensions
- Inheritance
-
VectorExtensions
- Inherited Members
Remarks
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
AbsoluteMaximum<T>(Vector<T>)
Finds the element with the largest absolute value in the vector.
public static T AbsoluteMaximum<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to search.
Returns
- T
The largest absolute value in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method ignores the sign of numbers and finds the largest value by magnitude. For example, in the vector [-10, 5, 3], the absolute maximum is 10 (from -10). This is useful in algorithms where the size of values matters more than their direction.
Exceptions
- ArgumentException
Thrown when the vector is empty.
Add<T>(Vector<T>, Vector<T>)
Adds two vectors element by element.
public static Vector<T> Add<T>(this Vector<T> left, Vector<T> right)
Parameters
leftVector<T>The first vector.
rightVector<T>The second vector.
Returns
- Vector<T>
A new vector where each element is the sum of the corresponding elements in the input vectors.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This adds two vectors together by adding their corresponding elements. For example, [1, 2, 3] + [4, 5, 6] = [5, 7, 9].
Exceptions
- ArgumentException
Thrown when the vectors have different lengths.
Add<T>(Vector<T>, T)
Adds a scalar value to each element of the vector.
public static Vector<T> Add<T>(this Vector<T> vector, T scalar)
Parameters
vectorVector<T>The vector to which the scalar will be added.
scalarTThe scalar value to add to each element.
Returns
- Vector<T>
A new vector with the scalar added to each element.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method adds the same number to every element in your vector. For example, adding 5 to [1, 2, 3] results in [6, 7, 8]. This is useful for operations like shifting data or applying offsets.
Argsort<T>(Vector<T>)
Returns the indices that would sort the vector in ascending order.
public static int[] Argsort<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to get the sorted indices for.
Returns
- int[]
An array of indices that would sort the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This doesn't actually sort the vector. Instead, it tells you the positions (indices) in which the elements would appear if they were sorted. For example, if your vector is [3, 1, 2], the result would be [1, 2, 0], meaning the smallest element is at position 1, the second smallest at position 2, and the largest at position 0.
Average<T>(Vector<T>)
Calculates the average (mean) of all elements in the vector.
public static T Average<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector whose elements will be averaged.
Returns
- T
The average value of all elements in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The average is calculated by summing all elements and dividing by the count. For example, the average of [2,4,6,8] is (2+4+6+8)/4 = 5. Averages are fundamental in statistics and machine learning for summarizing data.
Exceptions
- ArgumentException
Thrown when the vector is empty.
CreateDiagonal<T>(Vector<T>)
Creates a diagonal matrix from a vector.
public static Matrix<T> CreateDiagonal<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to create a diagonal matrix from.
Returns
- Matrix<T>
A square matrix with the vector elements on the diagonal and zeros elsewhere.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: A diagonal matrix has numbers only along its diagonal (top-left to bottom-right) and zeros everywhere else. This method takes a vector like [1, 2, 3] and creates a matrix like: [1, 0, 0] [0, 2, 0] [0, 0, 3]
Divide<T>(Vector<T>, T)
Divides each element of the vector by a scalar value.
public static Vector<T> Divide<T>(this Vector<T> vector, T scalar)
Parameters
vectorVector<T>The vector whose elements will be divided.
scalarTThe scalar value to divide by.
Returns
- Vector<T>
A new vector with each element divided by the scalar.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This divides every number in your vector by the same value. For example, if your vector is [10, 20, 30] and you divide by 10, the result is [1, 2, 3]. This is commonly used for normalization in machine learning, such as when calculating averages or scaling data.
DotProduct<T>(Vector<T>, Vector<T>)
Calculates the dot product (scalar product) of two vectors.
public static T DotProduct<T>(this Vector<T> left, Vector<T> right)
Parameters
leftVector<T>The first vector.
rightVector<T>The second vector.
Returns
- T
The scalar result of the dot product operation.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The dot product multiplies corresponding elements of two vectors and then adds all the results. For example, the dot product of [1, 2, 3] and [4, 5, 6] is (1×4) + (2×5) + (3×6) = 4 + 10 + 18 = 32. This is fundamental in machine learning for calculating similarities between vectors, projections, and in neural network operations.
Exceptions
- ArgumentException
Thrown when vectors have different lengths.
EuclideanDistance<T>(Vector<T>, Vector<T>)
Calculates the Euclidean distance between two vectors.
public static T EuclideanDistance<T>(this Vector<T> v1, Vector<T> v2)
Parameters
v1Vector<T>The first vector.
v2Vector<T>The second vector.
Returns
- T
The Euclidean distance between the two vectors.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: Euclidean distance is the "straight-line" distance between two points in space. If you think of each vector as coordinates for a point, this method calculates how far apart those points are.
For example, in 2D space, the Euclidean distance between points (1,2) and (4,6) would be: √[(4-1)² + (6-2)²] = √[9 + 16] = v25 = 5
This concept extends to any number of dimensions. In machine learning, Euclidean distance is often used to measure similarity between data points or as part of clustering algorithms like K-means.
Exceptions
- ArgumentException
Thrown when the vectors have different lengths.
Extract<T>(Vector<T>, int)
Creates a new vector containing the first specified number of elements from the original vector.
public static Vector<T> Extract<T>(this Vector<T> vector, int length)
Parameters
vectorVector<T>The source vector.
lengthintThe number of elements to extract from the beginning of the vector.
Returns
- Vector<T>
A new vector containing the first specified number of elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method creates a shorter version of your vector by taking only the first few elements. For example, if you have a vector [5, 10, 15, 20, 25] and call Extract(3), you'll get a new vector with just [5, 10, 15].
This is useful when you only need to work with the beginning portion of your data.
Magnitude<T>(Vector<T>)
Calculates the magnitude (length) of a vector.
public static T Magnitude<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector whose magnitude is to be calculated.
Returns
- T
The magnitude of the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The magnitude is the "length" of a vector, calculated using the Pythagorean theorem. For a vector [a,b,c], the magnitude is √(a² + b² + c²). This is useful for normalizing vectors or measuring distances in machine learning algorithms.
MaxIndex<T>(Vector<T>)
Finds the index of the maximum value in the vector.
public static int MaxIndex<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to search.
Returns
- int
The index of the maximum value in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method finds the position of the largest number in your vector. For example, in the vector [3, 7, 2, 5], the maximum value 7 is at index 1 (positions are counted starting from 0). This is useful when you need to know where the highest value is, not just what it is.
Exceptions
- ArgumentException
Thrown when the vector is empty.
Max<T>(Vector<T>)
Finds the maximum value in the vector.
public static T Max<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to search.
Returns
- T
The maximum value in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method finds the largest number in a vector. For example, in the vector [3,7,2,5], the maximum value is 7. This is commonly used in algorithms like gradient descent to check convergence or in classification to find the most likely class.
Exceptions
- ArgumentException
Thrown when the vector is empty.
Maximum<T>(Vector<T>, T)
Creates a new vector where each element is the maximum of the corresponding element in the input vector and a scalar value.
public static VectorBase<T> Maximum<T>(this Vector<T> vector, T scalar)
Parameters
vectorVector<T>The input vector.
scalarTThe scalar value to compare against.
Returns
- VectorBase<T>
A new vector where each element is the maximum of the original element and the scalar.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method compares each element in your vector with a given number, and keeps whichever is larger. For example, finding the maximum of each element in [1, 5, 3] compared to 2 results in [2, 5, 3]. This is useful for setting minimum thresholds in data.
Median<T>(Vector<T>)
Calculates the median value of the elements in the vector.
public static T Median<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to calculate the median for.
Returns
- T
The median value of the vector elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The median is the middle value when all numbers are arranged in order. It's often used instead of the average (mean) when there are outliers or extreme values that might skew the mean.
For example, in the vector [1, 3, 5, 7, 9], the median is 5.
If there's an even number of elements, the median is the average of the two middle values. For example, in [1, 3, 5, 7], the median is (3 + 5) / 2 = 4.
Exceptions
- ArgumentException
Thrown when the vector is null or empty.
MinIndex<T>(Vector<T>)
Finds the index of the minimum value in the vector.
public static int MinIndex<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to search.
Returns
- int
The index of the minimum value in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method finds the position of the smallest number in your vector. For example, in the vector [3, 7, 2, 5], the minimum value 2 is at index 2 (positions are counted starting from 0). This is useful when you need to know where the lowest value is, not just what it is.
Exceptions
- ArgumentException
Thrown when the vector is empty.
Min<T>(Vector<T>)
Finds the minimum value in the vector.
public static T Min<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to search.
Returns
- T
The minimum value in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method finds the smallest number in a vector. For example, in the vector [3,7,2,5], the minimum value is 2. Finding minimums is useful in optimization algorithms and for identifying lower bounds in data.
Exceptions
- ArgumentException
Thrown when the vector is empty.
Minimum<T>(Vector<T>)
Finds the minimum element value in the vector.
public static T Minimum<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to search.
Returns
- T
The smallest value in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method finds the smallest number in your vector. For example, in the vector [5, 2, 8, 1], the minimum is 1.
Exceptions
- ArgumentException
Thrown when the vector is empty.
Multiply<T>(Vector<T>, Matrix<T>)
Multiplies a vector by a matrix, performing a vector-matrix multiplication.
public static Vector<T> Multiply<T>(this Vector<T> vector, Matrix<T> matrix)
Parameters
vectorVector<T>The vector to multiply (must have length equal to the number of matrix rows).
matrixMatrix<T>The matrix to multiply by.
Returns
- Vector<T>
A new vector resulting from the vector-matrix multiplication.
Type Parameters
TThe numeric type of the vector and matrix elements.
Remarks
For Beginners: Vector-matrix multiplication transforms a vector using a matrix. The vector must have the same number of elements as the matrix has rows. This operation is fundamental in machine learning for transforming data, applying weights in neural networks, and implementing linear transformations.
For example, multiplying a vector [1, 2, 3] by a 3×2 matrix [[1, 4], [2, 5], [3, 6]] results in a vector [1×1 + 2×2 + 3×3, 1×4 + 2×5 + 3×6] = [14, 32].
Exceptions
- ArgumentException
Thrown when the vector length doesn't match the number of matrix rows.
Multiply<T>(Vector<T>, T)
Multiplies each element of the vector by a scalar value.
public static Vector<T> Multiply<T>(this Vector<T> vector, T scalar)
Parameters
vectorVector<T>The vector whose elements will be multiplied.
scalarTThe scalar value to multiply by.
Returns
- Vector<T>
A new vector with each element multiplied by the scalar.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This multiplies every number in your vector by the same value. For example, if your vector is [1, 2, 3] and you multiply by 10, the result is [10, 20, 30]. This is commonly used for scaling in machine learning, such as when applying learning rates or weights.
Norm<T>(Vector<T>)
Calculates the Euclidean norm (magnitude or length) of the vector.
public static T Norm<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to calculate the norm for.
Returns
- T
The Euclidean norm of the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The norm is the "length" of a vector. For a 2D vector [x, y], it's calculated as √(x² + y²), which is the same as the Pythagorean theorem. For vectors with more dimensions, it's the square root of the sum of all squared elements.
OuterProduct<T>(Vector<T>, Vector<T>)
Computes the outer product of two vectors, resulting in a matrix.
public static Matrix<T> OuterProduct<T>(this Vector<T> leftVector, Vector<T> rightVector)
Parameters
leftVectorVector<T>The left vector in the outer product.
rightVectorVector<T>The right vector in the outer product.
Returns
- Matrix<T>
A matrix where each element (i,j) is the product of leftVector[i] and rightVector[j].
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The outer product creates a matrix by multiplying each element of the first vector with each element of the second vector. If you have a vector [a,b,c] and another vector [x,y], the result is a 3×2 matrix: [a*x, a*y] [b*x, b*y] [c*x, c*y] This operation is useful in neural networks for creating weight matrices.
PointwiseAbs<T>(Vector<T>)
Creates a new vector where each element is the absolute value of the corresponding element in the input vector.
public static Vector<T> PointwiseAbs<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector whose absolute values will be calculated.
Returns
- Vector<T>
A new vector containing the absolute values of the original vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The absolute value of a number is its distance from zero, ignoring the sign. For example, applying this to [-5, 0, 3] results in [5, 0, 3]. This is useful when you need to work with magnitudes regardless of direction.
PointwiseDivide<T>(Vector<T>, Vector<T>)
Divides each element of the left vector by the corresponding element of the right vector.
public static Vector<T> PointwiseDivide<T>(this Vector<T> left, Vector<T> right)
Parameters
leftVector<T>The vector whose elements will be divided (dividend).
rightVector<T>The vector containing the divisors.
Returns
- Vector<T>
A new vector with each element being the result of left[i] / right[i].
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This performs element-by-element division between two vectors. For example, dividing [10,8,6] by [2,4,3] results in [5,2,2]. This is commonly used in data normalization and feature scaling.
Exceptions
- ArgumentException
Thrown when vectors have different lengths.
PointwiseExp<T>(Vector<T>)
Applies the exponential function to each element of the vector.
public static VectorBase<T> PointwiseExp<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to transform.
Returns
- VectorBase<T>
A new vector with the exponential function applied to each element.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The exponential function (e^x) is applied to each number in the vector. For example, if your vector is [0, 1, 2], the result would be approximately [1, 2.72, 7.39]. This is commonly used in machine learning algorithms like softmax.
PointwiseLog<T>(Vector<T>)
Applies the natural logarithm function to each element of the vector.
public static VectorBase<T> PointwiseLog<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to transform.
Returns
- VectorBase<T>
A new vector with the natural logarithm applied to each element.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The natural logarithm (ln) is the inverse of the exponential function. For example, if your vector is [1, 2.72, 7.39], the result would be approximately [0, 1, 2]. This is commonly used in machine learning for transforming data and in algorithms like cross-entropy loss calculation.
PointwiseMultiplyInPlace<T>(Vector<T>, Vector<T>)
Multiplies each element of the left vector by the corresponding element of the right vector and stores the result in the left vector.
public static void PointwiseMultiplyInPlace<T>(this Vector<T> left, Vector<T> right)
Parameters
leftVector<T>The vector to be modified with the multiplication results.
rightVector<T>The vector containing the multipliers.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method performs element-by-element multiplication and modifies the original vector. For example, if left is [1,2,3] and right is [4,5,6], after this operation left becomes [4,10,18]. The "InPlace" in the name indicates that the operation modifies the original vector rather than creating a new one.
Exceptions
- ArgumentException
Thrown when vectors have different lengths.
PointwiseMultiply<T>(Vector<T>, Vector<T>)
Multiplies corresponding elements of two vectors together (Hadamard product).
public static Vector<T> PointwiseMultiply<T>(this Vector<T> left, Vector<T> right)
Parameters
leftVector<T>The first vector.
rightVector<T>The second vector.
Returns
- Vector<T>
A new vector containing the element-wise product.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This multiplies each element in the first vector by the corresponding element in the second vector. For example, if you have [1, 2, 3] and [4, 5, 6], the result will be [4, 10, 18].
This operation (also called the Hadamard product) is commonly used in neural networks, particularly when applying masks, attention mechanisms, or element-wise gates in recurrent networks.
Exceptions
- ArgumentException
Thrown when vectors have different lengths.
PointwiseSign<T>(Vector<T>)
Returns a new vector where each element is the sign of the corresponding element in the input vector.
public static VectorBase<T> PointwiseSign<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector whose signs will be determined.
Returns
- VectorBase<T>
A new vector where each element is 1 for positive values, -1 for negative values, and 0 for zero.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The sign function returns: - 1 if the number is positive - -1 if the number is negative - 0 if the number is zero
For example, applying this to [-5,0,3] results in [-1,0,1]. This is useful in algorithms that need to know only the direction of values, not their magnitude.
PointwiseSqrt<T>(Vector<T>)
Creates a new vector where each element is the square root of the corresponding element in the input vector.
public static VectorBase<T> PointwiseSqrt<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector whose square roots will be calculated.
Returns
- VectorBase<T>
A new vector containing the square roots of the original vector elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: The square root of a number is a value that, when multiplied by itself, gives the original number. For example, applying this to [4, 9, 16] results in [2, 3, 4]. Square roots are commonly used in distance calculations and statistical analysis.
Repeat<T>(Vector<T>, int)
Creates a new vector by repeating the original vector a specified number of times.
public static Vector<T> Repeat<T>(this Vector<T> vector, int count)
Parameters
vectorVector<T>The vector to repeat.
countintThe number of times to repeat the vector.
Returns
- Vector<T>
A new vector containing the original vector repeated the specified number of times.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This creates a longer vector by copying the original vector multiple times. For example, if your vector is [1, 2] and you repeat it 3 times, you'll get [1, 2, 1, 2, 1, 2].
Reshape<T>(Vector<T>, int, int)
Reshapes a vector into a two-dimensional matrix with the specified number of rows and columns.
public static Matrix<T> Reshape<T>(this Vector<T> vector, int rows, int columns)
Parameters
vectorVector<T>The vector to reshape.
rowsintThe number of rows in the resulting matrix.
columnsintThe number of columns in the resulting matrix.
Returns
- Matrix<T>
A matrix with the specified dimensions containing the vector elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method converts your one-dimensional list of numbers (vector) into a two-dimensional grid (matrix). Think of it like rearranging a line of items into rows and columns.
For example, if you have a vector [1, 2, 3, 4, 5, 6] and reshape it to 2 rows and 3 columns, you'll get a matrix that looks like: [1, 2, 3] [4, 5, 6]
The total number of elements must stay the same (rows × columns = vector length).
Exceptions
- ArgumentNullException
Thrown when the vector is null.
- ArgumentException
Thrown when rows or columns are less than or equal to zero, or when their product doesn't equal the vector length.
Slice<T>(Vector<T>, int, int)
Creates a new vector containing a subset of elements from the original vector.
public static Vector<T> Slice<T>(this Vector<T> vector, int start, int length)
Parameters
vectorVector<T>The source vector to slice.
startintThe zero-based index at which to begin the slice.
lengthintThe number of elements to include in the slice.
Returns
- Vector<T>
A new vector containing the specified elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This is like taking a portion of a list. For example, if you have a vector [1, 2, 3, 4, 5] and you call Slice(1, 3), you'll get [2, 3, 4].
StandardDeviation<T>(Vector<T>)
Calculates the standard deviation of the elements in the vector.
public static T StandardDeviation<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to calculate the standard deviation for.
Returns
- T
The standard deviation of the vector elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: Standard deviation measures how spread out the numbers in your vector are. A low standard deviation means the values tend to be close to the average (mean), while a high standard deviation means the values are spread out over a wider range.
For example, the vector [2, 4, 4, 4, 5, 5, 7, 9] has a mean of 5 and a standard deviation of 2. Most values are within 2 units of the mean.
This method uses the "sample standard deviation" formula, which divides by (n-1) instead of n. This is the standard approach in statistics when working with a sample of data rather than the entire population.
SubVector<T>(Vector<T>, int, int)
Creates a new vector containing a subset of elements from the original vector.
public static Vector<T> SubVector<T>(this Vector<T> vector, int startIndex, int count)
Parameters
vectorVector<T>The source vector.
startIndexintThe zero-based index at which to start extracting elements.
countintThe number of elements to extract.
Returns
- Vector<T>
A new vector containing the specified subset of elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method lets you take a "slice" of your vector. For example, if you have a vector [5, 10, 15, 20, 25] and you call SubVector(1, 3), you'll get a new vector with [10, 15, 20]. The first parameter (1) is where to start, and the second parameter (3) is how many elements to include.
Exceptions
- ArgumentOutOfRangeException
Thrown when startIndex is negative or when the range exceeds the vector's length.
SubVector<T>(Vector<T>, int[])
Creates a new vector by selecting elements from the original vector at specified indices.
public static Vector<T> SubVector<T>(this Vector<T> vector, int[] indices)
Parameters
vectorVector<T>The source vector.
indicesint[]An array of indices specifying which elements to select.
Returns
- Vector<T>
A new vector containing the elements at the specified indices.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method lets you pick specific elements from your vector by their positions. For example, if you have a vector [5, 10, 15, 20, 25] and you call SubVector([0, 3, 4]), you'll get a new vector with [5, 20, 25]. The array [0, 3, 4] specifies which positions to select.
Exceptions
- ArgumentNullException
Thrown when the indices array is null.
- ArgumentOutOfRangeException
Thrown when any index in the array is outside the bounds of the vector.
Subtract<T>(Vector<T>, Vector<T>)
Subtracts the elements of the right vector from the corresponding elements of the left vector.
public static Vector<T> Subtract<T>(this Vector<T> left, Vector<T> right)
Parameters
leftVector<T>The vector to subtract from (minuend).
rightVector<T>The vector to subtract (subtrahend).
Returns
- Vector<T>
A new vector containing the element-wise difference.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This performs element-by-element subtraction between two vectors. For example, if you have [5, 8, 3] and [2, 3, 1], the result will be [3, 5, 2]. This is useful for calculating differences between data points or model predictions and actual values.
Exceptions
- ArgumentException
Thrown when vectors have different lengths.
Subtract<T>(Vector<T>, T)
Subtracts a scalar value from each element of the vector.
public static Vector<T> Subtract<T>(this Vector<T> vector, T scalar)
Parameters
vectorVector<T>The vector from which to subtract the scalar.
scalarTThe scalar value to subtract from each element.
Returns
- Vector<T>
A new vector with the scalar subtracted from each element.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method subtracts the same number from every element in your vector. For example, if you have a vector [10, 15, 20] and subtract 5, you'll get [5, 10, 15].
This is useful for operations like normalizing data or adjusting values by a constant amount.
Subvector<T>(Vector<T>, int[])
Creates a subvector from the given vector using the specified indices.
public static Vector<T> Subvector<T>(this Vector<T> vector, int[] indices)
Parameters
vectorVector<T>The source vector.
indicesint[]The indices of elements to include in the subvector.
Returns
- Vector<T>
A new vector containing only the specified elements.
Type Parameters
TThe type of elements in the vector.
Sum<T>(Vector<T>)
Calculates the sum of all elements in the vector.
public static T Sum<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector whose elements will be summed.
Returns
- T
The sum of all elements in the vector.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method adds up all the numbers in your vector. For example, the sum of [1, 2, 3, 4] is 10. Summing is a fundamental operation used in many algorithms, including calculating averages and totals.
ToColumnMatrix<T>(Vector<T>)
Converts the vector to a column matrix (n x 1).
public static Matrix<T> ToColumnMatrix<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to convert.
Returns
- Matrix<T>
A matrix with one column containing the vector elements.
Type Parameters
TThe type of the vector elements.
Remarks
For Beginners: This method converts your vector into a matrix with just one column. For example, the vector [3, 5, 7] would become the matrix: [3] [5] [7]
Column matrices are useful in matrix multiplication operations and when you need to represent your data in matrix form for certain algorithms. In machine learning, input features are often represented as column matrices.
Exceptions
- ArgumentNullException
Thrown if the input vector is null.
ToDiagonalMatrix<T>(Vector<T>)
Converts the vector to a diagonal matrix.
public static Matrix<T> ToDiagonalMatrix<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to convert.
Returns
- Matrix<T>
A square matrix with the vector elements on the diagonal and zeros elsewhere.
Type Parameters
TThe type of the vector elements.
Remarks
For Beginners: A diagonal matrix is a special type of square matrix where all elements are zero except those on the main diagonal (top-left to bottom-right). This method creates such a matrix using your vector's values for the diagonal elements.
For example, the vector [3, 5, 7] would become the matrix: [3, 0, 0] [0, 5, 0] [0, 0, 7]
Diagonal matrices have special properties that make them useful in many mathematical operations and algorithms.
Exceptions
- ArgumentNullException
Thrown if the input vector is null.
ToIntList<T>(IEnumerable<Vector<T>>)
Converts a collection of vectors to a list of integers.
public static List<int> ToIntList<T>(this IEnumerable<Vector<T>> vectors)
Parameters
vectorsIEnumerable<Vector<T>>The collection of vectors to convert.
Returns
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This does the opposite of ToVectorList. It takes a list of vectors (like [[1], [2], [3]]) and converts it to a simple list of numbers [1, 2, 3].
ToRealVector<T>(Vector<Complex<T>>)
Extracts the real parts from a vector of complex numbers.
public static Vector<T> ToRealVector<T>(this Vector<Complex<T>> vector)
Parameters
vectorVector<Complex<T>>The vector of complex numbers.
Returns
- Vector<T>
A new vector containing only the real parts of the complex numbers.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: Complex numbers have two parts: a real part and an imaginary part. This method takes a vector of complex numbers and creates a new vector containing only the real parts.
For example, if you have a vector of complex numbers [(3+2i), (5-1i), (7+0i)], this method would return [3, 5, 7].
This is useful in signal processing and other fields where complex numbers are used but you need to work with just the real components.
ToRowMatrix<T>(Vector<T>)
Converts the vector to a row matrix (1 x n).
public static Matrix<T> ToRowMatrix<T>(this Vector<T> vector)
Parameters
vectorVector<T>The vector to convert.
Returns
- Matrix<T>
A matrix with one row containing the vector elements.
Type Parameters
TThe type of the vector elements.
Remarks
For Beginners: This method converts your vector into a matrix with just one row. For example, the vector [3, 5, 7] would become the matrix: [3, 5, 7]
Row matrices are useful in matrix multiplication operations and when you need to represent your data in matrix form for certain algorithms.
Exceptions
- ArgumentNullException
Thrown if the input vector is null.
ToVectorList<T>(IEnumerable<int>)
Converts a collection of integer indices to a list of single-element vectors.
public static List<Vector<T>> ToVectorList<T>(this IEnumerable<int> indices)
Parameters
indicesIEnumerable<int>The collection of integer indices to convert.
Returns
- List<Vector<T>>
A list of vectors, each containing a single element representing an index.
Type Parameters
TThe numeric type to convert the indices to.
Remarks
For Beginners: This method takes a list of numbers (like [1, 2, 3]) and converts each number into its own tiny vector. So [1, 2, 3] becomes [[1], [2], [3]], where each inner bracket is a vector.
Transform<T>(Vector<T>, Func<T, T>)
Applies a function to each element of the vector and returns a new vector with the results.
public static Vector<T> Transform<T>(this Vector<T> vector, Func<T, T> function)
Parameters
vectorVector<T>The vector to transform.
functionFunc<T, T>The function to apply to each element.
Returns
- Vector<T>
A new vector containing the transformed elements.
Type Parameters
TThe numeric type of the vector elements.
Remarks
For Beginners: This method lets you apply any operation to each element in your vector. For example, you could multiply each element by 2, or add 5 to each element. This is a powerful tool that allows you to customize how you process your data.
Transform<T, TResult>(Vector<T>, Func<T, TResult>)
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.
public static VectorBase<TResult> Transform<T, TResult>(this Vector<T> vector, Func<T, TResult> function)
Parameters
vectorVector<T>The vector to transform.
functionFunc<T, TResult>The function to apply to each element.
Returns
- VectorBase<TResult>
A new vector containing the transformed elements.
Type Parameters
TThe numeric type of the input vector elements.
TResultThe numeric type of the output vector elements.
Remarks
For Beginners: This method is similar to the Transform method, but it allows you to change the type of your data. For example, you could convert a vector of integers to a vector of doubles, or apply any other type conversion to each element.