Click or drag to resize
Accord.NET (logo)

MatrixSolve Method (Single, Single, Boolean)

Returns the solution matrix if the matrix is square or the least squares solution otherwise.

Namespace:  Accord.Math
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
public static float[] Solve(
	this float[][] matrix,
	float[] rightSide,
	bool leastSquares = false
Request Example View Source


Type: SystemSingle
The matrix for the linear problem.
Type: SystemSingle
The right side b.
leastSquares (Optional)
Type: SystemBoolean
True to produce a solution even if the matrix is singular; false otherwise. Default is false.

Return Value

Type: Single

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type . When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).
Please note that this does not check if the matrix is non-singular before attempting to solve. If a least squares solution is desired in case the matrix is singular, pass true to the leastSquares parameter when calling this function.
// Create a matrix. Please note that this matrix
// is singular (i.e. not invertible), so only a 
// least squares solution would be feasible here.

Single[,] matrix = 
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 },

// Define a right side vector b:
Single[] rightSide = { 1, 2, 3 };

// Solve the linear system Ax = b by finding x:
Single[] x = Matrix.Solve(matrix, rightSide, leastSquares: true);

// The answer should be { -1/18, 2/18, 5/18 }.
See Also