我寫了這個功能,我試圖用Parallel.For
或其他方式來加速它。但是,當我用Parallel.For
替換其中一個循環時,它會給出錯誤的結果。使用Parallel.For加速功能
Public Function InverseFromUpperTriangular(U As Matrix) As Matrix
Dim n As Integer = U.RowCount
Dim Y As Matrix = MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.Dense(n, n)
Dim S As Matrix = MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.Dense(n, n)
For j = n - 1 To 0 Step -1
S(j, j) = 1.0/U(j, j)
For i = j To 0 Step -1
Y(i, j) = (S(i, j) - U.Row(i).SubVector(i, n - i) * Y.Column(j).SubVector(i, n - i))/U(i, i)
Y(j, i) = Y(i, j)
Next
Next
Return Y
End Function
編輯
這是我與Parallel.For
Public Function InverseFromUpperTriangular(U As Matrix) As Matrix
Dim n As Integer = U.RowCount
Dim Y As Matrix = MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.Dense(n, n)
Dim S As Matrix = MathNet.Numerics.LinearAlgebra.Double.Matrix.Build.Dense(n, n)
Parallel.For(0, n, Sub(ii)
Dim j = n - 1 - ii
S(j, j) = 1.0/U(j, j)
For i = j To 0 Step -1
Y(i, j) = (S(i, j) - U.Row(i).SubVector(i, n - i) * Y.Column(j).SubVector(i, n - i))/U(i, i)
Y(j, i) = Y(i, j)
Next
End Sub)
Return Y
End Function
C#或VB.Net?除此之外,谷歌並不是色情的,你可以找到大量的例子。看看這個:https://msdn.microsoft.com/de-de/library/dd460713(v=vs.110).aspx – muffi
你的水貨版本的代碼是什麼樣的? –
我對線性代數或矩陣不太熟悉,所以我可能會遺漏一些微妙之處,但循環內容似乎相互依賴(很多共享變量,尤其是'S'和'Y'') )。它看起來並不是我可以並行的。 –