.NET Matrix Library
Solve Method
See Also  Example Send Feedback



Solves a system of linear equations Solves a system of linear equations

Overload List

OverloadDescription
Solve(Matrix)Solves a system of linear equations for multiple right hands.  
Solve(Vector)Solves a system of linear equations for a single right hand vector.  

Example

This example demonstrates the use of the Solve method
C#Copy Code
using System;
using Bluebit.MatrixLibrary;
using Bluebit.MatrixLibrary;

namespace TestSML
{
   class Program
   {
       static void Main(string[] args)
       {
           // ==========================================
           // Solving systems of sparse linear equations
           // with Bluebit.MatrixLibrary
           //===========================================

           // Solving the following sparse linear system:

           //            +1*x1       +2*x4 = 3
           //      +3*x1                   = 2
           //                  +4*x3       = 1
           // 5*x0       +6*x2             = 4
           //                        +7*x4 = 5

           // Writting the above system in matrix format:

           // |     1   2 |   | x0 |   | 3 |
           // |   3       |   | x1 |   | 2 |
           // |       4   | X | x2 | = | 1 |
           // | 5   6     |   | x3 |   | 4 |
           // |         7 |   | x4 |   | 5 |

           // A * x = b

           // Initialize a 5x5 sparse matrix
           SparseMatrix A = new SparseMatrix(5, 5);

           // Assign the non-zero elements
           A[0, 2] = 1.0;
           A[0, 4] = 2.0;
           A[1, 1] = 3.0;
           A[2, 3] = 4.0;
           A[3, 0] = 5.0;
           A[3, 2] = 6.0;
           A[4, 4] = 7.0;

           // Write matrix A
           Console.WriteLine(A.ToString("#;#; "));

           // Define the right hand vector b
           Vector b = new double[5] { 3, 2, 1, 4, 5 };

           // Initialize a SparseSolver object for matrix A
           SparseSolver solver = new SparseSolver(A);

           // Solve for x
           Vector x = solver.Solve(b);

           // Write the solution
           Console.WriteLine("    x = {0}", x);

           // Verify the solution. A*x = b
           Console.WriteLine("A * x = {0}", A * x);
           Console.WriteLine("    b = {0}", b);

           // Change the right hand side and
           // re-use the solver
           b = new double[5] { 1, 2, 5, 5, 7 };

           // Compute the solution for the new
           // right hand
           x = solver.Solve(b);            
           Console.WriteLine("    x = {0}", x);

           // Modify the underlying matrix
           A[0, 2] = 5;

           // The solver can still be used
           // to solve the new linear system
           x = solver.Solve(b);
           Console.WriteLine("    x = {0}", x);

           Console.WriteLine("Finished");
           Console.ReadKey();
       }
   }
}

Requirements

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

See Also

.NET Matrix Library