Table of Contents

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

source float[,]

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

source float[,]

The two-dimensional array to process.

scalar float

The 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.

Complexity: O(n * m), where n = rows of source, m = columns of source.

Add(float[,], float[,])

public static float[,] Add(this float[,] source, float[,] matrix)

Parameters

source float[,]
matrix float[,]

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

source float[,,,]

The four-dimensional array whose elements will be incremented.

scalar float

The scalar value to add to each element of the array.

Remarks

This method modifies the contents of the source array directly.

Complexity: O(a * b * c * d) for dimensions of 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

source float[,,]

The three-dimensional array whose elements will be incremented.

scalar float

The scalar value to add to each element of the array.

Remarks

This method modifies the contents of the source array directly.

Complexity: O(a * b * c) for dimensions of 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

source float[,]

The two-dimensional array of single-precision floating-point numbers whose elements will be incremented. Cannot be null.

scalar float

The 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.

Complexity: O(n * m), where n = rows of 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

source float[,]

The source two-dimensional array.

vector float[]

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.

Complexity: O(n * m), where n = rows of 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

source float[,,,]

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

source float[,]

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

source float[]

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

source float[,,,]

The array used only for shape in all four dimensions.

onesProbability float

Probability of placing 1 in a cell. Must be between 0 and 1.

random Random

The random number generator instance.

Returns

float[,,,]

A new array with the same shape as source containing zeros and ones.

AsZeroOnes(float[,,], float, Random)

public static float[,,] AsZeroOnes(this float[,,] source, float onesProbability, Random random)

Parameters

source float[,,]
onesProbability float
random Random

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

source float[,]

The array used only for shape (rows and columns).

onesProbability float

Probability of placing 1 in a cell. Must be between 0 and 1.

random Random

The random number generator instance.

Returns

float[,]

A new array with the same shape as source containing 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

source float[,,,]

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

source float[,]

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

source float[,]

The two-dimensional array of single-precision floating-point numbers to be clipped.

min float

The minimum value to which elements in the array will be limited.

max float

The 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

source float[,]

The two-dimensional array to clip.

min float

The minimum value to clip the source elements to.

max float

The 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

source float[,]

The array whose elements will be divided.

scalar float

The divisor.

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

source float[,]

The array whose elements will be divided.

divisor float[,]

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

source float[,,,]

The array to modify.

scalar float

The divisor.

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

source float[,,]

The array to modify.

scalar float

The divisor.

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

source float[,]

The array to modify.

scalar float

The divisor.

Log(float[,])

Computes the natural logarithm elementwise and returns a new array.

public static float[,] Log(this float[,] source)

Parameters

source float[,]

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

source float[,,,]

The four-dimensional array to multiply.

scalar float

The multiplier.

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

source float[,,]
scalar float

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

source float[,]

The array whose elements will be multiplied.

scalar float

The multiplier.

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

source float[]

The one-dimensional array to multiply.

scalar float

The multiplier.

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

source float[,]

Left operand with shape [n, m].

matrix float[,]

Right operand with shape [m, p].

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

source float[,,,]

The left operand.

matrix float[,,,]

The source to multiply elementwise with.

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

source float[,,]
matrix float[,,]

Returns

float[,,]

MultiplyElementwise(float[,], float[,])

Performs elementwise multiplication between this source and another source.

public static float[,] MultiplyElementwise(this float[,] source, float[,] matrix)

Parameters

source float[,]

The left operand.

matrix float[,]

The source to multiply elementwise with.

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

source float[,]
matrix float[]

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

source float[]

The one-dimensional array treated as a row vector.

matrix float[,]

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

source float[,,,]
scalar int

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

source float[,]

The two-dimensional array whose elements will be exponentiated.

scalar int

The 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

source float[,,,]

The minuend array.

matrix float[,,,]

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

source float[,]

The two-dimensional minuend array.

matrix float[,]

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

source float[]

The minuend array.

matrix float[]

The subtrahend array. Must have the same length.

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

source float[,]

The two-dimensional array to transpose.

Returns

float[,]

A new array with shape [columns, rows].