subroutine Agen(Ag, job)
use work

implicit real(8) (a-h, o-z)

real(8) Ag(n,n)
character(20) job

real(8), allocatable :: b(:,:), c(:,:), d(:,:)

! Initialize:
ag=0D0

! Select:
Job_If: IF(job =='Ex1' .or. job =='ex1') THEN
! Ex.1 analitic

ag(1,1) =1
ag(1,2) =-dsqrt(2.D0)/2
ag(1,3)=0

ag(2,1) =ag(1,2)
ag(2,2) =1.5
ag(2,3) =-2/dsqrt(5.D0)

ag(3,1) =0
ag(3,2) =ag(2,3)
ag(3,3) =2.4
!

ELSEIF(job(1:3) =='3GL' .or. job(1:3) =='3gl') THEN

ag(1,:) =(/1, -1, 0/)

ag(2,:) =(/-1, 3, -2/)

ag(3,:) =(/0, -2, 6/)

ag =ag*3.D2

ELSEIF(job(1:4) =='Wilk' .or. job(1:4) =='wilk') THEN
! Wilkinson. Ex: wilk20. With 7th char ='a' => with "alpha"

alpha =1D-10

do i =1,n
ag(i,i) =dfloat(i)

if(i <=n-1) ag(i,i+1) =dfloat(n)
enddo

if(job(7:7) =='a') then
ag(n,1) = alpha
endif


ELSEIF(job(1:7) =='parlett') THEN
! Parlett 100x100

! j >i: 
! a(i,j) =0, except (1,2) and (1,100):
ag(1,2) =40.D0 / 102.D0
ag(1,100) =40.D0

! j =i:
do i =1,n
ag(i,i) =101.D0 -dfloat(i)
enddo

! j <i:
! ag(i,j) = (-1)**(i+j+1)*40.D0 / dfloat (i+j-2)
do i =2,n
do j =1, i-1

ag(i,j) = 40.D0 / dfloat (i+j-2)

if(mod(i+j+1, 2) /=0) then
ag(i,j) =-ag(i,j)
endif

enddo
enddo

if(job(8:9) =='_t' .or. job(8:9) =='_T') then
ag =transpose(ag)
endif


ELSEIF(job(1:3) =='Fib' .or. job(1:3) =='fib') THEN
! Fibonacci

f1 =3D0
f2 =5D0

!ag(1,1) =f1
!ag(1,2) =f2

do i = 1, n
	do j = 1, n

    if ( i == 1 ) then
        if ( j == 1 ) then
        ag(i,j) = f1
		else if ( j == 2 ) then
        ag(i,j) = f2
		else
        ag(i,j) = a(i,j-1) + a(i,j-2)
        end if
	else
        if ( j < n ) then
          ag(i,j) = a(i-1,j+1)
        else
          ag(i,j) = a(i-1,j) + a(i-1,j-1)
        end if
    endif

    enddo
enddo


ELSEIF(job =='gk316') THEN

do i=1,n
a(i,i) =1D0
enddo

do i=1,n
ag(i,n) =real(i)
enddo

do j=1,n
ag(n,j) =real(j)
enddo


ELSEIF(job =='Hanowa' .or. job =='hanowa') THEN

if ( mod ( n, 2 ) /= 0 ) then
write ( *, '(/, a)' ) 'Error: N must be even!'
write ( 1, '(/, a)' ) 'Error: N must be even!'
call EXIT
endif

alpha =17D0

do i = 1, n
	do j = 1, n

    if ( i == j ) then
    ag(i,j) = alpha
    
	else if ( i <= n/2 .and. j == i+n/2 ) then
    ag(i,j) = - real ( i )
    
	else if ( i > n/2 .and. j == i-n/2 ) then
    ag(i,j) = real ( i - n / 2 )
    
	else
    ag(i,j) = 0.0E+00
    endif

    enddo
enddo


ELSEIF(job(1:4) =='Ruti' .or. job(1:4) =='ruti') THEN
! Rutihauser n =6:
!    14 14  6  1  0  0
!    14 20 15  6  1  0
!     6 15 20 15  6  1
!     1  6 15 20 15  6
!     0  1  6 15 20 14
!     0  0  1  6 14 14

do i = 1, n

    if ( 1 <= i-3 ) then
      ag(i,i-3) = 1.0E+00
    end if

    if ( 1 <= i-2 ) then
      ag(i,i-2) = 6.0E+00
    end if

    if ( 1 <= i-1 ) then
      ag(i,i-1) = 15.0E+00
    end if

    ag(i,i) = 20.0E+00

    if ( i+1 <= n ) then
      ag(i,i+1) = 15.0E+00
    end if

    if ( i+2 <= n ) then
      ag(i,i+2) = 6.0E+00
    end if

    if ( i+3 <= n ) then
      ag(i,i+3) = 1.0E+00
    end if


end do

  ag(1,1:2) = 14.D0
  ag(2,1) = 14.D0

  ag(n,n-1:n) = 14.D0
  ag(n-1,n) = 14.D0

ELSEIF(job =='Lotkin' .or. job =='lotkin') THEN
a(1, 1:n) =1

do i=2,n
	do j=1,n
	a(i,j) =1D0/dfloat(i+j-1)
	enddo
enddo

ELSEIF(job(1:5) =='frank' .or. job(1:5) =='Frank') THEN
! Frank:
i_do: do i =1,n
ag(i,i) =dfloat(n-i+1)
if(i>=2) ag(i,i-1) =ag(i,i)

j_do: do j =i+1,n
ag(i,j) =dfloat(n -(j-1))

enddo j_do
enddo i_do

if(job(6:6) =='e') then
eps =1D-10
a(1,n) =a(1,n)+eps
endif

ELSEIF(job =='ebe16') THEN
allocate(c(4,4), b(8,8))
c(1,1) =-2; c(1,2:4) =2;
c(2,1) =-3; c(2,2) =3; c(2,3:4) =2;
c(3,1) =-2; c(3,2) =0; c(3,3) =4; c(3,4) =2;
c(4,1) =-1; c(4,2:3) =0; c(4,4) =5

b(1:4,1:4) =5*c; b(1:4,5:8) =-c;
b(5:8,1:4) =5*c; b(5:8,5:8) =c;

ag(1:8,1:8) =b; ag(1:8,9:16) =2*b;
ag(9:16,1:8) =4*b; ag(9:16,9:16) =3*b;

deallocate(c, b)

ELSEIF(job =='ebe40') THEN
allocate(d(5,5), c(10,10), b(20,20))
d =0.
d(1:5,1) =-1; d(1,2) =1.; d(2,3) =1.; d(3,4) =1.; d(4,5) =1.;

c(1:5,1:5) =4*d; c(1:5,6:10) =3*d;
c(6:10,1:5) =-2*d; c(6:10,6:10) =-d;

b(1:10,1:10) =c; b(1:10,11:20) =2*c;
b(11:20,1:10) =4*c; b(11:20,11:20) =3*c;

ag(1:20,1:20) =8*b; ag(1:20, 21:40) =4*b;
ag(21:40,1:20) =-5*b; ag(21:40, 21:40) =-b;

deallocate(d, c, b)

ELSEIF(job(1:2) =='w-') THEN
ag =0
n1 =(n-1)/2		! n = impar

! Diagonal:
do i =1, n+1
ag(i,i) =n1+1-i
enddo

do i =n1+2, 2*n1+1
ag(i,i) =n1+1 -i
enddo

! Supra-diagonal:
do i=1,n-1
ag(i,i+1) =1
enddo

! Sub-diagonal:
do i=2,n
ag(i,i-1) =1
enddo

ELSEIF(job(1:6) =='Combin' .or. job(1:6) =='combin') THEN
a1 =2; a2 =3;
aa =a1+a2

ag =a2
do i=1,n
ag(i,i) =aa
enddo

ELSEIF(job(1:9) =='Circulant' .or. job(1:9) =='circulant') THEN
! X ={1,2,...,n}
! K = 1 + mod ( J-I, N )
! A(I,J) = K

do i=1,n
	do j=1,n
	i_mod =mod(j-i, n)
	k =1 +i_mod
	if(i_mod <0) k =k +n
	
	ag(i,j) =dble(k)
	enddo
enddo


ELSEIF(job(1:7) =='Clement' .or. job(1:7) =='clement') THEN
! ag =0D0

  do i = 1, n
    do j = 1, n
      if ( j == i+1 ) then
        ag(i,j) = real ( i )
      else if ( j == i-1 ) then
        ag(i,j) = real ( n - j )
      else
        ag(i,j) = 0.0E+00
      end if
    end do
  end do

ELSE
! None

ENDIF Job_If

END

!==============================================
!* Wilkinson
!
!  Ex. n=20:
!
!    1 20  .  .  .  .  .  . 
!    .  2 20  .  .  .  .  . 
!    .  .  3 20  .  .  .  . 
!    .  .  .  .  .  .  .  . 
!    .  .  .  .  .  18 20  .
!    .  .  .  .  .  .  19 20
!  ALPHA.  .  .  .  .  .  20
!
!    If ALPHA = 1.0E-10, there are 6 real eigenvalues, and 14 complex
!    eigenvalues with considerable imaginary parts.
!
!  Reference:
!
!    Joan Westlake,
!    Test Matrix A40,
!    A Handbook of Numerical Matrix Inversion and Solution of Linear Equations,
!    John Wiley, 1968.
!
!    J H Wilkinson,
!    Rounding Errors in Algebraic Processes,
!    Prentice Hall, 1963,
!    page 138.
!
! ============================================
! Parlett matrix.
!
! Ex. n = 100
!
!    100.00    0.39    0       0       0     ...   40.00
!     40.00   99.00    0       0       0     ...    0
!    -20.00   13.33   98.00    0       0     ...    0
!     13.33  -10.00    8.00   97.00    0     ...    0
!    -10.00    8.00   -6.67    5.71   96.00  ...    0
!     ...     ...     ...     ...     ...    ...   ...
!      0.40   -0.40    0.39   -0.39    0.38  ...    1.00
!
!
!  Reference:
!
!    Robert Gregory and David Karney,
!    Example 5.19,
!    A Collection of Matrices for Testing Computational Algorithms,
!    Wiley, New York, 1969, page 99, QA263 G862.
!
!    Joan Westlake,
!    Test Matrix A32,
!    A Handbook of Numerical Matrix Inversion and Solution of Linear Equations,
!    John Wiley, 1968.
