NULAPACK
NUmerical Linear Algebra PACKage
Loading...
Searching...
No Matches
dgttsv.f
Go to the documentation of this file.
1C ====================================================================
2C N U L A P A C K
3C U U L A P A C K
4C L L L A P A C K
5C A A A A P A C K
6C P P P P P A C K
7C A A A A A A C K
8C C C C C C C C K
9C K K K K K K K K
10C
11C This file is part of NULAPACK - NUmerical Linear Algebra PACKage
12C
13C Copyright (C) 2025 Saud Zahir
14C
15C NULAPACK is free software: you can redistribute it and/or modify
16C it under the terms of the GNU General Public License as published by
17C the Free Software Foundation, either version 3 of the License, or
18C (at your option) any later version.
19C
20C NULAPACK is distributed in the hope that it will be useful,
21C but WITHOUT ANY WARRANTY; without even the implied warranty of
22C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23C GNU General Public License for more details.
24C
25C You should have received a copy of the GNU General Public License
26C along with NULAPACK. If not, see <https://www.gnu.org/licenses/>.
27C
28C ====================================================================
29C DGTTSV - Thomas Algorithm for (tridiagonal system)
30C ====================================================================
31C Description:
32C ------------------------------------------------------------------
33C Direct solver for tridiagonal linear systems A * X = B using
34C the Thomas algorithm. A is supplied as a FULL N x N matrix in
35C flat row-major storage and is overwritten during computation.
36C
37C On output: X contains the solution vector.
38C
39C No pivoting is performed. Zero diagonal entries produce failure.
40C ====================================================================
41C Arguments:
42C ------------------------------------------------------------------
43C N : INTEGER -> matrix size (N x N)
44C A(*) : DOUBLE PRECISION -> flat row-major matrix A (modified)
45C B(N) : DOUBLE PRECISION -> right-hand side (modified)
46C X(N) : DOUBLE PRECISION -> solution vector (output)
47C INFO : INTEGER -> return code:
48C 0 = success
49C <0 = zero diagonal detected
50C ====================================================================
51 SUBROUTINE dgttsv(N, A, B, X, INFO)
52
53C I m p l i c i t T y p e s
54C ------------------------------------------------------------------
55 IMPLICIT NONE
56
57C D u m m y A r g u m e n t s
58C ------------------------------------------------------------------
59 INTEGER :: N, INFO
60 DOUBLE PRECISION :: A(*), B(N), X(N)
61
62C L o c a l V a r i a b l e s
63C ------------------------------------------------------------------
64 INTEGER :: I, INDEX_D, INDEX_L, INDEX_U
65 DOUBLE PRECISION :: FACT
66
67C I n i t i a l S t a t u s
68C ------------------------------------------------------------------
69 info = 0
70
71C F o r w a r d E l i m i n a t i o n
72C ------------------------------------------------------------------
73 DO i = 2, n
74
75C Compute indices in flat row-major layout
76 index_l = (i-1) * n + (i-1)
77 index_d = (i-2) * n + (i-1)
78 index_u = (i-2) * n + i
79
80C Check previous diagonal
81 IF (a(index_d) .EQ. 0.0d0) THEN
82 info = -(i-1)
83 RETURN
84 END IF
85
86 fact = a(index_l) / a(index_d)
87
88C Update diagonal A(I,I)
89 index_d = (i-1) * n + i
90 a(index_d) = a(index_d) - fact * a(index_u)
91
92C Update RHS B(I)
93 b(i) = b(i) - fact * b(i-1)
94
95C Check new diagonal
96 IF (a(index_d) .EQ. 0.0d0) THEN
97 info = -i
98 RETURN
99 END IF
100 END DO
101
102C B a c k w a r d S u b s t i t u t i o n
103C ------------------------------------------------------------------
104 index_d = (n-1) * n + n
105 x(n) = b(n) / a(index_d)
106
107 DO i = n-1, 1, -1
108 index_d = (i-1) * n + i
109 index_u = (i-1) * n + (i+1)
110 x(i) = (b(i) - a(index_u) * x(i+1)) / a(index_d)
111 END DO
112
113C S u c c e s s
114C ------------------------------------------------------------------
115 RETURN
116 END
subroutine dgttsv(n, a, b, x, info)
Definition dgttsv.f:52