/* CREATE MATRICES FOR ROTATING OBJECTS (OF ANY DIMENSION) IN ANY DIMENSION */



#include <stdio.h>
#include <ctype.h>
#define SIZE 200
#define TRUE 1
#define FALS 0
main()
{
    int i,j,k,n,c,r,u;
    int KILL,MATS,NUM,TOP;
    static char matr [10][6][6][SIZE];  /* Note: Array size and data set here is designed for use in 4-d matrices */
    static char data [10][ 5] = {"tz","ty","tx","zy","zx","yx"};
    static char pers [10][ 5] = {"  ","  ","P1","P2","P3","P4","P5","P6"};
    static char tess [ 6] [6][SIZE];
    static char temp [SIZE];
    char *p,*q,*s,*t,*s1,*t1;

printf ("This program creates matrices for multidimensional rotations\n");
printf ("This program is presently set for four dimensions\n");n=4;
    
/* Prepare array space for matrices */
    KILL = n-2;                /*  KILLer matrices remove higher dimensionality from end matrix, redundant for display */
    MATS = (n-1)*n/2;          /*  MATriceS needed which when multiplied together will form final matrix               */
    NUM  = MATS+KILL;          /*  NUMber of arrays needed by the program to preform the manipulations                 */
    TOP  = n+1;                /*  TOP spatial size needed                                                             */

printf ("Number of Matrices = %d\n",MATS);
printf ("Number of kills    = %d\n",KILL);
printf ("Total Matrices     = %d\n",NUM );


/* THE MATRIX ARRAYS */

printf ("Initializing Matrices\n");

/* Initialization of matrices */
    for (i=0;i<NUM;++i)
 {                              /* My mats have a null char for 0 */
         for (j=0;j<TOP;++j)    /* and a space for one            */
      {
              strcpy(matr [ i ][ j ][ j ], " ");
              strcpy(tess [ j ][ j ], " ");
      }
 }
printf ("Now making Killer matrices\n");
printf ("Perspective variables introduced here also\n");
    if  (!KILL)
 {
         printf ("Its 2-d already!");
         goto overlord;
 };
    KILL=2;
    for (i=MATS;i<NUM;++i)
 {
         strcpy (matr [ i ][KILL][KILL],"");
         strcpy (matr [ i ][KILL][TOP-1],pers [KILL]);
         ++KILL;
 }



overlord:
printf ("The OVERLORD MATRIX\n");

/* THE OVERLORD MATRIX CREATOR */
    k=0;
    for (i=0;i<(n-1);++i)
 {
         for (j=(i+1);j<n;++j)
      {
              p=data [k];
              q=matr [ k ][ i ][ i ];strcpy(q,"C");strcat(q,p);  /* COS */
              q=matr [ k ][ j ][ j ];strcpy(q,"C");strcat(q,p);  /* COS */
              q=matr [ k ][ i ][ j ];strcpy(q,"S");strcat(q,p);  /* SIN */
              q=matr [ k ][ j ][ i ];strcpy(q,"N");strcat(q,p);  /* NIS */
              ++k;
      }
 }

printf ("Now displaying Matrices\n");

/* Matrix Display */ 
    for (i=0;i<NUM;++i)
 {
         printf ("Now Displaying Matrix #%d\n\n",i+1);
         for (k=0;k<TOP;++k)
      {
              for (j=0;j<TOP;++j)
           {
                   printf (" %s",matr [ i ][ k ][ j ]);
                   if (j<TOP-1)printf (",");
           }
              printf ("\n");
      }
 }


printf ("Now Multiplying Matrices!\n");

    for (n=0;n<NUM;++n)
 {
         printf ("\n\nmatrix #%d\n",(n+1));
         for (c=0;c<TOP;++c)
      {
              for (r=0;r<TOP;++r)
           {         /* Update true matrix from temporary */
                   strcpy (matr [NUM][c][r], tess [c][r]);
           }
      }

         for (c=0;c<TOP;++c)                    /* COLUMN */
      {
              for (r=0;r<TOP;++r)                   /* ROW */
           {
                     /* Reset Temporary row of matrix */
                   strcpy (temp,"");
                   for (i=0;i<TOP;++i)                 /* POSITION IN ROW */
                {
                        if (*(p=matr [NUM][c][i]) != 0 && *(q=matr [n][i][r]) !=0)
                     {
                            if (*p==' ' && *q==' '){strcpy(temp," ");goto SKIP;};

                            s=stpchr(p,'(');s1=strrchr(p,')');
                              t=stpchr(p,'+');t1=strrchr(p,'+');
                                u=!(t>=s && t1<=s1);
/* Look for '+' signs in eqn. and bracket if multiplied */
                                if (strlen(temp)>1)
                                    strcat(temp,"+");
                                if (*p==' '){strcat(temp,q);goto SKIP;};
                                if (*q==' '){strcat(temp,p);goto SKIP;};
                                if (u==TRUE)strcat(temp,"(");
                              strcat(temp,p);
                                if (u==TRUE)strcat(temp,")");
                              strcat(temp,q);
SKIP:
			;	/* Manx needs null statement at label */
                     }
                }

                   strcpy (tess [c][r],temp);
                   if (r<TOP)printf ("%s,",temp);
                        else printf ("%s",temp);
           }
              printf ("\n\n");
      }
 }
}
