accord-net / framework

Machine learning, computer vision, statistics and general scientific computing for .NET
http://accord-framework.net
GNU Lesser General Public License v2.1
4.48k stars 1.99k forks source link

Eigenvalues for Sparse Matrices #1079

Closed ajos6183 closed 6 years ago

ajos6183 commented 6 years ago

What would you like to submit? (put an 'x' inside the bracket that applies)

Issue description

Are there any plans of supporting the calculation of eigenvalues for sparse matrices? In my case, i have a generalized eigenvalue problem and i am trying to find the lowest positive eigenvalue.

Given your experience, do you know of any such .Net package?

cesarsouza commented 6 years ago

Hi @ajos6183,

Thanks for opening the issue! However, unfortunately there are currently no plans to add support for calculating eigenvalues for sparse matrices at the moment.

Have you tried to look for it in Math.NET? I couldn't confirm from their documentation that they support but I couldn't rule out that they doesn't either.

If you are willing to use a commercial software package, another alternative may be NMath. It seems that can perform sparse matrix factorizations and therefore may support calculating eigenvalues and eigenvalues as well.

Regards, Cesar

ajos6183 commented 6 years ago

Thanks Cesar for your response! I tried Math.Net but it doesn't have the option to calculate the first few eigenvalues. It calculates all of them which is really slow. I wrote a really short function to accomplish what i was after. Thanks again!

cesarsouza commented 6 years ago

Hi @ajos6183, cool! I am glad to hear you have found a solution! Wouldn't you be willing to contribute it to the framework as a PR (or at least share it here to help others who would also be interested in the solution)? 😄

xieliaing commented 6 years ago

I am curious about Sparse matrix support as well, not just sparse matrix SVD. Is it possible to use the new Tensor from .NET? Looks like the Tensor: https://blogs.msdn.microsoft.com/dotnet/2017/11/15/introducing-tensor-for-multi-dimensional-machine-learning-and-ai-data/

ajos6183 commented 6 years ago

@xieliaing, for your information, I used a really fast library called CSparse.Net available for free on Github. I found it very good.

@cesarsouza, sorry for the late response. This is my iterative function to solve a generalized eigenvalue problem (buckling in this case). I haven't spent any time making it general but i hope it is of some value to you.

` Friend Function InverseIteration(Ke As CSparse.Double.SparseMatrix, Kg As CSparse.Double.SparseMatrix, tolerance As Double, LoadCaseName As String, FrameName As String) As Tuple(Of Double(), Double) Dim N = Ke.ColumnCount Dim SC = CSparse.Double.Factorization.SparseCholesky.Create(Ke, CSparse.ColumnOrdering.MinimumDegreeAtPlusA) Dim Err As Double = 10 Dim EigenValueEstimate As Double = 0.0 Dim EigenVectorEstimate = CSparse.Double.Vector.Create(N, 1) Dim y1 = CSparse.Double.Vector.Create(N, 0) Dim y2 = CSparse.Double.Vector.Create(N, 0) Dim TotalIterations = 0 Dim MaxIterations = 1000 While Err > tolerance And TotalIterations <= MaxIterations TotalIterations = TotalIterations + 1 Kg.Multiply(-1, EigenVectorEstimate, 0, y1) SC.Solve(y1, EigenVectorEstimate) Kg.Multiply(-1, EigenVectorEstimate, 0, y2) Dim norm = CSparse.Double.Vector.DotProduct(EigenVectorEstimate, y2) CSparse.Double.Vector.Scale(1.0 / Math.Sqrt(Math.Abs(norm)), EigenVectorEstimate) Ke.Multiply(1, EigenVectorEstimate, 0, y2) Dim NewEigenValueEstimate = CSparse.Double.Vector.DotProduct(y2, EigenVectorEstimate) * (norm / Math.Abs(norm)) Err = Math.Abs((NewEigenValueEstimate - EigenValueEstimate) / EigenValueEstimate) EigenValueEstimate = NewEigenValueEstimate If Double.IsNaN(NewEigenValueEstimate) Then If Kg.NonZerosCount = 0 Then EigenValueEstimate = 1000000 Err = tolerance Else System.Windows.Forms.MessageBox.Show("Buckling Analysis did not converge. The Buckling Analysis was terminated and the results are not reliable!", "Buckling Analysis Divergance", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error) End If End If End While

    If TotalIterations >= MaxIterations Then
        System.Windows.Forms.MessageBox.Show("The buckling analysis of '" + FrameName + "' under load case '" + LoadCaseName + "' did not converge after " + MaxIterations.ToString +
                                             " iterations. The analysis was terminated and the results may not be reliable!", "Buckling Analysis Divergance",
                                             System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning)
    End If

    Return Tuple.Create(Of Double(), Double)(EigenVectorEstimate, EigenValueEstimate)
End Function

`