/* CollectionRank.c */
/* Given coordinates of 3 points in space, output condition of dependence
      of position vectors that locate them */

#include <stdio.h>

int DetRank(int*);

void main(void)
{
 int H,K,
  A[] = {6,9,12,2,3,5,4,6,110};

 H = K = DetRank(A);  if (K<0) K = -K;
 if (K==3)
  {
   puts("Position vectors to these three points are linearly independent");
   printf("  Their orientation is ");
    if (H>0) puts("dextral (+)"); else puts("sinestral (-)");
  }  
 else
  puts("Position vectors to these three points are linearly dependent");
 if (K==2)
  {
   puts("  Collection can be reduced to two fixed non-zero vectors");
   puts("  Parallelopiped collapses onto a plane containing the origin, ");
   puts("    and containing a face");
  }
 if (K==1)
  {
   puts("  Collection can be reduced to a single fixed non-zero vector");
   puts("  Parallelopiped collapses onto a line through the origin,");
   puts("    and containing one of the edges");
  } 
 if(!K)
  {
   puts("  All three vectors equal the zero vector");
   puts("  Collection can be reduced to a single zero vector");
   puts("  Parallelopiped collapses onto a single point, the origin");
  } 
 getchar();
}

/********************************************/

int DetRank(int* A)
{
 int I,K,
     INDX00[] = {4,3,3,1,0,0,1,0,0}, INDX01[] = {5,5,4,2,2,1,2,2,1},
     INDX10[] = {7,6,6,7,6,6,4,3,3}, INDX11[] = {8,8,7,8,8,7,5,5,4};

 for (I=0; I<9; I++) if (A[I]) goto NXT;   return 0;      /* return Rank = 0 */

NXT:
 K = A[0]*A[4]*A[8] + A[1]*A[5]*A[6] + A[2]*A[3]*A[7] -
     A[2]*A[4]*A[6] - A[0]*A[5]*A[7] - A[1]*A[3]*A[8];
 if (K>0)return 3;  if (K<0) return -3;  /* return Rank = 3 and orientation */

 for (I=0; I<9; I++)
  {
   K = A[INDX00[I]]*A[INDX11[I]] - A[INDX01[I]]*A[INDX10[I]];
   if (K) return 2;                                     /* return Rank = 2 */
  }

 return 1;                                              /* return Rank = 1 */
}


