C
C Program SMPOWER to compute matrix SM2, which is POWER-th power of
C input symetric matrix SM1.
C
C Version: 5.50
C Date: 2001, May 29
C
C Coded by Petr Bulant
C     Department of Geophysics, Charles University Prague,
C     Ke Karlovu 3, 121 16 Praha 2, Czech Republic,
C     E-mail: bulant@seis.karlov.mff.cuni.cz
C
C.......................................................................
C                                                    
C Description of data files:
C
C Input data read from the standard input device (*):
C     The data are read by the list directed input (free format) and
C     consist of a single string 'SEP':
C     'SEP'...String in apostrophes containing the name of the input
C             SEP parameter or history file with the input data.
C     No default, 'SEP' must be specified and cannot be blank.
C
C                                                     
C Input data file 'SEP':
C     File 'SEP' has the form of the SEP
C     parameter file.  The parameters, which do not differ from their
C     defaults, need not be specified in file 'SEP'.
C Data specifying dimensions of the matrices:
C     M1='string'... Name of the file containing a single integer number
C             specifying the number of rows (and columns) of symmetric
C             matrices A and R.
C             Default: M1=' ' means that the number is 1.
C Filenames of the files with the matrices:
C     SM1='string' ... Name of the input file containing symmetric
C             matrix SM1.
C             No default, 'SM1' must be specified and cannot be blank.
C     SM2='string' ... Name of the output file containing symmetric
C             matrix SM2=SM1**POWER.
C             No default, 'SM2' must be specified and cannot be blank.
C     For general description of the files with matrices refer to file
C     forms.htm.
C Form of the files with matrices:
C     FORMM='string' ... Form of the files with matrices. Allowed values
C             are FORMM='formatted' and FORMM='unformatted'. If the form
C             differs for input and for output files, FORMMR and FORMMW
C             should be used instead of FORMM.
C             Default: FORMM='formatted'
C     FORMMR='string' ... Form of the files with matrices to be read.
C             Default: FORMMR=FORMM
C     FORMMW='string' ... Form of the files with matrices to be written.
C             Default: FORMMW=FORMM
C Data specifying the power:
C     POWER=real ... Power of the matrix SM1.
C             Default: POWER=1.
C
C-----------------------------------------------------------------------
C Subroutines and external functions required:
      EXTERNAL SMBLO,SMPOW,IND,ERROR,RSEP1,RSEP3T,RSEP3R,RMAT,WMAT,EIGEN
      INTEGER IND
C     SMBLO,SMPOW,IND ... This file.
C     ERROR ... File error.for.
C     RSEP1,RSEP3T,RSEP3R ...
C             File sep.for.
C     RMAT,WMAT ... File forms.for.
C     EIGEN ... eigennr.for.
C
C Common block /RAMC/:
      INCLUDE 'ram.inc'
C     ram.inc
      INTEGER IRAM(MRAM)
      EQUIVALENCE (IRAM,RAM)
C
      CHARACTER*80 FILSEP,FILE1,FILE2
      INTEGER M1,N,NN,NB,LU1,I1,I2,I3,I4,IBMI,IBMA
      PARAMETER (LU1=1)
      REAL POWER
C
C-----------------------------------------------------------------------
C
C     Reading a name of the file with the input data:
      WRITE(*,'(A)') '+SMPOWER: Enter input filename: '
      FILSEP=' '
      READ(*,*) FILSEP
C
C     Reading all the data from the SEP file into the memory:
      IF (FILSEP.NE.' ') THEN
        CALL RSEP1(LU1,FILSEP)
      ELSE
C       SMPOWER-01
        CALL ERROR('SMPOWER-01: SEP file not given')
      ENDIF
C
C     Reading the dimension of the matrices:
      CALL RSEP3T('M1',FILE1,' ')
      IF (FILE1.EQ.' ') THEN
        M1=1
      ELSE
        OPEN(LU1,FILE=FILE1,STATUS='OLD')
        READ(LU1,*) M1
        CLOSE(LU1)
      ENDIF
      N=M1
      NN=M1*(M1+1)/2
      MAXRAM=MRAM-2*N
      IF (3*NN+N*N+N+N+1+N+N.GT.MRAM) THEN
C       SMPOWER-02
        CALL ERROR('SMPOWER-02: Small dimension MRAM of array RAM')
C       If possible, enlarge the dimension MRAM of array RAM in include
C       file ram.inc.
      ENDIF
C
C     Reading the names of the files with the matrices:
      CALL RSEP3T('SM1',FILE1,' ')
      CALL RSEP3T('SM2',FILE2,' ')
      IF (FILE1.EQ.' ') THEN
C       SMPOWER-03
        CALL ERROR('SMPOWER-03: Input file with matrix SM1 not given')
      ENDIF
      IF (FILE2.EQ.' ') THEN
C       SMPOWER-04
        CALL ERROR('SMPOWER-04: Output file with matrix SM2 not given')
      ENDIF
C
C     Reading input matrix:
      CALL RMAT(LU1,FILE1,M1,0,RAM(MAXRAM-NN+1))
C     Reading the power:
      CALL RSEP3R('POWER',POWER,1.)
C
      WRITE(*,'(A)') '+SMPOWER: Working...            '
C
C     Identification of blocks:
      CALL SMBLO(RAM(MAXRAM-NN+1),N,NN,NB,IRAM(MAXRAM-NN-N))
C
C     Preparing array for results:
      DO 5, I1=MAXRAM-2*NN-N,MAXRAM-NN-N-1
        RAM(I1)=0.
   5  CONTINUE
C
C     Computing the power:
      DO 20, I1=1,NB
C       Current block:
        IBMI=IRAM(MAXRAM-NN-N+I1-1)+1
        IBMA=IRAM(MAXRAM-NN-N+I1)
C       Moving the block to RAM(1):
        I4=0
        DO 10, I2=IBMI,IBMA
          DO 8, I3=IBMI,I2
            I4=I4+1
            RAM(I4)=RAM(MAXRAM-NN+IND(I3,I2))
   8      CONTINUE
  10    CONTINUE
        I2=IBMA-IBMI+1
        I3=I2*(I2+1)/2
C       Computing the power:
        CALL SMPOW(RAM,I2,I3,POWER)
C       Moving the block to RAM(MAXRAM-NN-N-1-NN+1):
        I4=0
        DO 14, I2=IBMI,IBMA
          DO 12, I3=IBMI,I2
            I4=I4+1
            RAM(MAXRAM-2*NN-N-1+IND(I3,I2))=RAM(I4)
  12      CONTINUE
  14    CONTINUE
  20  CONTINUE
C
C     Writing output matrix SM2:
      IF (FILE2.NE.' ') THEN
        CALL WMAT(LU1,FILE2,M1,0,RAM(MAXRAM-2*NN-N))
      ENDIF
      WRITE(*,'(A)') '+SMPOWER: Done.                 '
C
      STOP
      END
C
C=======================================================================
C
      SUBROUTINE SMPOW(SM,N,NN,POWER)
C
C=======================================================================
C     Computes the POWER-th power of symmetric matrix SM.
      INTEGER N,NN
      REAL SM(NN+N*N+N),POWER
C     Input: SM ... input symmetric matrix
C            N  ... number of rows (and columns) of the matrix
C            NN ... N*(N+1)/2
C            POWER ... the power
C     Output: SM ... the input matrix powered to POWER
      INTEGER I,J,I1,I2,I3
      REAL AUX,SMMAX
C-----------------------------------------------------------------------
      CALL EIGEN(SM,SM(NN+1),N,0)
      SMMAX=SM(1)
      DO 21 I=2,N
        SMMAX=AMAX1(SM(I*(I+1)/2),SMMAX)
   21 CONTINUE
      DO 22 I=1,N
        AUX=SM(I*(I+1)/2)
        IF(AUX.LE.0.) THEN
          IF(ABS(AUX).GT.0.000001*SMMAX
     *       .OR.(SMMAX.EQ.0..AND.POWER.LE.0.)) THEN
C           SMPOWER-05
            CALL ERROR('SMPOWER-05: Eigenvalue not positive')
          ENDIF
          IF(POWER.LE.0.) THEN
            AUX=0.000001*SMMAX
          ELSE
            AUX=0.
          END IF
        END IF
        SM(NN+N*N+I)=AUX**POWER
   22 CONTINUE
      DO 25 I=1,NN
        SM(I)=0.
   25 CONTINUE
      DO 28 I3=1,N
        AUX=SM(NN+N*N+I3)
        J=NN+N*(I3-1)
        I=0
        DO 27, I2=J+1,J+N
          DO 26, I1=J+1,I2
            I=I+1
            SM(I)=SM(I)+SM(I1)*SM(I2)*AUX
   26     CONTINUE
   27   CONTINUE
   28 CONTINUE
      RETURN
      END
C
C=======================================================================
C
      SUBROUTINE SMBLO(SM,N,NN,NB,IB)
C
C=======================================================================
C     Subroutine to find blocks of a symmetric matrix
      INTEGER N,NN,NB,IB(0:N)
      REAL SM(NN)
C     Input: SM ... symmetric matrix
C            N  ... number of rows (and columns) of the matrix
C            NN ... N*(N+1)/2
C     Output: NB ... number of blocks of the matrix
C             IB ... array with numbers of lines (and rows), at which
C                    the blocks finish
      INTEGER I1,I2
      EXTERNAL IND
      INTEGER IND
C-----------------------------------------------------------------------
C     Initiating first block:
      NB=1
      IB(0)=0
      IB(1)=1
      I1=0
  10  CONTINUE
C       Loop for lines of the matrix:
        I1=I1+1
        DO 20, I2=N,IB(NB)+1,-1
C         Loop for rows of the line I1:
          IF (SM(IND(I1,I2)).NE.0.) THEN
C           The block is larger:
            IB(NB)=I2
            GOTO 21
          ENDIF
  20    CONTINUE
  21    CONTINUE
        IF (IB(NB).EQ.N) THEN
C         End of the search for blocks.
          RETURN
        ENDIF
        IF (IB(NB).EQ.I1) THEN
C         Block finished, continuing with the next block.
          NB=NB+1
          IB(NB)=I1+1
        ENDIF
      GOTO 10
      END
C=======================================================================
      INTEGER FUNCTION IND(I,J)
      INTEGER I,J
C     I ... Index of a line.
C     J ... Index of a row.
      IND=J*(J-1)/2+I
      RETURN
      END
C
C=======================================================================
C
      INCLUDE 'error.for'
C     error.for
      INCLUDE 'sep.for'
C     sep.for
      INCLUDE 'forms.for'
C     forms.for
      INCLUDE 'length.for'
C     length.for
      INCLUDE 'eigennr.for'
C     eigennr.for
      INCLUDE 'indexx.for'
C     indexx.for
      INCLUDE 'tred2.for'
C     tred2.for
      INCLUDE 'tqli.for'
C     tqli.for
      INCLUDE 'pythag.for'
C     pythag.for
C
C=======================================================================
C