      FUNCTION ECHECK(N,T,TRI,CUBEL,MAXEC,NEWEC)
 
C     Equivalence check for POLPREC

      INTEGER N, T(30,3), TRI(30*10000,3), MAXEC
      REAL CUBEL(10000)
      LOGICAL NEWEC

      INTEGER L, LKM, RLKM, K, I
      LOGICAL ROWOK, NXTPRM
      INTEGER PT(30,3) 
C      INTEGER TRI(15*MAXEC,3)
C     X(MAXEC,15)

      EXTERNAL NXTPRM
      
      INTEGER TROW, TRIROW, TA, TB, TC, TRIA, TRIB, TRIC
      INTEGER TNB(MAXEC+1), PERM(6)
      INTEGER PPAIRS(15,2), NP(15,2), PERMNB(15), PAIRNB(6,6)
 
      DATA PAIRNB/0,1,2,3,4,5,1,0,6,7,8,9,2,6,0,10,11,12,
     *     3,7,10,0,13,14,4,8,11,13,0,15,5,9,12,14,15,0/

      DATA NP/ 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 2,
     *     3, 4, 5, 6, 3, 4, 5, 6, 4, 5, 6, 5, 6, 6/

      LKM=MAXEC
      RLKM=30*MAXEC

C     Count the doubles

C      CALL TRIPLE(CUBEL,MAXEC,TRI,T,TNB)

C      WRITE(*,*) 'double ok'
C      WRITE(*,*) DNB

C     All possible permutations:

 100  DO 101 K=1,6
         PERM(K)=K
 101  CONTINUE

C     Take the next permutation (or end)
 410  IF (.NOT. NXTPRM(PERM,6)) THEN
         GOTO 9999
      END IF

      DO 430 K=1,N
         DO 420 I=1,2
            PPAIRS(K,I)=NP(K,I)
 420     CONTINUE
 430  CONTINUE

 440  DO 450 K=1,N
         PPAIRS(K,1)=PERM(PPAIRS(K,1))
         PPAIRS(K,2)=PERM(PPAIRS(K,2))
 450  CONTINUE		

 500  DO 550 K=1,N
         PERMNB(K)=PAIRNB(PPAIRS(K,1), PPAIRS(K,2))
 550  CONTINUE

C Label the elements in T with new labels

 600  DO 650 K=1,(N+FLOOR((CUBEL(MAXEC+1)+2)/3))
         DO 640 I=1,3
	    PT(K,I)=PERMNB(T(K,I))
 640     CONTINUE
 650  CONTINUE
      

C	Then compare T (with permutated numbers) to each earlier matrix

      L=0
      
 700  IF (L .LT. MAXEC) THEN
C     If the same number of triples
	 IF (CUBEL(L+1) .EQ. CUBEL(MAXEC+1)) THEN
C            WRITE(*,*) L
            
            TROW=1
            ROWOK = .TRUE.

            
 1100       IF ((ROWOK .EQV. .TRUE.) .AND. 
     +           (TROW .LT. N+FLOOR((CUBEL(MAXEC+1)+2)/3)+1)) THEN
               TA=PT(TROW, 1)
               TB=PT(TROW, 2)
               TC=PT(TROW, 3)
               TRIROW=1

 1200          IF (TRIROW .LT. (N+FLOOR((CUBEL(MAXEC+1)+2)/3)+1)) THEN
                  TRIA=TRI(L*30+TRIROW,1)
                  TRIB=TRI(L*30+TRIROW,2)
                  TRIC=TRI(L*30+TRIROW,3)
 1300             IF(((TA.EQ.TRIA) .AND. (TB.EQ.TRIB) .AND. 
     *                 (TC.EQ.TRIC))
     *                 .OR.((TA.EQ.TRIB).AND.(TB.EQ.TRIC).AND.
     *                 (TC.EQ.TRIA))
     *                 .OR.((TA.EQ.TRIC).AND.(TB.EQ.TRIA).AND.
     *                 (TC.EQ.TRIB))) THEN
                     ROWOK=.TRUE.
                     TROW=TROW+1
C                     WRITE(*,*) 'TROW'
C                     WRITE(*,*) TROW
                     GOTO 1100
                  ELSE
                     ROWOK= .FALSE.
                     TRIROW=TRIROW+1
                     GOTO 1200
                  END IF
               ELSE
                  GOTO 1100
               END IF
C     kelpasi!
 1470       ELSE IF (ROWOK .EQV. .TRUE.) THEN
               GOTO 3800
C     ei kelvannut, seuraava
 1480       ELSE
               L=L+1
               GOTO 700
            END IF 
         ELSE
C     if different number of cubes, go to next matrix
            L=L+1
            GOTO 700
         END IF
      ELSE
C     if already last, take the next permutation
         GOTO 410
      END IF
      
      
 3800 NEWEC= .FALSE.
      WRITE(*,*) 'Equivalence class found!'
      WRITE(*,*) PERM
      WRITE(*,*) L+1
      WRITE(*, '(3I3)') ((T(I,J), J = 1, 3), I = 1,30)

 9999 END
