xref: /phasta/svLS/GE.f (revision 712d3df0b59ebebaaeaea358162c8d2c043c6e08)
1*1e99f302SBen Matthews!     This software is Copyright (c) 2012-2015 The Regents of the
2*1e99f302SBen Matthews!     University of California. All Rights Reserved.
3*1e99f302SBen Matthews!
4*1e99f302SBen Matthews!     Permission to copy and modify this software and its documentation
5*1e99f302SBen Matthews!     for educational, research and non-profit purposes, without fee,
6*1e99f302SBen Matthews!     and without a written agreement is hereby granted, provided that
7*1e99f302SBen Matthews!     the above copyright notice, this paragraph and the following three
8*1e99f302SBen Matthews!     paragraphs appear in all copies.
9*1e99f302SBen Matthews!
10*1e99f302SBen Matthews!     Permission to make commercial use of this software may be obtained
11*1e99f302SBen Matthews!     by contacting:
12*1e99f302SBen Matthews!
13*1e99f302SBen Matthews!     Technology Transfer Office
14*1e99f302SBen Matthews!     9500 Gilman Drive, Mail Code 0910
15*1e99f302SBen Matthews!     University of California
16*1e99f302SBen Matthews!     La Jolla, CA 92093-0910
17*1e99f302SBen Matthews!     (858) 534-5815
18*1e99f302SBen Matthews!     invent@ucsd.edu
19*1e99f302SBen Matthews!
20*1e99f302SBen Matthews!     This software program and documentation are copyrighted by The
21*1e99f302SBen Matthews!     Regents of the University of California. The software program and
22*1e99f302SBen Matthews!     documentation are supplied "as is", without any accompanying
23*1e99f302SBen Matthews!     services from The Regents. The Regents does not warrant that the
24*1e99f302SBen Matthews!     operation of the program will be uninterrupted or error-free. The
25*1e99f302SBen Matthews!     end-user understands that the program was developed for research
26*1e99f302SBen Matthews!     purposes and is advised not to rely exclusively on the program for
27*1e99f302SBen Matthews!     any reason.
28*1e99f302SBen Matthews!
29*1e99f302SBen Matthews!     IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY
30*1e99f302SBen Matthews!     PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
31*1e99f302SBen Matthews!     DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS
32*1e99f302SBen Matthews!     SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
33*1e99f302SBen Matthews!     CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*1e99f302SBen Matthews!     THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
35*1e99f302SBen Matthews!     WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36*1e99f302SBen Matthews!     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
37*1e99f302SBen Matthews!     SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE
38*1e99f302SBen Matthews!     UNIVERSITY OF CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE
39*1e99f302SBen Matthews!     MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
40*1e99f302SBen Matthews
41*1e99f302SBen Matthews      SUBROUTINE GE (N, A, B)
42*1e99f302SBen Matthews
43*1e99f302SBen Matthews      IMPLICIT NONE
44*1e99f302SBen Matthews
45*1e99f302SBen Matthews      INTEGER, INTENT(IN) :: N
46*1e99f302SBen Matthews      REAL*8, INTENT(IN) :: A(N,N)
47*1e99f302SBen Matthews      REAL*8, INTENT(INOUT) :: B(N)
48*1e99f302SBen Matthews
49*1e99f302SBen Matthews      INTEGER m, ipv, i, j
50*1e99f302SBen Matthews      REAL*8 pivot, saveEl
51*1e99f302SBen Matthews      REAL*8, ALLOCATABLE :: C(:,:)
52*1e99f302SBen Matthews
53*1e99f302SBen Matthews      IF (N .EQ. 2) THEN
54*1e99f302SBen Matthews         pivot  = A(1,1)*A(2,2) - A(2,1)*A(1,2)
55*1e99f302SBen Matthews         saveEl = (B(1)*A(2,2) - B(2)*A(1,2))/pivot
56*1e99f302SBen Matthews         B(2)   = (B(2)*A(1,1) - B(1)*A(2,1))/pivot
57*1e99f302SBen Matthews         B(1)   = saveEl
58*1e99f302SBen Matthews         RETURN
59*1e99f302SBen Matthews      END IF
60*1e99f302SBen Matthews      ALLOCATE(C(N,N+1))
61*1e99f302SBen Matthews
62*1e99f302SBen Matthews      C(:N,:N) = A
63*1e99f302SBen Matthews      C(:,N+1) = B
64*1e99f302SBen Matthews
65*1e99f302SBen Matthews      DO m=1,N-1
66*1e99f302SBen Matthews         ipv = m
67*1e99f302SBen Matthews         pivot = ABS(C(m,m))
68*1e99f302SBen Matthews         DO i=m+1,N
69*1e99f302SBen Matthews            IF (ABS(C(i,m)) .GT. pivot) THEN
70*1e99f302SBen Matthews               ipv = i
71*1e99f302SBen Matthews               pivot = ABS(C(i,m))
72*1e99f302SBen Matthews            END IF
73*1e99f302SBen Matthews         END DO
74*1e99f302SBen Matthews         IF (pivot .LT. 2D0*EPSILON(pivot)) THEN
75*1e99f302SBen Matthews            PRINT *, 'Singular matrix'
76*1e99f302SBen Matthews            STOP
77*1e99f302SBen Matthews         END IF
78*1e99f302SBen Matthews         IF (ipv .NE. m) THEN
79*1e99f302SBen Matthews            DO j=m, N+1
80*1e99f302SBen Matthews               saveEl = C(m,j)
81*1e99f302SBen Matthews               C(m,j) = C(ipv,j)
82*1e99f302SBen Matthews               C(ipv,j) = saveEl
83*1e99f302SBen Matthews            END DO
84*1e99f302SBen Matthews            DO j=1, m-1
85*1e99f302SBen Matthews            END DO
86*1e99f302SBen Matthews         END IF
87*1e99f302SBen Matthews
88*1e99f302SBen Matthews         DO i=m+1,N
89*1e99f302SBen Matthews            saveEl = C(i,m)/C(m,m)
90*1e99f302SBen Matthews            C(i,m) = 0D0
91*1e99f302SBen Matthews            DO j=m+1,N+1
92*1e99f302SBen Matthews               C(i,j) = C(i,j) - saveEl*C(m,j)
93*1e99f302SBen Matthews            END DO
94*1e99f302SBen Matthews         END DO
95*1e99f302SBen Matthews      END DO
96*1e99f302SBen Matthews
97*1e99f302SBen Matthews      DO j=N,1,-1
98*1e99f302SBen Matthews         DO i=j+1,N
99*1e99f302SBen Matthews            C(j,N+1) = C(j,N+1) - C(j,i)*C(i,N+1)
100*1e99f302SBen Matthews         END DO
101*1e99f302SBen Matthews         C(j,N+1) = C(j,N+1)/C(j,j)
102*1e99f302SBen Matthews      END DO
103*1e99f302SBen Matthews
104*1e99f302SBen Matthews      B = C(:,N+1)
105*1e99f302SBen Matthews
106*1e99f302SBen Matthews      DEALLOCATE(C)
107*1e99f302SBen Matthews
108*1e99f302SBen Matthews      RETURN
109*1e99f302SBen Matthews      END SUBROUTINE GE
110