.NET Matrix Library
Solve Method
See Also  Example Send Feedback



rightHands
A Matrix object containing the right-hand sides for the system of linear equations.
Solves a system of linear equations.

Syntax

Visual Basic (Declaration) 
Public Function Solve( _
   ByVal rightHands As Matrix _
) As Matrix
Visual Basic (Usage)Copy Code
Dim instance As Matrix
Dim rightHands As Matrix
Dim value As Matrix
 
value = instance.Solve(rightHands)
C# 
public Matrix Solve( 
   Matrix rightHands
)
C++/CLI 
public:
Matrix^ Solve( 
   Matrix^ rightHands
) 

Parameters

rightHands
A Matrix object containing the right-hand sides for the system of linear equations.

Return Value

A Matrix object that contains the solution of the system of linear equations.

Exceptions

ExceptionDescription
SizeMismatchExceptionThe rightHands parameter does not represent a matrix with the same number of rows as the current matrix.

Example

The following example solves a system of three simultaneous equations with three unknowns using the Solve method.
Visual BasicCopy Code
Imports System
Imports Bluebit.MatrixLibrary

Class Test

    Public Shared Sub Main()

        Console.WriteLine(" System of linear equations to be solved:")
        Console.WriteLine("   x + 2*y + 1*z = 1")
        Console.WriteLine(" 2*x + 3*y + 4*z = 2")
        Console.WriteLine(" 3*x + 2*y + 1*z = 2")
        Console.WriteLine()
        Console.WriteLine("The above system in matrix algebra notation:")
        Console.WriteLine("| 1  2  1 |   | x |    | 1 |")
        Console.WriteLine("| 2  3  4 | * | y | =  | 2 |")
        Console.WriteLine("| 3  2  1 |   | z |    | 2 |")
        Console.WriteLine()
        Console.WriteLine(" A * X = B")
        Console.WriteLine()

        'Matrix A will contain the coefficients of the unkowns
        Dim A As New Matrix(3, 3)
        A(0, 0) = 1 : A(0, 1) = 2 : A(0, 2) = 1
        A(1, 0) = 2 : A(1, 1) = 3 : A(1, 2) = 4
        A(2, 0) = 3 : A(2, 1) = 2 : A(2, 2) = 1

        'Matrix B will contain the right hand terms of equations
        Dim B As New Matrix(3, 1)

        B(0, 0) = 1
        B(1, 0) = 2
        B(2, 0) = 2

        'Matrix X will contain the solution
        Dim X As Matrix

        'find the solution
        X = A.Solve(B)

        Console.WriteLine("Solution :")
        Console.WriteLine("x = {0}, y ={1}, z = {2}", X(0, 0), X(1, 0), X(2, 0))

        Console.Read()

    End Sub

End Class
C#Copy Code
using System;
using Bluebit.MatrixLibrary;

class Test
{
    static void Main(string[] args)
    {
        Console.WriteLine(" System of linear equations to be solved:");
        Console.WriteLine("   x + 2*y + 1*z = 1");
        Console.WriteLine(" 2*x + 3*y + 4*z = 2");
        Console.WriteLine(" 3*x + 2*y + 1*z = 2");
        Console.WriteLine();
        Console.WriteLine("The above system in matrix algebra notation:");
        Console.WriteLine("| 1  2  1 |   | x |    | 1 |");
        Console.WriteLine("| 2  3  4 | * | y | =  | 2 |");
        Console.WriteLine("| 3  2  1 |   | z |    | 2 |");
        Console.WriteLine();
        Console.WriteLine(" A * X = B");
        Console.WriteLine();

        //Matrix A will contain the coefficients of the unkowns
        Matrix A = new Matrix(3, 3);
        A[0, 0] = 1; A[0, 1] = 2; A[0, 2] = 1;
        A[1, 0] = 2; A[1, 1] = 3; A[1, 2] = 4;
        A[2, 0] = 3; A[2, 1] = 2; A[2, 2] = 1;

        //Matrix B will contain the right hand terms of equations
        Matrix B = new Matrix(3, 1);

        B[0, 0] = 1;
        B[1, 0] = 2;
        B[2, 0] = 2;

        //Matrix X will contain the solution
        Matrix X;

        //find the solution
        X = A.Solve(B);

        Console.WriteLine("Solution :");
        Console.WriteLine("x = {0}, y = {1}, z = {2}", X[0, 0], X[1, 0], X[2, 0]);

        Console.Read();
    }
}
C++Copy Code
#include "stdafx.h"

using namespace System;
using namespace Bluebit::MatrixLibrary;

int main(array<System::String ^> ^args)
{
        Console::WriteLine(" System of linear equations to be solved:");
        Console::WriteLine("   x + 2*y + 1*z = 1");
        Console::WriteLine(" 2*x + 3*y + 4*z = 2");
        Console::WriteLine(" 3*x + 2*y + 1*z = 2");
        Console::WriteLine();
        Console::WriteLine("The above system written in matrix algebra notation:");
        Console::WriteLine("| 1  2  1 |   | x |    | 1 |");
        Console::WriteLine("| 2  3  4 | * | y | =  | 2 |");
        Console::WriteLine("| 3  2  1 |   | z |    | 2 |");
        Console::WriteLine();
        Console::WriteLine(" A * X = B");
        Console::WriteLine();

        //Matrix A will contain the coefficients of the unkowns
        Matrix^ A = gcnew Matrix(3, 3);
        A[0, 0] = 1; A[0, 1] = 2; A[0, 2] = 1;
        A[1, 0] = 2; A[1, 1] = 3; A[1, 2] = 4;
        A[2, 0] = 3; A[2, 1] = 2; A[2, 2] = 1;

        //Matrix B will contain the right hand terms of equations
        Matrix^ B = gcnew Matrix(3, 1);

        B[0, 0] = 1;
        B[1, 0] = 2;
        B[2, 0] = 2;

        //Matrix X will contain the solution
        Matrix^ X;

        //find the solution
        X = A->Solve(B);

        Console::WriteLine("Solution :");
        Console::WriteLine("x = {0}, y = {1}, z = {2}", X[0, 0], X[1, 0], X[2, 0] );

        Console::Read();
    
	return 0;
}

Remarks

The Solve method finds the solution to the system of linear equations A*X = B where:

A denotes the current matrix.
B denotes the matrix whose columns contain the right hand terms (rightHands parameter).
X denotes the solution matrix that is to be computed and will be returned be the method.

The system can be solved with multiple right-hand sides stored in the columns of the matrix B.

rightHands matrix must have the same number of rows as current matrix.

If current matrix is a mxn matrix then the following operations are used:

  • If m=n then LU factorization is used to solve the system. If matrix is found to be singular then LQ factorization is used to solve it as an underdetermined system.
  • If m<n then LQ factorization is used to compute the minimum norm solution for the underdetermined system.
  • If m>n then QR factorization is used to compute a least squares solution for the overdetermined system.

Requirements

Target Platforms: Windows 2000, Windows XP, Windows Server 2003 family, Windows Vista, Windows Server 2008 family, Windows 7

See Also

.NET Matrix Library