0% found this document useful (0 votes)
4 views1 page

Grasp

The document outlines an implementation of the Conjugate Gradient method for solving linear systems, utilizing sparse matrix-vector multiplication. It includes the initialization of residuals and iterative updates for the solution and search direction until convergence or a maximum number of iterations is reached. A helper function for sparse matrix-vector multiplication is also provided to facilitate the calculations.

Uploaded by

Le Fondateur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views1 page

Grasp

The document outlines an implementation of the Conjugate Gradient method for solving linear systems, utilizing sparse matrix-vector multiplication. It includes the initialization of residuals and iterative updates for the solution and search direction until convergence or a maximum number of iterations is reached. A helper function for sparse matrix-vector multiplication is also provided to facilitate the calculations.

Uploaded by

Le Fondateur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Dim alpha As Double, beta As Double

Dim rr As Double, rr_new As Double, pAp As Double

' r = F - K*U (initial residual)


SparseMatrixVectorMultiply sparse, U, r)
For i = 1 To n
r(i) = F(i) - r(i)
p(i) = r(i)
Next i

rr = DotProduct(r, r)

' Iterative solution


For iter = 1 To maxIter
' Ap = A*p
SparseMatrixVectorMultiply sparse, p, Ap)

' alpha = r*r / p*Ap


pAp = DotProduct(p, Ap)
If Abs(pAp) < 1E-15 Then Exit For
alpha = rr / pAp

' Update solution and residual


For i = 1 To n
U(i) = U(i) + alpha * p(i)
r(i) = r(i) - alpha * Ap(i)
Next i

' Check convergence


rr_new = DotProduct(r, r)
If Sqr(rr_new) < tol Then Exit For

' Update search direction


beta = rr_new / rr
For i = 1 To n
p(i) = r(i) + beta * p(i)
Next i

rr = rr_new
Next iter

If iter >= maxIter Then


MsgBox "Conjugate Gradient did not converge in " & maxIter & "
iterations", vbExclamation
End If
End Sub

' Helper function for sparse matrix-vector multiplication


Sub SparseMatrixVectorMultiply(sparse As SparseMatrix, x() As Double, ByRef
y() As Double)
Dim i As Long
For i = 1 To sparse.size
y(sparse.entries(i).row) = y(sparse.entries(i).row) + _
sparse.entries(i).value *
x(sparse.entries(i).col)
Next i
End Sub

P a g e 36 | 57

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy