Class MatrixRoutines
- Namespace
- NeuralNetworks.Core
- Assembly
- NeuralNetworks.dll
Provides extension methods for float array operations, including arithmetic, elementwise transformations, shape-preserving initialization, clipping, transposition, and matrix multiplication.
public static class MatrixRoutines
- Inheritance
-
MatrixRoutines
- Inherited Members
Remarks
Includes overloads for one-dimensional through four-dimensional arrays. Most operations return a new array, while methods named InPlace modify the input array directly. Some elementwise operations support broadcasting across compatible dimensions.
Methods
Abs(float[,])
public static float[,] Abs(this float[,] source)
Parameters
sourcefloat[,]
Returns
- float[,]
Add(float[,], float)
Adds a scalar value to each element of the source.
public static float[,] Add(this float[,] source, float scalar)
Parameters
sourcefloat[,]The two-dimensional array to process.
scalarfloatThe value to add to each element.
Returns
- float[,]
A new array of the same shape with the result of addition.
Remarks
This method creates a new array and does not modify the original source array.
source, m = columns of source.
Add(float[,], float[,])
public static float[,] Add(this float[,] source, float[,] matrix)
Parameters
Returns
- float[,]
AddInPlace(float[,,,], float)
Adds the specified scalar value to each element of the four-dimensional array in place.
public static void AddInPlace(this float[,,,] source, float scalar)
Parameters
sourcefloat[,,,]The four-dimensional array whose elements will be incremented.
scalarfloatThe scalar value to add to each element of the array.
Remarks
This method modifies the contents of the source array directly.
source.
AddInPlace(float[,,], float)
Adds the specified scalar value to each element of the three-dimensional array in place.
public static void AddInPlace(this float[,,] source, float scalar)
Parameters
sourcefloat[,,]The three-dimensional array whose elements will be incremented.
scalarfloatThe scalar value to add to each element of the array.
Remarks
This method modifies the contents of the source array directly.
source.
AddInPlace(float[,], float)
Adds the specified scalar value to each element of the two-dimensional array in place.
public static void AddInPlace(this float[,] source, float scalar)
Parameters
sourcefloat[,]The two-dimensional array of single-precision floating-point numbers whose elements will be incremented. Cannot be null.
scalarfloatThe scalar value to add to each element of the array.
Remarks
This method modifies the contents of the source array directly. The array must be initialized
before calling this method.
source, m = columns
of source.
AddRow(float[,], float[])
Adds a row to the current source by elementwise addition with the specified source.
public static float[,] AddRow(this float[,] source, float[] vector)
Parameters
sourcefloat[,]The source two-dimensional array.
vectorfloat[]The row values to be added elementwise to each row of the source. Length must equal the number of columns.
Returns
- float[,]
A new array containing the elementwise sum of each row with
vector.
Remarks
This method creates a new array and does not modify the original source array. It adds
vector to each row of source elementwise.
source, m = columns of source.
AsOnes(float[,,,])
Creates a new source filled with ones, with the same dimensions as the specified source.
public static float[,,,] AsOnes(this float[,,,] source)
Parameters
sourcefloat[,,,]The source used to determine the dimensions of the new source.
Returns
- float[,,,]
A new source filled with ones.
AsOnes(float[,])
Creates a new source filled with ones, with the same dimensions as the specified source.
public static float[,] AsOnes(this float[,] source)
Parameters
sourcefloat[,]The source used to determine the dimensions of the new source.
Returns
- float[,]
A new source filled with ones.
AsOnes(float[])
Creates a new one-dimensional array filled with ones, with the same length as the specified source.
public static float[] AsOnes(this float[] source)
Parameters
sourcefloat[]The one-dimensional array used to determine the length of the new array.
Returns
- float[]
A new one-dimensional array filled with ones.
Remarks
Example:
float[] source = new float[] { 2.0f, 3.0f, 4.0f }; var res = source.AsOnes(); // returns new
float[] { 1.0f, 1.0f, 1.0f}
AsZeroOnes(float[,,,], float, Random)
Creates a new four-dimensional array with elements set to 1 with probability onesProbability,
otherwise 0.
public static float[,,,] AsZeroOnes(this float[,,,] source, float onesProbability, Random random)
Parameters
sourcefloat[,,,]The array used only for shape in all four dimensions.
onesProbabilityfloatProbability of placing 1 in a cell. Must be between 0 and 1.
randomRandomThe random number generator instance.
Returns
- float[,,,]
A new array with the same shape as
sourcecontaining zeros and ones.
AsZeroOnes(float[,,], float, Random)
public static float[,,] AsZeroOnes(this float[,,] source, float onesProbability, Random random)
Parameters
Returns
- float[,,]
AsZeroOnes(float[,], float, Random)
Creates a new two-dimensional array with elements set to 1 with probability onesProbability,
otherwise 0.
public static float[,] AsZeroOnes(this float[,] source, float onesProbability, Random random)
Parameters
sourcefloat[,]The array used only for shape (rows and columns).
onesProbabilityfloatProbability of placing 1 in a cell. Must be between 0 and 1.
randomRandomThe random number generator instance.
Returns
- float[,]
A new array with the same shape as
sourcecontaining zeros and ones.
AsZeros(float[,,,])
Creates a new four-dimensional zero-filled array with the same shape as the source.
public static float[,,,] AsZeros(this float[,,,] source)
Parameters
sourcefloat[,,,]The array used only for shape.
Returns
- float[,,,]
A new zero-filled array.
AsZeros(float[,])
Creates a new two-dimensional zero-filled array with the same shape as the source.
public static float[,] AsZeros(this float[,] source)
Parameters
sourcefloat[,]The array used only for shape.
Returns
- float[,]
A new zero-filled array.
Clip(float[,], float, float)
Returns a new two-dimensional array with each element of the source array limited to the specified minimum and maximum values.
public static float[,] Clip(this float[,] source, float min, float max)
Parameters
sourcefloat[,]The two-dimensional array of single-precision floating-point numbers to be clipped.
minfloatThe minimum value to which elements in the array will be limited.
maxfloatThe maximum value to which elements in the array will be limited.
Returns
- float[,]
A new two-dimensional array where each element is set to the corresponding value from the source array, limited to the specified minimum and maximum values.
Remarks
If an element in the source array is less than the specified minimum, the result will contain the minimum value at that position. If an element is greater than the specified maximum, the result will contain the maximum value. The original array is not modified.
ClipInPlace(float[,], float, float)
Clips the values of the source in-place between the specified minimum and maximum values.
public static void ClipInPlace(this float[,] source, float min, float max)
Parameters
sourcefloat[,]The two-dimensional array to clip.
minfloatThe minimum value to clip the source elements to.
maxfloatThe maximum value to clip the source elements to.
Divide(float[,], float)
Divides each element of the two-dimensional array by a scalar and returns a new array.
public static float[,] Divide(this float[,] source, float scalar)
Parameters
Returns
- float[,]
A new array containing the division results.
DivideElementwise(float[,], float[,])
Divides each element of the two-dimensional array by the corresponding element of another array elementwise.
public static float[,] DivideElementwise(this float[,] source, float[,] divisor)
Parameters
sourcefloat[,]The array whose elements will be divided.
divisorfloat[,]The array containing the divisors.
Returns
- float[,]
A new array containing the division results.
DivideInPlace(float[,,,], float)
Divides each element of the four-dimensional array by a scalar in place.
public static void DivideInPlace(this float[,,,] source, float scalar)
Parameters
DivideInPlace(float[,,], float)
Divides each element of a three-dimensional array by a scalar in place.
public static void DivideInPlace(this float[,,] source, float scalar)
Parameters
DivideInPlace(float[,], float)
Divides each element of the two-dimensional array by a scalar in place.
public static void DivideInPlace(this float[,] source, float scalar)
Parameters
Log(float[,])
Computes the natural logarithm elementwise and returns a new array.
public static float[,] Log(this float[,] source)
Parameters
sourcefloat[,]The array whose elements will be transformed.
Returns
- float[,]
A new array with
log(x)applied elementwise.
Multiply(float[,,,], float)
Multiplies each element of the source by a scalar value.
public static float[,,,] Multiply(this float[,,,] source, float scalar)
Parameters
Returns
- float[,,,]
A new array with each element multiplied by the scalar value.
Multiply(float[,,], float)
public static float[,,] Multiply(this float[,,] source, float scalar)
Parameters
Returns
- float[,,]
Multiply(float[,], float)
Multiplies each element of the source by a scalar value.
public static float[,] Multiply(this float[,] source, float scalar)
Parameters
Returns
- float[,]
A new array with the multiplication results.
Multiply(float[], float)
Multiplies each element of the source by a scalar value.
public static float[] Multiply(this float[] source, float scalar)
Parameters
Returns
- float[]
A new array containing the multiplication results.
Remarks
Complexity: O(n), where n = length of source
MultiplyDot(float[,], float[,])
Multiplies the current source with another source using the dot product.
public static float[,] MultiplyDot(this float[,] source, float[,] matrix)
Parameters
Returns
- float[,]
A new array with shape [n, p] containing the dot product result.
Remarks
Complexity: O(n * m * p), where n = rows of source, m = shared dimension, p = columns of
matrix
MultiplyElementwise(float[,,,], float[,,,])
Performs elementwise multiplication between this source and another source.
public static float[,,,] MultiplyElementwise(this float[,,,] source, float[,,,] matrix)
Parameters
Returns
- float[,,,]
A new source resulting from the elementwise multiplication.
Remarks
Multiplies each element of the source with the corresponding element of another source. If the dimensions of the two matrices are not the same, the smaller source is broadcasted to match the larger source. If the size of this source is (a * b * c * d), and the size of source is (e * f * g * h), then the resulting size is (max(a,e) * max(b,f) * max(c,g) * max(d,h))
MultiplyElementwise(float[,,], float[,,])
public static float[,,] MultiplyElementwise(this float[,,] source, float[,,] matrix)
Parameters
Returns
- float[,,]
MultiplyElementwise(float[,], float[,])
Performs elementwise multiplication between this source and another source.
public static float[,] MultiplyElementwise(this float[,] source, float[,] matrix)
Parameters
Returns
- float[,]
A new source resulting from the elementwise multiplication.
Remarks
Multiplies each element of the source with the corresponding element of another source. If the dimensions of the two matrices are not the same, the smaller source is broadcasted to match the larger source. If the size of this source is (a * b), and the size of source is (c * d), then the resulting size is (max(a,c) * max(b,d))
MultiplyElementwise(float[,], float[])
public static float[,] MultiplyElementwise(this float[,] source, float[] matrix)
Parameters
Returns
- float[,]
MultiplyElementwise(float[], float[,])
Performs elementwise multiplication between a vector and a matrix, with broadcasting across dim1.
public static float[,] MultiplyElementwise(this float[] source, float[,] matrix)
Parameters
sourcefloat[]The one-dimensional array treated as a row vector.
matrixfloat[,]The two-dimensional array to multiply elementwise.
Returns
- float[,]
A new two-dimensional array of shape [rows of
matrix, max(columns)] containing the elementwise product.
Remarks
If the dimensions are not the same, the smaller array is broadcasted across columns.
Power(float[,,,], int)
public static float[,,,] Power(this float[,,,] source, int scalar)
Parameters
Returns
- float[,,,]
Power(float[,], int)
Raises each element of the source to the specified power.
public static float[,] Power(this float[,] source, int scalar)
Parameters
sourcefloat[,]The two-dimensional array whose elements will be exponentiated.
scalarintThe exponent (integer).
Returns
- float[,]
A new array with each element raised to
scalar.
Subtract(float[,,,], float[,,,])
Subtracts the elements of the specified four-dimensional array from the current four-dimensional array.
public static float[,,,] Subtract(this float[,,,] source, float[,,,] matrix)
Parameters
sourcefloat[,,,]The minuend array.
matrixfloat[,,,]The subtrahend array. Must have the same shape as
source.
Returns
- float[,,,]
A new array containing the elementwise difference.
Subtract(float[,], float[,])
Subtracts the elements of the specified source from the current source.
public static float[,] Subtract(this float[,] source, float[,] matrix)
Parameters
sourcefloat[,]The two-dimensional minuend array.
matrixfloat[,]The two-dimensional subtrahend array. Must have the same shape.
Returns
- float[,]
A new array containing the elementwise difference.
Subtract(float[], float[])
Subtracts the elements of the specified one-dimensional array from the current one-dimensional array.
public static float[] Subtract(this float[] source, float[] matrix)
Parameters
Returns
- float[]
A new array containing the elementwise difference.
Transpose(float[,])
Transposes the matrix by swapping its rows and columns.
public static float[,] Transpose(this float[,] source)
Parameters
sourcefloat[,]The two-dimensional array to transpose.
Returns
- float[,]
A new array with shape [columns, rows].