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