This project has moved. For the latest updates, please go here.

Solving Ax=B in visual basic

Nov 22, 2012 at 4:47 PM

Hei.

I've been facing a problem while trying to solve a Ax=B matricial system on my VB project. I used to program in Fortran and solve that problem through DGESV LaPack routine, but since I'm new in VB programming I cant seem to get a clue about how to do it.

Thanks in advance, any help would be much appreciated.

Nov 23, 2012 at 7:42 AM
Edited Nov 23, 2012 at 7:42 AM

Hi,

Not VB, but this how you'd do it in C# (from the examples project). DGESV uses LU decomp, so the equivalent would be:

 // Solve next system of linear equations (Ax=b):
 // 5*x + 2*y - 4*z = -7
 // 3*x - 7*y + 6*z = 38
 // 4*x + 1*y + 5*z = 43
 // Create matrix "A" with coefficients 
  var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } });

 // Create vector "b" with the constant terms.
 var vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 });
 
 // 1. Solve linear equations using LU decomposition
 var resultX = matrixA.LU().Solve(vectorB);

Nov 30, 2012 at 1:40 AM

Hi cuda, thanks for your help!

I followed your example (programming in VB ofc), although when using this LU decomposition, I run into a series of NaN (not a number) results. I've been debugging this project (all the "pre-system-solving" code) using an old example I had programmed in Fortran. Both the A matrix and the B vector are similar to the ones of the so called example, although LaPack's DGESV seems to be capable of solving it, but this one does not. Any thoughts ?

Thank you in advance, once again. 

Nov 30, 2012 at 6:10 AM

Could you post your A matrix and B vector here so I could test a couple things? or e-mail them to me at marcus@cuda.org.

Thanks,
Marcus 

Dec 6, 2012 at 6:59 PM

Hi. Sorry for the delay. I've finally managed to put the solving function to work, it was my error on the elements' calculations...

It's actually a HUGE matrix, about 2679x2679, it takes me around 1 min to solve, while in fortran it took only 2 or 3 secs. I know it can't actually be direcly compared, but it's like 20 or 30 times slower :\ Is there any chance to make this faster?

Thanks A LOT, again :)

Dec 7, 2012 at 9:48 AM

In the next couple of weeks there will be a nuget package that includes the MKL native provider. In the mean time, here are links to them:

http://files.marcuscuda.com/mathnet.native.x86-64.zip - 64bit
http://files.marcuscuda.com/mathnet.native.x86.zip  - 32bit

Put the two DLLs in the same directory as your executable and then add
Control.LinearAlgebraProvider = new MathNet.Numerics.Algorithms.LinearAlgebra.Mkl.MklLinearAlgebraProvider();
to your code.

Let me know how it performs.

Dec 7, 2012 at 2:12 PM

Hi Marcus.

 

I downloaded those 2 DLL's to my main project folder, then I added that line you mentioned. Although, when I started debugging, Visual Studio poped up a window to browse a .CS file to add to the project. I guess i should just import the DLLs somehow, tried also to add them as references, but VS won't let me do that, saying that they're not "a valid assembly or COM component"... Any thoughts ?

Dec 7, 2012 at 2:22 PM

Hi,

You don't add them as a reference. You need to copy them to the output folder (i.e.:<project>\bin\release). What I normally do is add them to the project (Add -> Existing item) and then set the "Copy to output directory" property to "Copy Always" for both files.

Dec 7, 2012 at 3:59 PM
Edited Dec 7, 2012 at 4:00 PM

WOW !

It just flies! As I told you, on my mac it was taking about 2 or 3 seconds per iteration, while with mathnetnumerics it took about 1 min. Now it is performing like Fortran's LAPACK or even better. It's actually hard to tell once it's so fast ! :)

Problem solved... THANK YOU ! :)