Using Mathnet with Excel DNA

947 views
Skip to first unread message

ajwillshire

unread,
Oct 27, 2010, 6:39:15 AM10/27/10
to Excel-Dna
Hi everyone,

In the interests of sharing, I thought I'd post a little about using
Mathnet and Excel DNA together.

Mathnet is an open source set of numerical methods but with the big
advantage that the licence permits use in proprietary applications.
I've only scratched the surface of it but the capabilities are
immense.

You can get the latest version of Mathnet.iridium here:

http://www.mathdotnet.com/downloads/Iridium-2008-8-16-470.ashx?From=IridiumCurrentRelease

I think it's in the middle of being combined with another open-source
project to form Mathnet.Numerics but I don't think the functions that
I wanted have been transferred yet.

I think updates will be posted here in future:
http://www.mathdotnet.com/

(I may have misunderstood some of the project history and current
status - apologies if so.)

Anyway, here's the code from a module that I've written. What I've
done is nothing earth-shattering but it shows how it can be used. What
I like is that I don't need to refer back to Excel functions in the
middle of a subroutine using the XlCall method, but also the breadth
of algorithms included.

Imports ExcelDna.Integration
Imports MathNet.Numerics.LinearAlgebra


Public Class UsingIridium

'This just returns the inverse of a matrix

Public Shared Function InvertMatrix(ByVal InputMatrix As
Double(,)) As Double(,)

Dim TempMatrix As Matrix = Matrix.Create(InputMatrix)

TempMatrix = TempMatrix.Inverse()

Return TempMatrix.CopyToArray()


End Function


'This just multiplies matrices
Public Shared Function MultiplyMatrix(ByVal InputMatrix1 As
Double(,), ByVal InputMatrix2 As Double(,)) As Double(,)

Dim TempMatrix1 As Matrix = Matrix.Create(InputMatrix1)
Dim TempMatrix2 As Matrix = Matrix.Create(InputMatrix2)

TempMatrix1 = TempMatrix1 * Tempmatrix2

Return TempMatrix1.CopyToArray()


End Function

'This solves a system of linear equations with a Least Squares
method.

Public Shared Function LeastSquares(ByVal YArray As Double(,),
ByVal XArray As Double(,)) As Double(,)

Dim XMatrix As Matrix = Matrix.Create(XArray)
Dim YMatrix As Matrix = Matrix.Create(YArray)

Dim OutputMatrix As Matrix = XMatrix.Solve(YMatrix)

OutputMatrix.Transpose()

Return OutputMatrix.CopyToArray

End Function

End Class


I hope this is useful to some people - I've found that the combination
of Mathnet and Excel DNA offers so many possibilities to expanding
Excel's capabilities quickly and easily. Obviously what I've posted
here can be done inside Excel, but when porting non-trivial VBA code
into VB.net (for example) this library might solve a few problems for
you.

Andrew

Govert van Drimmelen

unread,
Oct 28, 2010, 11:42:35 AM10/28/10
to Excel-Dna
Hi Andrew,

Thanks for posting the great example!

Math.Net Numerics is shaping up to be an awesome library - now that
dnAnalytics and Math.Net are being merged. I think it will end up
having a very strong performance story given its ability to use high-
performance native libraries like MKL from a nice managed interface.

I want to point to another open-source maths library I've noticed
recently, called Meta-Numerics: http://www.meta-numerics.net/ and
http://metanumerics.codeplex.com. It seems pretty strong on the
special functions and stats distributions. It has a single developer
who works for Microsoft.

I have not used either of these myself. But both seem like great
libraries to show what's possible with .NET, and why you'd want to use
Excel-Dna to supercharge Excel.

Cheers,
Govert


On Oct 27, 12:39 pm, ajwillshire <ajwillsh...@ieee.org> wrote:
> Hi everyone,
>
> In the interests of sharing, I thought I'd post a little about using
> Mathnet and Excel DNA together.
>
> Mathnet is an open source set of numerical methods but with the big
> advantage that the licence permits use in proprietary applications.
> I've only scratched the surface of it but the capabilities are
> immense.
>
> You can get the latest version of Mathnet.iridium here:
>
> http://www.mathdotnet.com/downloads/Iridium-2008-8-16-470.ashx?From=I...
Reply all
Reply to author
Forward
0 new messages