|
|
◆ ZCocr_s()
| Sub ZCocr_s |
( |
N As |
Long, |
|
|
Matvec As |
LongPtr, |
|
|
Psolve As |
LongPtr, |
|
|
ChkConv As |
LongPtr, |
|
|
B() As |
Complex, |
|
|
X() As |
Complex, |
|
|
Optional Info As |
Long, |
|
|
Optional Iter As |
Long, |
|
|
Optional Res As |
Double, |
|
|
Optional Mode As |
Long = 0, |
|
|
Optional MaxIter As |
Long = 500 |
|
) |
| |
Solution of linear system Ax = b using conjugate orthogonal conjugate residual (COCR) method (Complex symmetric matrices) (Subroutine version)
- Purpose
- This routine solves the linear system Ax = b with complex symmetric coefficient matrix using the conjugate orthogonal conjugate residual (COCR) method with preconditioning.
- Parameters
-
| [in] | N | Dimension of the matrix A. (N >= 0) (if N = 0, returns without computation) |
| [in] | Matvec | User supplied subroutine which calculates the product of matrix A and vector x as follows.
Sub Matvec(N As Long, X() As Complex, Y() As Complex)
Compute A*x, and return in Y().
End Sub
|
| [in] | Psolve | User supplied subroutine which perform the preconditioner solve routine for the linear system M*x = b as follows, where M is a preconditioner metrix.
Sub Psolve(N As Long, B() As Complex, X() As Complex)
Solve M*x = b, and return solution in X().
End Sub
|
| [in] | ChkConv | User supplied subroutine which is called on every iteration for the convergence test as follows, where X() is the current approximate solution, Res is the current residual norm norm(b - A*x), and Iter is the current number of iterations. This routine can also be used to output the intermediate results.
Sub ChkConv(N As Long, X() As Complex, Res As Double, Iter As Long, IChk As Long)
Set IChk = 1 if converged. Otherwise, set IChk = 0.
End Sub
|
| [in] | B() | Array B(LB - 1) (LB >= N)
Right hand side vector b. |
| [in,out] | X() | Array X(LX - 1) (LX >= N)
[in] Initial guess of solution.
[out] Obtained approximate solution. |
| [out] | Info | (Optional)
= 0: Successful exit
< 0: The (-Info)-th argument is invalid.
= 1: (Warning) Matrix A is not positive definite (computation continued).
= 2: (Warning) Preconditioner matrix M is not positive definite (computation continued).
= 11: Maximum number of iterations exceeded.
= 12: Matrix A is singular (zero diagonal element). |
| [out] | Iter | (Optional)
Actual number of iterations performed for convergence. |
| [out] | Res | (Optional)
Final residual norm value norm(b - A*x). |
| [in] | Mode | (Optional)
The residual norm returned via parameters chkcnv and res can be specified. (default = 0)
= 0: norm(b - A*x) is returned. However, one additional matvec operation per one iteration is required.
= 1: M^(-1)*norm(b - A*x) is returned. |
| [in] | MaxIter | (Optional)
Maximum number of iterations. (MaxIter > 0) (default = 500) |
- Example Program
- Solve the system of linear equations Ax = B, where
( 0.31+0.77i 0.25+0.23i -0.81-0.83i )
A = ( 0.25+0.23i 0.26-0.26i -0.58-0.08i )
( -0.81-0.83i -0.56-0.08i 2.09+0.6i )
( 0.3941-1.2711i )
B = ( 0.0036-0.72i )
( 0.3628+1.9977i )
Const N = 3, Nnz = N * N, Tol = 0.0000000001
Dim A(Nnz - 1) As Complex, Ia(N) As Long, Ja(Nnz - 1) As Long
Sub Matvec(N As Long, X() As Complex, Y() As Complex)
End Sub
Sub Psolve(N As Long, B() As Complex, X() As Complex)
Dim I As Long
For I = 0 To N - 1
X(I) = B(I)
Next
End Sub
Sub ChkConv(N As Long, X() As Complex, Res As Double, Iter As Long, IChk As Long)
If Res < Tol Then
IChk = 1
Else
IChk = 0
End If
End Sub
Sub Ex_ZCocr_s()
Dim B(N - 1) As Complex, X(N - 1) As Complex
Dim Iter As Long, Res As Double, Info As Long
A(0) = Cmplx(0.31, 0.77): A(1) = Cmplx(0.25, 0.23): A(2) = Cmplx(0.26, -0.26): A(3) = Cmplx(-0.81, -0.83): A(4) = Cmplx(-0.56, -0.08): A(5) = Cmplx(2.09, 0.6)
Ia(0) = 0: Ia(1) = 1: Ia(2) = 3: Ia(3) = 6
Ja(0) = 0: Ja(1) = 0: Ja(2) = 1: Ja(3) = 0: Ja(4) = 1: Ja(5) = 2
B(0) = Cmplx(0.3941, -1.2711): B(1) = Cmplx(0.0036, -0.72): B(2) = Cmplx(0.3628, 1.9977)
Call ZCocr_s(N, AddressOf Matvec, AddressOf Psolve, AddressOf ChkConv, B(), X(), Info, Iter, Res)
Debug.Print "X ="
Debug.Print "(" + CStr( Creal(X(0))) + "," + CStr( Cimag(X(0))) + ")"
Debug.Print "(" + CStr( Creal(X(1))) + "," + CStr( Cimag(X(1))) + ")"
Debug.Print "(" + CStr( Creal(X(2))) + "," + CStr( Cimag(X(2))) + ")"
Debug.Print "Iter =" + Str(Iter) + ", Res =" + Str(Res) + ", Info =" + Str(Info)
End Sub
Function Cmplx(R As Double, Optional I As Double=0) As Complex Building complex number
Function Cimag(A As Complex) As Double Imaginary part of complex number
Function Creal(A As Complex) As Double Real part of complex number
Sub SsrZusmv(Uplo As String, N As Long, Alpha As Complex, Val() As Complex, Rowptr() As Long, Colind() As Long, X() As Complex, Beta As Complex, Y() As Complex, Optional Info As Long, Optional Base As Long=-1, Optional IncX As Long=1, Optional IncY As Long=1) y <- αAx + βy (CSR) (Complex symmetric matrix)
Sub ZCocr_s(N As Long, Matvec As LongPtr, Psolve As LongPtr, ChkConv As LongPtr, B() As Complex, X() As Complex, Optional Info As Long, Optional Iter As Long, Optional Res As Double, Optional Mode As Long=0, Optional MaxIter As Long=500) Solution of linear system Ax = b using conjugate orthogonal conjugate residual (COCR) method (Complex...
- Example Results
X =
(-0.820000000000007,-0.940000000000006)
(0.739999999999996,0.2)
(0.480000000000016,0.210000000000004)
Iter = 3, Res = 5.1647781609597E-14, Info = 0
|