-
Notifications
You must be signed in to change notification settings - Fork 0
/
cg_preconditioner.m
62 lines (60 loc) · 1.59 KB
/
cg_preconditioner.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
function [P, Pb, Px] = cg_preconditioner(A, b, type)
%function [P, Pb, Px] = cg_preconditioner(A, b, type)
%
% This function is used to obtain function to use as preconditioner in
% conjugate gradient algorithm.
%
% Input:
%
% - A the coefficient matrix;
%
% - b the known terms vector;
%
% - type (integer between 0 and 2) to choose which preconditioner uses:
% = 0 for Jacobi preconditioner;
% = 1 for Incomplete LU preconditioner;
% = 2 for Incomplete Cholesky preconditioner.
%
% Output:
%
% - P (function(A, x)): computes matrix vector product with preconditioner;
%
% - Pb (real column vector): computes the known terms vector of
% preconditioned system;
%
% - Px (function(x)): allows to obtain the solution of initial system from
% the solution of preconditioned system.
%
%
%{
=======================================
Authors: Giuseppe Grieco, Mattia Sangermano
Date: 03-18-20
=======================================
%}
switch (type)
% Jacobi preconditioner %
case 0
n = size(A);
n = n(1);
C = sqrt(spdiags(diag(A), 0, n, n));
P = @(A, x) C \ (A * (C' \ x));
Pb = C \ b;
Px = @(x) C' \ x;
%{
% Incomplete LU preconditioner
case 1
[L, U] = ilu(A);
P = @(A, x) U \ (L \ (A * x));
Pb = (L * U) \ b;
Px = @(x) x;
%}
% Cholesky preconditioner %
case 1
L = ichol(A);
P = @(A, x) L \ (A * (L' \ x));
Pb = L \ b;
Px = @(x) L' \ x;
otherwise
error("Type not supported, type 0 for diagonal, 1 for incomplete LU or 2 for incomplete Cholesky")
end