Click here to Skip to main content
Click here to Skip to main content

Library for linear algebra methods in C#.NET and its application to computational heat transfer

By , 25 May 2005
Rate this:
Please Sign up or sign in to vote.


This article presents a library developed in Microsoft VC#.NET, which contains methods used in linear algebra. Methods for inversion of matrices, finding determinant and solving linear systems are presented here. The advantages of using an object oriented language for computational problems is also discussed here.


I was developing a mathematical library for computational methods for fluid flow, where I needed a linear algebra module for solving the formulations made by finite volume methods. Due to the relative simplicity of the Gaussian elimination scheme, I decided to use it and I could achieve reasonable speed in solving large sparse systems. The time taken is proportional to the square of the unknowns.

Using the code

As described above, the matrix is represented by a two dimensional double array. The following code snippet gives an example of how to use the library. Consider a simple system of linear equations:

2a + b + 3c = 26 
a + 10b + c = 48
7a + 2b + c = 28

The following code snippet is used to solve the simple system. The unique solution is obtained as a=2, b=4 and c=6. The methods in the library are efficient for systems of size 300. I usually use them for systems of size 75-100 in my application:

using System;
namespace LinearAlgebra
    class Class1
        static void Main(string[] args)
            double[,] DATA = {{2,1,3},{1,10,1},{7,2,1}};
            Matrix COF = new Matrix(DATA);
            Matrix CON = new Matrix(3,1);
            Console.WriteLine ("Determinent of COF matrix = " + 
                                             COF.Det().ToString() );
            Matrix Sol = COF.Inverse()*CON;

Example – Steady state heat conduction

As described above, the library is used to solve a steady state heat conduction problem. The problem set up is shown below:

A uniform mesh of size 10*5 is adopted. The linear system of equation is developed using the steady state heat conduction with convection at all boundaries. The convective heat transfer coefficient is taken as 10 W/m² K and thermal conductivity is taken as 4 W/m K. The equation system is solved and the results are written to a text file. The temperature distribution is then plotted using the MATLAB software. Given below is the comparison of results obtained using this library and the FEMLAB software.

The temperature distribution in the bottom edge of the slab is also compared.

The minor difference in the temperatures is due to the sparse grid adopted in the program.


There are many advantages that I have observed in using an object oriented language like C# for computational problems. For example, the nodal points in a mesh can be defined as objects with coordinates and topology as properties. The type of the nodes, for example, whether the node is internal or a boundary node, the type of boundary conditions etc. can also be added as properties. In pipe network simulation programs, the different components can be derived from, say, a Branch class, which simplifies the code to a large extent.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

No Biography provided

Comments and Discussions

GeneralTerrible PinmemberHerbert Sauro25-May-05 6:48 
GeneralRe: Terrible PinmemberVijayaSekhar Gullapalli26-May-05 3:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 25 May 2005
Article Copyright 2005 by VijayaSekhar Gullapalli
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid